This repository was archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.js
68 lines (61 loc) · 2.32 KB
/
client.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
import * as Matrix from 'matrix-js-sdk'
function getRoomWithState(roomId) {
const checkForState = resolve => _ => {
const room = this.getRoom(roomId)
if (room) resolve(room)
else setTimeout(checkForState(resolve), 500)
}
return new Promise(resolve => checkForState(resolve)())
}
function getHttpUriForMxcFromHS(...theArgs) {
return Matrix.getHttpUriForMxc(this.getHomeserverUrl(), ...theArgs)
}
export default class Client {
static client
// XXX Currently unsure whether this is good enough for the deviceId of the
// client, since that has some crypto dimensions
static deviceId = Math.random().toString(16).substr(2, 14)
static isResumable() {
return !!localStorage.getItem('accessToken') &&
!!localStorage.getItem('userId') &&
!!localStorage.getItem('baseUrl')
}
static async initClient () {
let indexedDB
try { indexedDB = window.indexedDB; } catch (e) {}
const clientOpts = {
baseUrl: localStorage.getItem('baseUrl'),
userId: localStorage.getItem('userId'),
accessToken: localStorage.getItem('accessToken'),
timelineSupport: true,
unstableClientRelationAggregation: true
}
if (indexedDB) {
console.log("using indexedDB")
clientOpts.store = new Matrix.IndexedDBStore({
indexedDB,
localStorage,
dbName: "populus-web-sync",
workerScript: './indexeddb-worker.js'
})
await clientOpts.store.startup()
Client.client = Matrix.createClient(clientOpts)
} else {
Client.client = Matrix.createClient(clientOpts)
}
Client.client.getRoomWithState = getRoomWithState.bind(Client.client)
Client.client.getHttpUriForMxcFromHS = getHttpUriForMxcFromHS.bind(Client.client)
const notifTimelineSet = new Matrix.EventTimelineSet(null, { timelineSupport: true });
notifTimelineSet.getLiveTimeline().setPaginationToken("", Matrix.EventTimeline.BACKWARDS);
// XXX: following
// https://github.com/matrix-org/matrix-react-sdk/blob/2d1d42b90e8418017348cae1bd17a8a92340fdfb/src/MatrixClientPeg.ts#L296
// for original pagination token though this might not be correct.
Client.client.setNotifTimelineSet(notifTimelineSet);
return Client.client
}
static restart () {
Client.client.stopClient()
Client.client.store.deleteAllData()
Client.initClient()
}
}