mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-23 18:15:22 +00:00
Refactor
This commit is contained in:
parent
8e53383ab9
commit
b9d8d100d9
@ -83,11 +83,13 @@ public class TypingStatusSender {
|
||||
private void sendTyping(long threadId, boolean typingStarted) {
|
||||
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(context);
|
||||
Recipient recipient = threadDatabase.getRecipientForThreadId(threadId);
|
||||
// Loki - Check whether we want to send a typing indicator to this user
|
||||
if (!SessionMetaProtocol.shouldSendTypingIndicator(recipient, context)) { return; }
|
||||
// Loki - Take into account multi device
|
||||
Set<String> linkedDevices = MultiDeviceProtocol.shared.getAllLinkedDevices(recipient.getAddress().serialize());
|
||||
for (String device : linkedDevices) {
|
||||
Recipient deviceAsRecipient = Recipient.from(context, Address.fromSerialized(device), false);
|
||||
long deviceThreadID = threadDatabase.getThreadIdFor(deviceAsRecipient)
|
||||
long deviceThreadID = threadDatabase.getThreadIdFor(deviceAsRecipient);
|
||||
ApplicationContext.getInstance(context).getJobManager().add(new TypingSendJob(deviceThreadID, typingStarted));
|
||||
}
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ public class SendReadReceiptJob extends BaseJob implements InjectableType {
|
||||
SignalServiceAddress remoteAddress = new SignalServiceAddress(address);
|
||||
SignalServiceReceiptMessage receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.READ, messageIds, timestamp);
|
||||
|
||||
messageSender.sendReceipt(0, remoteAddress, // The message ID doesn't matter
|
||||
messageSender.sendReceipt(remoteAddress,
|
||||
UnidentifiedAccessUtil.getAccessFor(context, Recipient.from(context, Address.fromSerialized(address), false)),
|
||||
receiptMessage);
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ import org.thoughtcrime.securesms.dependencies.InjectableType;
|
||||
import org.thoughtcrime.securesms.jobmanager.Data;
|
||||
import org.thoughtcrime.securesms.jobmanager.Job;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.util.GroupUtil;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
@ -22,7 +21,9 @@ import org.whispersystems.signalservice.api.messages.SignalServiceTypingMessage.
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@ -85,11 +86,11 @@ public class TypingSendJob extends BaseJob implements InjectableType {
|
||||
throw new IllegalStateException("Tried to send a typing indicator to a non-existent thread.");
|
||||
}
|
||||
|
||||
List<Recipient> recipients = Collections.singletonList(recipient);
|
||||
Optional<byte[]> groupId = Optional.absent();
|
||||
Set<Recipient> recipients = new HashSet<>(Collections.singletonList(recipient));
|
||||
Optional<byte[]> groupId = Optional.absent();
|
||||
|
||||
if (recipient.isGroupRecipient()) {
|
||||
recipients = DatabaseFactory.getGroupDatabase(context).getGroupMembers(recipient.getAddress().toGroupString(), false);
|
||||
recipients = new HashSet<>(DatabaseFactory.getGroupDatabase(context).getGroupMembers(recipient.getAddress().toGroupString(), false));
|
||||
groupId = Optional.of(GroupUtil.getDecodedId(recipient.getAddress().toGroupString()));
|
||||
}
|
||||
|
||||
@ -97,9 +98,7 @@ public class TypingSendJob extends BaseJob implements InjectableType {
|
||||
List<Optional<UnidentifiedAccessPair>> unidentifiedAccess = Stream.of(recipients).map(r -> UnidentifiedAccessUtil.getAccessFor(context, r)).toList();
|
||||
SignalServiceTypingMessage typingMessage = new SignalServiceTypingMessage(typing ? Action.STARTED : Action.STOPPED, System.currentTimeMillis(), groupId);
|
||||
|
||||
if (SessionMetaProtocol.shouldSendTypingIndicator(recipient, context)) {
|
||||
messageSender.sendTyping(addresses, unidentifiedAccess, typingMessage);
|
||||
}
|
||||
messageSender.sendTyping(addresses, unidentifiedAccess, typingMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -4,34 +4,38 @@ 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.protocol.multidevice.MultiDeviceProtocol
|
||||
import org.whispersystems.signalservice.loki.protocol.todo.LokiThreadFriendRequestStatus
|
||||
|
||||
object SessionMetaProtocol {
|
||||
|
||||
/**
|
||||
* Should be invoked for the recipient's master device.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun canUserReplyToNotification(recipient: Recipient, context: Context): Boolean {
|
||||
val isGroup = recipient.isGroupRecipient
|
||||
if (isGroup) { return !recipient.address.isRSSFeed }
|
||||
val linkedDevices = MultiDeviceProtocol.shared.getAllLinkedDevices(recipient.address.serialize())
|
||||
return linkedDevices.any { device ->
|
||||
val recipient = Recipient.from(context, Address.fromSerialized(device), false)
|
||||
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient)
|
||||
DatabaseFactory.getLokiThreadDatabase(context).getFriendRequestStatus(threadID) == LokiThreadFriendRequestStatus.FRIENDS
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun shouldSendReadReceipt(publicKey: String, context: Context): Boolean {
|
||||
val recipient = Recipient.from(context, Address.fromSerialized(publicKey),false)
|
||||
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient)
|
||||
return DatabaseFactory.getLokiThreadDatabase(context).getFriendRequestStatus(threadID) == LokiThreadFriendRequestStatus.FRIENDS
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be invoked for the recipient's master device.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun shouldSendReadReceipt(address: Address, context: Context): Boolean {
|
||||
if (address.isGroup) { return false }
|
||||
val recipient = Recipient.from(context, address,false)
|
||||
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient)
|
||||
return DatabaseFactory.getLokiThreadDatabase(context).getFriendRequestStatus(threadID) == LokiThreadFriendRequestStatus.FRIENDS
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be invoked for the recipient's master device.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun shouldSendTypingIndicator(recipient: Recipient, context: Context): Boolean {
|
||||
if (recipient.isGroupRecipient) { return false }
|
||||
val recipient = Recipient.from(context, Address.fromSerialized(recipient.address.serialize()),false)
|
||||
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient)
|
||||
return DatabaseFactory.getLokiThreadDatabase(context).getFriendRequestStatus(threadID) == LokiThreadFriendRequestStatus.FRIENDS
|
||||
}
|
||||
|
@ -6,6 +6,6 @@ object SyncMessagesProtocol {
|
||||
|
||||
@JvmStatic
|
||||
fun shouldSyncReadReceipt(address: Address): Boolean {
|
||||
return !address.isOpenGroup
|
||||
return !address.isGroup
|
||||
}
|
||||
}
|
@ -20,11 +20,9 @@ import org.thoughtcrime.securesms.database.MessagingDatabase.SyncMessageId;
|
||||
import org.thoughtcrime.securesms.jobs.MultiDeviceReadUpdateJob;
|
||||
import org.thoughtcrime.securesms.jobs.SendReadReceiptJob;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.loki.MultiDeviceUtilities;
|
||||
import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol;
|
||||
import org.thoughtcrime.securesms.loki.protocol.SyncMessagesProtocol;
|
||||
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.whispersystems.signalservice.loki.protocol.multidevice.MultiDeviceProtocol;
|
||||
|
||||
import java.util.LinkedList;
|
||||
@ -32,8 +30,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import kotlin.Unit;
|
||||
|
||||
public class MarkReadReceiver extends BroadcastReceiver {
|
||||
|
||||
private static final String TAG = MarkReadReceiver.class.getSimpleName();
|
||||
@ -81,9 +77,8 @@ public class MarkReadReceiver extends BroadcastReceiver {
|
||||
for (MarkedMessageInfo messageInfo : markedReadMessages) {
|
||||
scheduleDeletion(context, messageInfo.getExpirationInfo());
|
||||
|
||||
if (SyncMessagesProtocol.shouldSyncReadReceipt(messageInfo.getSyncMessageId().getAddress())) {
|
||||
syncMessageIds.add(messageInfo.getSyncMessageId());
|
||||
}
|
||||
if (!SyncMessagesProtocol.shouldSyncReadReceipt(messageInfo.getSyncMessageId().getAddress())) { continue; }
|
||||
syncMessageIds.add(messageInfo.getSyncMessageId());
|
||||
}
|
||||
|
||||
ApplicationContext.getInstance(context)
|
||||
@ -96,12 +91,15 @@ public class MarkReadReceiver extends BroadcastReceiver {
|
||||
|
||||
for (Address address : addressMap.keySet()) {
|
||||
List<Long> timestamps = Stream.of(addressMap.get(address)).map(SyncMessageId::getTimetamp).toList();
|
||||
// Loki - Check whether we want to send a read receipt to this user
|
||||
if (!SessionMetaProtocol.shouldSendReadReceipt(address, context)) { continue; }
|
||||
// Loki - Take into account multi device
|
||||
Set<String> linkedDevices = MultiDeviceProtocol.shared.getAllLinkedDevices(address.serialize());
|
||||
for (String device : linkedDevices) {
|
||||
if (!SessionMetaProtocol.shouldSendReadReceipt(device, context)) { continue; }
|
||||
Address deviceAsAddress = Address.fromExternal(context, device);
|
||||
ApplicationContext.getInstance(context)
|
||||
.getJobManager()
|
||||
.add(new SendReadReceiptJob(address, timestamps));
|
||||
.add(new SendReadReceiptJob(deviceAsAddress, timestamps));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user