Multi-device sending typing and receipt messages.

This commit is contained in:
Mikunj 2019-10-07 10:25:14 +11:00
parent 0bfa3c33c7
commit da67bfab4c
2 changed files with 46 additions and 9 deletions

View File

@ -5,13 +5,21 @@ import android.content.Context;
import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.jobs.TypingSendJob;
import org.thoughtcrime.securesms.loki.MultiDeviceUtilKt;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.signalservice.loki.api.LokiStorageAPI;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import kotlin.Unit;
@SuppressLint("UseSparseArrays")
public class TypingStatusSender {
@ -74,7 +82,25 @@ public class TypingStatusSender {
}
private void sendTyping(long threadId, boolean typingStarted) {
ApplicationContext.getInstance(context).getJobManager().add(new TypingSendJob(threadId, typingStarted));
LokiStorageAPI storageAPI = LokiStorageAPI.Companion.getShared();
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(context);
Recipient originalRecipient = threadDatabase.getRecipientForThreadId(threadId);
// Send normal message if storage api wasn't found
if (storageAPI == null || originalRecipient == null) {
ApplicationContext.getInstance(context).getJobManager().add(new TypingSendJob(threadId, typingStarted));
return;
}
MultiDeviceUtilKt.getAllDevices(context, originalRecipient.getAddress().serialize(), storageAPI, (devicePubKey, isFriend, friendCount) -> {
Recipient device = Recipient.from(context, Address.fromSerialized(devicePubKey), false);
long deviceThreadId = threadDatabase.getThreadIdIfExistsFor(device);
if (deviceThreadId > -1) {
ApplicationContext.getInstance(context).getJobManager().add(new TypingSendJob(deviceThreadId, typingStarted));
}
return Unit.INSTANCE;
});
}
private class StartRunnable implements Runnable {

View File

@ -21,13 +21,17 @@ import org.thoughtcrime.securesms.jobs.MultiDeviceReadUpdateJob;
import org.thoughtcrime.securesms.jobs.SendReadReceiptJob;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.loki.LokiThreadDatabase;
import org.thoughtcrime.securesms.loki.MultiDeviceUtilKt;
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
import org.whispersystems.signalservice.loki.api.LokiStorageAPI;
import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import kotlin.Unit;
public class MarkReadReceiver extends BroadcastReceiver {
private static final String TAG = MarkReadReceiver.class.getSimpleName();
@ -86,17 +90,24 @@ public class MarkReadReceiver extends BroadcastReceiver {
.collect(Collectors.groupingBy(SyncMessageId::getAddress));
for (Address address : addressMap.keySet()) {
// Loki - This also prevents read receipts from being sent in group chats as they don't maintain a friend request status
LokiThreadDatabase lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context);
long threadID = lokiThreadDatabase.getThreadID(address.serialize());
LokiThreadFriendRequestStatus friendRequestStatus = lokiThreadDatabase.getFriendRequestStatus(threadID);
if (friendRequestStatus != LokiThreadFriendRequestStatus.FRIENDS) { return; }
LokiStorageAPI storageAPI = LokiStorageAPI.Companion.getShared();
if (storageAPI == null) {
Log.w("Loki", "LokiStorageAPI is not initialized!");
return;
}
List<Long> timestamps = Stream.of(addressMap.get(address)).map(SyncMessageId::getTimetamp).toList();
ApplicationContext.getInstance(context)
.getJobManager()
.add(new SendReadReceiptJob(address, timestamps));
MultiDeviceUtilKt.getAllDevices(context, address.serialize(), storageAPI, (devicePubKey, isFriend, friendCount) -> {
// Loki - This also prevents read receipts from being sent in group chats as they don't maintain a friend request status
if (isFriend) {
Address deviceAddress = Address.fromSerialized(devicePubKey);
ApplicationContext.getInstance(context)
.getJobManager()
.add(new SendReadReceiptJob(deviceAddress, timestamps));
}
return Unit.INSTANCE;
});
}
}