mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-25 09:17:44 +00:00
Refactor.
This commit is contained in:
parent
fb3967db55
commit
8a3b4a6a14
@ -59,7 +59,7 @@ import org.thoughtcrime.securesms.loki.LokiMessageDatabase;
|
|||||||
import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase;
|
import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase;
|
||||||
import org.thoughtcrime.securesms.loki.LokiPreKeyRecordDatabase;
|
import org.thoughtcrime.securesms.loki.LokiPreKeyRecordDatabase;
|
||||||
import org.thoughtcrime.securesms.loki.LokiThreadDatabase;
|
import org.thoughtcrime.securesms.loki.LokiThreadDatabase;
|
||||||
import org.thoughtcrime.securesms.loki.UtilitiesKt;
|
import org.thoughtcrime.securesms.loki.MultiDeviceUtilKt;
|
||||||
import org.thoughtcrime.securesms.mms.IncomingMediaMessage;
|
import org.thoughtcrime.securesms.mms.IncomingMediaMessage;
|
||||||
import org.thoughtcrime.securesms.mms.MmsException;
|
import org.thoughtcrime.securesms.mms.MmsException;
|
||||||
import org.thoughtcrime.securesms.mms.OutgoingExpirationUpdateMessage;
|
import org.thoughtcrime.securesms.mms.OutgoingExpirationUpdateMessage;
|
||||||
@ -130,7 +130,6 @@ import javax.inject.Inject;
|
|||||||
|
|
||||||
import kotlin.Unit;
|
import kotlin.Unit;
|
||||||
import network.loki.messenger.R;
|
import network.loki.messenger.R;
|
||||||
import nl.komponents.kovenant.Promise;
|
|
||||||
|
|
||||||
public class PushDecryptJob extends BaseJob implements InjectableType {
|
public class PushDecryptJob extends BaseJob implements InjectableType {
|
||||||
|
|
||||||
@ -1147,7 +1146,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
|
|||||||
if (!envelope.isFriendRequest()) { return; }
|
if (!envelope.isFriendRequest()) { return; }
|
||||||
|
|
||||||
// This handles the case where another user sends us a regular message without the authorisation
|
// This handles the case where another user sends us a regular message without the authorisation
|
||||||
UtilitiesKt.shouldAutomaticallyBecomeFriendsWithDevice(content.getSender(), context).success(becomeFriends -> {
|
MultiDeviceUtilKt.shouldAutomaticallyBecomeFriendsWithDevice(content.getSender(), context).success(becomeFriends -> {
|
||||||
if (becomeFriends) {
|
if (becomeFriends) {
|
||||||
// Become friends AND update the message they sent
|
// Become friends AND update the message they sent
|
||||||
becomeFriendsWithContact(content.getSender());
|
becomeFriendsWithContact(content.getSender());
|
||||||
|
@ -5,9 +5,12 @@ import android.os.Handler
|
|||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
import nl.komponents.kovenant.Promise
|
import nl.komponents.kovenant.Promise
|
||||||
import nl.komponents.kovenant.deferred
|
import nl.komponents.kovenant.deferred
|
||||||
|
import nl.komponents.kovenant.functional.map
|
||||||
import org.thoughtcrime.securesms.ApplicationContext
|
import org.thoughtcrime.securesms.ApplicationContext
|
||||||
import org.thoughtcrime.securesms.database.Address
|
import org.thoughtcrime.securesms.database.Address
|
||||||
import org.thoughtcrime.securesms.database.DatabaseFactory
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||||
|
import org.thoughtcrime.securesms.jobs.BaseJob
|
||||||
|
import org.thoughtcrime.securesms.jobs.PushTextSendJob
|
||||||
import org.thoughtcrime.securesms.logging.Log
|
import org.thoughtcrime.securesms.logging.Log
|
||||||
import org.thoughtcrime.securesms.recipients.Recipient
|
import org.thoughtcrime.securesms.recipients.Recipient
|
||||||
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
||||||
@ -22,21 +25,22 @@ import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation
|
|||||||
import org.whispersystems.signalservice.loki.api.LokiStorageAPI
|
import org.whispersystems.signalservice.loki.api.LokiStorageAPI
|
||||||
import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus
|
import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus
|
||||||
|
|
||||||
fun isGroupChat(pubKey: String): Boolean {
|
fun getAllDevices(context: Context, pubKey: String, storageAPI: LokiStorageAPI, block: (devicePubKey: String, isFriend: Boolean, friendCount: Int) -> Unit) {
|
||||||
return (LokiGroupChatAPI.publicChatServer == pubKey)
|
val ourPubKey = TextSecurePreferences.getLocalNumber(context)
|
||||||
}
|
|
||||||
|
|
||||||
fun getFriends(context: Context, devices: Set<String>): Set<String> {
|
// Get all the devices and run our logic on them
|
||||||
val lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context)
|
storageAPI.getAllDevices(pubKey).success { items ->
|
||||||
|
val devices = items.toMutableSet()
|
||||||
|
// Remove our self if we intended this message to go to another recipient
|
||||||
|
if (pubKey != ourPubKey) {
|
||||||
|
devices.remove(ourPubKey)
|
||||||
|
}
|
||||||
|
|
||||||
return devices.mapNotNull { device ->
|
val friends = getFriends(context, devices)
|
||||||
val address = Address.fromSerialized(device)
|
for (device in devices) {
|
||||||
val recipient = Recipient.from(context, address, false)
|
block(device, friends.contains(device), friends.count())
|
||||||
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(recipient)
|
}
|
||||||
if (threadID < 0) { return@mapNotNull null }
|
}
|
||||||
|
|
||||||
if (lokiThreadDatabase.getFriendRequestStatus(threadID) == LokiThreadFriendRequestStatus.FRIENDS) device else null
|
|
||||||
}.toSet()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun shouldAutomaticallyBecomeFriendsWithDevice(pubKey: String, context: Context): Promise<Boolean, Unit> {
|
fun shouldAutomaticallyBecomeFriendsWithDevice(pubKey: String, context: Context): Promise<Boolean, Unit> {
|
25
src/org/thoughtcrime/securesms/loki/Util.kt
Normal file
25
src/org/thoughtcrime/securesms/loki/Util.kt
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package org.thoughtcrime.securesms.loki
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import org.thoughtcrime.securesms.database.Address
|
||||||
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||||
|
import org.thoughtcrime.securesms.recipients.Recipient
|
||||||
|
import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI
|
||||||
|
import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus
|
||||||
|
|
||||||
|
fun isGroupChat(pubKey: String): Boolean {
|
||||||
|
return (LokiGroupChatAPI.publicChatServer == pubKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getFriends(context: Context, devices: Set<String>): Set<String> {
|
||||||
|
val lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context)
|
||||||
|
|
||||||
|
return devices.mapNotNull { device ->
|
||||||
|
val address = Address.fromSerialized(device)
|
||||||
|
val recipient = Recipient.from(context, address, false)
|
||||||
|
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(recipient)
|
||||||
|
if (threadID < 0) { return@mapNotNull null }
|
||||||
|
|
||||||
|
if (lokiThreadDatabase.getFriendRequestStatus(threadID) == LokiThreadFriendRequestStatus.FRIENDS) device else null
|
||||||
|
}.toSet()
|
||||||
|
}
|
@ -42,7 +42,8 @@ import org.thoughtcrime.securesms.jobs.SmsSendJob;
|
|||||||
import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository;
|
import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository;
|
||||||
import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil;
|
import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil;
|
||||||
import org.thoughtcrime.securesms.logging.Log;
|
import org.thoughtcrime.securesms.logging.Log;
|
||||||
import org.thoughtcrime.securesms.loki.UtilitiesKt;
|
import org.thoughtcrime.securesms.loki.MultiDeviceUtilKt;
|
||||||
|
import org.thoughtcrime.securesms.loki.UtilKt;
|
||||||
import org.thoughtcrime.securesms.mms.MmsException;
|
import org.thoughtcrime.securesms.mms.MmsException;
|
||||||
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
||||||
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
||||||
@ -57,9 +58,6 @@ import org.whispersystems.signalservice.loki.api.LokiStorageAPI;
|
|||||||
import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus;
|
import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import kotlin.Unit;
|
import kotlin.Unit;
|
||||||
|
|
||||||
@ -213,40 +211,27 @@ public class MessageSender {
|
|||||||
LokiStorageAPI storageAPI = LokiStorageAPI.Companion.getShared();
|
LokiStorageAPI storageAPI = LokiStorageAPI.Companion.getShared();
|
||||||
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
|
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
|
||||||
|
|
||||||
// Just send the message normally if the job manager failed or if it's a group message
|
// Just send the message normally if the storage api is not set or if it's a group message
|
||||||
String recipientPubKey = recipient.getAddress().serialize();
|
String recipientPubKey = recipient.getAddress().serialize();
|
||||||
if (storageAPI == null || UtilitiesKt.isGroupChat(recipientPubKey)) {
|
if (storageAPI == null || UtilKt.isGroupChat(recipientPubKey)) {
|
||||||
|
if (storageAPI == null) { Log.w("Loki", "LokiStorageAPI is not initialized!"); }
|
||||||
jobManager.add(new PushTextSendJob(messageId, recipient.getAddress()));
|
jobManager.add(new PushTextSendJob(messageId, recipient.getAddress()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String ourPubKey = TextSecurePreferences.getLocalNumber(context);
|
MultiDeviceUtilKt.getAllDevices(context, recipientPubKey, storageAPI, (devicePubKey, isFriend, friendCount) -> {
|
||||||
|
Address deviceAddress = Address.fromSerialized(devicePubKey);
|
||||||
// Get all the devices and run our logic on them
|
long messageIdToUse = recipientPubKey.equals(devicePubKey) ? messageId : -1L;
|
||||||
storageAPI.getAllDevices(recipientPubKey).success(devices -> {
|
|
||||||
// Remove our self if we intended this message to go to another recipient
|
|
||||||
if (!recipientPubKey.equals(ourPubKey)) { devices.remove(ourPubKey); }
|
|
||||||
|
|
||||||
Set<String> friends = UtilitiesKt.getFriends(context, devices);
|
|
||||||
Set<String> nonFriends = new HashSet<>(devices);
|
|
||||||
nonFriends.removeAll(friends);
|
|
||||||
|
|
||||||
// Send a normal message to our friends
|
// Send a normal message to our friends
|
||||||
for (String friend : friends) {
|
if (isFriend) {
|
||||||
// If this message had the same recipient then point it to the correct message id otherwise point it to a non-existing message
|
jobManager.add(new PushTextSendJob(messageId, messageIdToUse, deviceAddress));
|
||||||
long messageIdToUse = recipientPubKey.equals(friend) ? messageId : -1L;
|
} else {
|
||||||
jobManager.add(new PushTextSendJob(messageId, messageIdToUse, Address.fromSerialized(friend)));
|
// Send friend requests to non friends
|
||||||
}
|
|
||||||
|
|
||||||
// Send friend requests to non friends
|
|
||||||
for (String stranger : nonFriends) {
|
|
||||||
// If this message had the same recipient then point it to the correct message id otherwise point it to a non-existing message
|
|
||||||
long messageIdToUse = recipientPubKey.equals(stranger) ? messageId : -1L;
|
|
||||||
|
|
||||||
// If we're friends with one of the devices then send out a default friend request message
|
// If we're friends with one of the devices then send out a default friend request message
|
||||||
boolean isFriendsWithAny = friends.size() > 0;
|
boolean isFriendsWithAny = friendCount > 0;
|
||||||
String defaultFriendRequestMessage = isFriendsWithAny ? "This is a friend request for devices linked to " + recipientPubKey : null;
|
String defaultFriendRequestMessage = isFriendsWithAny ? "This is a friend request for devices linked to " + recipientPubKey : null;
|
||||||
jobManager.add(new PushTextSendJob(messageId, messageIdToUse, Address.fromSerialized(stranger), true, defaultFriendRequestMessage));
|
jobManager.add(new PushTextSendJob(messageId, messageIdToUse, deviceAddress, true, defaultFriendRequestMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Unit.INSTANCE;
|
return Unit.INSTANCE;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user