Sync blocked contacts across devices

This commit is contained in:
nielsandriesse 2020-07-16 12:49:37 +10:00
parent 56c2cd3ca9
commit 662d9c7c63
4 changed files with 50 additions and 3 deletions

View File

@ -27,7 +27,6 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
public class RecipientDatabase extends Database { public class RecipientDatabase extends Database {
@ -35,7 +34,7 @@ public class RecipientDatabase extends Database {
static final String TABLE_NAME = "recipient_preferences"; static final String TABLE_NAME = "recipient_preferences";
private static final String ID = "_id"; private static final String ID = "_id";
static final String ADDRESS = "recipient_ids"; public static final String ADDRESS = "recipient_ids";
private static final String BLOCK = "block"; private static final String BLOCK = "block";
private static final String NOTIFICATION = "notification"; private static final String NOTIFICATION = "notification";
private static final String VIBRATE = "vibrate"; private static final String VIBRATE = "vibrate";

View File

@ -315,6 +315,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
else if (syncMessage.getContacts().isPresent()) SyncMessagesProtocol.handleContactSyncMessage(context, content, syncMessage.getContacts().get()); else if (syncMessage.getContacts().isPresent()) SyncMessagesProtocol.handleContactSyncMessage(context, content, syncMessage.getContacts().get());
else if (syncMessage.getGroups().isPresent()) SyncMessagesProtocol.handleClosedGroupSyncMessage(context, content, syncMessage.getGroups().get()); else if (syncMessage.getGroups().isPresent()) SyncMessagesProtocol.handleClosedGroupSyncMessage(context, content, syncMessage.getGroups().get());
else if (syncMessage.getOpenGroups().isPresent()) SyncMessagesProtocol.handleOpenGroupSyncMessage(context, content, syncMessage.getOpenGroups().get()); else if (syncMessage.getOpenGroups().isPresent()) SyncMessagesProtocol.handleOpenGroupSyncMessage(context, content, syncMessage.getOpenGroups().get());
else if (syncMessage.getBlockedList().isPresent()) SyncMessagesProtocol.handleBlockedContactsSyncMessage(context, content, syncMessage.getBlockedList().get());
else Log.w(TAG, "Contains no known sync types..."); else Log.w(TAG, "Contains no known sync types...");
} else if (content.getCallMessage().isPresent()) { } else if (content.getCallMessage().isPresent()) {
Log.i(TAG, "Got call message..."); Log.i(TAG, "Got call message...");

View File

@ -2,13 +2,17 @@ package org.thoughtcrime.securesms.loki.activities
import android.app.AlertDialog import android.app.AlertDialog
import android.arch.lifecycle.Observer import android.arch.lifecycle.Observer
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter
import android.database.Cursor import android.database.Cursor
import android.os.AsyncTask import android.os.AsyncTask
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.support.v4.app.LoaderManager import android.support.v4.app.LoaderManager
import android.support.v4.content.Loader import android.support.v4.content.Loader
import android.support.v4.content.LocalBroadcastManager
import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.LinearLayoutManager
import android.text.Spannable import android.text.Spannable
import android.text.SpannableString import android.text.SpannableString
@ -47,6 +51,7 @@ import org.whispersystems.signalservice.loki.protocol.syncmessages.SyncMessagesP
class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListener, SeedReminderViewDelegate, NewConversationButtonSetViewDelegate { class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListener, SeedReminderViewDelegate, NewConversationButtonSetViewDelegate {
private lateinit var glide: GlideRequests private lateinit var glide: GlideRequests
private var broadcastReceiver: BroadcastReceiver? = null
private val publicKey: String private val publicKey: String
get() { get() {
@ -166,6 +171,15 @@ class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListe
it.recipient.address.toPhoneString() it.recipient.address.toPhoneString()
}.toSet() }.toSet()
FileServerAPI.shared.getDeviceLinks(publicKeys) FileServerAPI.shared.getDeviceLinks(publicKeys)
// Observe blocked contacts changed events
val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
recyclerView.adapter!!.notifyDataSetChanged()
}
}
this.broadcastReceiver = broadcastReceiver
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter("blockedContactsChanged"))
} }
override fun onResume() { override fun onResume() {
@ -198,6 +212,14 @@ class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListe
createNewPrivateChat() createNewPrivateChat()
} }
} }
override fun onDestroy() {
val broadcastReceiver = this.broadcastReceiver
if (broadcastReceiver != null) {
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver)
}
super.onDestroy()
}
// endregion // endregion
// region Updating // region Updating

View File

@ -7,6 +7,7 @@ import org.thoughtcrime.securesms.contacts.ContactAccessor.ContactData
import org.thoughtcrime.securesms.contacts.ContactAccessor.NumberData import org.thoughtcrime.securesms.contacts.ContactAccessor.NumberData
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.database.RecipientDatabase
import org.thoughtcrime.securesms.groups.GroupManager import org.thoughtcrime.securesms.groups.GroupManager
import org.thoughtcrime.securesms.groups.GroupMessageProcessor import org.thoughtcrime.securesms.groups.GroupMessageProcessor
import org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob import org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob
@ -19,6 +20,7 @@ import org.whispersystems.signalservice.api.messages.SignalServiceAttachment
import org.whispersystems.signalservice.api.messages.SignalServiceContent import org.whispersystems.signalservice.api.messages.SignalServiceContent
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage
import org.whispersystems.signalservice.api.messages.SignalServiceGroup import org.whispersystems.signalservice.api.messages.SignalServiceGroup
import org.whispersystems.signalservice.api.messages.multidevice.BlockedListMessage
import org.whispersystems.signalservice.api.messages.multidevice.ContactsMessage import org.whispersystems.signalservice.api.messages.multidevice.ContactsMessage
import org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsInputStream import org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsInputStream
import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsInputStream import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsInputStream
@ -138,4 +140,27 @@ object SyncMessagesProtocol {
OpenGroupUtilities.addGroup(context, url, channel) OpenGroupUtilities.addGroup(context, url, channel)
} }
} }
@JvmStatic
fun handleBlockedContactsSyncMessage(context: Context, content: SignalServiceContent, blockedContacts: BlockedListMessage) {
val recipientDB = DatabaseFactory.getRecipientDatabase(context)
val cursor = recipientDB.blocked
val blockedPublicKeys = blockedContacts.numbers.toSet()
val publicKeysToUnblock = mutableSetOf<String>()
fun addToUnblockListIfNeeded() {
val publicKey = cursor.getString(cursor.getColumnIndex(RecipientDatabase.ADDRESS)) ?: return
if (blockedPublicKeys.contains(publicKey)) { return }
publicKeysToUnblock.add(publicKey)
}
while (cursor.moveToNext()) {
addToUnblockListIfNeeded()
}
publicKeysToUnblock.forEach {
recipientDB.setBlocked(recipient(context, it), false)
}
blockedPublicKeys.forEach {
recipientDB.setBlocked(recipient(context, it), true)
}
ApplicationContext.getInstance(context).broadcaster.broadcast("blockedContactsChanged")
}
} }