forked from circuit/circuit-google-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuitClient.js
208 lines (184 loc) · 6.26 KB
/
circuitClient.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
'use strict';
const Circuit = require('circuit-sdk');
/**
* Wrapper class for Circuit.Client
*/
class CircuitClient {
constructor(credentials) {
// Create client instance
this.client = new Circuit.Client(credentials);
// Add peerUserId attribute for direct conversations
Circuit.Injectors.conversationInjector = c => {
if (c.type === 'DIRECT') {
c.peerUserId = c.participants.filter(userId => {
return userId !== this.client.loggedOnUser.userId;
})[0];
}
return c;
}
// Function bindings
this.addParticipant = this.client.addParticipant;
this.addTextItem = this.client.addTextItem;
this.getConversationsByIds = this.client.getConversationsByIds;
this.getDevices = this.client.getDevices;
this.getDirectConversationWithUser = this.client.getDirectConversationWithUser;
this.getPresence = this.client.getPresence;
this.getStartedCalls = this.client.getStartedCalls;
this.joinConference = this.client.joinConference;
this.removeParticipant = this.client.removeParticipant;
this.sendClickToCallRequest = this.client.sendClickToCallRequest;
this.setPresence = this.client.setPresence;
// Properties
Object.defineProperty(this, 'user', {
get: _ => { return this.client.loggedOnUser; }
});
}
/////////////////////////////////////
/// Public functions
/////////////////////////////////////
async searchUsers (query) {
const self = this;
return new Promise(async resolve => {
let searchId;
let userIds;
function searchResultHandler(evt) {
if (evt.data.searchId !== searchId) {
return;
}
if (!evt.data.users || !evt.data.users.length) {
return;
}
userIds = evt.data.users;
}
async function searchStatusHandler(evt) {
// Indicates is search is finished
if (evt.data.searchId !== searchId) {
return;
}
if (evt.data.status === 'FINISHED' || evt.data.status === 'NO_RESULT') {
self.client.removeEventListener('basicSearchResults', searchResultHandler);
self.client.removeEventListener('searchStatus', searchResultHandler);
if (userIds) {
resolve(await self.client.getUsersById(userIds));
} else {
resolve([]);
}
}
}
self.client.addEventListener('basicSearchResults', searchResultHandler);
self.client.addEventListener('searchStatus', searchStatusHandler);
searchId = await self.client.startUserSearch(query);
});
}
async searchConversationsByName(query) {
const self = this;
return new Promise(async resolve => {
let searchId;
let convIds;
function searchResultHandler(evt) {
if (evt.data.searchId !== searchId) {
return;
}
if (!evt.data.searchResults || !evt.data.searchResults.length) {
return;
}
console.log('basicSearchResults', evt);
convIds = evt.data.searchResults.filter(c => c.convId).map(c => c.convId);
}
async function searchStatusHandler(evt) {
// Indicates is search is finished
console.log('searchStatus', evt);
if (evt.data.searchId !== searchId) {
return;
}
if (evt.data.status === 'FINISHED' || evt.data.status === 'NO_RESULT') {
self.client.removeEventListener('basicSearchResults', searchResultHandler);
self.client.removeEventListener('searchStatus', searchResultHandler);
if (convIds) {
resolve(await self.client.getConversationsByIds(convIds));
} else {
resolve([]);
}
}
}
self.client.addEventListener('basicSearchResults', searchResultHandler);
self.client.addEventListener('searchStatus', searchStatusHandler);
searchId = await self.client.startBasicSearch([{
scope: Circuit.Enums.SearchScope.CONVERSATIONS,
searchTerm: query
}]);
});
}
async setPresenceAvailable() {
const statusMsg = await this.client.getStatusMessage();
try {
return this.client.setPresence({
state: Circuit.Enums.PresenceState.AVAILABLE,
statusMessage: statusMsg
});
} catch (e) {
console.log('Could not set user to available', e);
}
}
async setPresenceDnd(untilTime, duration) {
const statusMsg = await this.client.getStatusMessage();
if (untilTime !== '') {
untilTime = new Date(untilTime).getTime();
return this.client.setPresence({
state: Circuit.Enums.PresenceState.DND,
dndUntil: untilTime,
statusMessage: statusMsg
});
} else if (duration !== '') {
if (duration.unit === 'min') {
return this.client.setPresence({
state: Circuit.Enums.PresenceState.DND,
dndUntil: Date.now() + (60000 * duration.amount), //sets to 1 minute in ms * amount of minutes
statusMessage: statusMsg
});
} else if (duration.unit === 'h') {
return this.client.setPresence({
state: Circuit.Enums.PresenceState.DND,
dndUntil: Date.now() + (3600000 * duration.amount), //sets to 1 hour in ms * amount of hours
statusMessage: statusMsg
});
} else {
console.log('User has not entered a time in hours or minutes. Presence cannot be set.');
}
}
}
getDndTime() {
return this.client.getPresence(this.user.userId)
.then(res => res[0].dndUntil)
.catch(console.error);
}
getUserPresence() {
return this.client.getPresence(this.user.userId)
.then(res => res[0].state)
.catch(console.error);
}
/**
* logon
*/
logon (accessToken) {
return this.client.logon(accessToken ? { accessToken: accessToken } : undefined)
.then(user => console.log(`Logged on to Circuit: ${user.displayName}`));
}
/**
* logout
*/
logout() {
if (!this.client) {
return Promise.resolve();
}
const displayName = this.client.loggedOnUser.displayName;
return this.client.logout()
.then(_ => {
console.log(`Logged out of Circuit: ${displayName}`);
});
}
/////////////////////////////////////
/// Private functions
/////////////////////////////////////
}
module.exports = CircuitClient;