mirror of
https://github.com/oxen-io/session-android.git
synced 2025-08-26 04:17:27 +00:00
Add support for syncing pinned status with storage service.
This commit is contained in:
@@ -32,6 +32,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import net.sqlcipher.database.SQLiteDatabase;
|
||||
|
||||
import org.jsoup.helper.StringUtil;
|
||||
import org.signal.zkgroup.InvalidInputException;
|
||||
import org.signal.zkgroup.groups.GroupMasterKey;
|
||||
import org.thoughtcrime.securesms.database.MessageDatabase.MarkedMessageInfo;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.RecipientSettings;
|
||||
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
||||
@@ -39,6 +41,8 @@ import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord;
|
||||
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
||||
import org.thoughtcrime.securesms.database.model.MmsMessageRecord;
|
||||
import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
||||
import org.thoughtcrime.securesms.groups.BadGroupIdException;
|
||||
import org.thoughtcrime.securesms.groups.GroupId;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.mms.Slide;
|
||||
import org.thoughtcrime.securesms.mms.SlideDeck;
|
||||
@@ -758,6 +762,25 @@ public class ThreadDatabase extends Database {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Pinned recipients, in order from top to bottom.
|
||||
*/
|
||||
public @NonNull List<RecipientId> getPinnedRecipientIds() {
|
||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||
String[] projection = new String[]{RECIPIENT_ID};
|
||||
String query = PINNED + " > ?";
|
||||
String[] args = SqlUtil.buildArgs(0);
|
||||
List<RecipientId> pinned = new LinkedList<>();
|
||||
|
||||
try (Cursor cursor = db.query(TABLE_NAME, projection, query, args, null, null, PINNED + " ASC")) {
|
||||
while (cursor.moveToNext()) {
|
||||
pinned.add(RecipientId.from(CursorUtil.requireLong(cursor, RECIPIENT_ID)));
|
||||
}
|
||||
}
|
||||
|
||||
return pinned;
|
||||
}
|
||||
|
||||
public void pinConversations(@NonNull Set<Long> threadIds) {
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
|
||||
@@ -771,6 +794,7 @@ public class ThreadDatabase extends Database {
|
||||
contentValues.put(PINNED, ++pinnedCount);
|
||||
|
||||
db.update(TABLE_NAME, contentValues, ID_WHERE, SqlUtil.buildArgs(threadId));
|
||||
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
@@ -780,6 +804,9 @@ public class ThreadDatabase extends Database {
|
||||
}
|
||||
|
||||
notifyConversationListListeners();
|
||||
|
||||
DatabaseFactory.getRecipientDatabase(context).markNeedsSync(Recipient.self().getId());
|
||||
StorageSyncHelper.scheduleSyncForDataChange();
|
||||
}
|
||||
|
||||
public void unpinConversations(@NonNull Set<Long> threadIds) {
|
||||
@@ -792,6 +819,9 @@ public class ThreadDatabase extends Database {
|
||||
|
||||
db.update(TABLE_NAME, contentValues, selection, SqlUtil.buildArgs(Stream.of(threadIds).toArray()));
|
||||
notifyConversationListListeners();
|
||||
|
||||
DatabaseFactory.getRecipientDatabase(context).markNeedsSync(Recipient.self().getId());
|
||||
StorageSyncHelper.scheduleSyncForDataChange();
|
||||
}
|
||||
|
||||
public void archiveConversation(long threadId) {
|
||||
@@ -1025,7 +1055,57 @@ public class ThreadDatabase extends Database {
|
||||
}
|
||||
|
||||
public void applyStorageSyncUpdate(@NonNull RecipientId recipientId, @NonNull SignalAccountRecord record) {
|
||||
applyStorageSyncUpdate(recipientId, record.isNoteToSelfArchived(), record.isNoteToSelfForcedUnread());
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
|
||||
db.beginTransaction();
|
||||
try {
|
||||
applyStorageSyncUpdate(recipientId, record.isNoteToSelfArchived(), record.isNoteToSelfForcedUnread());
|
||||
|
||||
ContentValues clearPinnedValues = new ContentValues();
|
||||
clearPinnedValues.put(PINNED, 0);
|
||||
db.update(TABLE_NAME, clearPinnedValues, null, null);
|
||||
|
||||
int pinnedPosition = 1;
|
||||
for (SignalAccountRecord.PinnedConversation pinned : record.getPinnedConversations()) {
|
||||
ContentValues pinnedValues = new ContentValues();
|
||||
pinnedValues.put(PINNED, pinnedPosition);
|
||||
|
||||
Recipient pinnedRecipient;
|
||||
|
||||
if (pinned.getContact().isPresent()) {
|
||||
pinnedRecipient = Recipient.externalPush(context, pinned.getContact().get());
|
||||
} else if (pinned.getGroupV1Id().isPresent()) {
|
||||
try {
|
||||
pinnedRecipient = Recipient.externalGroup(context, GroupId.v1Exact(pinned.getGroupV1Id().get()));
|
||||
} catch (BadGroupIdException e) {
|
||||
Log.w(TAG, "Failed to parse pinned groupV1 ID!", e);
|
||||
pinnedRecipient = null;
|
||||
}
|
||||
} else if (pinned.getGroupV2MasterKey().isPresent()) {
|
||||
try {
|
||||
pinnedRecipient = Recipient.externalGroup(context, GroupId.v2(new GroupMasterKey(pinned.getGroupV2MasterKey().get())));
|
||||
} catch (InvalidInputException e) {
|
||||
Log.w(TAG, "Failed to parse pinned groupV2 master key!", e);
|
||||
pinnedRecipient = null;
|
||||
}
|
||||
} else {
|
||||
Log.w(TAG, "Empty pinned conversation on the AccountRecord?");
|
||||
pinnedRecipient = null;
|
||||
}
|
||||
|
||||
if (pinnedRecipient != null) {
|
||||
db.update(TABLE_NAME, pinnedValues, RECIPIENT_ID + " = ?", SqlUtil.buildArgs(pinnedRecipient.getId()));
|
||||
}
|
||||
|
||||
pinnedPosition++;
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
|
||||
notifyConversationListListeners();
|
||||
}
|
||||
|
||||
private void applyStorageSyncUpdate(@NonNull RecipientId recipientId, boolean archived, boolean forcedUnread) {
|
||||
|
@@ -88,7 +88,7 @@ final class MessageRequestRepository {
|
||||
}
|
||||
|
||||
return MessageRequestState.REQUIRED;
|
||||
} else if (FeatureFlags.modernProfileSharing() && !recipient.isPushV2Group() && !recipient.isProfileSharing()) {
|
||||
} else if (FeatureFlags.modernProfileSharing() && !RecipientUtil.isLegacyProfileSharingAccepted(recipient)) {
|
||||
return MessageRequestState.REQUIRED;
|
||||
} else if (RecipientUtil.isPreMessageRequestThread(context, threadId) && !RecipientUtil.isLegacyProfileSharingAccepted(recipient)) {
|
||||
return MessageRequestState.PRE_MESSAGE_REQUEST;
|
||||
|
@@ -256,7 +256,8 @@ public class RecipientUtil {
|
||||
return threadRecipient.isLocalNumber() ||
|
||||
threadRecipient.isProfileSharing() ||
|
||||
threadRecipient.isSystemContact() ||
|
||||
!threadRecipient.isRegistered();
|
||||
!threadRecipient.isRegistered() ||
|
||||
threadRecipient.isForceSmsSelection();
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
|
@@ -6,11 +6,13 @@ import androidx.annotation.Nullable;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
import org.whispersystems.signalservice.api.storage.SignalAccountRecord;
|
||||
import org.whispersystems.signalservice.api.storage.SignalAccountRecord.PinnedConversation;
|
||||
import org.whispersystems.signalservice.internal.storage.protos.AccountRecord;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -66,9 +68,10 @@ class AccountConflictMerger implements StorageSyncHelper.ConflictMerger<SignalAc
|
||||
boolean sealedSenderIndicators = remote.isSealedSenderIndicatorsEnabled();
|
||||
boolean linkPreviews = remote.isLinkPreviewsEnabled();
|
||||
boolean unlisted = remote.isPhoneNumberUnlisted();
|
||||
List<PinnedConversation> pinnedConversations = remote.getPinnedConversations();
|
||||
AccountRecord.PhoneNumberSharingMode phoneNumberSharingMode = remote.getPhoneNumberSharingMode();
|
||||
boolean matchesRemote = doParamsMatch(remote, unknownFields, givenName, familyName, avatarUrlPath, profileKey, noteToSelfArchived, noteToSelfForcedUnread, readReceipts, typingIndicators, sealedSenderIndicators, linkPreviews, phoneNumberSharingMode, unlisted);
|
||||
boolean matchesLocal = doParamsMatch(local, unknownFields, givenName, familyName, avatarUrlPath, profileKey, noteToSelfArchived, noteToSelfForcedUnread, readReceipts, typingIndicators, sealedSenderIndicators, linkPreviews, phoneNumberSharingMode, unlisted );
|
||||
boolean matchesRemote = doParamsMatch(remote, unknownFields, givenName, familyName, avatarUrlPath, profileKey, noteToSelfArchived, noteToSelfForcedUnread, readReceipts, typingIndicators, sealedSenderIndicators, linkPreviews, phoneNumberSharingMode, unlisted, pinnedConversations);
|
||||
boolean matchesLocal = doParamsMatch(local, unknownFields, givenName, familyName, avatarUrlPath, profileKey, noteToSelfArchived, noteToSelfForcedUnread, readReceipts, typingIndicators, sealedSenderIndicators, linkPreviews, phoneNumberSharingMode, unlisted, pinnedConversations);
|
||||
|
||||
if (matchesRemote) {
|
||||
return remote;
|
||||
@@ -90,6 +93,7 @@ class AccountConflictMerger implements StorageSyncHelper.ConflictMerger<SignalAc
|
||||
.setUnlistedPhoneNumber(unlisted)
|
||||
.setPhoneNumberSharingMode(phoneNumberSharingMode)
|
||||
.setUnlistedPhoneNumber(unlisted)
|
||||
.setPinnedConversations(pinnedConversations)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -107,7 +111,8 @@ class AccountConflictMerger implements StorageSyncHelper.ConflictMerger<SignalAc
|
||||
boolean sealedSenderIndicators,
|
||||
boolean linkPreviewsEnabled,
|
||||
AccountRecord.PhoneNumberSharingMode phoneNumberSharingMode,
|
||||
boolean unlistedPhoneNumber)
|
||||
boolean unlistedPhoneNumber,
|
||||
@NonNull List<PinnedConversation> pinnedConversations)
|
||||
{
|
||||
return Arrays.equals(contact.serializeUnknownFields(), unknownFields) &&
|
||||
Objects.equals(contact.getGivenName().or(""), givenName) &&
|
||||
@@ -121,6 +126,7 @@ class AccountConflictMerger implements StorageSyncHelper.ConflictMerger<SignalAc
|
||||
contact.isSealedSenderIndicatorsEnabled() == sealedSenderIndicators &&
|
||||
contact.isLinkPreviewsEnabled() == linkPreviewsEnabled &&
|
||||
contact.getPhoneNumberSharingMode() == phoneNumberSharingMode &&
|
||||
contact.isPhoneNumberUnlisted() == unlistedPhoneNumber;
|
||||
contact.isPhoneNumberUnlisted() == unlistedPhoneNumber &&
|
||||
Objects.equals(contact.getPinnedConversations(), pinnedConversations);
|
||||
}
|
||||
}
|
||||
|
@@ -412,7 +412,11 @@ public final class StorageSyncHelper {
|
||||
}
|
||||
|
||||
public static SignalStorageRecord buildAccountRecord(@NonNull Context context, @NonNull Recipient self) {
|
||||
RecipientSettings settings = DatabaseFactory.getRecipientDatabase(context).getRecipientSettingsForSync(self.getId());
|
||||
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
||||
RecipientSettings settings = recipientDatabase.getRecipientSettingsForSync(self.getId());
|
||||
List<RecipientSettings> pinned = Stream.of(DatabaseFactory.getThreadDatabase(context).getPinnedRecipientIds())
|
||||
.map(recipientDatabase::getRecipientSettingsForSync)
|
||||
.toList();
|
||||
|
||||
SignalAccountRecord account = new SignalAccountRecord.Builder(self.getStorageServiceId())
|
||||
.setUnknownFields(settings != null ? settings.getSyncExtras().getStorageProto() : null)
|
||||
@@ -427,30 +431,13 @@ public final class StorageSyncHelper {
|
||||
.setSealedSenderIndicatorsEnabled(TextSecurePreferences.isShowUnidentifiedDeliveryIndicatorsEnabled(context))
|
||||
.setLinkPreviewsEnabled(SignalStore.settings().isLinkPreviewsEnabled())
|
||||
.setUnlistedPhoneNumber(SignalStore.phoneNumberPrivacy().getPhoneNumberListingMode().isUnlisted())
|
||||
.setPhoneNumberSharingMode(localToRemotePhoneNumberSharingMode(SignalStore.phoneNumberPrivacy().getPhoneNumberSharingMode()))
|
||||
.setPhoneNumberSharingMode(StorageSyncModels.localToRemotePhoneNumberSharingMode(SignalStore.phoneNumberPrivacy().getPhoneNumberSharingMode()))
|
||||
.setPinnedConversations(StorageSyncModels.localToRemotePinnedConversations(pinned))
|
||||
.build();
|
||||
|
||||
return SignalStorageRecord.forAccount(account);
|
||||
}
|
||||
|
||||
private static AccountRecord.PhoneNumberSharingMode localToRemotePhoneNumberSharingMode(PhoneNumberPrivacyValues.PhoneNumberSharingMode phoneNumberPhoneNumberSharingMode) {
|
||||
switch (phoneNumberPhoneNumberSharingMode) {
|
||||
case EVERYONE: return AccountRecord.PhoneNumberSharingMode.EVERYBODY;
|
||||
case CONTACTS: return AccountRecord.PhoneNumberSharingMode.CONTACTS_ONLY;
|
||||
case NOBODY : return AccountRecord.PhoneNumberSharingMode.NOBODY;
|
||||
default : throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
private static PhoneNumberPrivacyValues.PhoneNumberSharingMode remoteToLocalPhoneNumberSharingMode(AccountRecord.PhoneNumberSharingMode phoneNumberPhoneNumberSharingMode) {
|
||||
switch (phoneNumberPhoneNumberSharingMode) {
|
||||
case EVERYBODY : return PhoneNumberPrivacyValues.PhoneNumberSharingMode.EVERYONE;
|
||||
case CONTACTS_ONLY: return PhoneNumberPrivacyValues.PhoneNumberSharingMode.CONTACTS;
|
||||
case NOBODY : return PhoneNumberPrivacyValues.PhoneNumberSharingMode.NOBODY;
|
||||
default : return PhoneNumberPrivacyValues.PhoneNumberSharingMode.CONTACTS;
|
||||
}
|
||||
}
|
||||
|
||||
public static void applyAccountStorageSyncUpdates(@NonNull Context context, Optional<StorageSyncHelper.RecordUpdate<SignalAccountRecord>> update) {
|
||||
if (!update.isPresent()) {
|
||||
return;
|
||||
@@ -466,7 +453,7 @@ public final class StorageSyncHelper {
|
||||
TextSecurePreferences.setShowUnidentifiedDeliveryIndicatorsEnabled(context, update.isSealedSenderIndicatorsEnabled());
|
||||
SignalStore.settings().setLinkPreviewsEnabled(update.isLinkPreviewsEnabled());
|
||||
SignalStore.phoneNumberPrivacy().setPhoneNumberListingMode(update.isPhoneNumberUnlisted() ? PhoneNumberPrivacyValues.PhoneNumberListingMode.UNLISTED : PhoneNumberPrivacyValues.PhoneNumberListingMode.LISTED);
|
||||
SignalStore.phoneNumberPrivacy().setPhoneNumberSharingMode(remoteToLocalPhoneNumberSharingMode(update.getPhoneNumberSharingMode()));
|
||||
SignalStore.phoneNumberPrivacy().setPhoneNumberSharingMode(StorageSyncModels.remoteToLocalPhoneNumberSharingMode(update.getPhoneNumberSharingMode()));
|
||||
|
||||
if (fetchProfile && update.getAvatarUrlPath().isPresent()) {
|
||||
ApplicationDependencies.getJobManager().add(new RetrieveProfileAvatarJob(Recipient.self(), update.getAvatarUrlPath().get()));
|
||||
|
@@ -2,18 +2,26 @@ package org.thoughtcrime.securesms.storage;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
|
||||
import org.signal.zkgroup.groups.GroupMasterKey;
|
||||
import org.thoughtcrime.securesms.database.IdentityDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.RecipientSettings;
|
||||
import org.thoughtcrime.securesms.groups.GroupId;
|
||||
import org.thoughtcrime.securesms.keyvalue.PhoneNumberPrivacyValues;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientUtil;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.signalservice.api.storage.SignalAccountRecord;
|
||||
import org.whispersystems.signalservice.api.storage.SignalContactRecord;
|
||||
import org.whispersystems.signalservice.api.storage.SignalGroupV1Record;
|
||||
import org.whispersystems.signalservice.api.storage.SignalGroupV2Record;
|
||||
import org.whispersystems.signalservice.api.storage.SignalStorageRecord;
|
||||
import org.whispersystems.signalservice.internal.storage.protos.AccountRecord;
|
||||
import org.whispersystems.signalservice.internal.storage.protos.ContactRecord.IdentityState;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public final class StorageSyncModels {
|
||||
@@ -37,6 +45,42 @@ public final class StorageSyncModels {
|
||||
}
|
||||
}
|
||||
|
||||
public static AccountRecord.PhoneNumberSharingMode localToRemotePhoneNumberSharingMode(PhoneNumberPrivacyValues.PhoneNumberSharingMode phoneNumberPhoneNumberSharingMode) {
|
||||
switch (phoneNumberPhoneNumberSharingMode) {
|
||||
case EVERYONE: return AccountRecord.PhoneNumberSharingMode.EVERYBODY;
|
||||
case CONTACTS: return AccountRecord.PhoneNumberSharingMode.CONTACTS_ONLY;
|
||||
case NOBODY : return AccountRecord.PhoneNumberSharingMode.NOBODY;
|
||||
default : throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
public static PhoneNumberPrivacyValues.PhoneNumberSharingMode remoteToLocalPhoneNumberSharingMode(AccountRecord.PhoneNumberSharingMode phoneNumberPhoneNumberSharingMode) {
|
||||
switch (phoneNumberPhoneNumberSharingMode) {
|
||||
case EVERYBODY : return PhoneNumberPrivacyValues.PhoneNumberSharingMode.EVERYONE;
|
||||
case CONTACTS_ONLY: return PhoneNumberPrivacyValues.PhoneNumberSharingMode.CONTACTS;
|
||||
case NOBODY : return PhoneNumberPrivacyValues.PhoneNumberSharingMode.NOBODY;
|
||||
default : return PhoneNumberPrivacyValues.PhoneNumberSharingMode.CONTACTS;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<SignalAccountRecord.PinnedConversation> localToRemotePinnedConversations(@NonNull List<RecipientSettings> settings) {
|
||||
return Stream.of(settings)
|
||||
.filter(s -> s.getGroupType() == RecipientDatabase.GroupType.SIGNAL_V1 ||
|
||||
s.getGroupType() == RecipientDatabase.GroupType.SIGNAL_V2 ||
|
||||
s.getRegistered() == RecipientDatabase.RegisteredState.REGISTERED)
|
||||
.map(StorageSyncModels::localToRemotePinnedConversation)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static @NonNull SignalAccountRecord.PinnedConversation localToRemotePinnedConversation(@NonNull RecipientSettings settings) {
|
||||
switch (settings.getGroupType()) {
|
||||
case NONE : return SignalAccountRecord.PinnedConversation.forContact(new SignalServiceAddress(settings.getUuid(), settings.getE164()));
|
||||
case SIGNAL_V1: return SignalAccountRecord.PinnedConversation.forGroupV1(settings.getGroupId().requireV1().getDecodedId());
|
||||
case SIGNAL_V2: return SignalAccountRecord.PinnedConversation.forGroupV2(settings.getSyncExtras().getGroupMasterKey().serialize());
|
||||
default : throw new AssertionError("Unexpected group type!");
|
||||
}
|
||||
}
|
||||
|
||||
private static @NonNull SignalContactRecord localToRemoteContact(@NonNull RecipientSettings recipient, byte[] rawStorageId) {
|
||||
if (recipient.getUuid() == null && recipient.getE164() == null) {
|
||||
throw new AssertionError("Must have either a UUID or a phone number!");
|
||||
|
Reference in New Issue
Block a user