-
Notifications
You must be signed in to change notification settings - Fork 291
/
User.js
270 lines (226 loc) · 8.75 KB
/
User.js
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
/*
* 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 ko from 'knockout';
import {Availability} from '@wireapp/protocol-messaging';
import {t} from 'Util/LocalizerUtil';
import {TIME_IN_MILLIS} from 'Util/TimeUtil';
import {clamp} from 'Util/NumberUtil';
import {compareTransliteration, startsWith, getFirstChar} from 'Util/StringUtil';
import {ACCENT_ID} from '../Config';
import {ROLE as TEAM_ROLE} from '../user/UserPermission';
import {WebAppEvents} from '../event/WebApp';
import {ConnectionEntity} from '../connection/ConnectionEntity';
// Please note: The own user has a "locale"
class User {
static get ACCENT_COLOR() {
return {
[ACCENT_ID.BLUE]: '#2391d3',
[ACCENT_ID.GREEN]: '#00c800',
[ACCENT_ID.ORANGE]: '#ff8900',
[ACCENT_ID.PINK]: '#fe5ebd',
[ACCENT_ID.PURPLE]: '#9c00fe',
[ACCENT_ID.RED]: '#fb0807',
[ACCENT_ID.YELLOW]: '#febf02',
};
}
static get CONFIG() {
return {
MANAGED_BY: {
SCIM: 'scim',
WIRE: 'wire',
},
TEMPORARY_GUEST: {
EXPIRATION_INTERVAL: TIME_IN_MILLIS.MINUTE,
EXPIRATION_THRESHOLD: TIME_IN_MILLIS.SECOND * 10,
LIFETIME: TIME_IN_MILLIS.DAY,
},
};
}
constructor(id = '') {
this.id = id;
this.is_me = false;
this.isService = false;
this.isSingleSignOn = false;
this.isDeleted = false;
this.providerId = undefined;
this.serviceId = undefined;
this.providerName = ko.observable(undefined);
this.joaatHash = -1;
this.accent_id = ko.observable(ACCENT_ID.BLUE);
this.accent_color = ko.pureComputed(() => User.ACCENT_COLOR[this.accent_id()] || User.ACCENT_COLOR[ACCENT_ID.BLUE]);
this.email = ko.observable();
this.phone = ko.observable();
this.name = ko.observable('');
this.first_name = ko.pureComputed(() => {
const [firstName] = this.name().split(' ');
return firstName || '';
});
this.managedBy = ko.observable(User.CONFIG.MANAGED_BY.WIRE);
this.last_name = ko.pureComputed(() => {
const nameParts = this.name().split(' ');
if (nameParts.length > 1) {
return nameParts.pop();
}
});
this.initials = ko.pureComputed(() => {
let initials = '';
if (this.first_name() && this.last_name()) {
const first = getFirstChar(this.first_name());
const last = getFirstChar(this.last_name());
initials = `${first}${last}`;
} else {
initials = this.first_name().slice(0, 2);
}
return initials.toUpperCase();
});
this.username = ko.observable('');
this.previewPictureResource = ko.observable();
this.mediumPictureResource = ko.observable();
this.connection = ko.observable(new ConnectionEntity());
this.isBlocked = ko.pureComputed(() => this.connection().isBlocked());
this.isCanceled = ko.pureComputed(() => this.connection().isCanceled());
this.isConnected = ko.pureComputed(() => this.connection().isConnected());
this.isIgnored = ko.pureComputed(() => this.connection().isIgnored());
this.isIncomingRequest = ko.pureComputed(() => this.connection().isIncomingRequest());
this.isOutgoingRequest = ko.pureComputed(() => this.connection().isOutgoingRequest());
this.isUnknown = ko.pureComputed(() => this.connection().isUnknown());
this.inTeam = ko.observable(false);
this.isGuest = ko.observable(false);
this.isTemporaryGuest = ko.observable(false);
this.isTeamMember = ko.observable(false);
this.teamRole = ko.observable(TEAM_ROLE.NONE);
this.teamId = undefined;
this.isRequest = ko.pureComputed(() => this.connection().isRequest());
this.devices = ko.observableArray(); // does not include current client/device
this.is_verified = ko.pureComputed(() => {
if (this.devices().length === 0 && !this.is_me) {
return false;
}
return this.devices().every(client_et => client_et.meta.isVerified());
});
this.isOnLegalHold = ko.pureComputed(() => {
return this.devices().some(client_et => client_et.isLegalHold());
});
const _hasPendingLegalHold = ko.observable(false);
this.hasPendingLegalHold = ko.pureComputed({
owner: this,
read: () => this.is_me && !this.isOnLegalHold() && _hasPendingLegalHold(),
write: value => _hasPendingLegalHold(value),
});
this.availability = ko.observable(Availability.Type.NONE);
this.expirationRemaining = ko.observable(0);
this.expirationText = ko.observable('');
this.expirationIsUrgent = ko.observable(false);
this.expirationRemainingText = ko.observable('');
this.expirationIntervalId = undefined;
this.expirationTimeoutId = undefined;
this.isExpired = ko.observable(false);
}
subscribeToChanges() {
this.availability.subscribe(() => amplify.publish(WebAppEvents.USER.PERSIST, this));
}
add_client(new_client_et) {
for (const client_et of this.devices()) {
if (client_et.id === new_client_et.id) {
return false;
}
}
this.devices.push(new_client_et);
if (this.is_me) {
this.devices.sort((client_a, client_b) => new Date(client_b.time) - new Date(client_a.time));
}
return true;
}
hasActivatedIdentity() {
return this.email() || this.phone() || this.isSingleSignOn;
}
remove_client(client_id) {
return this.devices.remove(client_et => client_et.id === client_id);
}
/**
* Check whether handle or name matches the given query
* @param {string} query - Query
* @param {boolean} is_handle - Query string is handle
* @param {array} excludedChars - list of chars to exclude from getSlug
* @returns {undefined} No return value
*/
matches(query, is_handle, excludedChars = []) {
if (is_handle) {
return startsWith(this.username(), query);
}
return compareTransliteration(this.name(), query, excludedChars) || this.username() === query;
}
serialize() {
return {
availability: this.availability(),
id: this.id,
};
}
setGuestExpiration(timestamp) {
if (this.expirationIntervalId) {
window.clearInterval(this.expirationIntervalId);
this.expirationIntervalId = undefined;
}
this._setRemainingExpirationTime(timestamp);
const expirationInterval = User.CONFIG.TEMPORARY_GUEST.EXPIRATION_INTERVAL;
this.expirationIntervalId = window.setInterval(
() => this._setRemainingExpirationTime(timestamp),
expirationInterval,
);
window.setTimeout(() => {
this.isExpired(true);
window.clearInterval(this.expirationIntervalId);
}, this.expirationRemaining());
}
clearExpirationTimeout() {
if (this.expirationTimeoutId) {
window.clearTimeout(this.expirationTimeoutId);
this.expirationTimeoutId = undefined;
}
}
checkGuestExpiration() {
const checkExpiration = this.isTemporaryGuest() && !this.expirationTimeoutId;
if (checkExpiration) {
if (this.isExpired()) {
return amplify.publish(WebAppEvents.USER.UPDATE, this.id);
}
const timeout = this.expirationRemaining() + User.CONFIG.TEMPORARY_GUEST.EXPIRATION_THRESHOLD;
this.expirationTimeoutId = window.setTimeout(() => amplify.publish(WebAppEvents.USER.UPDATE, this.id), timeout);
}
}
_setRemainingExpirationTime(expirationTime) {
const remainingTime = clamp(expirationTime - Date.now(), 0, User.CONFIG.TEMPORARY_GUEST.LIFETIME);
const remainingMinutes = Math.ceil(remainingTime / TIME_IN_MILLIS.MINUTE);
if (remainingMinutes <= 45) {
const remainingQuarters = Math.max(1, Math.ceil(remainingMinutes / 15));
const timeValue = remainingQuarters * 15;
this.expirationText(t('userRemainingTimeMinutes', timeValue));
this.expirationRemaining(timeValue * TIME_IN_MILLIS.MINUTE);
this.expirationRemainingText(`${timeValue}m`);
} else {
const showOneAndAHalf = remainingMinutes > 60 && remainingMinutes <= 90;
const timeValue = showOneAndAHalf ? 1.5 : Math.ceil(remainingMinutes / 60);
this.expirationText(t('userRemainingTimeHours', timeValue));
this.expirationRemaining(timeValue * TIME_IN_MILLIS.HOUR);
this.expirationRemainingText(`${timeValue}h`);
}
this.expirationIsUrgent(remainingMinutes < 120);
}
}
export {User};