mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 07:09:56 +01:00
Add a ConversationItemReceivedEvent
This commit is contained in:
@@ -16,20 +16,18 @@ import org.briarproject.android.contact.ConversationActivity;
|
|||||||
import org.briarproject.android.forum.ForumActivity;
|
import org.briarproject.android.forum.ForumActivity;
|
||||||
import org.briarproject.api.contact.Contact;
|
import org.briarproject.api.contact.Contact;
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
|
import org.briarproject.api.conversation.ConversationManager;
|
||||||
import org.briarproject.api.db.DatabaseExecutor;
|
import org.briarproject.api.db.DatabaseExecutor;
|
||||||
import org.briarproject.api.db.DbException;
|
import org.briarproject.api.db.DbException;
|
||||||
|
import org.briarproject.api.event.ConversationItemReceivedEvent;
|
||||||
import org.briarproject.api.event.Event;
|
import org.briarproject.api.event.Event;
|
||||||
import org.briarproject.api.event.EventListener;
|
import org.briarproject.api.event.EventListener;
|
||||||
import org.briarproject.api.event.ForumInvitationReceivedEvent;
|
|
||||||
import org.briarproject.api.event.IntroductionRequestReceivedEvent;
|
|
||||||
import org.briarproject.api.event.IntroductionResponseReceivedEvent;
|
|
||||||
import org.briarproject.api.event.IntroductionSucceededEvent;
|
import org.briarproject.api.event.IntroductionSucceededEvent;
|
||||||
import org.briarproject.api.event.MessageStateChangedEvent;
|
import org.briarproject.api.event.MessageStateChangedEvent;
|
||||||
import org.briarproject.api.event.SettingsUpdatedEvent;
|
import org.briarproject.api.event.SettingsUpdatedEvent;
|
||||||
import org.briarproject.api.forum.ForumManager;
|
import org.briarproject.api.forum.ForumManager;
|
||||||
import org.briarproject.api.lifecycle.Service;
|
import org.briarproject.api.lifecycle.Service;
|
||||||
import org.briarproject.api.lifecycle.ServiceException;
|
import org.briarproject.api.lifecycle.ServiceException;
|
||||||
import org.briarproject.api.messaging.MessagingManager;
|
|
||||||
import org.briarproject.api.settings.Settings;
|
import org.briarproject.api.settings.Settings;
|
||||||
import org.briarproject.api.settings.SettingsManager;
|
import org.briarproject.api.settings.SettingsManager;
|
||||||
import org.briarproject.api.sync.ClientId;
|
import org.briarproject.api.sync.ClientId;
|
||||||
@@ -75,9 +73,9 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
|
|||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(AndroidNotificationManagerImpl.class.getName());
|
Logger.getLogger(AndroidNotificationManagerImpl.class.getName());
|
||||||
|
|
||||||
|
private final ConversationManager conversationManager;
|
||||||
private final Executor dbExecutor;
|
private final Executor dbExecutor;
|
||||||
private final SettingsManager settingsManager;
|
private final SettingsManager settingsManager;
|
||||||
private final MessagingManager messagingManager;
|
|
||||||
private final ForumManager forumManager;
|
private final ForumManager forumManager;
|
||||||
private final AndroidExecutor androidExecutor;
|
private final AndroidExecutor androidExecutor;
|
||||||
private final Context appContext;
|
private final Context appContext;
|
||||||
@@ -94,13 +92,15 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
|
|||||||
private volatile Settings settings = new Settings();
|
private volatile Settings settings = new Settings();
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public AndroidNotificationManagerImpl(@DatabaseExecutor Executor dbExecutor,
|
public AndroidNotificationManagerImpl(
|
||||||
SettingsManager settingsManager, MessagingManager messagingManager,
|
ConversationManager conversationManager,
|
||||||
|
@DatabaseExecutor Executor dbExecutor,
|
||||||
|
SettingsManager settingsManager,
|
||||||
ForumManager forumManager, AndroidExecutor androidExecutor,
|
ForumManager forumManager, AndroidExecutor androidExecutor,
|
||||||
Application app) {
|
Application app) {
|
||||||
|
this.conversationManager = conversationManager;
|
||||||
this.dbExecutor = dbExecutor;
|
this.dbExecutor = dbExecutor;
|
||||||
this.settingsManager = settingsManager;
|
this.settingsManager = settingsManager;
|
||||||
this.messagingManager = messagingManager;
|
|
||||||
this.forumManager = forumManager;
|
this.forumManager = forumManager;
|
||||||
this.androidExecutor = androidExecutor;
|
this.androidExecutor = androidExecutor;
|
||||||
appContext = app.getApplicationContext();
|
appContext = app.getApplicationContext();
|
||||||
@@ -157,27 +157,19 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
|
|||||||
if (e instanceof SettingsUpdatedEvent) {
|
if (e instanceof SettingsUpdatedEvent) {
|
||||||
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
|
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
|
||||||
if (s.getNamespace().equals(SETTINGS_NAMESPACE)) loadSettings();
|
if (s.getNamespace().equals(SETTINGS_NAMESPACE)) loadSettings();
|
||||||
|
} else if (e instanceof ConversationItemReceivedEvent) {
|
||||||
|
ContactId c = ((ConversationItemReceivedEvent) e).getContactId();
|
||||||
|
showNotificationForPrivateConversation(c);
|
||||||
} else if (e instanceof MessageStateChangedEvent) {
|
} else if (e instanceof MessageStateChangedEvent) {
|
||||||
MessageStateChangedEvent m = (MessageStateChangedEvent) e;
|
MessageStateChangedEvent m = (MessageStateChangedEvent) e;
|
||||||
if (!m.isLocal() && m.getState() == DELIVERED) {
|
if (!m.isLocal() && m.getState() == DELIVERED) {
|
||||||
ClientId c = m.getClientId();
|
ClientId c = m.getClientId();
|
||||||
if (c.equals(messagingManager.getClientId()))
|
if (c.equals(forumManager.getClientId()))
|
||||||
showPrivateMessageNotification(m.getMessage().getGroupId());
|
|
||||||
else if (c.equals(forumManager.getClientId()))
|
|
||||||
showForumPostNotification(m.getMessage().getGroupId());
|
showForumPostNotification(m.getMessage().getGroupId());
|
||||||
}
|
}
|
||||||
} else if (e instanceof IntroductionRequestReceivedEvent) {
|
|
||||||
ContactId c = ((IntroductionRequestReceivedEvent) e).getContactId();
|
|
||||||
showNotificationForPrivateConversation(c);
|
|
||||||
} else if (e instanceof IntroductionResponseReceivedEvent) {
|
|
||||||
ContactId c = ((IntroductionResponseReceivedEvent) e).getContactId();
|
|
||||||
showNotificationForPrivateConversation(c);
|
|
||||||
} else if (e instanceof IntroductionSucceededEvent) {
|
} else if (e instanceof IntroductionSucceededEvent) {
|
||||||
Contact c = ((IntroductionSucceededEvent) e).getContact();
|
Contact c = ((IntroductionSucceededEvent) e).getContact();
|
||||||
showIntroductionSucceededNotification(c);
|
showIntroductionSucceededNotification(c);
|
||||||
} else if (e instanceof ForumInvitationReceivedEvent) {
|
|
||||||
ContactId c = ((ForumInvitationReceivedEvent) e).getContactId();
|
|
||||||
showNotificationForPrivateConversation(c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -384,7 +376,7 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
GroupId group = messagingManager.getConversationId(c);
|
GroupId group = conversationManager.getConversationId(c);
|
||||||
showPrivateMessageNotification(group);
|
showPrivateMessageNotification(group);
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
if (LOG.isLoggable(WARNING))
|
if (LOG.isLoggable(WARNING))
|
||||||
|
|||||||
@@ -87,15 +87,6 @@ public abstract class BaseContactListAdapter<VH extends BaseContactListAdapter.B
|
|||||||
return INVALID_POSITION; // Not found
|
return INVALID_POSITION; // Not found
|
||||||
}
|
}
|
||||||
|
|
||||||
int findItemPosition(GroupId g) {
|
|
||||||
int count = getItemCount();
|
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
ContactListItem item = getItem(i);
|
|
||||||
if (item.getGroupId().equals(g)) return i;
|
|
||||||
}
|
|
||||||
return INVALID_POSITION; // Not found
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addAll(List<ContactListItem> contacts) {
|
public void addAll(List<ContactListItem> contacts) {
|
||||||
this.contacts.addAll(contacts);
|
this.contacts.addAll(contacts);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ import org.briarproject.android.util.BriarRecyclerView;
|
|||||||
import org.briarproject.api.contact.Contact;
|
import org.briarproject.api.contact.Contact;
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
import org.briarproject.api.contact.ContactManager;
|
import org.briarproject.api.contact.ContactManager;
|
||||||
|
import org.briarproject.api.conversation.ConversationItem;
|
||||||
|
import org.briarproject.api.conversation.ConversationItem.IncomingItem;
|
||||||
import org.briarproject.api.conversation.ConversationManager;
|
import org.briarproject.api.conversation.ConversationManager;
|
||||||
import org.briarproject.api.db.DbException;
|
import org.briarproject.api.db.DbException;
|
||||||
import org.briarproject.api.db.NoSuchContactException;
|
import org.briarproject.api.db.NoSuchContactException;
|
||||||
@@ -31,16 +33,13 @@ import org.briarproject.api.event.ContactConnectedEvent;
|
|||||||
import org.briarproject.api.event.ContactDisconnectedEvent;
|
import org.briarproject.api.event.ContactDisconnectedEvent;
|
||||||
import org.briarproject.api.event.ContactRemovedEvent;
|
import org.briarproject.api.event.ContactRemovedEvent;
|
||||||
import org.briarproject.api.event.ContactStatusChangedEvent;
|
import org.briarproject.api.event.ContactStatusChangedEvent;
|
||||||
|
import org.briarproject.api.event.ConversationItemReceivedEvent;
|
||||||
import org.briarproject.api.event.Event;
|
import org.briarproject.api.event.Event;
|
||||||
import org.briarproject.api.event.EventBus;
|
import org.briarproject.api.event.EventBus;
|
||||||
import org.briarproject.api.event.EventListener;
|
import org.briarproject.api.event.EventListener;
|
||||||
import org.briarproject.api.event.MessageStateChangedEvent;
|
|
||||||
import org.briarproject.api.event.PrivateMessageReceivedEvent;
|
|
||||||
import org.briarproject.api.identity.IdentityManager;
|
import org.briarproject.api.identity.IdentityManager;
|
||||||
import org.briarproject.api.identity.LocalAuthor;
|
import org.briarproject.api.identity.LocalAuthor;
|
||||||
import org.briarproject.api.messaging.PrivateMessageHeader;
|
|
||||||
import org.briarproject.api.plugins.ConnectionRegistry;
|
import org.briarproject.api.plugins.ConnectionRegistry;
|
||||||
import org.briarproject.api.sync.ClientId;
|
|
||||||
import org.briarproject.api.sync.GroupId;
|
import org.briarproject.api.sync.GroupId;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -53,7 +52,6 @@ import static android.support.v4.app.ActivityOptionsCompat.makeSceneTransitionAn
|
|||||||
import static java.util.logging.Level.INFO;
|
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.android.BriarActivity.GROUP_ID;
|
import static org.briarproject.android.BriarActivity.GROUP_ID;
|
||||||
import static org.briarproject.api.sync.ValidationManager.State.DELIVERED;
|
|
||||||
|
|
||||||
public class ContactListFragment extends BaseFragment implements EventListener {
|
public class ContactListFragment extends BaseFragment implements EventListener {
|
||||||
|
|
||||||
@@ -197,12 +195,14 @@ public class ContactListFragment extends BaseFragment implements EventListener {
|
|||||||
connectionRegistry.isConnected(c.getId());
|
connectionRegistry.isConnected(c.getId());
|
||||||
LocalAuthor localAuthor = identityManager
|
LocalAuthor localAuthor = identityManager
|
||||||
.getLocalAuthor(c.getLocalAuthorId());
|
.getLocalAuthor(c.getLocalAuthorId());
|
||||||
long timestamp = conversationManager.getTimestamp(id);
|
long timestamp =
|
||||||
|
conversationManager.getTimestamp(id);
|
||||||
long now1 = System.currentTimeMillis();
|
long now1 = System.currentTimeMillis();
|
||||||
int unread = conversationManager.getUnreadCount(id);
|
int unread = conversationManager.getUnreadCount(id);
|
||||||
long duration = System.currentTimeMillis() - now1;
|
long duration = System.currentTimeMillis() - now1;
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Loading unread messages took " + duration + " ms");
|
LOG.info("Loading unread messages took " +
|
||||||
|
duration + " ms");
|
||||||
contacts.add(new ContactListItem(c, localAuthor,
|
contacts.add(new ContactListItem(c, localAuthor,
|
||||||
connected, groupId, timestamp, unread));
|
connected, groupId, timestamp, unread));
|
||||||
} catch (NoSuchContactException e) {
|
} catch (NoSuchContactException e) {
|
||||||
@@ -248,64 +248,27 @@ public class ContactListFragment extends BaseFragment implements EventListener {
|
|||||||
} else if (e instanceof ContactRemovedEvent) {
|
} else if (e instanceof ContactRemovedEvent) {
|
||||||
LOG.info("Contact removed");
|
LOG.info("Contact removed");
|
||||||
removeItem(((ContactRemovedEvent) e).getContactId());
|
removeItem(((ContactRemovedEvent) e).getContactId());
|
||||||
} else if (e instanceof PrivateMessageReceivedEvent) {
|
} else if (e instanceof ConversationItemReceivedEvent) {
|
||||||
LOG.info("Message received, update contact");
|
LOG.info("Message received, update contact");
|
||||||
PrivateMessageReceivedEvent p = (PrivateMessageReceivedEvent) e;
|
ConversationItemReceivedEvent event =
|
||||||
PrivateMessageHeader h = p.getMessageHeader();
|
(ConversationItemReceivedEvent) e;
|
||||||
updateItem(p.getGroupId(), h.getTimestamp(), h.isRead());
|
ConversationItem item = event.getItem();
|
||||||
} else if (e instanceof MessageStateChangedEvent) {
|
updateItem(event.getContactId(), item.getTime(),
|
||||||
MessageStateChangedEvent m = (MessageStateChangedEvent) e;
|
((IncomingItem) item).isRead());
|
||||||
ClientId c = m.getClientId();
|
|
||||||
if (m.getState() == DELIVERED && conversationManager.isWrappedClient(c)) {
|
|
||||||
LOG.info("Message added, reloading");
|
|
||||||
reloadConversation(m.getMessage().getGroupId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reloadConversation(final GroupId g) {
|
private void updateItem(final ContactId c, final long timestamp,
|
||||||
listener.runOnDbThread(new Runnable() {
|
final boolean read) {
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
ContactId c = conversationManager.getContactId(g);
|
|
||||||
long timestamp = conversationManager.getTimestamp(c);
|
|
||||||
int unread = conversationManager.getUnreadCount(c);
|
|
||||||
updateItem(c, timestamp, unread);
|
|
||||||
} catch (NoSuchContactException e) {
|
|
||||||
LOG.info("Contact removed");
|
|
||||||
} catch (DbException e) {
|
|
||||||
if (LOG.isLoggable(WARNING))
|
|
||||||
LOG.log(WARNING, e.toString(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateItem(final ContactId c, final long timestamp, final int unread) {
|
|
||||||
listener.runOnUiThread(new Runnable() {
|
listener.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
int position = adapter.findItemPosition(c);
|
int position = adapter.findItemPosition(c);
|
||||||
ContactListItem item = adapter.getItem(position);
|
ContactListItem item = adapter.getItem(position);
|
||||||
if (item != null) {
|
if (item != null) {
|
||||||
item.setTimestamp(timestamp);
|
if (timestamp > item.getTimestamp())
|
||||||
item.setUnreadCount(unread);
|
item.setTimestamp(timestamp);
|
||||||
adapter.updateItem(position, item);
|
if (!read)
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateItem(final GroupId g, final long timestamp, final boolean unread) {
|
|
||||||
listener.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
int position = adapter.findItemPosition(g);
|
|
||||||
ContactListItem item = adapter.getItem(position);
|
|
||||||
if (item != null) {
|
|
||||||
item.setTimestamp(timestamp);
|
|
||||||
if (unread)
|
|
||||||
item.setUnreadCount(item.getUnreadCount() + 1);
|
item.setUnreadCount(item.getUnreadCount() + 1);
|
||||||
adapter.updateItem(position, item);
|
adapter.updateItem(position, item);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,6 @@ import org.briarproject.api.FormatException;
|
|||||||
import org.briarproject.api.contact.Contact;
|
import org.briarproject.api.contact.Contact;
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
import org.briarproject.api.contact.ContactManager;
|
import org.briarproject.api.contact.ContactManager;
|
||||||
import org.briarproject.api.conversation.ConversationIntroductionRequestItem;
|
|
||||||
import org.briarproject.api.conversation.ConversationIntroductionResponseItem;
|
|
||||||
import org.briarproject.api.conversation.ConversationItem;
|
import org.briarproject.api.conversation.ConversationItem;
|
||||||
import org.briarproject.api.conversation.ConversationItem.IncomingItem;
|
import org.briarproject.api.conversation.ConversationItem.IncomingItem;
|
||||||
import org.briarproject.api.conversation.ConversationManager;
|
import org.briarproject.api.conversation.ConversationManager;
|
||||||
@@ -44,17 +42,12 @@ import org.briarproject.api.db.NoSuchContactException;
|
|||||||
import org.briarproject.api.event.ContactConnectedEvent;
|
import org.briarproject.api.event.ContactConnectedEvent;
|
||||||
import org.briarproject.api.event.ContactDisconnectedEvent;
|
import org.briarproject.api.event.ContactDisconnectedEvent;
|
||||||
import org.briarproject.api.event.ContactRemovedEvent;
|
import org.briarproject.api.event.ContactRemovedEvent;
|
||||||
|
import org.briarproject.api.event.ConversationItemReceivedEvent;
|
||||||
import org.briarproject.api.event.Event;
|
import org.briarproject.api.event.Event;
|
||||||
import org.briarproject.api.event.EventBus;
|
import org.briarproject.api.event.EventBus;
|
||||||
import org.briarproject.api.event.EventListener;
|
import org.briarproject.api.event.EventListener;
|
||||||
import org.briarproject.api.event.ForumInvitationReceivedEvent;
|
|
||||||
import org.briarproject.api.event.IntroductionRequestReceivedEvent;
|
|
||||||
import org.briarproject.api.event.IntroductionResponseReceivedEvent;
|
|
||||||
import org.briarproject.api.event.MessagesAckedEvent;
|
import org.briarproject.api.event.MessagesAckedEvent;
|
||||||
import org.briarproject.api.event.MessagesSentEvent;
|
import org.briarproject.api.event.MessagesSentEvent;
|
||||||
import org.briarproject.api.event.PrivateMessageReceivedEvent;
|
|
||||||
import org.briarproject.api.introduction.IntroductionRequest;
|
|
||||||
import org.briarproject.api.introduction.IntroductionResponse;
|
|
||||||
import org.briarproject.api.messaging.PrivateMessage;
|
import org.briarproject.api.messaging.PrivateMessage;
|
||||||
import org.briarproject.api.messaging.PrivateMessageFactory;
|
import org.briarproject.api.messaging.PrivateMessageFactory;
|
||||||
import org.briarproject.api.plugins.ConnectionRegistry;
|
import org.briarproject.api.plugins.ConnectionRegistry;
|
||||||
@@ -399,15 +392,13 @@ public class ConversationActivity extends BriarActivity
|
|||||||
LOG.info("Contact removed");
|
LOG.info("Contact removed");
|
||||||
finishOnUiThread();
|
finishOnUiThread();
|
||||||
}
|
}
|
||||||
} else if (e instanceof PrivateMessageReceivedEvent) {
|
} else if (e instanceof ConversationItemReceivedEvent) {
|
||||||
PrivateMessageReceivedEvent p = (PrivateMessageReceivedEvent) e;
|
ConversationItemReceivedEvent event =
|
||||||
if (p.getGroupId().equals(groupId)) {
|
(ConversationItemReceivedEvent) e;
|
||||||
|
if (event.getContactId().equals(contactId)) {
|
||||||
LOG.info("Message received, adding");
|
LOG.info("Message received, adding");
|
||||||
PrivateMessageHeader h = p.getMessageHeader();
|
addConversationItem(event.getItem());
|
||||||
ConversationMessageItem m = ConversationMessageItem.from(h);
|
markMessageReadIfNew(event.getItem());
|
||||||
conversationManager.loadMessageContent(m);
|
|
||||||
addConversationItem(m);
|
|
||||||
markMessageReadIfNew(m);
|
|
||||||
}
|
}
|
||||||
} else if (e instanceof MessagesSentEvent) {
|
} else if (e instanceof MessagesSentEvent) {
|
||||||
MessagesSentEvent m = (MessagesSentEvent) e;
|
MessagesSentEvent m = (MessagesSentEvent) e;
|
||||||
@@ -435,30 +426,6 @@ public class ConversationActivity extends BriarActivity
|
|||||||
connected = false;
|
connected = false;
|
||||||
displayContactDetails();
|
displayContactDetails();
|
||||||
}
|
}
|
||||||
} else if (e instanceof IntroductionRequestReceivedEvent) {
|
|
||||||
IntroductionRequestReceivedEvent event =
|
|
||||||
(IntroductionRequestReceivedEvent) e;
|
|
||||||
if (event.getContactId().equals(contactId)) {
|
|
||||||
IntroductionRequest ir = event.getIntroductionRequest();
|
|
||||||
ConversationItem item =
|
|
||||||
ConversationIntroductionRequestItem.from(ir);
|
|
||||||
addConversationItem(item);
|
|
||||||
}
|
|
||||||
} else if (e instanceof IntroductionResponseReceivedEvent) {
|
|
||||||
IntroductionResponseReceivedEvent event =
|
|
||||||
(IntroductionResponseReceivedEvent) e;
|
|
||||||
if (event.getContactId().equals(contactId)) {
|
|
||||||
IntroductionResponse ir = event.getIntroductionResponse();
|
|
||||||
ConversationItem item =
|
|
||||||
ConversationIntroductionResponseItem.from(ir);
|
|
||||||
addConversationItem(item);
|
|
||||||
}
|
|
||||||
} else if (e instanceof ForumInvitationReceivedEvent) {
|
|
||||||
ForumInvitationReceivedEvent event =
|
|
||||||
(ForumInvitationReceivedEvent) e;
|
|
||||||
if (event.getContactId().equals(contactId)) {
|
|
||||||
loadMessages();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,11 +17,6 @@ public interface ConversationManager {
|
|||||||
*/
|
*/
|
||||||
ClientId getClientId();
|
ClientId getClientId();
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if this is the id of a wrapped client.
|
|
||||||
*/
|
|
||||||
boolean isWrappedClient(ClientId clientId);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores a local private message, and returns the corresponding item.
|
* Stores a local private message, and returns the corresponding item.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.briarproject.api.event;
|
||||||
|
|
||||||
|
import org.briarproject.api.contact.ContactId;
|
||||||
|
import org.briarproject.api.conversation.ConversationItem;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event that is broadcast when a new conversation item is received.
|
||||||
|
*/
|
||||||
|
public class ConversationItemReceivedEvent extends Event {
|
||||||
|
|
||||||
|
private final ConversationItem item;
|
||||||
|
private final ContactId contactId;
|
||||||
|
|
||||||
|
public ConversationItemReceivedEvent(ConversationItem item,
|
||||||
|
ContactId contactId) {
|
||||||
|
this.item = item;
|
||||||
|
this.contactId = contactId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConversationItem getItem() {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ContactId getContactId() {
|
||||||
|
return contactId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,14 @@ import org.briarproject.api.conversation.ConversationMessageItem;
|
|||||||
import org.briarproject.api.db.DatabaseExecutor;
|
import org.briarproject.api.db.DatabaseExecutor;
|
||||||
import org.briarproject.api.db.DbException;
|
import org.briarproject.api.db.DbException;
|
||||||
import org.briarproject.api.db.NoSuchMessageException;
|
import org.briarproject.api.db.NoSuchMessageException;
|
||||||
|
import org.briarproject.api.event.ConversationItemReceivedEvent;
|
||||||
|
import org.briarproject.api.event.Event;
|
||||||
|
import org.briarproject.api.event.EventBus;
|
||||||
|
import org.briarproject.api.event.EventListener;
|
||||||
|
import org.briarproject.api.event.ForumInvitationReceivedEvent;
|
||||||
|
import org.briarproject.api.event.IntroductionRequestReceivedEvent;
|
||||||
|
import org.briarproject.api.event.IntroductionResponseReceivedEvent;
|
||||||
|
import org.briarproject.api.event.PrivateMessageReceivedEvent;
|
||||||
import org.briarproject.api.forum.ForumInvitationMessage;
|
import org.briarproject.api.forum.ForumInvitationMessage;
|
||||||
import org.briarproject.api.forum.ForumSharingManager;
|
import org.briarproject.api.forum.ForumSharingManager;
|
||||||
import org.briarproject.api.introduction.IntroductionManager;
|
import org.briarproject.api.introduction.IntroductionManager;
|
||||||
@@ -41,7 +49,8 @@ import javax.inject.Inject;
|
|||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
|
|
||||||
public class ConversationManagerImpl implements ConversationManager {
|
public class ConversationManagerImpl implements ConversationManager,
|
||||||
|
EventListener {
|
||||||
|
|
||||||
static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString(
|
static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString(
|
||||||
"05313ec596871e5305181220488fc9c0"
|
"05313ec596871e5305181220488fc9c0"
|
||||||
@@ -51,6 +60,7 @@ public class ConversationManagerImpl implements ConversationManager {
|
|||||||
Logger.getLogger(ConversationManagerImpl.class.getName());
|
Logger.getLogger(ConversationManagerImpl.class.getName());
|
||||||
|
|
||||||
private final Executor dbExecutor;
|
private final Executor dbExecutor;
|
||||||
|
private final EventBus eventBus;
|
||||||
private final ForumSharingManager forumSharingManager;
|
private final ForumSharingManager forumSharingManager;
|
||||||
private final IntroductionManager introductionManager;
|
private final IntroductionManager introductionManager;
|
||||||
private final MessagingManager messagingManager;
|
private final MessagingManager messagingManager;
|
||||||
@@ -60,13 +70,16 @@ public class ConversationManagerImpl implements ConversationManager {
|
|||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ConversationManagerImpl(@DatabaseExecutor Executor dbExecutor,
|
ConversationManagerImpl(@DatabaseExecutor Executor dbExecutor,
|
||||||
ForumSharingManager forumSharingManager,
|
EventBus eventBus, ForumSharingManager forumSharingManager,
|
||||||
IntroductionManager introductionManager,
|
IntroductionManager introductionManager,
|
||||||
MessagingManager messagingManager) {
|
MessagingManager messagingManager) {
|
||||||
this.dbExecutor = dbExecutor;
|
this.dbExecutor = dbExecutor;
|
||||||
|
this.eventBus = eventBus;
|
||||||
this.forumSharingManager = forumSharingManager;
|
this.forumSharingManager = forumSharingManager;
|
||||||
this.introductionManager = introductionManager;
|
this.introductionManager = introductionManager;
|
||||||
this.messagingManager = messagingManager;
|
this.messagingManager = messagingManager;
|
||||||
|
|
||||||
|
eventBus.addListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -74,12 +87,6 @@ public class ConversationManagerImpl implements ConversationManager {
|
|||||||
return CLIENT_ID;
|
return CLIENT_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isWrappedClient(ClientId clientId) {
|
|
||||||
return clientId.equals(introductionManager.getClientId()) ||
|
|
||||||
clientId.equals(forumSharingManager.getClientId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConversationItem addLocalMessage(PrivateMessage m, byte[] body)
|
public ConversationItem addLocalMessage(PrivateMessage m, byte[] body)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
@@ -218,4 +225,57 @@ public class ConversationManagerImpl implements ConversationManager {
|
|||||||
forumSharingManager.setReadFlag(c, id, local, read);
|
forumSharingManager.setReadFlag(c, id, local, read);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eventOccurred(Event e) {
|
||||||
|
if (e instanceof PrivateMessageReceivedEvent) {
|
||||||
|
PrivateMessageReceivedEvent p = (PrivateMessageReceivedEvent) e;
|
||||||
|
PrivateMessageHeader h = p.getMessageHeader();
|
||||||
|
ConversationItem m = ConversationMessageItemImpl.from(h);
|
||||||
|
loadMessageContent((Partial) m);
|
||||||
|
try {
|
||||||
|
ContactId c = getContactId(p.getGroupId());
|
||||||
|
eventBus.broadcast(new ConversationItemReceivedEvent(m, c));
|
||||||
|
} catch (DbException dbe) {
|
||||||
|
if (LOG.isLoggable(WARNING))
|
||||||
|
LOG.log(WARNING, dbe.toString(), dbe);
|
||||||
|
}
|
||||||
|
} else if (e instanceof IntroductionRequestReceivedEvent) {
|
||||||
|
IntroductionRequestReceivedEvent event =
|
||||||
|
(IntroductionRequestReceivedEvent) e;
|
||||||
|
IntroductionRequest ir = event.getIntroductionRequest();
|
||||||
|
ConversationItem item =
|
||||||
|
ConversationIntroductionRequestItemImpl.from(ir);
|
||||||
|
eventBus.broadcast(
|
||||||
|
new ConversationItemReceivedEvent(item,
|
||||||
|
event.getContactId()));
|
||||||
|
} else if (e instanceof IntroductionResponseReceivedEvent) {
|
||||||
|
IntroductionResponseReceivedEvent event =
|
||||||
|
(IntroductionResponseReceivedEvent) e;
|
||||||
|
IntroductionResponse ir = event.getIntroductionResponse();
|
||||||
|
ConversationItem item =
|
||||||
|
ConversationIntroductionResponseItemImpl.from(ir);
|
||||||
|
eventBus.broadcast(new ConversationItemReceivedEvent(item,
|
||||||
|
event.getContactId()));
|
||||||
|
} else if (e instanceof ForumInvitationReceivedEvent) {
|
||||||
|
ForumInvitationReceivedEvent event =
|
||||||
|
(ForumInvitationReceivedEvent) e;
|
||||||
|
try {
|
||||||
|
Collection<ForumInvitationMessage> msgs = forumSharingManager
|
||||||
|
.getInvitationMessages(event.getContactId());
|
||||||
|
for (ForumInvitationMessage i : msgs) {
|
||||||
|
if (i.getForumName().equals(event.getForum().getName())) {
|
||||||
|
ConversationItem item =
|
||||||
|
ConversationForumInvitationItemImpl.from(i);
|
||||||
|
eventBus.broadcast(
|
||||||
|
new ConversationItemReceivedEvent(item,
|
||||||
|
event.getContactId()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (DbException dbe) {
|
||||||
|
if (LOG.isLoggable(WARNING))
|
||||||
|
LOG.log(WARNING, dbe.toString(), dbe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user