Commit 59fae2fa authored by Torsten Grote's avatar Torsten Grote

Unify all private message requests in one PrivateRequest class

parent 2d9345c0
......@@ -43,8 +43,8 @@ import org.briarproject.briar.api.introduction.event.IntroductionRequestReceived
import org.briarproject.briar.api.introduction.event.IntroductionResponseReceivedEvent;
import org.briarproject.briar.api.messaging.ConversationManager;
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import org.briarproject.briar.api.messaging.PrivateRequest;
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent;
import org.briarproject.briar.api.sharing.InvitationRequest;
import org.briarproject.briar.api.sharing.InvitationResponse;
import org.briarproject.briar.api.sharing.event.InvitationRequestReceivedEvent;
import org.briarproject.briar.api.sharing.event.InvitationResponseReceivedEvent;
......@@ -271,7 +271,7 @@ public class ContactListFragment extends BaseFragment implements EventListener {
LOG.info("Invitation Request received, update item");
InvitationRequestReceivedEvent m =
(InvitationRequestReceivedEvent) e;
InvitationRequest ir = m.getRequest();
PrivateRequest ir = m.getRequest();
updateItem(m.getContactId(), ir);
} else if (e instanceof InvitationResponseReceivedEvent) {
LOG.info("Invitation response received, updating item");
......
......@@ -62,7 +62,6 @@ import org.briarproject.briar.api.client.ProtocolStateException;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.api.forum.ForumSharingManager;
import org.briarproject.briar.api.introduction.IntroductionManager;
import org.briarproject.briar.api.introduction.IntroductionMessage;
import org.briarproject.briar.api.introduction.IntroductionRequest;
import org.briarproject.briar.api.introduction.IntroductionResponse;
import org.briarproject.briar.api.introduction.event.IntroductionRequestReceivedEvent;
......@@ -71,10 +70,9 @@ import org.briarproject.briar.api.messaging.MessagingManager;
import org.briarproject.briar.api.messaging.PrivateMessage;
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import org.briarproject.briar.api.messaging.PrivateRequest;
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent;
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager;
import org.briarproject.briar.api.sharing.InvitationMessage;
import org.briarproject.briar.api.sharing.InvitationRequest;
import org.briarproject.briar.api.sharing.InvitationResponse;
import org.briarproject.briar.api.sharing.event.InvitationRequestReceivedEvent;
import org.briarproject.briar.api.sharing.event.InvitationResponseReceivedEvent;
......@@ -344,15 +342,15 @@ public class ConversationActivity extends BriarActivity
long start = now();
Collection<PrivateMessageHeader> headers =
messagingManager.getMessageHeaders(contactId);
Collection<IntroductionMessage> introductions =
Collection<PrivateMessageHeader> introductions =
introductionManager.getIntroductionMessages(contactId);
Collection<InvitationMessage> forumInvitations =
Collection<PrivateMessageHeader> forumInvitations =
forumSharingManager.getInvitationMessages(contactId);
Collection<InvitationMessage> blogInvitations =
Collection<PrivateMessageHeader> blogInvitations =
blogSharingManager.getInvitationMessages(contactId);
Collection<InvitationMessage> groupInvitations =
Collection<PrivateMessageHeader> groupInvitations =
groupInvitationManager.getInvitationMessages(contactId);
List<InvitationMessage> invitations = new ArrayList<>(
List<PrivateMessageHeader> invitations = new ArrayList<>(
forumInvitations.size() + blogInvitations.size() +
groupInvitations.size());
invitations.addAll(forumInvitations);
......@@ -370,8 +368,8 @@ public class ConversationActivity extends BriarActivity
private void displayMessages(int revision,
Collection<PrivateMessageHeader> headers,
Collection<IntroductionMessage> introductions,
Collection<InvitationMessage> invitations) {
Collection<PrivateMessageHeader> introductions,
Collection<PrivateMessageHeader> invitations) {
runOnUiThreadUnlessDestroyed(() -> {
if (revision == adapter.getRevision()) {
adapter.incrementRevision();
......@@ -397,8 +395,8 @@ public class ConversationActivity extends BriarActivity
@SuppressWarnings("ConstantConditions")
private List<ConversationItem> createItems(
Collection<PrivateMessageHeader> headers,
Collection<IntroductionMessage> introductions,
Collection<InvitationMessage> invitations) {
Collection<PrivateMessageHeader> introductions,
Collection<PrivateMessageHeader> invitations) {
int size =
headers.size() + introductions.size() + invitations.size();
List<ConversationItem> items = new ArrayList<>(size);
......@@ -409,7 +407,7 @@ public class ConversationActivity extends BriarActivity
else item.setBody(body);
items.add(item);
}
for (IntroductionMessage m : introductions) {
for (PrivateMessageHeader m : introductions) {
ConversationItem item;
if (m instanceof IntroductionRequest) {
IntroductionRequest i = (IntroductionRequest) m;
......@@ -420,11 +418,11 @@ public class ConversationActivity extends BriarActivity
}
items.add(item);
}
for (InvitationMessage i : invitations) {
for (PrivateMessageHeader i : invitations) {
ConversationItem item;
if (i instanceof InvitationRequest) {
InvitationRequest r = (InvitationRequest) i;
item = ConversationItem.from(this, contactName, r);
if (i instanceof PrivateRequest) {
item = ConversationItem
.from(this, contactName, (PrivateRequest) i);
} else {
InvitationResponse r = (InvitationResponse) i;
item = ConversationItem.from(this, contactName, r);
......@@ -525,7 +523,7 @@ public class ConversationActivity extends BriarActivity
(InvitationRequestReceivedEvent) e;
if (event.getContactId().equals(contactId)) {
LOG.info("Invitation received, adding...");
InvitationRequest ir = event.getRequest();
PrivateRequest ir = event.getRequest();
handleInvitationRequest(ir);
}
} else if (e instanceof InvitationResponseReceivedEvent) {
......@@ -586,7 +584,7 @@ public class ConversationActivity extends BriarActivity
});
}
private void handleInvitationRequest(InvitationRequest m) {
private void handleInvitationRequest(PrivateRequest m) {
getContactNameTask().addListener(new FutureTaskListener<String>() {
@Override
public void onSuccess(String contactName) {
......
......@@ -16,10 +16,11 @@ import org.briarproject.briar.api.forum.ForumInvitationResponse;
import org.briarproject.briar.api.introduction.IntroductionRequest;
import org.briarproject.briar.api.introduction.IntroductionResponse;
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import org.briarproject.briar.api.messaging.PrivateRequest;
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest;
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse;
import org.briarproject.briar.api.sharing.InvitationRequest;
import org.briarproject.briar.api.sharing.InvitationResponse;
import org.briarproject.briar.api.sharing.Shareable;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
......@@ -86,35 +87,6 @@ abstract class ConversationItem {
}
}
static ConversationItem from(Context ctx, String contactName,
IntroductionRequest ir) {
if (ir.isLocal()) {
String text = ctx.getString(R.string.introduction_request_sent,
contactName, ir.getName());
return new ConversationNoticeOutItem(ir.getId(),
ir.getGroupId(), text, ir.getMessage(), ir.getTimestamp(),
ir.isSent(), ir.isSeen());
} else {
String text;
if (ir.wasAnswered()) {
text = ctx.getString(
R.string.introduction_request_answered_received,
contactName, ir.getName());
} else if (ir.contactExists()){
text = ctx.getString(
R.string.introduction_request_exists_received,
contactName, ir.getName());
} else {
text = ctx.getString(R.string.introduction_request_received,
contactName, ir.getName());
}
return new ConversationRequestItem(ir.getId(),
ir.getGroupId(), INTRODUCTION, ir.getSessionId(), text,
ir.getMessage(), ir.getTimestamp(), ir.isRead(), null,
ir.wasAnswered(), false);
}
}
static ConversationItem from(Context ctx, String contactName,
IntroductionResponse ir) {
if (ir.isLocal()) {
......@@ -156,21 +128,22 @@ abstract class ConversationItem {
}
static ConversationItem from(Context ctx, String contactName,
InvitationRequest ir) {
PrivateRequest ir) {
if (ir.isLocal()) {
String text;
if (ir instanceof ForumInvitationRequest) {
if (ir instanceof IntroductionRequest) {
text = ctx.getString(R.string.introduction_request_sent,
contactName, ir.getName());
} else if (ir instanceof ForumInvitationRequest) {
text = ctx.getString(R.string.forum_invitation_sent,
((ForumInvitationRequest) ir).getForumName(),
contactName);
ir.getName(), contactName);
} else if (ir instanceof BlogInvitationRequest) {
text = ctx.getString(R.string.blogs_sharing_invitation_sent,
((BlogInvitationRequest) ir).getBlogAuthorName(),
contactName);
ir.getName(), contactName);
} else if (ir instanceof GroupInvitationRequest) {
text = ctx.getString(
R.string.groups_invitations_invitation_sent,
contactName, ir.getShareable().getName());
contactName, ir.getName());
} else {
throw new IllegalArgumentException("Unknown InvitationRequest");
}
......@@ -180,20 +153,36 @@ abstract class ConversationItem {
} else {
String text;
RequestType type;
if (ir instanceof ForumInvitationRequest) {
if (ir instanceof IntroductionRequest) {
type = INTRODUCTION;
if (ir.wasAnswered()) {
text = ctx.getString(
R.string.introduction_request_answered_received,
contactName, ir.getName());
} else if (ir.doesExist()) {
text = ctx.getString(
R.string.introduction_request_exists_received,
contactName, ir.getName());
} else {
text = ctx.getString(R.string.introduction_request_received,
contactName, ir.getName());
}
return new ConversationRequestItem(ir.getId(),
ir.getGroupId(), type, ir.getSessionId(), text,
ir.getMessage(), ir.getTimestamp(), ir.isRead(), null,
ir.wasAnswered(), false);
} else if (ir instanceof ForumInvitationRequest) {
text = ctx.getString(R.string.forum_invitation_received,
contactName,
((ForumInvitationRequest) ir).getForumName());
contactName, ir.getName());
type = FORUM;
} else if (ir instanceof BlogInvitationRequest) {
text = ctx.getString(R.string.blogs_sharing_invitation_received,
contactName,
((BlogInvitationRequest) ir).getBlogAuthorName());
contactName, ir.getName());
type = BLOG;
} else if (ir instanceof GroupInvitationRequest) {
text = ctx.getString(
R.string.groups_invitations_invitation_received,
contactName, ir.getShareable().getName());
contactName, ir.getName());
type = GROUP;
} else {
throw new IllegalArgumentException("Unknown InvitationRequest");
......@@ -201,8 +190,8 @@ abstract class ConversationItem {
return new ConversationRequestItem(ir.getId(),
ir.getGroupId(), type, ir.getSessionId(), text,
ir.getMessage(), ir.getTimestamp(), ir.isRead(),
ir.getShareable().getId(), !ir.isAvailable(),
ir.canBeOpened());
((Shareable) ir.getObject()).getId(), !ir.wasAnswered(),
ir.doesExist());
}
}
......@@ -276,8 +265,8 @@ abstract class ConversationItem {
return from(ctx, "", (IntroductionRequest) h);
} else if(h instanceof IntroductionResponse) {
return from(ctx, "", (IntroductionResponse) h);
} else if(h instanceof InvitationRequest) {
return from(ctx, "", (InvitationRequest) h);
} else if(h instanceof PrivateRequest) {
return from(ctx, "", (PrivateRequest) h);
} else if(h instanceof InvitationResponse) {
return from(ctx, "", (InvitationResponse) h);
} else {
......
package org.briarproject.briar.api.blog;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.api.sharing.InvitationRequest;
import org.briarproject.briar.api.messaging.PrivateRequest;
import javax.annotation.Nullable;
@NotNullByDefault
public class BlogInvitationRequest extends InvitationRequest<Blog> {
public class BlogInvitationRequest extends PrivateRequest<Blog> {
public BlogInvitationRequest(MessageId id, GroupId groupId, long time,
boolean local, boolean sent, boolean seen, boolean read,
SessionId sessionId, Blog blog, ContactId contactId,
SessionId sessionId, Blog blog,
@Nullable String message, boolean available, boolean canBeOpened) {
super(id, groupId, time, local, sent, seen, read, sessionId, blog,
message, available, canBeOpened);
}
public String getBlogAuthorName() {
return getShareable().getName();
}
}
......@@ -3,7 +3,7 @@ package org.briarproject.briar.api.blog.event;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.api.blog.Blog;
import org.briarproject.briar.api.sharing.InvitationRequest;
import org.briarproject.briar.api.messaging.PrivateRequest;
import org.briarproject.briar.api.sharing.event.InvitationRequestReceivedEvent;
import javax.annotation.concurrent.Immutable;
......@@ -14,7 +14,7 @@ public class BlogInvitationRequestReceivedEvent extends
InvitationRequestReceivedEvent<Blog> {
public BlogInvitationRequestReceivedEvent(Blog blog, ContactId contactId,
InvitationRequest<Blog> request) {
PrivateRequest<Blog> request) {
super(blog, contactId, request);
}
......
package org.briarproject.briar.api.forum;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.api.sharing.InvitationRequest;
import org.briarproject.briar.api.messaging.PrivateRequest;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
@Immutable
@NotNullByDefault
public class ForumInvitationRequest extends InvitationRequest<Forum> {
public class ForumInvitationRequest extends PrivateRequest<Forum> {
public ForumInvitationRequest(MessageId id, GroupId groupId, long time,
boolean local, boolean sent, boolean seen, boolean read,
SessionId sessionId, Forum forum, ContactId contactId,
SessionId sessionId, Forum forum,
@Nullable String message, boolean available, boolean canBeOpened) {
super(id, groupId, time, local, sent, seen, read, sessionId, forum,
message, available, canBeOpened);
}
public String getForumName() {
return getShareable().getName();
}
}
......@@ -3,7 +3,7 @@ package org.briarproject.briar.api.forum.event;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.api.forum.Forum;
import org.briarproject.briar.api.sharing.InvitationRequest;
import org.briarproject.briar.api.messaging.PrivateRequest;
import org.briarproject.briar.api.sharing.event.InvitationRequestReceivedEvent;
import javax.annotation.concurrent.Immutable;
......@@ -14,7 +14,7 @@ public class ForumInvitationRequestReceivedEvent extends
InvitationRequestReceivedEvent<Forum> {
public ForumInvitationRequestReceivedEvent(Forum forum, ContactId contactId,
InvitationRequest<Forum> request) {
PrivateRequest<Forum> request) {
super(forum, contactId, request);
}
......
package org.briarproject.briar.api.introduction;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.api.messaging.Nameable;
import javax.annotation.concurrent.Immutable;
import static org.briarproject.briar.api.introduction.Role.INTRODUCER;
@Immutable
@NotNullByDefault
public class Introduction implements Nameable {
private final Author introducedAuthor;
private final Role ourRole;
public Introduction(Author introducedAuthor, Role ourRole) {
this.introducedAuthor = introducedAuthor;
this.ourRole = ourRole;
}
@Override
public String getName() {
return introducedAuthor.getName();
}
public boolean isIntroducer() {
return ourRole == INTRODUCER;
}
}
......@@ -7,6 +7,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.ClientId;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.api.messaging.ConversationManager.ConversationClient;
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import java.util.Collection;
......@@ -50,7 +51,7 @@ public interface IntroductionManager extends ConversationClient {
/**
* Returns all introduction messages for the given contact.
*/
Collection<IntroductionMessage> getIntroductionMessages(ContactId contactId)
Collection<PrivateMessageHeader> getIntroductionMessages(ContactId contactId)
throws DbException;
}
package org.briarproject.briar.api.introduction;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import javax.annotation.concurrent.Immutable;
@Immutable
@NotNullByDefault
public abstract class IntroductionMessage extends PrivateMessageHeader {
private final SessionId sessionId;
IntroductionMessage(SessionId sessionId, MessageId messageId,
GroupId groupId, long time, boolean local, boolean sent,
boolean seen, boolean read) {
super(messageId, groupId, time, local, sent, seen, read);
this.sessionId = sessionId;
}
public SessionId getSessionId() {
return sessionId;
}
}
package org.briarproject.briar.api.introduction;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.api.messaging.PrivateRequest;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
@Immutable
@NotNullByDefault
public class IntroductionRequest extends IntroductionMessage {
public class IntroductionRequest extends PrivateRequest<Introduction> {
private final Author introducedAuthor;
@Nullable
private final String message;
private final boolean answered, exists;
public IntroductionRequest(SessionId sessionId, MessageId messageId,
GroupId groupId, long time, boolean local, boolean sent,
boolean seen, boolean read, Author introducedAuthor,
public IntroductionRequest(MessageId messageId, GroupId groupId,
long time, boolean local, boolean sent, boolean seen, boolean read,
SessionId sessionId, Introduction introduction,
@Nullable String message, boolean answered, boolean exists) {
super(sessionId, messageId, groupId, time, local, sent, seen, read);
this.introducedAuthor = introducedAuthor;
this.message = message;
this.answered = answered;
this.exists = exists;
}
public String getName() {
return introducedAuthor.getName();
}
@Nullable
public String getMessage() {
return message;
}
public boolean wasAnswered() {
return answered;
super(messageId, groupId, time, local, sent, seen, read, sessionId,
introduction, message, answered, exists);
}
public boolean contactExists() {
return exists;
}
}
......@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import javax.annotation.concurrent.Immutable;
......@@ -11,8 +12,9 @@ import static org.briarproject.briar.api.introduction.Role.INTRODUCER;
@Immutable
@NotNullByDefault
public class IntroductionResponse extends IntroductionMessage {
public class IntroductionResponse extends PrivateMessageHeader {
private final SessionId sessionId;
private final String name;
private final Role role;
private final boolean accepted;
......@@ -20,12 +22,17 @@ public class IntroductionResponse extends IntroductionMessage {
public IntroductionResponse(SessionId sessionId, MessageId messageId,
GroupId groupId, Role role, long time, boolean local, boolean sent,
boolean seen, boolean read, String name, boolean accepted) {
super(sessionId, messageId, groupId, time, local, sent, seen, read);
super(messageId, groupId, time, local, sent, seen, read);
this.sessionId = sessionId;
this.name = name;
this.role = role;
this.accepted = accepted;
}
public SessionId getSessionId() {
return sessionId;
}
public String getName() {
return name;
}
......
package org.briarproject.briar.api.messaging;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@NotNullByDefault
public interface Nameable {
String getName();
}
package org.briarproject.briar.api.sharing;
package org.briarproject.briar.api.messaging;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
......@@ -10,40 +10,48 @@ import javax.annotation.concurrent.Immutable;
@Immutable
@NotNullByDefault
public abstract class InvitationRequest<S extends Shareable>
extends InvitationMessage {
public class PrivateRequest<O extends Nameable> extends PrivateMessageHeader {
private final S shareable;
private final SessionId sessionId;
private final O object;
@Nullable
private final String message;
private final boolean available, canBeOpened;
private final boolean answered, exists;
public InvitationRequest(MessageId id, GroupId groupId, long time,
public PrivateRequest(MessageId messageId, GroupId groupId, long time,
boolean local, boolean sent, boolean seen, boolean read,
SessionId sessionId, S shareable,
@Nullable String message, boolean available, boolean canBeOpened) {
super(id, groupId, time, local, sent, seen, read, sessionId);
this.shareable = shareable;
SessionId sessionId, O object, @Nullable String message,
boolean answered, boolean exists) {
super(messageId, groupId, time, local, sent, seen, read);
this.sessionId = sessionId;
this.object = object;
this.message = message;
this.available = available;
this.canBeOpened = canBeOpened;
this.answered = answered;
this.exists = exists;
}
@Nullable
public String getMessage() {
return message;
public SessionId getSessionId() {
return sessionId;
}
public boolean isAvailable() {
return available;
public O getObject() {
return object;
}
public boolean canBeOpened() {
return canBeOpened;
public String getName() {
return object.getName();
}
public S getShareable() {
return shareable;
@Nullable
public String getMessage() {
return message;
}
public boolean wasAnswered() {
return answered;
}
public boolean doesExist() {
return exists;
}
}
......@@ -9,8 +9,8 @@ import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.briar.api.client.ProtocolStateException;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.api.messaging.ConversationManager.ConversationClient;
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import org.briarproject.briar.api.privategroup.PrivateGroup;
import org.briarproject.briar.api.sharing.InvitationMessage;
import java.util.Collection;
......@@ -77,7 +77,7 @@ public interface GroupInvitationManager extends ConversationClient {
* Returns all private group invitation messages related to the given
* contact.
*/
Collection<InvitationMessage> getInvitationMessages(ContactId c)
Collection<PrivateMessageHeader> getInvitationMessages(ContactId c)
throws DbException;
/**
......
package org.briarproject.briar.api.privategroup.invitation;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.api.messaging.PrivateRequest;
import org.briarproject.briar.api.privategroup.PrivateGroup;