/
PropertiesRepository.ts
341 lines (309 loc) · 12.3 KB
/
PropertiesRepository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/
import {amplify} from 'amplify';
import {Confirmation} from '@wireapp/protocol-messaging';
import {WebAppEvents} from '@wireapp/webapp-events';
import {AudioPreference, NotificationPreference, WebappProperties} from '@wireapp/api-client/dist/user/data';
import {ConsentType} from '@wireapp/api-client/dist/self';
import {Environment} from 'Util/Environment';
import {t} from 'Util/LocalizerUtil';
import {Logger, getLogger} from 'Util/Logger';
import {Config} from '../Config';
import type {User} from '../entity/User';
import type {SelfService} from '../self/SelfService';
import {ConsentValue} from '../user/ConsentValue';
import {ModalsViewModel} from '../view_model/ModalsViewModel';
import type {PropertiesService} from './PropertiesService';
import {PROPERTIES_TYPE} from './PropertiesType';
export class PropertiesRepository {
// Value names are specified by the protocol but key names can be changed.
static get CONFIG() {
return {
WEBAPP_ACCOUNT_SETTINGS: 'webapp',
WIRE_MARKETING_CONSENT: {
defaultValue: ConsentValue.NOT_GIVEN,
key: 'WIRE_MARKETING_CONSENT',
},
WIRE_RECEIPT_MODE: {
defaultValue: Confirmation.Type.DELIVERED,
key: 'WIRE_RECEIPT_MODE',
},
};
}
private readonly logger: Logger;
private readonly propertiesService: PropertiesService;
public readonly receiptMode: ko.Observable<Confirmation.Type>;
private readonly selfService: SelfService;
private readonly selfUser: ko.Observable<User>;
public properties: WebappProperties;
public readonly marketingConsent: ko.Observable<ConsentValue | boolean>;
constructor(propertiesService: PropertiesService, selfService: SelfService) {
this.propertiesService = propertiesService;
this.selfService = selfService;
this.logger = getLogger('PropertiesRepository');
this.properties = {
contact_import: {
macos: undefined,
},
enable_debugging: false,
settings: {
call: {
enable_vbr_encoding: true,
},
emoji: {
replace_inline: true,
},
interface: {
theme: 'default',
view_folders: false,
},
notifications: NotificationPreference.ON,
previews: {
send: true,
},
privacy: {
improve_wire: undefined,
report_errors: undefined,
},
sound: {
alerts: AudioPreference.ALL,
},
},
version: 1,
};
this.selfUser = ko.observable();
this.receiptMode = ko.observable(PropertiesRepository.CONFIG.WIRE_RECEIPT_MODE.defaultValue);
/** @type {ko.Observable<ConsentValue | boolean>} */
this.marketingConsent = ko.observable(PropertiesRepository.CONFIG.WIRE_MARKETING_CONSENT.defaultValue);
}
checkPrivacyPermission(): Promise<void> {
const isCheckConsentDisabled = !Config.getConfig().FEATURE.CHECK_CONSENT;
const isPrivacyPreferenceSet = this.getPreference(PROPERTIES_TYPE.PRIVACY) !== undefined;
return isCheckConsentDisabled || isPrivacyPreferenceSet
? Promise.resolve()
: new Promise(resolve => {
amplify.publish(WebAppEvents.WARNING.MODAL, ModalsViewModel.TYPE.CONFIRM, {
preventClose: true,
primaryAction: {
action: () => {
this.savePreference(PROPERTIES_TYPE.PRIVACY, true);
this.publishProperties();
resolve();
},
text: t('modalImproveWireAction'),
},
secondaryAction: {
action: () => {
this.savePreference(PROPERTIES_TYPE.PRIVACY, false);
resolve();
},
text: t('modalImproveWireSecondary'),
},
text: {
message: t('modalImproveWireMessage', Config.getConfig().BRAND_NAME),
title: t('modalImproveWireHeadline', Config.getConfig().BRAND_NAME),
},
});
});
}
getPreference(propertiesType: string): any {
const typeParts = propertiesType.split('.');
const [partOne, partTwo, partThree] = typeParts;
switch (typeParts.length) {
case 1:
return this.properties[partOne];
case 2:
return this.properties[partOne][partTwo];
case 3:
return this.properties[partOne][partTwo][partThree];
default:
throw new Error(`Failed to get preference of type ${propertiesType}`);
}
}
init(selfUserEntity: User): Promise<void> | Promise<WebappProperties> {
this.selfUser(selfUserEntity);
return this.selfUser().isTemporaryGuest() ? this.initTemporaryGuestAccount() : this.initActivatedAccount();
}
private fetchWebAppAccountSettings(): Promise<void> {
return this.propertiesService
.getPropertiesByKey(PropertiesRepository.CONFIG.WEBAPP_ACCOUNT_SETTINGS)
.then(properties => {
$.extend(true, this.properties, properties);
})
.catch(() => {
this.logger.warn(
`Property "${PropertiesRepository.CONFIG.WEBAPP_ACCOUNT_SETTINGS}" doesn't exist for this account. Continuing with the default value of "${this.properties.settings}".`,
);
});
}
private fetchReadReceiptsSetting(): Promise<void> {
const property = PropertiesRepository.CONFIG.WIRE_RECEIPT_MODE;
return this.propertiesService
.getPropertiesByKey(property.key)
.then(value => {
this.setProperty(property.key, value);
})
.catch(() => {
const message = `Property "${property.key}" doesn't exist for this account. Continuing with the default value of "${property.defaultValue}".`;
this.logger.warn(message);
});
}
private initActivatedAccount(): Promise<void> {
return Promise.all([this.fetchWebAppAccountSettings(), this.fetchReadReceiptsSetting()]).then(() => {
this.logger.info('Loaded user properties', this.properties);
this.publishProperties();
});
}
private initTemporaryGuestAccount(): Promise<WebappProperties> {
this.logger.info('Temporary guest user: Using default properties');
this.savePreference(PROPERTIES_TYPE.PRIVACY, false);
return Promise.resolve(this.publishProperties());
}
private publishProperties(): WebappProperties {
amplify.publish(WebAppEvents.PROPERTIES.UPDATED, this.properties);
return this.properties;
}
savePreference(propertiesType: string, updatedPreference: any): void {
if (updatedPreference === undefined) {
switch (propertiesType) {
case PROPERTIES_TYPE.CONTACT_IMPORT.MACOS:
updatedPreference = Date.now();
break;
default:
updatedPreference = true;
}
}
if (updatedPreference !== this.getPreference(propertiesType)) {
this.setPreference(propertiesType, updatedPreference);
const savePromise = this.selfUser().isTemporaryGuest()
? this.savePreferenceTemporaryGuestAccount(propertiesType, updatedPreference)
: this.savePreferenceActivatedAccount(propertiesType, updatedPreference);
savePromise.then(() => this.publishPropertyUpdate(propertiesType, updatedPreference));
}
}
deleteProperty(key: string): void {
switch (key) {
case PropertiesRepository.CONFIG.WIRE_RECEIPT_MODE.key:
this.setProperty(key, Confirmation.Type.DELIVERED);
break;
case PropertiesRepository.CONFIG.WIRE_MARKETING_CONSENT.key:
this.setProperty(key, ConsentValue.NOT_GIVEN);
break;
}
}
setProperty(key: string, value: any): void {
this.logger.info(`Setting key "${key}"...`, value);
switch (key) {
case PropertiesRepository.CONFIG.WEBAPP_ACCOUNT_SETTINGS:
if (this.properties.version === value.version) {
this.properties = {...this.properties, ...value};
this.publishProperties();
}
break;
case PropertiesRepository.CONFIG.WIRE_MARKETING_CONSENT.key:
this.marketingConsent(value);
break;
case PropertiesRepository.CONFIG.WIRE_RECEIPT_MODE.key:
this.receiptMode(value);
break;
}
}
updateProperty(key: string, value: any): Promise<void> | void {
switch (key) {
case PropertiesRepository.CONFIG.WIRE_RECEIPT_MODE.key:
if (value === Confirmation.Type.DELIVERED) {
return this.propertiesService.deletePropertiesByKey(key);
}
return this.propertiesService.putPropertiesByKey(key, value);
break;
case PropertiesRepository.CONFIG.WIRE_MARKETING_CONSENT.key:
return this.selfService
.putSelfConsent(ConsentType.MARKETING, value, `Webapp ${Environment.version(false)}`)
.then(() => {
if (value === ConsentValue.NOT_GIVEN) {
return this.propertiesService.deletePropertiesByKey(key);
}
return this.propertiesService.putPropertiesByKey(key, value);
});
break;
}
}
private savePreferenceActivatedAccount(propertiesType: string, updatedPreference: any): Promise<void> {
return this.propertiesService
.putPropertiesByKey(PropertiesRepository.CONFIG.WEBAPP_ACCOUNT_SETTINGS, this.properties)
.then(() => this.logger.info(`Saved updated preference "${propertiesType}": ${updatedPreference}`));
}
private savePreferenceTemporaryGuestAccount(propertiesType: string, updatedPreference: any): Promise<void> {
this.logger.info(`Updated preference: '${propertiesType}' - '${updatedPreference}'`);
return Promise.resolve();
}
private publishPropertyUpdate(propertiesType: string, updatedPreference: any): void {
switch (propertiesType) {
case PROPERTIES_TYPE.CONTACT_IMPORT.MACOS:
amplify.publish(WebAppEvents.PROPERTIES.UPDATE.CONTACTS, updatedPreference);
break;
case PROPERTIES_TYPE.INTERFACE.THEME:
amplify.publish(WebAppEvents.PROPERTIES.UPDATE.INTERFACE.THEME, updatedPreference);
break;
case PROPERTIES_TYPE.INTERFACE.VIEW_FOLDERS:
amplify.publish(WebAppEvents.PROPERTIES.UPDATE.INTERFACE.VIEW_FOLDERS, updatedPreference);
break;
case PROPERTIES_TYPE.EMOJI.REPLACE_INLINE:
amplify.publish(WebAppEvents.PROPERTIES.UPDATE.EMOJI.REPLACE_INLINE, updatedPreference);
break;
case PROPERTIES_TYPE.ENABLE_DEBUGGING:
amplify.publish(getLogger.prototype.LOG_ON_DEBUG, updatedPreference);
break;
case PROPERTIES_TYPE.NOTIFICATIONS:
amplify.publish(WebAppEvents.PROPERTIES.UPDATE.NOTIFICATIONS, updatedPreference);
break;
case PROPERTIES_TYPE.PREVIEWS.SEND:
amplify.publish(WebAppEvents.PROPERTIES.UPDATE.PREVIEWS.SEND, updatedPreference);
break;
case PROPERTIES_TYPE.PRIVACY:
amplify.publish(WebAppEvents.PROPERTIES.UPDATE.PRIVACY, updatedPreference);
break;
case PROPERTIES_TYPE.SOUND_ALERTS:
amplify.publish(WebAppEvents.PROPERTIES.UPDATE.SOUND_ALERTS, updatedPreference);
break;
case PROPERTIES_TYPE.CALL.ENABLE_VBR_ENCODING:
amplify.publish(WebAppEvents.PROPERTIES.UPDATE.CALL.ENABLE_VBR_ENCODING, updatedPreference);
break;
default:
throw new Error(`Failed to update preference of unhandled type '${propertiesType}'`);
}
}
private setPreference(propertiesType: string, changedPreference: {}): void {
const typeParts = propertiesType.split('.');
const [partOne, partTwo, partThree] = typeParts;
switch (typeParts.length) {
case 1:
this.properties[partOne] = changedPreference;
break;
case 2:
this.properties[partOne][partTwo] = changedPreference;
break;
case 3:
this.properties[partOne][partTwo][partThree] = changedPreference;
break;
default:
throw new Error(`Failed to set preference of type ${propertiesType}`);
}
}
}