Store RSS feeds in a separate dedicated blog

A fake LocalAuthor is created for this new blog and stored in the feed's metadata.
This commit is contained in:
Torsten Grote
2017-04-10 12:15:48 -03:00
parent 9755cd9ab4
commit c7ff1ba974
10 changed files with 245 additions and 116 deletions

View File

@@ -179,7 +179,6 @@ public class FeedFragment extends BaseFragment implements
case R.id.action_rss_feeds_import:
Intent i2 =
new Intent(getActivity(), RssFeedImportActivity.class);
i2.putExtra(GROUP_ID, personalBlog.getId().getBytes());
startActivity(i2);
return true;
case R.id.action_rss_feeds_manage:

View File

@@ -1,7 +1,6 @@
package org.briarproject.briar.android.blog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
@@ -15,7 +14,6 @@ import android.widget.ProgressBar;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.lifecycle.IoExecutor;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.briar.R;
import org.briarproject.briar.android.activity.ActivityComponent;
import org.briarproject.briar.android.activity.BriarActivity;
@@ -44,9 +42,6 @@ public class RssFeedImportActivity extends BriarActivity {
@IoExecutor
Executor ioExecutor;
// Fields that are accessed from background threads must be volatile
private volatile GroupId groupId = null;
@Inject
@SuppressWarnings("WeakerAccess")
volatile FeedManager feedManager;
@@ -55,12 +50,6 @@ public class RssFeedImportActivity extends BriarActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// GroupId from Intent
Intent i = getIntent();
byte[] b = i.getByteArrayExtra(GROUP_ID);
if (b == null) throw new IllegalStateException("No Group in intent.");
groupId = new GroupId(b);
setContentView(R.layout.activity_rss_feed_import);
urlInput = (EditText) findViewById(R.id.urlInput);
@@ -128,7 +117,7 @@ public class RssFeedImportActivity extends BriarActivity {
@Override
public void run() {
try {
feedManager.addFeed(url, groupId);
feedManager.addFeed(url);
feedImported();
} catch (DbException | IOException e) {
if (LOG.isLoggable(WARNING))

View File

@@ -87,7 +87,6 @@ public class RssFeedManageActivity extends BriarActivity
return true;
case R.id.action_rss_feeds_import:
Intent i = new Intent(this, RssFeedImportActivity.class);
i.putExtra(GROUP_ID, groupId.getBytes());
startActivity(i);
return true;
default:

View File

@@ -1,39 +1,32 @@
package org.briarproject.briar.api.feed;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.data.BdfDictionary;
import org.briarproject.bramble.api.data.BdfEntry;
import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.briar.api.blog.Blog;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_BLOG_GROUP_ID;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_ADDED;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_AUTHOR;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_DESC;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_LAST_ENTRY;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_TITLE;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_UPDATED;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_URL;
@Immutable
@NotNullByDefault
public class Feed {
private final String url;
private final GroupId blogId;
private final Blog blog;
private final LocalAuthor localAuthor;
@Nullable
private final String title, description, author;
private final long added, updated, lastEntryTime;
public Feed(String url, GroupId blogId, @Nullable String title,
@Nullable String description, @Nullable String author,
long added, long updated, long lastEntryTime) {
public Feed(String url, Blog blog, LocalAuthor localAuthor,
@Nullable String title, @Nullable String description,
@Nullable String author, long added, long updated,
long lastEntryTime) {
this.url = url;
this.blogId = blogId;
this.blog = blog;
this.localAuthor = localAuthor;
this.title = title;
this.description = description;
this.author = author;
@@ -42,13 +35,14 @@ public class Feed {
this.lastEntryTime = lastEntryTime;
}
public Feed(String url, GroupId blogId, @Nullable String title,
@Nullable String description, @Nullable String author, long added) {
this(url, blogId, title, description, author, added, 0L, 0L);
public Feed(String url, Blog blog, LocalAuthor localAuthor,
@Nullable String title, @Nullable String description,
@Nullable String author, long added) {
this(url, blog, localAuthor, title, description, author, added, 0L, 0L);
}
public Feed(String url, GroupId blogId, long added) {
this(url, blogId, null, null, null, added, 0L, 0L);
public Feed(String url, Blog blog, LocalAuthor localAuthor, long added) {
this(url, blog, localAuthor, null, null, null, added, 0L, 0L);
}
public String getUrl() {
@@ -56,34 +50,15 @@ public class Feed {
}
public GroupId getBlogId() {
return blogId;
return blog.getId();
}
public BdfDictionary toBdfDictionary() {
BdfDictionary d = BdfDictionary.of(
new BdfEntry(KEY_FEED_URL, url),
new BdfEntry(KEY_BLOG_GROUP_ID, blogId.getBytes()),
new BdfEntry(KEY_FEED_ADDED, added),
new BdfEntry(KEY_FEED_UPDATED, updated),
new BdfEntry(KEY_FEED_LAST_ENTRY, lastEntryTime)
);
if (title != null) d.put(KEY_FEED_TITLE, title);
if (description != null) d.put(KEY_FEED_DESC, description);
if (author != null) d.put(KEY_FEED_AUTHOR, author);
return d;
public Blog getBlog() {
return blog;
}
public static Feed from(BdfDictionary d) throws FormatException {
String url = d.getString(KEY_FEED_URL);
GroupId blogId = new GroupId(d.getRaw(KEY_BLOG_GROUP_ID));
String title = d.getOptionalString(KEY_FEED_TITLE);
String desc = d.getOptionalString(KEY_FEED_DESC);
String author = d.getOptionalString(KEY_FEED_AUTHOR);
long added = d.getLong(KEY_FEED_ADDED, 0L);
long updated = d.getLong(KEY_FEED_UPDATED, 0L);
long lastEntryTime = d.getLong(KEY_FEED_LAST_ENTRY, 0L);
return new Feed(url, blogId, title, desc, author, added, updated,
lastEntryTime);
public LocalAuthor getLocalAuthor() {
return localAuthor;
}
@Nullable
@@ -118,13 +93,13 @@ public class Feed {
if (this == o) return true;
if (o instanceof Feed) {
Feed f = (Feed) o;
return url.equals(f.url) && blogId.equals(f.getBlogId()) &&
equalsWithNull(title, f.getTitle()) &&
equalsWithNull(description, f.getDescription()) &&
equalsWithNull(author, f.getAuthor()) &&
added == f.getAdded() &&
updated == f.getUpdated() &&
lastEntryTime == f.getLastEntryTime();
return url.equals(f.url) && blog.equals(f.blog) &&
equalsWithNull(title, f.title) &&
equalsWithNull(description, f.description) &&
equalsWithNull(author, f.author) &&
added == f.added &&
updated == f.updated &&
lastEntryTime == f.lastEntryTime;
}
return false;
}
@@ -134,4 +109,5 @@ public class Feed {
if (a == null || b == null) return false;
return a.equals(b);
}
}

View File

@@ -18,7 +18,9 @@ public interface FeedConstants {
// group metadata keys
String KEY_FEEDS = "feeds";
String KEY_FEED_URL = "feedURL";
String KEY_BLOG_GROUP_ID = "blogGroupId";
String KEY_BLOG_TITLE = "blogTitle";
String KEY_PUBLIC_KEY = "publicKey";
String KEY_PRIVATE_KEY = "privateKey";
String KEY_FEED_TITLE = "feedTitle";
String KEY_FEED_DESC = "feedDesc";
String KEY_FEED_AUTHOR = "feedAuthor";

View File

@@ -3,7 +3,6 @@ package org.briarproject.briar.api.feed;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.ClientId;
import org.briarproject.bramble.api.sync.GroupId;
import java.io.IOException;
import java.util.List;
@@ -17,9 +16,9 @@ public interface FeedManager {
ClientId CLIENT_ID = new ClientId("org.briarproject.briar.feed");
/**
* Adds an RSS feed.
* Adds an RSS feed as a new dedicated blog.
*/
void addFeed(String url, GroupId g) throws DbException, IOException;
void addFeed(String url) throws DbException, IOException;
/**
* Removes an RSS feed.

View File

@@ -0,0 +1,34 @@
package org.briarproject.briar.feed;
import com.rometools.rome.feed.synd.SyndFeed;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.data.BdfDictionary;
import org.briarproject.briar.api.feed.Feed;
interface FeedFactory {
/**
* Create a new feed based on the feed url
* and the metadata of an existing {@link SyndFeed}.
*/
Feed createFeed(String url, SyndFeed feed);
/**
* Creates a new updated feed, based on the given existing feed,
* new metadata from the given {@link SyndFeed}
* and the time of the last feed entry.
*/
Feed createFeed(Feed feed, SyndFeed f, long lastEntryTime);
/**
* De-serializes a {@link BdfDictionary} into a {@link Feed}.
*/
Feed createFeed(BdfDictionary d) throws FormatException;
/**
* Serializes a {@link Feed} into a {@link BdfDictionary}.
*/
BdfDictionary feedToBdfDictionary(Feed feed);
}

View File

@@ -0,0 +1,111 @@
package org.briarproject.briar.feed;
import com.rometools.rome.feed.synd.SyndFeed;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.KeyPair;
import org.briarproject.bramble.api.data.BdfDictionary;
import org.briarproject.bramble.api.data.BdfEntry;
import org.briarproject.bramble.api.identity.AuthorFactory;
import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.briar.api.blog.Blog;
import org.briarproject.briar.api.blog.BlogFactory;
import org.briarproject.briar.api.feed.Feed;
import javax.inject.Inject;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_BLOG_TITLE;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_ADDED;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_AUTHOR;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_DESC;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_LAST_ENTRY;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_TITLE;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_UPDATED;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_URL;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_PRIVATE_KEY;
import static org.briarproject.briar.api.feed.FeedConstants.KEY_PUBLIC_KEY;
class FeedFactoryImpl implements FeedFactory {
private final CryptoComponent cryptoComponent;
private final AuthorFactory authorFactory;
private final BlogFactory blogFactory;
private final Clock clock;
@Inject
FeedFactoryImpl(CryptoComponent cryptoComponent,
AuthorFactory authorFactory, BlogFactory blogFactory, Clock clock) {
this.cryptoComponent = cryptoComponent;
this.authorFactory = authorFactory;
this.blogFactory = blogFactory;
this.clock = clock;
}
@Override
public Feed createFeed(String url, SyndFeed syndFeed) {
if (syndFeed.getTitle() == null) syndFeed.setTitle("RSS feed");
KeyPair keyPair = cryptoComponent.generateSignatureKeyPair();
LocalAuthor localAuthor = authorFactory
.createLocalAuthor(syndFeed.getTitle(),
keyPair.getPublic().getEncoded(),
keyPair.getPrivate().getEncoded());
Blog blog = blogFactory.createBlog(localAuthor);
long added = clock.currentTimeMillis();
return new Feed(url, blog, localAuthor, added);
}
@Override
public Feed createFeed(Feed feed, SyndFeed f, long lastEntryTime) {
long updated = clock.currentTimeMillis();
return new Feed(feed.getUrl(), feed.getBlog(), feed.getLocalAuthor(),
f.getTitle(), f.getDescription(), f.getAuthor(),
feed.getAdded(), updated, lastEntryTime);
}
@Override
public Feed createFeed(BdfDictionary d) throws FormatException {
String url = d.getString(KEY_FEED_URL);
String blogTitle = d.getString(KEY_BLOG_TITLE);
byte[] publicKey = d.getRaw(KEY_PUBLIC_KEY);
byte[] privateKey = d.getRaw(KEY_PRIVATE_KEY);
LocalAuthor localAuthor = authorFactory
.createLocalAuthor(blogTitle, publicKey, privateKey);
Blog blog = blogFactory.createBlog(localAuthor);
String title = d.getOptionalString(KEY_FEED_TITLE);
String desc = d.getOptionalString(KEY_FEED_DESC);
String author = d.getOptionalString(KEY_FEED_AUTHOR);
long added = d.getLong(KEY_FEED_ADDED, 0L);
long updated = d.getLong(KEY_FEED_UPDATED, 0L);
long lastEntryTime = d.getLong(KEY_FEED_LAST_ENTRY, 0L);
return new Feed(url, blog, localAuthor, title, desc, author, added,
updated, lastEntryTime);
}
@Override
public BdfDictionary feedToBdfDictionary(Feed feed) {
BdfDictionary d = BdfDictionary.of(
new BdfEntry(KEY_FEED_URL, feed.getUrl()),
new BdfEntry(KEY_BLOG_TITLE, feed.getLocalAuthor().getName()),
new BdfEntry(KEY_PUBLIC_KEY,
feed.getLocalAuthor().getPublicKey()),
new BdfEntry(KEY_PRIVATE_KEY,
feed.getLocalAuthor().getPrivateKey()),
new BdfEntry(KEY_FEED_ADDED, feed.getAdded()),
new BdfEntry(KEY_FEED_UPDATED, feed.getUpdated()),
new BdfEntry(KEY_FEED_LAST_ENTRY, feed.getLastEntryTime())
);
if (feed.getTitle() != null) d.put(KEY_FEED_TITLE, feed.getTitle());
if (feed.getDescription() != null)
d.put(KEY_FEED_DESC, feed.getDescription());
if (feed.getAuthor() != null) d.put(KEY_FEED_AUTHOR, feed.getAuthor());
return d;
}
}

View File

@@ -18,7 +18,6 @@ import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.event.Event;
import org.briarproject.bramble.api.event.EventListener;
import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.lifecycle.IoExecutor;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@@ -88,9 +87,9 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
private final DatabaseComponent db;
private final ContactGroupFactory contactGroupFactory;
private final ClientHelper clientHelper;
private final IdentityManager identityManager;
private final BlogManager blogManager;
private final BlogPostFactory blogPostFactory;
private final FeedFactory feedFactory;
private final SocketFactory torSocketFactory;
private final Clock clock;
private final AtomicBoolean fetcherStarted = new AtomicBoolean(false);
@@ -99,8 +98,8 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
FeedManagerImpl(@Scheduler ScheduledExecutorService scheduler,
@IoExecutor Executor ioExecutor, DatabaseComponent db,
ContactGroupFactory contactGroupFactory, ClientHelper clientHelper,
IdentityManager identityManager, BlogManager blogManager,
BlogPostFactory blogPostFactory, SocketFactory torSocketFactory,
BlogManager blogManager, BlogPostFactory blogPostFactory,
FeedFactory feedFactory, SocketFactory torSocketFactory,
Clock clock) {
this.scheduler = scheduler;
@@ -108,9 +107,9 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
this.db = db;
this.contactGroupFactory = contactGroupFactory;
this.clientHelper = clientHelper;
this.identityManager = identityManager;
this.blogManager = blogManager;
this.blogPostFactory = blogPostFactory;
this.feedFactory = feedFactory;
this.torSocketFactory = torSocketFactory;
this.clock = clock;
}
@@ -158,21 +157,22 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
}
@Override
public void addFeed(String url, GroupId g) throws DbException, IOException {
LOG.info("Adding new RSS feed...");
public void addFeed(String url) throws DbException, IOException {
// TODO check for existing feed?
// fetch feed to get its metadata
Feed feed = new Feed(url, g, clock.currentTimeMillis());
// fetch syndication feed to get its metadata
SyndFeed f;
try {
feed = fetchFeed(feed, false);
f = fetchSyndFeed(url);
} catch (FeedException e) {
throw new IOException(e);
}
// store feed
Feed feed = feedFactory.createFeed(url, f);
// store feed and new blog
Transaction txn = db.startTransaction(false);
try {
blogManager.addBlog(txn, feed.getBlog());
List<Feed> feeds = getFeeds(txn);
feeds.add(feed);
storeFeeds(txn, feeds);
@@ -181,10 +181,10 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
db.endTransaction(txn);
}
// fetch feed again, post entries this time
// fetch feed again and post entries
Feed updatedFeed;
try {
updatedFeed = fetchFeed(feed, true);
updatedFeed = fetchFeed(feed);
} catch (FeedException e) {
throw new IOException(e);
}
@@ -208,16 +208,17 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
Transaction txn = db.startTransaction(false);
try {
List<Feed> feeds = getFeeds(txn);
boolean found = false;
for (Feed feed : feeds) {
if (feed.getUrl().equals(url)) {
found = true;
feeds.remove(feed);
Feed feed = null;
for (Feed f : feeds) {
if (f.getUrl().equals(url)) {
feed = f;
feeds.remove(f);
break;
}
}
if (!found) throw new DbException();
if (feed == null) throw new DbException();
storeFeeds(txn, feeds);
// TODO blogManager.removeBlog(txn, feed.getBlog());
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
@@ -246,7 +247,7 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
for (Object object : d.getList(KEY_FEEDS)) {
if (!(object instanceof BdfDictionary))
throw new FormatException();
feeds.add(Feed.from((BdfDictionary) object));
feeds.add(feedFactory.createFeed((BdfDictionary) object));
}
} catch (FormatException e) {
throw new DbException(e);
@@ -259,7 +260,7 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
BdfList feedList = new BdfList();
for (Feed feed : feeds) {
feedList.add(feed.toBdfDictionary());
feedList.add(feedFactory.feedToBdfDictionary(feed));
}
BdfDictionary gm = BdfDictionary.of(new BdfEntry(KEY_FEEDS, feedList));
try {
@@ -300,7 +301,7 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
List<Feed> newFeeds = new ArrayList<Feed>(feeds.size());
for (Feed feed : feeds) {
try {
newFeeds.add(fetchFeed(feed, true));
newFeeds.add(fetchFeed(feed));
} catch (FeedException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
@@ -323,31 +324,45 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
LOG.info("Done updating RSS feeds");
}
private Feed fetchFeed(Feed feed, boolean post)
throws FeedException, IOException, DbException {
String title, description, author;
long updated = clock.currentTimeMillis();
long lastEntryTime = feed.getLastEntryTime();
SyndFeed f = getSyndFeed(getFeedInputStream(feed.getUrl()));
title = StringUtils.isNullOrEmpty(f.getTitle()) ? null : f.getTitle();
if (title != null) title = clean(title, STRIP_ALL);
description = StringUtils.isNullOrEmpty(f.getDescription()) ? null :
f.getDescription();
if (description != null) description = clean(description, STRIP_ALL);
author =
StringUtils.isNullOrEmpty(f.getAuthor()) ? null : f.getAuthor();
if (author != null) author = clean(author, STRIP_ALL);
private SyndFeed fetchSyndFeed(String url)
throws FeedException, IOException {
// fetch feed
SyndFeed f = getSyndFeed(getFeedInputStream(url));
if (f.getEntries().size() == 0)
throw new FeedException("Feed has no entries");
// clean title
String title =
StringUtils.isNullOrEmpty(f.getTitle()) ? null : f.getTitle();
if (title != null) title = clean(title, STRIP_ALL);
f.setTitle(title);
// clean description
String description =
StringUtils.isNullOrEmpty(f.getDescription()) ? null :
f.getDescription();
if (description != null) description = clean(description, STRIP_ALL);
f.setDescription(description);
// clean author
String author =
StringUtils.isNullOrEmpty(f.getAuthor()) ? null : f.getAuthor();
if (author != null) author = clean(author, STRIP_ALL);
f.setAuthor(author);
return f;
}
private Feed fetchFeed(Feed feed)
throws FeedException, IOException, DbException {
// fetch and clean feed
SyndFeed f = fetchSyndFeed(feed.getUrl());
// sort and add new entries
if (post) {
lastEntryTime = postFeedEntries(feed, f.getEntries());
}
return new Feed(feed.getUrl(), feed.getBlogId(), title, description,
author, feed.getAdded(), updated, lastEntryTime);
long lastEntryTime = postFeedEntries(feed, f.getEntries());
return feedFactory.createFeed(feed, f, lastEntryTime);
}
private InputStream getFeedInputStream(String url) throws IOException {
@@ -461,9 +476,9 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
String body = getPostBody(b.toString());
try {
// create and store post
LocalAuthor author = identityManager.getLocalAuthor(txn);
LocalAuthor localAuthor = feed.getLocalAuthor();
BlogPost post = blogPostFactory
.createBlogPost(groupId, time, null, author, body);
.createBlogPost(groupId, time, null, localAuthor, body);
blogManager.addLocalPost(txn, post);
} catch (DbException e) {
if (LOG.isLoggable(WARNING))

View File

@@ -28,4 +28,9 @@ public class FeedModule {
return feedManager;
}
@Provides
FeedFactory provideFeedFactory(FeedFactoryImpl feedFactory) {
return feedFactory;
}
}