Skip to content

Commit

Permalink
Storage service: save new AccountRecord fields to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn-Signal authored and josh-signal committed Apr 5, 2021
1 parent 6cfb3c9 commit a7c78b3
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 13 deletions.
32 changes: 20 additions & 12 deletions protos/SignalStorage.proto
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ message GroupV2Record {
}

message AccountRecord {
enum PhoneNumberSharingMode {
EVERYBODY = 0;
CONTACTS_ONLY = 1;
NOBODY = 2;
}

message PinnedConversation {
message Contact {
optional string uuid = 1;
Expand All @@ -106,16 +112,18 @@ message AccountRecord {
}
}

optional bytes profileKey = 1;
optional string givenName = 2;
optional string familyName = 3;
optional string avatarUrl = 4;
optional bool noteToSelfArchived = 5;
optional bool readReceipts = 6;
optional bool sealedSenderIndicators = 7;
optional bool typingIndicators = 8;
optional bool proxiedLinkPreviews = 9;
optional bool noteToSelfMarkedUnread = 10;
optional bool linkPreviews = 11;
repeated PinnedConversation pinnedConversations = 14;
optional bytes profileKey = 1;
optional string givenName = 2;
optional string familyName = 3;
optional string avatarUrl = 4;
optional bool noteToSelfArchived = 5;
optional bool readReceipts = 6;
optional bool sealedSenderIndicators = 7;
optional bool typingIndicators = 8;
optional bool proxiedLinkPreviews = 9;
optional bool noteToSelfMarkedUnread = 10;
optional bool linkPreviews = 11;
optional PhoneNumberSharingMode phoneNumberSharingMode = 12;
optional bool notDiscoverableByPhoneNumber = 13;
repeated PinnedConversation pinnedConversations = 14;
}
81 changes: 80 additions & 1 deletion ts/services/storageRecordOps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Signal Messenger, LLC
// Copyright 2020-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { isNumber } from 'lodash';
Expand All @@ -22,6 +22,16 @@ import {
waitThenMaybeUpdateGroup,
waitThenRespondToGroupV2Migration,
} from '../groups';
import { assert } from '../util/assert';
import { missingCaseError } from '../util/missingCaseError';
import {
PhoneNumberSharingMode,
parsePhoneNumberSharingMode,
} from '../util/phoneNumberSharingMode';
import {
PhoneNumberDiscoverability,
parsePhoneNumberDiscoverability,
} from '../util/phoneNumberDiscoverability';
import { ConversationModel } from '../models/conversations';
import { ConversationAttributesTypeType } from '../model-types.d';

Expand Down Expand Up @@ -158,6 +168,43 @@ export async function toAccountRecord(
window.storage.get('typingIndicators')
);
accountRecord.linkPreviews = Boolean(window.storage.get('linkPreviews'));

const PHONE_NUMBER_SHARING_MODE_ENUM =
window.textsecure.protobuf.AccountRecord.PhoneNumberSharingMode;
const phoneNumberSharingMode = parsePhoneNumberSharingMode(
window.storage.get('phoneNumberSharingMode')
);
switch (phoneNumberSharingMode) {
case PhoneNumberSharingMode.Everybody:
accountRecord.phoneNumberSharingMode =
PHONE_NUMBER_SHARING_MODE_ENUM.EVERYBODY;
break;
case PhoneNumberSharingMode.ContactsOnly:
accountRecord.phoneNumberSharingMode =
PHONE_NUMBER_SHARING_MODE_ENUM.CONTACTS_ONLY;
break;
case PhoneNumberSharingMode.Nobody:
accountRecord.phoneNumberSharingMode =
PHONE_NUMBER_SHARING_MODE_ENUM.NOBODY;
break;
default:
throw missingCaseError(phoneNumberSharingMode);
}

const phoneNumberDiscoverability = parsePhoneNumberDiscoverability(
window.storage.get('phoneNumberDiscoverability')
);
switch (phoneNumberDiscoverability) {
case PhoneNumberDiscoverability.Discoverable:
accountRecord.notDiscoverableByPhoneNumber = false;
break;
case PhoneNumberDiscoverability.NotDiscoverable:
accountRecord.notDiscoverableByPhoneNumber = true;
break;
default:
throw missingCaseError(phoneNumberDiscoverability);
}

const pinnedConversations = window.storage
.get<Array<string>>('pinnedConversationIds', [])
.map(id => {
Expand Down Expand Up @@ -655,8 +702,10 @@ export async function mergeAccountRecord(
const {
avatarUrl,
linkPreviews,
notDiscoverableByPhoneNumber,
noteToSelfArchived,
noteToSelfMarkedUnread,
phoneNumberSharingMode,
pinnedConversations: remotelyPinnedConversationClasses,
profileKey,
readReceipts,
Expand All @@ -678,6 +727,36 @@ export async function mergeAccountRecord(
window.storage.put('linkPreviews', linkPreviews);
}

const PHONE_NUMBER_SHARING_MODE_ENUM =
window.textsecure.protobuf.AccountRecord.PhoneNumberSharingMode;
let phoneNumberSharingModeToStore: PhoneNumberSharingMode;
switch (phoneNumberSharingMode) {
case undefined:
case null:
case PHONE_NUMBER_SHARING_MODE_ENUM.EVERYBODY:
phoneNumberSharingModeToStore = PhoneNumberSharingMode.Everybody;
break;
case PHONE_NUMBER_SHARING_MODE_ENUM.CONTACTS_ONLY:
phoneNumberSharingModeToStore = PhoneNumberSharingMode.ContactsOnly;
break;
case PHONE_NUMBER_SHARING_MODE_ENUM.NOBODY:
phoneNumberSharingModeToStore = PhoneNumberSharingMode.Nobody;
break;
default:
assert(
false,
`storageService.mergeAccountRecord: Got an unexpected phone number sharing mode: ${phoneNumberSharingMode}. Falling back to default`
);
phoneNumberSharingModeToStore = PhoneNumberSharingMode.Everybody;
break;
}
window.storage.put('phoneNumberSharingMode', phoneNumberSharingModeToStore);

const discoverability = notDiscoverableByPhoneNumber
? PhoneNumberDiscoverability.NotDiscoverable
: PhoneNumberDiscoverability.Discoverable;
window.storage.put('phoneNumberDiscoverability', discoverability);

if (profileKey) {
window.storage.put('profileKey', profileKey.toArrayBuffer());
}
Expand Down
10 changes: 10 additions & 0 deletions ts/textsecure.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1107,8 +1107,16 @@ export declare class PinnedConversationClass {
groupMasterKey?: ProtoBinaryType;
}

declare enum AccountRecordPhoneNumberSharingMode {
EVERYBODY = 0,
CONTACTS_ONLY = 1,
NOBODY = 2,
}

export declare class AccountRecordClass {
static PhoneNumberSharingMode: typeof AccountRecordPhoneNumberSharingMode;
static PinnedConversation: typeof PinnedConversationClass;

static decode: (
data: ArrayBuffer | ByteBufferClass,
encoding?: string
Expand All @@ -1124,6 +1132,8 @@ export declare class AccountRecordClass {
sealedSenderIndicators?: boolean | null;
typingIndicators?: boolean | null;
linkPreviews?: boolean | null;
phoneNumberSharingMode?: AccountRecordPhoneNumberSharingMode;
notDiscoverableByPhoneNumber?: boolean;
pinnedConversations?: PinnedConversationClass[];
noteToSelfMarkedUnread?: boolean;

Expand Down
15 changes: 15 additions & 0 deletions ts/util/phoneNumberDiscoverability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { makeEnumParser } from './enum';

// These strings are saved to disk, so be careful when changing them.
export enum PhoneNumberDiscoverability {
Discoverable = 'Discoverable',
NotDiscoverable = 'NotDiscoverable',
}

export const parsePhoneNumberDiscoverability = makeEnumParser(
PhoneNumberDiscoverability,
PhoneNumberDiscoverability.Discoverable
);
16 changes: 16 additions & 0 deletions ts/util/phoneNumberSharingMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { makeEnumParser } from './enum';

// These strings are saved to disk, so be careful when changing them.
export enum PhoneNumberSharingMode {
Everybody = 'Everybody',
ContactsOnly = 'ContactsOnly',
Nobody = 'Nobody',
}

export const parsePhoneNumberSharingMode = makeEnumParser(
PhoneNumberSharingMode,
PhoneNumberSharingMode.Everybody
);

0 comments on commit a7c78b3

Please sign in to comment.