-
-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathsession.ts
372 lines (334 loc) · 9.03 KB
/
session.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { Toast } from '@capacitor/toast'
import { Device } from '@capacitor/device'
import throttle from 'lodash-es/throttle'
import redraw from './utils/redraw'
import signals from './signals'
import { SESSION_ID_KEY, fetchJSON, fetchText } from './http'
import { hasNetwork, handleXhrError, serializeQueryParameters } from './utils'
import { getAtPath, setAtPath, pick } from './utils/object'
import i18n from './i18n'
import push from './push'
import settings, { Prop } from './settings'
import { TempBan, LobbyData, NowPlayingGame } from './lichess/interfaces'
import { PlayTime, Perfs } from './lichess/interfaces/user'
import friendsApi from './lichess/friends'
import { PrefValue } from './lichess/prefs'
import challengesApi from './lichess/challenges'
import storage from './storage'
import asyncStorage from './asyncStorage'
import announce, { Announcement } from './announce'
interface Prefs {
[key: string]: PrefValue
}
export type EmailConfirm = { email_confirm: boolean }
export type SignupData = Session | EmailConfirm
export interface Profile {
readonly country?: string
readonly location?: string
readonly bio?: string
readonly firstName?: string
readonly lastName?: string
readonly fideRating?: number
readonly links?: string
}
export interface Session {
readonly id: string
readonly username: string
readonly title?: string
readonly online: boolean
readonly tosViolation?: boolean
readonly troll?: boolean
readonly kid: boolean
readonly patron: boolean
readonly language?: string
readonly profile?: Profile
readonly perfs: Perfs
readonly createdAt: number
readonly seenAt: number
readonly playTime: PlayTime
readonly nowPlaying: ReadonlyArray<NowPlayingGame>
readonly prefs: Prefs
readonly nbChallenges: number
readonly nbFollowers: number
readonly playban?: TempBan
// sent on login/signup only
readonly sessionId?: string
readonly announce?: Announcement
}
let session: Session | undefined
function isConnected(): boolean {
return session !== undefined
}
function getSession(): Session | undefined {
return session
}
// store session data for offline usage
function storeSession(d: Session): void {
asyncStorage.set('session', d)
}
// clear session data stored in async storage and sessionId
function onLogout(): void {
asyncStorage.remove('session')
storage.remove(SESSION_ID_KEY)
signals.afterLogout.dispatch()
}
function restoreStoredSession(): void {
asyncStorage.get<Session>('session')
.then(d => {
session = d || undefined
if (d !== null) {
signals.sessionRestored.dispatch()
redraw()
}
})
}
function getUserId(): string | undefined {
return session && session.id
}
function nowPlaying(): readonly NowPlayingGame[] {
const np = session && session.nowPlaying || []
return np.filter(e =>
settings.game.supportedVariants.indexOf(e.variant.key) !== -1
)
}
function isKidMode(): boolean {
return !!(session && session.kid)
}
function isShadowban(): boolean {
return !!(session && session.troll)
}
function myTurnGames(): readonly NowPlayingGame[] {
return nowPlaying().filter(e => e.isMyTurn)
}
function showSavedPrefToast(data: string): string {
Toast.show({ text: '✓ lichess.org: ' + i18n('yourPreferencesHaveBeenSaved'), position: 'center', duration: 'short' })
return data
}
function setKidMode(v: boolean, data: Record<string, string>): Promise<void> {
return fetchText(`/account/kid?v=${v}`, {
method: 'POST',
body: new URLSearchParams(data),
headers: {
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': 'application/json, text/*'
},
}, true)
.then(refresh)
}
function numValue(v: string | boolean | number): string {
if (v === true) return '1'
else if (v === false) return '0'
else return String(v)
}
function makeReducer(prefix: string) {
return function(acc: Partial<Prefs>, [k, v]: [string, PrefValue]) {
return {
...acc,
[prefix + k]: numValue(v)
}
}
}
const savePreferences = throttle((): Promise<string> => {
const prefs = session && session.prefs || {}
const display = Object.entries(pick(prefs, [
'animation',
'captured',
'highlight',
'destination',
'coords',
'replay',
'blindfold'
])).reduce(makeReducer('display.'), {}) as Record<string, string>
const behavior = Object.entries(pick(prefs, [
'premove',
'takeback',
'autoQueen',
'autoThreefold',
'submitMove',
'confirmResign',
'moretime',
])).reduce(makeReducer('behavior.'), {}) as Record<string, string>
const rest = Object.entries(pick(prefs, [
'clockTenths',
'clockBar',
'clockSound',
'follow',
'challenge',
'message',
'insightShare'
])).reduce(makeReducer(''), {}) as Record<string, string>
return fetchText('/account/preferences', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': 'application/json, text/*'
},
body: serializeQueryParameters({ ...rest, ...display, ...behavior })
}, true)
.then(showSavedPrefToast)
}, 1000)
function lichessBackedProp<T extends PrefValue>(path: string, defaultVal: T): Prop<T> {
return function(...args: unknown[]) {
if (args.length) {
let oldPref: T
if (session) {
oldPref = <T>getAtPath(session, path)
setAtPath(session, path, args[0])
}
savePreferences()
.catch((err) => {
if (session) setAtPath(session, path, oldPref)
if (hasNetwork()) handleXhrError(err)
})
}
return session ? <T>getAtPath(session, path) : defaultVal
}
}
function isSession(data: Session | LobbyData | SignupData): data is Session {
return (<Session>data).id !== undefined
}
function login(username: string, password: string, token: string | null): Promise<Session> {
return fetchJSON<Session | LobbyData>('/login', {
method: 'POST',
body: JSON.stringify({
username,
password,
token,
})
}, true)
.then(data => {
if (isSession(data)) {
session = data
if (session.sessionId) {
storage.set(SESSION_ID_KEY, session.sessionId)
}
storeSession(data)
sendUUID()
return session
} else {
throw { ipban: true }
}
})
}
function logout(): Promise<void> {
return push.unregister()
.then(() =>
fetchJSON('/logout', { method: 'POST' }, true)
.then(() => {
session = undefined
onLogout()
friendsApi.clear()
redraw()
})
)
.catch(handleXhrError)
}
function confirmEmail(token: string): Promise<Session> {
return fetchJSON<Session>(`/signup/confirm/${token}`, undefined, true)
.then(data => {
session = data
storeSession(data)
return session
})
}
function signup(
username: string,
email: string,
password: string
): Promise<SignupData> {
return fetchJSON<SignupData>('/signup', {
method: 'POST',
body: JSON.stringify({
username,
email,
password,
'can-confirm': true
})
}, true)
.then(d => {
if (isSession(d)) {
session = d
if (session.sessionId) {
storage.set(SESSION_ID_KEY, session.sessionId)
}
sendUUID()
}
return d
})
}
function rememberLogin(): Promise<Session> {
return fetchJSON<Session>('/account/info')
.then(data => {
session = data
storeSession(data)
return data
})
}
async function refresh(): Promise<void> {
try {
const data = await fetchJSON<Session>('/account/info', { cache: 'reload' })
session = data
storeSession(data)
announce.set(data.announce)
// if server tells me, reload challenges
if (session.nbChallenges !== challengesApi.incoming().length) {
challengesApi.refresh().then(redraw)
}
redraw()
} catch (err) {
if (session !== undefined && err.status === 401) {
session = undefined
onLogout()
redraw()
Toast.show({ text: 'You have been signed out', position: 'center', duration: 'short' })
}
}
}
async function backgroundRefresh(): Promise<void> {
if (hasNetwork() && isConnected()) {
const data = await fetchJSON<Session>('/account/info')
session = data
storeSession(data)
announce.set(data.announce)
// if server tells me, reload challenges
if (session.nbChallenges !== challengesApi.incoming().length) {
challengesApi.refresh().then(redraw)
}
}
}
function sendUUID(): void {
Device.getId()
.then(({ uuid }) => {
if (uuid !== 'web') {
fetchText(`/auth/set-fp/${uuid}/0`, { method: 'POST' })
}
})
}
export default {
isConnected,
isKidMode,
isShadowban,
logout,
signup,
login: throttle(login, 1000),
rememberLogin: throttle(rememberLogin, 1000),
restoreStoredSession,
refresh: throttle(refresh, 1000),
backgroundRefresh: throttle(backgroundRefresh, 1000),
get: getSession,
getUserId,
appUser(fallback: string): string {
if (session)
return session && session.username
else
return fallback
},
nowPlaying,
myTurnGames,
lichessBackedProp,
setKidMode,
confirmEmail,
hasCurrentBan(): boolean {
return session?.playban !== undefined
},
}