Allow unsubscribing from shared blogs

Only personal blogs from non-contacts can be removed.
This also adds integration tests that check if blogs can actually be removed.

Closes #579
This commit is contained in:
Torsten Grote
2016-08-04 12:35:39 -03:00
parent 3bbc8dcc4e
commit 1ec56fa3ef
15 changed files with 264 additions and 45 deletions

View File

@@ -4,7 +4,6 @@ import org.briarproject.api.FormatException;
import org.briarproject.api.blogs.Blog;
import org.briarproject.api.blogs.BlogFactory;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorFactory;
@@ -32,18 +31,13 @@ class BlogFactoryImpl implements BlogFactory {
}
@Override
public Blog createBlog(@NotNull String name, @NotNull String description,
@NotNull Author author) {
return createBlog(name, description, author, false);
public Blog createPersonalBlog(@NotNull Author a) {
return createBlog(PERSONAL_BLOG_NAME, "", a);
}
@Override
public Blog createPersonalBlog(@NotNull Author a) {
return createBlog(PERSONAL_BLOG_NAME, "", a, true);
}
private Blog createBlog(@NotNull String name, @NotNull String description,
@NotNull Author author, boolean permanent) {
public Blog createBlog(@NotNull String name, @NotNull String description,
@NotNull Author author) {
try {
BdfList blog = BdfList.of(
name,
@@ -53,7 +47,7 @@ class BlogFactoryImpl implements BlogFactory {
byte[] descriptor = clientHelper.toByteArray(blog);
Group g = groupFactory
.createGroup(BlogManagerImpl.CLIENT_ID, descriptor);
return new Blog(g, name, description, author, permanent);
return new Blog(g, name, description, author);
} catch (FormatException e) {
throw new RuntimeException(e);
}
@@ -66,11 +60,10 @@ class BlogFactoryImpl implements BlogFactory {
byte[] descriptor = g.getDescriptor();
// Blog Name, Author Name, Public Key
BdfList blog = clientHelper.toList(descriptor, 0, descriptor.length);
String name = blog.getString(0);
Author a =
authorFactory.createAuthor(blog.getString(1), blog.getRaw(2));
// TODO change permanent depending on how this will be used
boolean permanent = true;
return new Blog(g, blog.getString(0), description, a, permanent);
return new Blog(g, name, description, a);
}
}

View File

@@ -10,6 +10,7 @@ import org.briarproject.api.clients.Client;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfEntry;
import org.briarproject.api.data.BdfList;
@@ -73,17 +74,19 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
private final DatabaseComponent db;
private final IdentityManager identityManager;
private final ContactManager contactManager;
private final BlogFactory blogFactory;
private final List<RemoveBlogHook> removeHooks;
@Inject
BlogManagerImpl(DatabaseComponent db, IdentityManager identityManager,
ClientHelper clientHelper, MetadataParser metadataParser,
BlogFactory blogFactory) {
ContactManager contactManager, BlogFactory blogFactory) {
super(clientHelper, metadataParser);
this.db = db;
this.identityManager = identityManager;
this.contactManager = contactManager;
this.blogFactory = blogFactory;
removeHooks = new CopyOnWriteArrayList<RemoveBlogHook>();
}
@@ -189,11 +192,36 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
return b;
}
@Override
public boolean canBeRemoved(GroupId g) throws DbException {
Transaction txn = db.startTransaction(true);
try {
boolean canBeRemoved = canBeRemoved(txn, g);
txn.setComplete();
return canBeRemoved;
} finally {
db.endTransaction(txn);
}
}
private boolean canBeRemoved(Transaction txn, GroupId g)
throws DbException {
boolean canBeRemoved;
Blog b = getBlog(txn, g);
AuthorId authorId = b.getAuthor().getId();
LocalAuthor localAuthor = identityManager.getLocalAuthor(txn);
canBeRemoved = !contactManager
.contactExists(txn, authorId, localAuthor.getId());
return canBeRemoved;
}
@Override
public void removeBlog(Blog b) throws DbException {
// TODO if this gets used, check for RSS feeds posting into this blog
Transaction txn = db.startTransaction(false);
try {
if (!canBeRemoved(txn, b.getId()))
throw new DbException();
for (RemoveBlogHook hook : removeHooks)
hook.removingBlog(txn, b);
db.removeGroup(txn, b.getGroup());