Use new metadata queries in Forum Sharing Client

Closes #392
This commit is contained in:
Torsten Grote
2016-05-12 15:26:38 -03:00
parent 7e9ff40837
commit 1a175beac9

View File

@@ -55,6 +55,7 @@ import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.api.clients.ProtocolEngine.StateUpdate; import static org.briarproject.api.clients.ProtocolEngine.StateUpdate;
import static org.briarproject.api.forum.ForumConstants.CONTACT_ID; import static org.briarproject.api.forum.ForumConstants.CONTACT_ID;
import static org.briarproject.api.forum.ForumConstants.FORUM_ID;
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH; import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH;
import static org.briarproject.api.forum.ForumConstants.IS_SHARER; import static org.briarproject.api.forum.ForumConstants.IS_SHARER;
import static org.briarproject.api.forum.ForumConstants.LOCAL; import static org.briarproject.api.forum.ForumConstants.LOCAL;
@@ -67,6 +68,7 @@ import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_ACCEPT;
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_DECLINE; import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_DECLINE;
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_INVITATION; import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_INVITATION;
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_LEAVE; import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_LEAVE;
import static org.briarproject.api.forum.ForumConstants.STATE;
import static org.briarproject.api.forum.ForumConstants.TASK_ADD_FORUM_TO_LIST_SHARED_WITH_US; import static org.briarproject.api.forum.ForumConstants.TASK_ADD_FORUM_TO_LIST_SHARED_WITH_US;
import static org.briarproject.api.forum.ForumConstants.TASK_ADD_FORUM_TO_LIST_TO_BE_SHARED_BY_US; import static org.briarproject.api.forum.ForumConstants.TASK_ADD_FORUM_TO_LIST_TO_BE_SHARED_BY_US;
import static org.briarproject.api.forum.ForumConstants.TASK_ADD_SHARED_FORUM; import static org.briarproject.api.forum.ForumConstants.TASK_ADD_SHARED_FORUM;
@@ -155,16 +157,18 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
@Override @Override
public void removingContact(Transaction txn, Contact c) throws DbException { public void removingContact(Transaction txn, Contact c) throws DbException {
// query for this contact c
BdfDictionary query = BdfDictionary.of(
new BdfEntry(CONTACT_ID, c.getId().getInt())
);
// clean up session states with that contact from localGroup // clean up session states with that contact from localGroup
Long id = (long) c.getId().getInt();
try { try {
Map<MessageId, BdfDictionary> map = clientHelper Map<MessageId, BdfDictionary> map = clientHelper
.getMessageMetadataAsDictionary(txn, localGroup.getId()); .getMessageMetadataAsDictionary(txn, localGroup.getId(),
query);
for (Map.Entry<MessageId, BdfDictionary> entry : map.entrySet()) { for (Map.Entry<MessageId, BdfDictionary> entry : map.entrySet()) {
BdfDictionary d = entry.getValue(); deleteMessage(txn, entry.getKey());
if (id.equals(d.getLong(CONTACT_ID))) {
deleteMessage(txn, entry.getKey());
}
} }
} catch (FormatException e) { } catch (FormatException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
@@ -312,6 +316,11 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
public Collection<ForumInvitationMessage> getForumInvitationMessages( public Collection<ForumInvitationMessage> getForumInvitationMessages(
ContactId contactId) throws DbException { ContactId contactId) throws DbException {
// query for all invitations
BdfDictionary query = BdfDictionary.of(
new BdfEntry(TYPE, SHARE_MSG_TYPE_INVITATION)
);
Transaction txn = db.startTransaction(false); Transaction txn = db.startTransaction(false);
try { try {
Contact contact = db.getContact(txn, contactId); Contact contact = db.getContact(txn, contactId);
@@ -320,13 +329,10 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
Collection<ForumInvitationMessage> list = Collection<ForumInvitationMessage> list =
new ArrayList<ForumInvitationMessage>(); new ArrayList<ForumInvitationMessage>();
Map<MessageId, BdfDictionary> map = clientHelper Map<MessageId, BdfDictionary> map = clientHelper
.getMessageMetadataAsDictionary(txn, group.getId()); .getMessageMetadataAsDictionary(txn, group.getId(), query);
for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) { for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) {
BdfDictionary d = m.getValue(); BdfDictionary d = m.getValue();
try { try {
if (d.getLong(TYPE) != SHARE_MSG_TYPE_INVITATION)
continue;
Invitation msg = Invitation.from(group.getId(), d); Invitation msg = Invitation.from(group.getId(), d);
MessageStatus status = MessageStatus status =
db.getMessageStatus(txn, contactId, m.getKey()); db.getMessageStatus(txn, contactId, m.getKey());
@@ -542,17 +548,25 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
try { try {
return getSessionStateForSharer(txn, sessionId); return getSessionStateForSharer(txn, sessionId);
} catch (NoSuchMessageException e) { } catch (NoSuchMessageException e) {
// State not found directly, so iterate over all states // State not found directly, so query for state for invitee
// to find state for invitee BdfDictionary query = BdfDictionary.of(
new BdfEntry(SESSION_ID, sessionId)
);
Map<MessageId, BdfDictionary> map = clientHelper Map<MessageId, BdfDictionary> map = clientHelper
.getMessageMetadataAsDictionary(txn, localGroup.getId()); .getMessageMetadataAsDictionary(txn, localGroup.getId(),
for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) { query);
BdfDictionary state = m.getValue();
if (Arrays.equals(state.getRaw(SESSION_ID), if (map.size() > 1 && LOG.isLoggable(WARNING)) {
sessionId.getBytes())) { LOG.warning(
return fromBdfDictionary(state); "More than one session state found for message with session ID " +
} Arrays.hashCode(sessionId.getBytes()));
} }
for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) {
return fromBdfDictionary(m.getValue());
}
if (warn && LOG.isLoggable(WARNING)) { if (warn && LOG.isLoggable(WARNING)) {
LOG.warning( LOG.warning(
"No session state found for message with session ID " + "No session state found for message with session ID " +
@@ -578,25 +592,36 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
private InviteeSessionState getSessionStateForResponse(Transaction txn, private InviteeSessionState getSessionStateForResponse(Transaction txn,
Forum f) throws DbException, FormatException { Forum f) throws DbException, FormatException {
Map<MessageId, BdfDictionary> map = clientHelper // query for invitee states for that forum in state await response
.getMessageMetadataAsDictionary(txn, localGroup.getId()); BdfDictionary query = BdfDictionary.of(
for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) { new BdfEntry(IS_SHARER, false),
BdfDictionary d = m.getValue(); new BdfEntry(FORUM_ID, f.getId()),
try { new BdfEntry(STATE,
ForumSharingSessionState s = fromBdfDictionary(d); InviteeSessionState.State.AWAIT_LOCAL_RESPONSE
if (!(s instanceof InviteeSessionState)) continue; .getValue())
if (!f.getId().equals(s.getForumId())) continue; );
InviteeSessionState state = (InviteeSessionState) s; Map<MessageId, BdfDictionary> map = clientHelper
if (state.getState() == .getMessageMetadataAsDictionary(txn, localGroup.getId(), query);
InviteeSessionState.State.AWAIT_LOCAL_RESPONSE) {
// Note that there should always be only one session // TODO we might have been invited to the same forum from several people
// in this state for the same forum // in different sessions, so we need to handle this (#391)
return state; if (map.size() > 1 && LOG.isLoggable(WARNING)) {
} LOG.warning(
} catch (FormatException e) { "More than one session state found for forum with ID " +
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); Arrays.hashCode(f.getId().getBytes()) +
} " in state AWAIT_LOCAL_RESPONSE");
}
for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) {
return (InviteeSessionState) fromBdfDictionary(m.getValue());
}
if (LOG.isLoggable(WARNING)) {
LOG.warning(
"No session state found for forum with ID " +
Arrays.hashCode(f.getId().getBytes()) +
" in state AWAIT_LOCAL_RESPONSE");
} }
throw new DbException(); throw new DbException();
} }
@@ -604,22 +629,17 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
private ForumSharingSessionState getSessionStateForLeaving(Transaction txn, private ForumSharingSessionState getSessionStateForLeaving(Transaction txn,
Forum f, ContactId c) throws DbException, FormatException { Forum f, ContactId c) throws DbException, FormatException {
BdfDictionary query = BdfDictionary.of(
new BdfEntry(CONTACT_ID, c.getInt()),
new BdfEntry(FORUM_ID, f.getId())
);
Map<MessageId, BdfDictionary> map = clientHelper Map<MessageId, BdfDictionary> map = clientHelper
.getMessageMetadataAsDictionary(txn, localGroup.getId()); .getMessageMetadataAsDictionary(txn, localGroup.getId(), query);
for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) { for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) {
BdfDictionary d = m.getValue(); BdfDictionary d = m.getValue();
try { try {
ForumSharingSessionState s = fromBdfDictionary(d); ForumSharingSessionState s = fromBdfDictionary(d);
// check that this session is with the right contact
if (!c.equals(s.getContactId())) continue;
// check that this state actually concerns this forum
if (!s.getForumName().equals(f.getName()) ||
!Arrays.equals(s.getForumSalt(), f.getSalt())) {
continue;
}
// check that a forum get be left in current session // check that a forum get be left in current session
if (s instanceof SharerSessionState) { if (s instanceof SharerSessionState) {
SharerSessionState state = (SharerSessionState) s; SharerSessionState state = (SharerSessionState) s;