-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchatkithelper.js
129 lines (116 loc) · 4.02 KB
/
chatkithelper.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
import Rx from "rxjs/Rx";
const request = require('request');
export class ChatkitHelper {
constructor(chatkitInstance, pushHelperInstance) {
/**
* @type Chatkit
*/
this.chatkitInstance = chatkitInstance;
/**
* @type PushHelper
*/
this.pushHelperInstance = pushHelperInstance;
}
/**
* @returns {Observable}
*/
getUsers() {
return Rx.Observable.fromPromise(this.chatkitInstance.getUsers());
}
/**
*
* @param {string} userId
* @returns {Observable}
*/
getUserRooms(userId) {
return Rx.Observable.fromPromise(this.chatkitInstance.apiInstance.request({
method: 'GET',
path: "/users/" + userId + "/rooms",
jwt: this.chatkitInstance.getServerToken(),
}).then(function (res) {
return JSON.parse(res.body);
}).catch(err => console.log(err)));
}
/**
* @param userId
* @param roomId
* @param limit
* @returns {Observable}
*/
getRoomMessages(userId, roomId, limit) {
return Rx.Observable.fromPromise(this.chatkitInstance.getRoomMessages(userId, roomId, {
limit: limit
}));
}
/**
* @param userId
* @returns {Observable}
*/
getUserCursors(userId) {
return Rx.Observable.fromPromise(new Promise((resolve, reject) => {
const [_, apiVersion, location, instanceId] = this.chatkitInstance.instanceLocator.match(/^(v\d*):([a-z0-9]*):([a-f0-9\-]*)$/);
request('https://' + location + '.pusherplatform.io/services/chatkit_cursors/' + apiVersion + '/' + instanceId + '/cursors/0/users/' + userId, {
headers: {
'Authorization': 'Bearer ' + this.chatkitInstance.getServerToken()
}
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
resolve(JSON.parse(body));
} else {
reject(error);
console.error("ERROR", error);
}
});
}));
}
populateUsersWithRoomsAndMessages(users, messagesLimit) {
return Rx.Observable.combineLatest(
...users.map(user => this
.getUserRooms(user.id)
.do(rooms => user.rooms = rooms)
.flatMap(rooms =>
rooms.length ? Rx.Observable.combineLatest(
...rooms.map(room => this
.getRoomMessages(user.id, room.id, messagesLimit)
.do(messages => room.messages = messages)
)
) : Rx.Observable.of([])
)
.map(() => user)
)
)
}
populateUsersWithCursors(users) {
return Rx.Observable.combineLatest(
...users.map(user => this
.getUserCursors(user.id)
.do(cursors => {
user.cursors = {};
for (const x in cursors) {
if (cursors.hasOwnProperty(x)) {
user.cursors[cursors[x].room_id] = cursors[x].position;
}
}
})
.map(() => user)
)
)
}
/**
* @param users
*/
filterUsersRoomsAndMessages(users) {
return users.map(user => {
// Remove rooms that doesn't have any unread messages
user.rooms = user.rooms.map(room => {
room.messages = room.messages
.filter(message => message.id > (user.cursors[room.id] || 0)) // Filter out messages that's unread
.filter(message => message.id > this.pushHelperInstance.getLastPushedMessage(user.id, room.id));
return room;
}).filter(room => room.messages.length > 0);
return user;
}).filter(user => {
return user.rooms.length > 0;
});
}
}