|
@@ -0,0 +1,74 @@
|
|
1
|
+import * as matrixSDK from 'matrix-js-sdk'
|
|
2
|
+
|
|
3
|
+function fetchTopics() {
|
|
4
|
+ matrixSDK.createClient({
|
|
5
|
+ baseUrl: 'https://matrix.trancendances.fr',
|
|
6
|
+ })
|
|
7
|
+ .registerGuest()
|
|
8
|
+ .then((data) => {
|
|
9
|
+ return matrixSDK.createClient({
|
|
10
|
+ baseUrl: 'https://matrix.trancendances.fr',
|
|
11
|
+ accessToken: data['access_token'],
|
|
12
|
+ userId: data['user_id'],
|
|
13
|
+ deviceId: data['device_id'],
|
|
14
|
+ })
|
|
15
|
+ })
|
|
16
|
+ .then((client) => {
|
|
17
|
+ client.publicRooms(function(err, data) {
|
|
18
|
+ for (let publicRoom of data.chunk) {
|
|
19
|
+ if (!publicRoom['guest_can_join']) {
|
|
20
|
+ continue
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ client.roomInitialSync(publicRoom["room_id"], 20)
|
|
24
|
+ .then(async (syncData) => {
|
|
25
|
+ let name, creator, creationDate, categories
|
|
26
|
+ let nbMessages = 0
|
|
27
|
+ for (let stateEvent of syncData.state) {
|
|
28
|
+ if (stateEvent.type === "m.room.create") {
|
|
29
|
+ let userID = stateEvent.content.creator
|
|
30
|
+ let profileInfo = await client.getProfileInfo(stateEvent.content.creator, 'displayname')
|
|
31
|
+ creator = {
|
|
32
|
+ userID: userID,
|
|
33
|
+ displayName: profileInfo.displayname,
|
|
34
|
+ }
|
|
35
|
+ creationDate = new Date(stateEvent['origin_server_ts'])
|
|
36
|
+
|
|
37
|
+ }
|
|
38
|
+ if (stateEvent.type === "m.room.name") {
|
|
39
|
+ name = stateEvent.content.name
|
|
40
|
+ }
|
|
41
|
+ if (stateEvent.type === "m.forum.topic.categories") {
|
|
42
|
+ categories = stateEvent.content.categories.join(',')
|
|
43
|
+ }
|
|
44
|
+ }
|
|
45
|
+ for (let messageEvent of syncData.messages.chunk) {
|
|
46
|
+ if (messageEvent.type === "m.room.message") {
|
|
47
|
+ nbMessages++;
|
|
48
|
+ }
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ appendTopic(name, publicRoom['room_id'], creator, creationDate, nbMessages, categories)
|
|
52
|
+ })
|
|
53
|
+ }
|
|
54
|
+ });
|
|
55
|
+ })
|
|
56
|
+ .catch((err) => {
|
|
57
|
+ console.log(err);
|
|
58
|
+ })
|
|
59
|
+}
|
|
60
|
+
|
|
61
|
+function appendTopic(name, roomID, creator, creationDate, nbMessages, categories) {
|
|
62
|
+ let list = document.getElementById('topics')
|
|
63
|
+ let el = document.createElement('li')
|
|
64
|
+ let topicMsg = "Topic " + name + " (id " + roomID + ") created by "
|
|
65
|
+ + creator.displayName + " (" + creator.userID + ") on "
|
|
66
|
+ + creationDate.toString() + " containing " + nbMessages + " messages"
|
|
67
|
+ if (categories) {
|
|
68
|
+ topicMsg += ", listed in categorie(s) " + categories
|
|
69
|
+ }
|
|
70
|
+ el.innerHTML = topicMsg
|
|
71
|
+ list.appendChild(el)
|
|
72
|
+}
|
|
73
|
+
|
|
74
|
+fetchTopics()
|