Skip to content

Commit

Permalink
Insert session switchover events when appropriate.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Jan 25, 2023
1 parent 7745ae6 commit a7d9bd9
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 76 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetPni(result.id, PNI_A),
PnpOperation.SetAci(result.id, ACI_A)
)
Expand All @@ -131,9 +131,9 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetPni(result.id, PNI_A),
PnpOperation.SessionSwitchoverInsert(result.id)
PnpOperation.SessionSwitchoverInsert(result.id, E164_A)
)
),
result.changeSet
Expand All @@ -151,7 +151,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetPni(result.id, PNI_A)
)
),
Expand All @@ -170,7 +170,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetAci(result.id, ACI_A)
)
),
Expand All @@ -189,9 +189,9 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetAci(result.id, ACI_A),
PnpOperation.SessionSwitchoverInsert(result.id)
PnpOperation.SessionSwitchoverInsert(result.id, E164_A)
)
),
result.changeSet
Expand All @@ -209,7 +209,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetPni(result.id, PNI_A)
)
),
Expand All @@ -228,7 +228,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetE164(result.id, E164_A),
PnpOperation.SetAci(result.id, ACI_A)
)
Expand All @@ -248,10 +248,10 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetE164(result.id, E164_A),
PnpOperation.SetAci(result.id, ACI_A),
PnpOperation.SessionSwitchoverInsert(result.id)
PnpOperation.SessionSwitchoverInsert(result.id, E164_A)
)
),
result.changeSet
Expand All @@ -269,15 +269,15 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetE164(result.id, E164_A),
PnpOperation.SetAci(result.id, ACI_A),
PnpOperation.ChangeNumberInsert(
recipientId = result.id,
oldE164 = E164_B,
newE164 = E164_A
),
PnpOperation.SessionSwitchoverInsert(result.id)
PnpOperation.SessionSwitchoverInsert(result.id, E164_A)
)
),
result.changeSet
Expand All @@ -295,7 +295,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetE164(result.id, E164_A),
)
),
Expand All @@ -314,7 +314,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetE164(result.id, E164_A),
PnpOperation.ChangeNumberInsert(
recipientId = result.id,
Expand All @@ -338,7 +338,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetE164(result.id, E164_A),
PnpOperation.SetPni(result.id, PNI_A)
)
Expand All @@ -358,7 +358,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.id),
operations = listOf(
operations = linkedSetOf(
PnpOperation.SetE164(result.id, E164_A),
PnpOperation.SetPni(result.id, PNI_A),
PnpOperation.ChangeNumberInsert(
Expand Down Expand Up @@ -387,7 +387,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.thirdId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.Merge(
primaryId = result.firstId,
secondaryId = result.secondId
Expand Down Expand Up @@ -416,7 +416,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.firstId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.Merge(
primaryId = result.firstId,
secondaryId = result.secondId
Expand All @@ -441,7 +441,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.firstId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.Merge(
primaryId = result.firstId,
secondaryId = result.secondId
Expand Down Expand Up @@ -470,7 +470,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.firstId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.RemovePni(result.secondId),
PnpOperation.SetPni(
recipientId = result.firstId,
Expand All @@ -496,7 +496,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.firstId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.RemovePni(result.firstId),
PnpOperation.Merge(
primaryId = result.firstId,
Expand All @@ -522,7 +522,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.firstId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.RemovePni(result.secondId),
PnpOperation.SetPni(result.firstId, PNI_A)
)
Expand All @@ -545,11 +545,11 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.firstId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.RemovePni(result.secondId),
PnpOperation.SetPni(result.firstId, PNI_A),
PnpOperation.SessionSwitchoverInsert(result.secondId),
PnpOperation.SessionSwitchoverInsert(result.firstId)
PnpOperation.SessionSwitchoverInsert(result.secondId, E164_A),
PnpOperation.SessionSwitchoverInsert(result.firstId, E164_A)
)
),
result.changeSet
Expand All @@ -570,7 +570,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.secondId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.Merge(
primaryId = result.secondId,
secondaryId = result.firstId
Expand All @@ -595,7 +595,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.secondId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.RemovePni(result.firstId),
PnpOperation.SetPni(
recipientId = result.secondId,
Expand Down Expand Up @@ -625,7 +625,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.secondId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.RemovePni(result.firstId),
PnpOperation.SetPni(
recipientId = result.secondId,
Expand Down Expand Up @@ -660,7 +660,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.secondId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.RemovePni(result.secondId),
PnpOperation.RemoveE164(result.secondId),
PnpOperation.Merge(
Expand Down Expand Up @@ -692,7 +692,7 @@ class RecipientTableTest_processPnpTupleToChangeSet {
assertEquals(
PnpChangeSet(
id = PnpIdResolver.PnpNoopId(result.secondId),
operations = listOf(
operations = linkedSetOf(
PnpOperation.RemoveE164(result.secondId),
PnpOperation.Merge(
primaryId = result.secondId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.thoughtcrime.securesms.util;

/**
* A class that allows us to inject feature flags during tests.
*/
public final class FeatureFlagsAccessor {

public static void forceValue(String key, Object value) {
FeatureFlags.FORCED_VALUES.put(FeatureFlags.PHONE_NUMBER_PRIVACY, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.thoughtcrime.securesms.database.model.databaseprotos.MessageExportState;
import org.thoughtcrime.securesms.database.model.databaseprotos.ProfileChangeDetails;
import org.thoughtcrime.securesms.database.model.databaseprotos.ThreadMergeEvent;
import org.thoughtcrime.securesms.database.model.databaseprotos.SessionSwitchoverEvent;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.groups.GroupMigrationMembershipChange;
import org.thoughtcrime.securesms.insights.InsightsConstants;
Expand All @@ -94,6 +95,7 @@
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
import org.thoughtcrime.securesms.stories.Stories;
import org.thoughtcrime.securesms.util.Base64;
import org.thoughtcrime.securesms.util.FeatureFlags;
import org.thoughtcrime.securesms.util.JsonUtils;
import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
Expand Down Expand Up @@ -1125,6 +1127,26 @@ public void insertThreadMergeEvent(@NonNull RecipientId recipientId, long thread
ApplicationDependencies.getDatabaseObserver().notifyConversationListeners(threadId);
}

public void insertSessionSwitchoverEvent(@NonNull RecipientId recipientId, long threadId, @NonNull SessionSwitchoverEvent event) {
if (!FeatureFlags.phoneNumberPrivacy()) {
throw new IllegalStateException("Should not occur in a non-PNP world!");
}

ContentValues values = new ContentValues();
values.put(RECIPIENT_ID, recipientId.serialize());
values.put(RECIPIENT_DEVICE_ID, 1);
values.put(DATE_RECEIVED, System.currentTimeMillis());
values.put(DATE_SENT, System.currentTimeMillis());
values.put(READ, 1);
values.put(TYPE, MessageTypes.SESSION_SWITCHOVER_TYPE);
values.put(THREAD_ID, threadId);
values.put(BODY, Base64.encodeBytes(event.toByteArray()));

getWritableDatabase().insert(TABLE_NAME, null, values);

ApplicationDependencies.getDatabaseObserver().notifyConversationListeners(threadId);
}

public void insertSmsExportMessage(@NonNull RecipientId recipientId, long threadId) {
ContentValues values = new ContentValues();
values.put(RECIPIENT_ID, recipientId.serialize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public interface MessageTypes {
long BOOST_REQUEST_TYPE = 15;
long THREAD_MERGE_TYPE = 16;
long SMS_EXPORT_TYPE = 17;
long SESSION_SWITCHOVER_TYPE = 18;

long BASE_INBOX_TYPE = 20;
long BASE_OUTBOX_TYPE = 21;
Expand Down Expand Up @@ -207,6 +208,10 @@ static boolean isThreadMergeType(long type) {
return (type & BASE_TYPE_MASK) == THREAD_MERGE_TYPE;
}

static boolean isSessionSwitchoverType(long type) {
return (type & BASE_TYPE_MASK) == SESSION_SWITCHOVER_TYPE;
}

static boolean isSecureType(long type) {
return (type & SECURE_MESSAGE_BIT) != 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ data class PnpDataSet(
* Applies the set of operations and returns the resulting dataset.
* Important: This only occurs _in memory_. You must still apply the operations to disk to persist them.
*/
fun perform(operations: List<PnpOperation>): PnpDataSet {
fun perform(operations: LinkedHashSet<PnpOperation>): PnpDataSet {
if (operations.isEmpty()) {
return this
}
Expand Down Expand Up @@ -136,7 +136,7 @@ data class PnpDataSet(
*/
data class PnpChangeSet(
val id: PnpIdResolver,
val operations: List<PnpOperation> = emptyList(),
val operations: LinkedHashSet<PnpOperation> = linkedSetOf(),
val breadCrumbs: List<String> = emptyList()
) {
// We want to exclude breadcrumbs from equality for testing purposes
Expand Down Expand Up @@ -213,7 +213,8 @@ sealed class PnpOperation {
}

data class SessionSwitchoverInsert(
override val recipientId: RecipientId
override val recipientId: RecipientId,
val e164: String?
) : PnpOperation()

data class ChangeNumberInsert(
Expand Down

0 comments on commit a7d9bd9

Please sign in to comment.