From 353f456ae2c1157b310bf61f32c887c38ff39a8a Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 3 Feb 2020 10:52:15 +1100 Subject: [PATCH] Accept session request if it was sent by a member of our group or if it was sent by one of our friends. --- .../securesms/database/GroupDatabase.java | 19 ++++++++++++++++++ .../securesms/jobs/PushDecryptJob.java | 20 +++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/GroupDatabase.java b/src/org/thoughtcrime/securesms/database/GroupDatabase.java index 92c5e13262..ebba2103ba 100644 --- a/src/org/thoughtcrime/securesms/database/GroupDatabase.java +++ b/src/org/thoughtcrime/securesms/database/GroupDatabase.java @@ -160,6 +160,23 @@ public class GroupDatabase extends Database { return recipients; } + public boolean signalGroupsHaveMember(String hexEncodedPublicKey) { + try { + Address address = Address.fromSerialized(hexEncodedPublicKey); + Reader reader = DatabaseFactory.getGroupDatabase(context).getGroups(); + GroupRecord record; + while ((record = reader.getNext()) != null) { + if (record.isSignalGroup() && record.members.contains(address)) { + return true; + } + } + + return false; + } catch (Exception e) { + return false; + } + } + public void create(@NonNull String groupId, @Nullable String title, @NonNull List
members, @Nullable SignalServiceAttachmentPointer avatar, @Nullable String relay, @Nullable List
admins) { @@ -476,6 +493,8 @@ public class GroupDatabase extends Database { public boolean isRSSFeed() { return Address.fromSerialized(id).isRSSFeed(); } + public boolean isSignalGroup() { return Address.fromSerialized(id).isSignalGroup(); } + public String getUrl() { return url; } public List
getAdmins() { return admins; } diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 393c1989e3..1ea8bc924d 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -1228,12 +1228,20 @@ public class PushDecryptJob extends BaseJob implements InjectableType { private void handleSessionRequestIfNeeded(@NonNull SignalServiceContent content) { if (content.isFriendRequest() && isSessionRequest(content)) { - // TODO: Check if member is in one of our private groups - boolean isInOneOfOurGroups = false; - if (isInOneOfOurGroups) { - // Send a background message to acknowledge session request - MessageSender.sendBackgroundMessage(context, content.getSender()); - } + // Check if the session request from a member in one of our groups or our friend + LokiStorageAPI.shared.getPrimaryDevicePublicKey(content.getSender()).success(primaryDevicePublicKey -> { + String sender = primaryDevicePublicKey != null ? primaryDevicePublicKey : content.getSender(); + long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(Recipient.from(context, Address.fromSerialized(sender), false)); + LokiThreadFriendRequestStatus threadFriendRequestStatus = DatabaseFactory.getLokiThreadDatabase(context).getFriendRequestStatus(threadID); + boolean isOurFriend = threadFriendRequestStatus == LokiThreadFriendRequestStatus.FRIENDS; + boolean isInOneOfOurGroups = DatabaseFactory.getGroupDatabase(context).signalGroupsHaveMember(sender); + boolean shouldAcceptSessionRequest = isOurFriend || isInOneOfOurGroups; + if (shouldAcceptSessionRequest) { + // Send a background message to acknowledge session request + MessageSender.sendBackgroundMessage(context, content.getSender()); + } + return Unit.INSTANCE; + }); } }