I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Unverified Commit 26b22395 authored by zzz's avatar zzz
Browse files

Console: News fetch improvements

Show status in sidebar for manual update checks
Ignore last-mod after changing languages to force refetch
parent 39950780
No related branches found
No related tags found
1 merge request!163Console: News fetch improvements
Pipeline #1436 passed
...@@ -70,6 +70,9 @@ class NewsFetcher extends UpdateRunner { ...@@ -70,6 +70,9 @@ class NewsFetcher extends UpdateRunner {
private long _newLastModified; private long _newLastModified;
private final File _newsFile; private final File _newsFile;
private final File _tempFile; private final File _tempFile;
private final long _timeout;
private final boolean _showStatus;
private String _failMsg;
/** is the news newer */ /** is the news newer */
private boolean _isNewer; private boolean _isNewer;
private boolean _success; private boolean _success;
...@@ -80,14 +83,28 @@ class NewsFetcher extends UpdateRunner { ...@@ -80,14 +83,28 @@ class NewsFetcher extends UpdateRunner {
static final String PROP_BLOCKLIST_TIME = "router.blocklistVersion"; static final String PROP_BLOCKLIST_TIME = "router.blocklistVersion";
private static final String BLOCKLIST_DIR = "docs/feed/blocklist"; private static final String BLOCKLIST_DIR = "docs/feed/blocklist";
private static final String BLOCKLIST_FILE = "blocklist.txt"; private static final String BLOCKLIST_FILE = "blocklist.txt";
private static final long DEFAULT_TIMEOUT = 60*1000;
public NewsFetcher(RouterContext ctx, ConsoleUpdateManager mgr, List<URI> uris) { public NewsFetcher(RouterContext ctx, ConsoleUpdateManager mgr, List<URI> uris) {
this(ctx, mgr, uris, DEFAULT_TIMEOUT);
}
/**
* @param timeout if less than 60 seconds, we assume this was manually initiated,
* and we will log status to the sidebar
* @since 0.9.62
*/
public NewsFetcher(RouterContext ctx, ConsoleUpdateManager mgr, List<URI> uris, long timeout) {
super(ctx, mgr, NEWS, uris); super(ctx, mgr, NEWS, uris);
_newsFile = new File(ctx.getRouterDir(), NewsHelper.NEWS_FILE); _newsFile = new File(ctx.getRouterDir(), NewsHelper.NEWS_FILE);
_tempFile = new File(ctx.getTempDir(), "tmp-" + ctx.random().nextLong() + TEMP_NEWS_FILE); _tempFile = new File(ctx.getTempDir(), "tmp-" + ctx.random().nextLong() + TEMP_NEWS_FILE);
long lastMod = NewsHelper.lastUpdated(ctx); _timeout = timeout;
if (lastMod > 0) _showStatus = timeout < DEFAULT_TIMEOUT;
_lastModified = RFC822Date.to822Date(lastMod); if (!langChanged()) {
long lastMod = NewsHelper.lastUpdated(ctx);
if (lastMod > 0)
_lastModified = RFC822Date.to822Date(lastMod);
}
} }
@Override @Override
...@@ -140,7 +157,7 @@ class NewsFetcher extends UpdateRunner { ...@@ -140,7 +157,7 @@ class NewsFetcher extends UpdateRunner {
long start = _context.clock().now(); long start = _context.clock().now();
// will be adjusted in headerReceived() below // will be adjusted in headerReceived() below
_newLastModified = start; _newLastModified = start;
if (get.fetch()) { if (get.fetch(_timeout)) {
int status = get.getStatusCode(); int status = get.getStatusCode();
if (status == 200 || status == 304) { if (status == 200 || status == 304) {
Map<String, String> opts = new HashMap<String, String>(3); Map<String, String> opts = new HashMap<String, String>(3);
...@@ -148,12 +165,39 @@ class NewsFetcher extends UpdateRunner { ...@@ -148,12 +165,39 @@ class NewsFetcher extends UpdateRunner {
if (status == 200 && _isNewer) { if (status == 200 && _isNewer) {
String lastMod = Long.toString(_newLastModified); String lastMod = Long.toString(_newLastModified);
opts.put(NewsHelper.PROP_LAST_UPDATED, lastMod); opts.put(NewsHelper.PROP_LAST_UPDATED, lastMod);
String lang = Translate.getLanguage(_context);
opts.put(NewsHelper.PROP_LAST_LANG, lang);
if (_gotNewEntry) if (_gotNewEntry)
opts.put(NewsHelper.PROP_LAST_NEW_ENTRY, lastMod); opts.put(NewsHelper.PROP_LAST_NEW_ENTRY, lastMod);
} }
_context.router().saveConfig(opts, null); _context.router().saveConfig(opts, null);
if (_failMsg != null) {
// from checkForUpdates()
_mgr.notifyComplete(this, "<b>" + _failMsg + "</b>");
} else if (_showStatus) {
if (status == 200)
_mgr.notifyComplete(this, "News updated from " + _currentURI.getHost());
else
_mgr.notifyComplete(this, "No new news available from " + _currentURI.getHost());
}
return; return;
} }
} else {
int status = get.getStatusCode();
String msg;
if (status == 504 || status <= 0)
msg = "Unable to connect to news server " + _currentURI.getHost();
else if (status == 500)
msg = "News server " + _currentURI.getHost() + " not found in address book";
else if (status == 404)
msg = "News file not found on news server at " + newsURL;
else
msg = status + " " + DataHelper.stripHTML(get.getStatusText());
// only display if manually initiated
if (_showStatus)
updateStatus("<b>" + msg + "</b>");
if (_log.shouldWarn())
_log.warn(msg);
} }
} catch (Throwable t) { } catch (Throwable t) {
_log.error("Error fetching the news", t); _log.error("Error fetching the news", t);
...@@ -194,6 +238,17 @@ class NewsFetcher extends UpdateRunner { ...@@ -194,6 +238,17 @@ class NewsFetcher extends UpdateRunner {
return uri; return uri;
} }
} }
/**
* @since 0.9.62
*/
private boolean langChanged() {
String old = _context.getProperty(NewsHelper.PROP_LAST_LANG);
if (old == null)
return false;
String lang = Translate.getLanguage(_context);
return !lang.equals(old);
}
// Fake XML parsing // Fake XML parsing
// Line must contain this, and full entry must be on one line // Line must contain this, and full entry must be on one line
...@@ -213,6 +268,7 @@ class NewsFetcher extends UpdateRunner { ...@@ -213,6 +268,7 @@ class NewsFetcher extends UpdateRunner {
// unused // unused
//private static final String I2P_SUD_KEY = "sudi2p"; //private static final String I2P_SUD_KEY = "sudi2p";
//private static final String I2P_SU2_KEY = "su2i2p"; //private static final String I2P_SU2_KEY = "su2i2p";
/** /**
* @since 0.9.52 * @since 0.9.52
*/ */
...@@ -325,6 +381,7 @@ class NewsFetcher extends UpdateRunner { ...@@ -325,6 +381,7 @@ class NewsFetcher extends UpdateRunner {
} catch (IOException ioe) { } catch (IOException ioe) {
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
_log.warn("Error checking the news for an update", ioe); _log.warn("Error checking the news for an update", ioe);
_failMsg = "Error checking the news for an update: " + ioe;
return; return;
} finally { } finally {
if (in != null) try { in.close(); } catch (IOException ioe) {} if (in != null) try { in.close(); } catch (IOException ioe) {}
...@@ -486,9 +543,38 @@ class NewsFetcher extends UpdateRunner { ...@@ -486,9 +543,38 @@ class NewsFetcher extends UpdateRunner {
_success = true; _success = true;
} }
/** override to prevent status update */ /**
* override for status update
* @since 0.9.62
*/
@Override @Override
public void transferFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt) {} public void attemptFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt, int numRetries, Exception cause) {
// only display if manually initiated
if (_showStatus) {
String msg = "Unable to connect to news server " + url + ": " + DataHelper.stripHTML(cause.toString());
updateStatus("<b>" + msg + "</b>");
if (_log.shouldWarn())
_log.warn(msg);
}
// update manager will also log
_mgr.notifyAttemptFailed(this, url, null);
}
/**
* override for status update
*/
@Override
public void transferFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt) {
// only display if manually initiated
if (_showStatus) {
String msg = "Failed downloading news from " + url;
updateStatus("<b>" + msg + "</b>");
if (_log.shouldWarn())
_log.warn(msg);
}
// update manager will also log
_mgr.notifyAttemptFailed(this, url, null);
}
/** /**
* Process the fetched su3 news file _tempFile. * Process the fetched su3 news file _tempFile.
......
...@@ -67,7 +67,7 @@ public class NewsHandler extends UpdateHandler implements Checker { ...@@ -67,7 +67,7 @@ public class NewsHandler extends UpdateHandler implements Checker {
//updateSources.add(new URI(BACKUP_NEWS_URL)); //updateSources.add(new URI(BACKUP_NEWS_URL));
updateSources.add(new URI(_context.getProperty(PROP_BACKUP_NEWS_URL_SU3, DEFAULT_BACKUP_NEWS_URL_SU3))); updateSources.add(new URI(_context.getProperty(PROP_BACKUP_NEWS_URL_SU3, DEFAULT_BACKUP_NEWS_URL_SU3)));
} catch (URISyntaxException use) {} } catch (URISyntaxException use) {}
UpdateRunner update = new NewsFetcher(_context, _mgr, updateSources); UpdateRunner update = new NewsFetcher(_context, _mgr, updateSources, maxTime);
return update; return update;
} }
......
...@@ -27,6 +27,8 @@ public class NewsHelper extends ContentHelper { ...@@ -27,6 +27,8 @@ public class NewsHelper extends ContentHelper {
public static final String PROP_LAST_UPDATED = "routerconsole.newsLastUpdated"; public static final String PROP_LAST_UPDATED = "routerconsole.newsLastUpdated";
/** @since 0.9.55 */ /** @since 0.9.55 */
public static final String PROP_LAST_NEW_ENTRY = "routerconsole.newsLastNewEntry"; public static final String PROP_LAST_NEW_ENTRY = "routerconsole.newsLastNewEntry";
/** @since 0.9.62 */
public static final String PROP_LAST_LANG = "routerconsole.newsLastLanguage";
/** /**
* Default true * Default true
* @since 0.9.21 * @since 0.9.21
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment