Implement BlogSharingManager

This commit is contained in:
str4d
2016-06-16 05:21:57 +00:00
parent 9ae64124d3
commit 13e3eec6b3
11 changed files with 757 additions and 0 deletions

View File

@@ -22,6 +22,12 @@ public interface BlogConstants {
/** The maximum length of a blog post's body in bytes. */
int MAX_BLOG_POST_BODY_LENGTH = MAX_MESSAGE_BODY_LENGTH - 1024;
/* Blog Sharing Constants */
String BLOG_TITLE = "blogTitle";
String BLOG_DESC = "blogDescription";
String BLOG_AUTHOR_NAME = "blogAuthorName";
String BLOG_PUBLIC_KEY = "blogPublicKey";
// Metadata keys
String KEY_DESCRIPTION = "description";
String KEY_TITLE = "title";

View File

@@ -0,0 +1,26 @@
package org.briarproject.api.blogs;
import org.briarproject.api.clients.SessionId;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.sharing.InvitationMessage;
import org.briarproject.api.sync.MessageId;
public class BlogInvitationMessage extends InvitationMessage {
private final String blogTitle;
public BlogInvitationMessage(MessageId id, SessionId sessionId,
ContactId contactId, String blogTitle, String message,
boolean available, long time, boolean local, boolean sent,
boolean seen, boolean read) {
super(id, sessionId, contactId, message, available, time, local, sent,
seen, read);
this.blogTitle = blogTitle;
}
public String getBlogTitle() {
return blogTitle;
}
}

View File

@@ -0,0 +1,60 @@
package org.briarproject.api.blogs;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DbException;
import org.briarproject.api.sharing.SharingManager;
import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.GroupId;
import java.util.Collection;
public interface BlogSharingManager
extends SharingManager<Blog, BlogInvitationMessage> {
/**
* Returns the unique ID of the blog sharing client.
*/
ClientId getClientId();
/**
* Sends an invitation to share the given blog with the given contact
* and sends an optional message along with it.
*/
void sendInvitation(GroupId groupId, ContactId contactId,
String message) throws DbException;
/**
* Responds to a pending blog invitation
*/
void respondToInvitation(Blog b, Contact c, boolean accept)
throws DbException;
/**
* Returns all blogs sharing messages sent by the Contact
* identified by contactId.
*/
Collection<BlogInvitationMessage> getInvitationMessages(
ContactId contactId) throws DbException;
/**
* Returns all blogs to which the user could subscribe.
*/
Collection<Blog> getAvailable() throws DbException;
/**
* Returns all contacts who are sharing the given blog with us.
*/
Collection<Contact> getSharedBy(GroupId g) throws DbException;
/**
* Returns the IDs of all contacts with whom the given blog is shared.
*/
Collection<Contact> getSharedWith(GroupId g) throws DbException;
/**
* Returns true if the blog not already shared and no invitation is open
*/
boolean canBeShared(GroupId g, Contact c) throws DbException;
}

View File

@@ -0,0 +1,89 @@
package org.briarproject.api.blogs;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.SessionId;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.sharing.SharingMessage.Invitation;
import org.briarproject.api.sync.GroupId;
import static org.briarproject.api.blogs.BlogConstants.BLOG_AUTHOR_NAME;
import static org.briarproject.api.blogs.BlogConstants.BLOG_DESC;
import static org.briarproject.api.blogs.BlogConstants.BLOG_PUBLIC_KEY;
import static org.briarproject.api.blogs.BlogConstants.BLOG_TITLE;
import static org.briarproject.api.sharing.SharingConstants.INVITATION_MSG;
import static org.briarproject.api.sharing.SharingConstants.SESSION_ID;
public interface BlogSharingMessage {
class BlogInvitation extends Invitation {
private final String blogTitle;
private final String blogDesc;
private final String blogAuthorName;
private final byte[] blogPublicKey;
public BlogInvitation(GroupId groupId, SessionId sessionId,
String blogTitle, String blogDesc, String blogAuthorName,
byte[] blogPublicKey, String message) {
super(groupId, sessionId, message);
this.blogTitle = blogTitle;
this.blogDesc = blogDesc;
this.blogAuthorName = blogAuthorName;
this.blogPublicKey = blogPublicKey;
}
@Override
public BdfList toBdfList() {
BdfList list = super.toBdfList();
list.add(blogTitle);
list.add(blogDesc);
list.add(BdfList.of(blogAuthorName, blogPublicKey));
if (message != null) list.add(message);
return list;
}
@Override
public BdfDictionary toBdfDictionary() {
BdfDictionary d = toBdfDictionaryHelper();
d.put(BLOG_TITLE, blogTitle);
d.put(BLOG_DESC, blogDesc);
d.put(BLOG_AUTHOR_NAME, blogAuthorName);
d.put(BLOG_PUBLIC_KEY, blogPublicKey);
if (message != null) d.put(INVITATION_MSG, message);
return d;
}
public static BlogInvitation from(GroupId groupId, BdfDictionary d)
throws FormatException {
SessionId sessionId = new SessionId(d.getRaw(SESSION_ID));
String blogTitle = d.getString(BLOG_TITLE);
String blogDesc = d.getString(BLOG_DESC);
String blogAuthorName = d.getString(BLOG_AUTHOR_NAME);
byte[] blogPublicKey = d.getRaw(BLOG_PUBLIC_KEY);
String message = d.getOptionalString(INVITATION_MSG);
return new BlogInvitation(groupId, sessionId, blogTitle,
blogDesc, blogAuthorName, blogPublicKey, message);
}
public String getBlogTitle() {
return blogTitle;
}
public String getBlogDesc() {
return blogDesc;
}
public String getBlogAuthorName() {
return blogAuthorName;
}
public byte[] getBlogPublicKey() {
return blogPublicKey;
}
}
}

View File

@@ -0,0 +1,18 @@
package org.briarproject.api.event;
import org.briarproject.api.blogs.Blog;
import org.briarproject.api.contact.ContactId;
public class BlogInvitationReceivedEvent extends InvitationReceivedEvent {
private final Blog blog;
public BlogInvitationReceivedEvent(Blog blog, ContactId contactId) {
super(contactId);
this.blog = blog;
}
public Blog getBlog() {
return blog;
}
}

View File

@@ -0,0 +1,18 @@
package org.briarproject.api.event;
import org.briarproject.api.contact.ContactId;
public class BlogInvitationResponseReceivedEvent extends InvitationResponseReceivedEvent {
private final String blogTitle;
public BlogInvitationResponseReceivedEvent(String blogTitle,
ContactId contactId) {
super(contactId);
this.blogTitle = blogTitle;
}
public String getBlogTitle() {
return blogTitle;
}
}