mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Don't overwrite the list of feeds after fetching.
This commit is contained in:
@@ -47,8 +47,11 @@ import java.security.GeneralSecurityException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.ListIterator;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@@ -61,6 +64,7 @@ import okhttp3.Request;
|
|||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
import okhttp3.ResponseBody;
|
||||||
|
|
||||||
|
import static java.util.Collections.singletonList;
|
||||||
import static java.util.Collections.sort;
|
import static java.util.Collections.sort;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
@@ -195,12 +199,7 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook,
|
|||||||
Feed updatedFeed = feedFactory.updateFeed(feed, sf, lastEntryTime);
|
Feed updatedFeed = feedFactory.updateFeed(feed, sf, lastEntryTime);
|
||||||
|
|
||||||
// store feed metadata again to also store last entry time
|
// store feed metadata again to also store last entry time
|
||||||
db.transaction(false, txn -> {
|
updateFeeds(singletonList(updatedFeed));
|
||||||
List<Feed> feeds = getFeeds(txn);
|
|
||||||
feeds.remove(feed);
|
|
||||||
feeds.add(updatedFeed);
|
|
||||||
storeFeeds(txn, feeds);
|
|
||||||
});
|
|
||||||
|
|
||||||
return updatedFeed;
|
return updatedFeed;
|
||||||
}
|
}
|
||||||
@@ -270,8 +269,23 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void storeFeeds(List<Feed> feeds) throws DbException {
|
/**
|
||||||
db.transaction(false, txn -> storeFeeds(txn, feeds));
|
* Updates the given feeds in the stored list of feeds, without affecting
|
||||||
|
* any other feeds in the list or re-adding any of the given feeds that
|
||||||
|
* have been removed from the list.
|
||||||
|
*/
|
||||||
|
private void updateFeeds(List<Feed> updatedFeeds) throws DbException {
|
||||||
|
Map<GroupId, Feed> updatedMap = new HashMap<>();
|
||||||
|
for (Feed feed : updatedFeeds) updatedMap.put(feed.getBlogId(), feed);
|
||||||
|
db.transaction(false, txn -> {
|
||||||
|
List<Feed> feeds = getFeeds(txn);
|
||||||
|
ListIterator<Feed> it = feeds.listIterator();
|
||||||
|
while (it.hasNext()) {
|
||||||
|
Feed updated = updatedMap.get(it.next().getBlogId());
|
||||||
|
if (updated != null) it.set(updated);
|
||||||
|
}
|
||||||
|
storeFeeds(txn, feeds);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -297,8 +311,13 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (feeds.isEmpty()) {
|
||||||
|
LOG.info("No RSS feeds to update");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch and update all feeds
|
// Fetch and update all feeds
|
||||||
List<Feed> newFeeds = new ArrayList<>(feeds.size());
|
List<Feed> updatedFeeds = new ArrayList<>(feeds.size());
|
||||||
for (Feed feed : feeds) {
|
for (Feed feed : feeds) {
|
||||||
try {
|
try {
|
||||||
String url = feed.getProperties().getUrl();
|
String url = feed.getProperties().getUrl();
|
||||||
@@ -307,16 +326,16 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook,
|
|||||||
SyndFeed sf = fetchSyndFeed(url);
|
SyndFeed sf = fetchSyndFeed(url);
|
||||||
// sort and add new entries
|
// sort and add new entries
|
||||||
long lastEntryTime = postFeedEntries(feed, sf.getEntries());
|
long lastEntryTime = postFeedEntries(feed, sf.getEntries());
|
||||||
newFeeds.add(feedFactory.updateFeed(feed, sf, lastEntryTime));
|
updatedFeeds.add(
|
||||||
|
feedFactory.updateFeed(feed, sf, lastEntryTime));
|
||||||
} catch (IOException | DbException e) {
|
} catch (IOException | DbException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
newFeeds.add(feed);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store updated feeds
|
// Store updated feeds
|
||||||
try {
|
try {
|
||||||
storeFeeds(newFeeds);
|
updateFeeds(updatedFeeds);
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ public class FeedManagerImplTest extends BrambleMockTestCase {
|
|||||||
public void testFetchFeedsEmptyList() throws Exception {
|
public void testFetchFeedsEmptyList() throws Exception {
|
||||||
// The list of feeds is empty
|
// The list of feeds is empty
|
||||||
expectGetFeeds();
|
expectGetFeeds();
|
||||||
expectStoreFeeds();
|
|
||||||
feedManager.setTorActive(true);
|
feedManager.setTorActive(true);
|
||||||
feedManager.fetchFeeds();
|
feedManager.fetchFeeds();
|
||||||
}
|
}
|
||||||
@@ -120,7 +120,7 @@ public class FeedManagerImplTest extends BrambleMockTestCase {
|
|||||||
Feed feed = createFeed(url, blog);
|
Feed feed = createFeed(url, blog);
|
||||||
|
|
||||||
expectGetFeeds(feed);
|
expectGetFeeds(feed);
|
||||||
expectStoreFeeds(feed);
|
expectGetAndStoreFeeds(feed);
|
||||||
|
|
||||||
feedManager.setTorActive(true);
|
feedManager.setTorActive(true);
|
||||||
feedManager.fetchFeeds();
|
feedManager.fetchFeeds();
|
||||||
@@ -136,7 +136,7 @@ public class FeedManagerImplTest extends BrambleMockTestCase {
|
|||||||
Feed feed = createFeed(url, blog);
|
Feed feed = createFeed(url, blog);
|
||||||
|
|
||||||
expectGetFeeds(feed);
|
expectGetFeeds(feed);
|
||||||
expectStoreFeeds(feed);
|
expectGetAndStoreFeeds(feed);
|
||||||
|
|
||||||
feedManager.setTorActive(true);
|
feedManager.setTorActive(true);
|
||||||
feedManager.fetchFeeds();
|
feedManager.fetchFeeds();
|
||||||
@@ -155,7 +155,7 @@ public class FeedManagerImplTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
expectGetFeeds(feed);
|
expectGetFeeds(feed);
|
||||||
expectUpdateFeedNoEntries(feed);
|
expectUpdateFeedNoEntries(feed);
|
||||||
expectStoreFeeds(feed);
|
expectGetAndStoreFeeds(feed);
|
||||||
|
|
||||||
feedManager.setTorActive(true);
|
feedManager.setTorActive(true);
|
||||||
feedManager.fetchFeeds();
|
feedManager.fetchFeeds();
|
||||||
@@ -176,7 +176,7 @@ public class FeedManagerImplTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
expectGetFeeds(feed);
|
expectGetFeeds(feed);
|
||||||
expectUpdateFeedOneEntry(feed);
|
expectUpdateFeedOneEntry(feed);
|
||||||
expectStoreFeeds(feed);
|
expectGetAndStoreFeeds(feed);
|
||||||
|
|
||||||
feedManager.setTorActive(true);
|
feedManager.setTorActive(true);
|
||||||
feedManager.fetchFeeds();
|
feedManager.fetchFeeds();
|
||||||
@@ -298,14 +298,6 @@ public class FeedManagerImplTest extends BrambleMockTestCase {
|
|||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void expectStoreFeeds(Feed... feeds) throws Exception {
|
|
||||||
Transaction txn = new Transaction(null, false);
|
|
||||||
context.checking(new DbExpectations() {{
|
|
||||||
oneOf(db).transaction(with(false), withDbRunnable(txn));
|
|
||||||
}});
|
|
||||||
expectStoreFeeds(txn, feeds);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void expectStoreFeeds(Transaction txn, Feed... feeds)
|
private void expectStoreFeeds(Transaction txn, Feed... feeds)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
BdfList feedList = new BdfList();
|
BdfList feedList = new BdfList();
|
||||||
|
|||||||
Reference in New Issue
Block a user