Merge pull request #236 from loki-project/protocol

Update for Upcoming Desktop Protocol Changes
This commit is contained in:
Niels Andriesse 2020-06-22 08:47:26 +10:00 committed by GitHub
commit bb69780661
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 32 deletions

View File

@ -66,10 +66,13 @@ import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil;
import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.loki.activities.HomeActivity; import org.thoughtcrime.securesms.loki.activities.HomeActivity;
import org.thoughtcrime.securesms.loki.database.LokiMessageDatabase; import org.thoughtcrime.securesms.loki.database.LokiMessageDatabase;
import org.thoughtcrime.securesms.loki.database.LokiThreadDatabase;
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocol; import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocol;
import org.thoughtcrime.securesms.loki.protocol.EphemeralMessage;
import org.thoughtcrime.securesms.loki.protocol.FriendRequestProtocol; import org.thoughtcrime.securesms.loki.protocol.FriendRequestProtocol;
import org.thoughtcrime.securesms.loki.protocol.LokiSessionResetImplementation; import org.thoughtcrime.securesms.loki.protocol.LokiSessionResetImplementation;
import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol; import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol;
import org.thoughtcrime.securesms.loki.protocol.PushEphemeralMessageSendJob;
import org.thoughtcrime.securesms.loki.protocol.SessionManagementProtocol; import org.thoughtcrime.securesms.loki.protocol.SessionManagementProtocol;
import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol; import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol;
import org.thoughtcrime.securesms.loki.protocol.SyncMessagesProtocol; import org.thoughtcrime.securesms.loki.protocol.SyncMessagesProtocol;
@ -127,7 +130,7 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.loki.api.fileserver.LokiFileServerAPI; import org.whispersystems.signalservice.loki.api.fileserver.LokiFileServerAPI;
import org.whispersystems.signalservice.loki.crypto.LokiServiceCipher; import org.whispersystems.signalservice.loki.crypto.LokiServiceCipher;
import org.whispersystems.signalservice.loki.protocol.mentions.MentionsManager; import org.whispersystems.signalservice.loki.protocol.mentions.MentionsManager;
import org.whispersystems.signalservice.loki.protocol.meta.LokiServiceMessage; import org.whispersystems.signalservice.loki.protocol.todo.LokiThreadFriendRequestStatus;
import org.whispersystems.signalservice.loki.utilities.PublicKeyValidation; import org.whispersystems.signalservice.loki.utilities.PublicKeyValidation;
import java.security.MessageDigest; import java.security.MessageDigest;
@ -142,6 +145,8 @@ import javax.inject.Inject;
import network.loki.messenger.R; import network.loki.messenger.R;
import static org.thoughtcrime.securesms.loki.utilities.RecipientUtilitiesKt.recipient;
public class PushDecryptJob extends BaseJob implements InjectableType { public class PushDecryptJob extends BaseJob implements InjectableType {
public static final String KEY = "PushDecryptJob"; public static final String KEY = "PushDecryptJob";
@ -274,22 +279,11 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
return; return;
} }
// Loki - Handle friend request acceptance if needed
FriendRequestProtocol.handleFriendRequestAcceptanceIfNeeded(context, content.getSender(), content);
// Loki - Handle pre key bundle message if needed // Loki - Handle pre key bundle message if needed
SessionManagementProtocol.handlePreKeyBundleMessageIfNeeded(context, content); SessionManagementProtocol.handlePreKeyBundleMessageIfNeeded(context, content);
// Loki - Handle session request if needed // Loki - Handle session request if needed
SessionManagementProtocol.handleSessionRequestIfNeeded(context, content); if (SessionManagementProtocol.handleSessionRequestIfNeeded(context, content)) { return; } // Don't process the message any further
// Loki - Handle address message if needed
if (content.lokiServiceMessage.isPresent()) {
LokiServiceMessage lokiMessage = content.lokiServiceMessage.get();
if (lokiMessage.getAddressMessage() != null) {
// TODO: Loki - Handle address message
}
}
// Loki - Handle profile update if needed // Loki - Handle profile update if needed
SessionMetaProtocol.handleProfileUpdateIfNeeded(context, content); SessionMetaProtocol.handleProfileUpdateIfNeeded(context, content);
@ -304,8 +298,11 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
if (message.isUnlinkingRequest()) { if (message.isUnlinkingRequest()) {
MultiDeviceProtocol.handleUnlinkingRequestIfNeeded(context, content); MultiDeviceProtocol.handleUnlinkingRequestIfNeeded(context, content);
} else { } else {
// Loki - Don't process session restoration requests or session requests any further // Loki - Don't process session restoration requests any further
if (message.isSessionRestorationRequest() || message.isSessionRequest()) { return; } if (message.isSessionRestorationRequest()) { return; }
// Loki - Handle friend request acceptance if needed
FriendRequestProtocol.handleFriendRequestAcceptanceIfNeeded(context, content.getSender(), content);
if (message.isEndSession()) { if (message.isEndSession()) {
handleEndSessionMessage(content, smsMessageId); handleEndSessionMessage(content, smsMessageId);
@ -315,8 +312,52 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
handleExpirationUpdate(content, message, smsMessageId); handleExpirationUpdate(content, message, smsMessageId);
} else if (isMediaMessage) { } else if (isMediaMessage) {
handleMediaMessage(content, message, smsMessageId, Optional.absent()); handleMediaMessage(content, message, smsMessageId, Optional.absent());
// Loki - This is needed for compatibility with refactored desktop clients
if (!message.isGroupMessage()) {
Recipient recipient = recipient(context, content.getSender());
long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);
LokiThreadDatabase threadDB = DatabaseFactory.getLokiThreadDatabase(context);
LokiThreadFriendRequestStatus threadFriendRequestStatus = threadDB.getFriendRequestStatus(threadID);
if (threadFriendRequestStatus == LokiThreadFriendRequestStatus.NONE || threadFriendRequestStatus == LokiThreadFriendRequestStatus.REQUEST_EXPIRED) {
threadDB.setFriendRequestStatus(threadID, LokiThreadFriendRequestStatus.REQUEST_RECEIVED);
} else if (threadFriendRequestStatus == LokiThreadFriendRequestStatus.REQUEST_SENT) {
threadDB.setFriendRequestStatus(threadID, LokiThreadFriendRequestStatus.FRIENDS);
EphemeralMessage ephemeralMessage = EphemeralMessage.create(content.getSender());
ApplicationContext.getInstance(context).getJobManager().add(new PushEphemeralMessageSendJob(ephemeralMessage));
SyncMessagesProtocol.syncContact(context, Address.fromSerialized(content.getSender()));
}
// Loki - Handle friend request message if needed
FriendRequestProtocol.handleFriendRequestMessageIfNeeded(context, content.getSender(), content);
}
} else if (message.getBody().isPresent()) { } else if (message.getBody().isPresent()) {
handleTextMessage(content, message, smsMessageId, Optional.absent()); handleTextMessage(content, message, smsMessageId, Optional.absent());
// Loki - This is needed for compatibility with refactored desktop clients
if (!message.isGroupMessage()) {
Recipient recipient = recipient(context, content.getSender());
long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);
LokiThreadDatabase threadDB = DatabaseFactory.getLokiThreadDatabase(context);
LokiThreadFriendRequestStatus threadFriendRequestStatus = threadDB.getFriendRequestStatus(threadID);
if (threadFriendRequestStatus == LokiThreadFriendRequestStatus.NONE || threadFriendRequestStatus == LokiThreadFriendRequestStatus.REQUEST_EXPIRED) {
threadDB.setFriendRequestStatus(threadID, LokiThreadFriendRequestStatus.REQUEST_RECEIVED);
} else if (threadFriendRequestStatus == LokiThreadFriendRequestStatus.REQUEST_SENT) {
threadDB.setFriendRequestStatus(threadID, LokiThreadFriendRequestStatus.FRIENDS);
EphemeralMessage ephemeralMessage = EphemeralMessage.create(content.getSender());
ApplicationContext.getInstance(context).getJobManager().add(new PushEphemeralMessageSendJob(ephemeralMessage));
SyncMessagesProtocol.syncContact(context, Address.fromSerialized(content.getSender()));
}
// Loki - Handle friend request message if needed
FriendRequestProtocol.handleFriendRequestMessageIfNeeded(context, content.getSender(), content);
}
} else {
// Loki - This is needed for compatibility with refactored desktop clients
if (envelope.isFriendRequest()) {
EphemeralMessage ephemeralMessage = EphemeralMessage.create(content.getSender());
ApplicationContext.getInstance(context).getJobManager().add(new PushEphemeralMessageSendJob(ephemeralMessage));
}
} }
if (message.getGroupInfo().isPresent() && groupDatabase.isUnknownGroup(GroupUtil.getEncodedId(message.getGroupInfo().get()))) { if (message.getGroupInfo().isPresent() && groupDatabase.isUnknownGroup(GroupUtil.getEncodedId(message.getGroupInfo().get()))) {
@ -330,9 +371,6 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
if (content.isNeedsReceipt()) { if (content.isNeedsReceipt()) {
handleNeedsDeliveryReceipt(content, message); handleNeedsDeliveryReceipt(content, message);
} }
// Loki - Handle friend request message if needed
FriendRequestProtocol.handleFriendRequestMessageIfNeeded(context, content.getSender(), content);
} }
} else if (content.getSyncMessage().isPresent()) { } else if (content.getSyncMessage().isPresent()) {
TextSecurePreferences.setMultiDevice(context, true); TextSecurePreferences.setMultiDevice(context, true);

View File

@ -61,29 +61,19 @@ object SessionManagementProtocol {
Log.d("Loki", "Received a pre key bundle from: " + content.sender.toString() + ".") Log.d("Loki", "Received a pre key bundle from: " + content.sender.toString() + ".")
val preKeyBundle = preKeyBundleMessage.getPreKeyBundle(registrationID) val preKeyBundle = preKeyBundleMessage.getPreKeyBundle(registrationID)
lokiPreKeyBundleDatabase.setPreKeyBundle(content.sender, preKeyBundle) lokiPreKeyBundleDatabase.setPreKeyBundle(content.sender, preKeyBundle)
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient)
val lokiThreadDB = DatabaseFactory.getLokiThreadDatabase(context)
val threadFRStatus = lokiThreadDB.getFriendRequestStatus(threadID)
// If we received a friend request (i.e. also a new pre key bundle), but we were already friends with the other user, reset the session.
if (content.isFriendRequest && threadFRStatus == LokiThreadFriendRequestStatus.FRIENDS) {
val sessionStore = TextSecureSessionStore(context)
sessionStore.archiveAllSessions(content.sender)
val ephemeralMessage = EphemeralMessage.create(content.sender)
ApplicationContext.getInstance(context).jobManager.add(PushEphemeralMessageSendJob(ephemeralMessage))
}
} }
@JvmStatic @JvmStatic
fun handleSessionRequestIfNeeded(context: Context, content: SignalServiceContent) { fun handleSessionRequestIfNeeded(context: Context, content: SignalServiceContent): Boolean {
if (!content.dataMessage.isPresent || !content.dataMessage.get().isSessionRequest) { return } if (!content.dataMessage.isPresent || !content.dataMessage.get().isSessionRequest) { return false }
val sentSessionRequestTimestamp = DatabaseFactory.getLokiAPIDatabase(context).getSessionRequestTimestamp(content.sender) val sentSessionRequestTimestamp = DatabaseFactory.getLokiAPIDatabase(context).getSessionRequestTimestamp(content.sender)
if (sentSessionRequestTimestamp != null && content.timestamp < sentSessionRequestTimestamp) { if (sentSessionRequestTimestamp != null && content.timestamp < sentSessionRequestTimestamp) {
// We sent a session request after this one was sent // We sent a session request after this one was sent
return return false
} }
// Auto-accept all session requests
val ephemeralMessage = EphemeralMessage.create(content.sender) val ephemeralMessage = EphemeralMessage.create(content.sender)
ApplicationContext.getInstance(context).jobManager.add(PushEphemeralMessageSendJob(ephemeralMessage)) ApplicationContext.getInstance(context).jobManager.add(PushEphemeralMessageSendJob(ephemeralMessage))
return true
} }
@JvmStatic @JvmStatic