blob: bd8a53759d6dacf353747bd8b5c40210baff08d1 [file] [log] [blame]
Adrien Béraud6ecaa402021-04-06 17:37:25 -04001/*
simon26e79f72022-10-05 22:16:08 -04002 * Copyright (C) 2017-2021 Savoir-faire Linux Inc.
Adrien Béraud6ecaa402021-04-06 17:37:25 -04003 *
simon26e79f72022-10-05 22:16:08 -04004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
Adrien Béraud6ecaa402021-04-06 17:37:25 -04008 *
simon26e79f72022-10-05 22:16:08 -04009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
Adrien Béraud6ecaa402021-04-06 17:37:25 -040013 *
simon26e79f72022-10-05 22:16:08 -040014 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
Adrien Béraud6ecaa402021-04-06 17:37:25 -040017 */
simond47ef9e2022-09-28 22:24:28 -040018'use strict';
Adrien Béraud947e8792021-04-15 18:32:44 -040019
simon20076982022-10-11 15:04:13 -040020import {
21 Account,
22 AccountConfig,
23 AccountDetails,
24 Contact,
25 Conversation,
26 Lookup,
27 LookupResolveValue,
28 Message,
29 PromiseExecutor,
30 RegistrationState,
31 VolatileDetails,
32} from 'jami-web-common';
simond47ef9e2022-09-28 22:24:28 -040033import { createRequire } from 'module';
34import path from 'path';
simonc7d52452022-09-23 02:09:42 -040035
Adrien Béraude74741b2021-04-19 13:22:54 -040036const require = createRequire(import.meta.url);
Adrien Béraud6ecaa402021-04-06 17:37:25 -040037
Adrien Béraud6ecaa402021-04-06 17:37:25 -040038class JamiDaemon {
simon06527b02022-10-01 15:01:47 -040039 private accounts: Account[];
40 private readonly lookups: Lookup[];
41 private readonly tempAccounts: Record<string, PromiseExecutor<string>>;
42 private dring: Record<string, any>;
43
44 constructor(onMessage: (account: Account, conversation: Conversation, message: Message) => void) {
simond47ef9e2022-09-28 22:24:28 -040045 this.accounts = [];
46 this.lookups = [];
simon06527b02022-10-01 15:01:47 -040047 this.tempAccounts = {};
simond47ef9e2022-09-28 22:24:28 -040048 this.dring = require(path.join(process.cwd(), 'jamid.node'));
49 this.dring.init({
50 AccountsChanged: () => {
51 console.log('AccountsChanged');
simon06527b02022-10-01 15:01:47 -040052 const newAccounts: Account[] = [];
simond47ef9e2022-09-28 22:24:28 -040053 JamiDaemon.vectToJs(this.dring.getAccountList()).forEach((accountId) => {
54 for (const account of this.accounts) {
55 if (account.getId() === accountId) {
56 newAccounts.push(account);
57 return;
58 }
59 }
60 newAccounts.push(
61 new Account(
62 accountId,
simon06527b02022-10-01 15:01:47 -040063 JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails,
64 JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId)) as VolatileDetails
simond47ef9e2022-09-28 22:24:28 -040065 )
66 );
67 });
68 this.accounts = newAccounts;
69 },
simon06527b02022-10-01 15:01:47 -040070 AccountDetailsChanged: (accountId: string, details: AccountDetails) => {
simond47ef9e2022-09-28 22:24:28 -040071 console.log(`AccountDetailsChanged ${accountId}`);
72 const account = this.getAccount(accountId);
73 if (!account) {
74 console.log(`Unknown account ${accountId}`);
75 return;
76 }
77 account.details = details;
78 },
simon06527b02022-10-01 15:01:47 -040079 VolatileDetailsChanged: (accountId: string, details: VolatileDetails) => {
simond47ef9e2022-09-28 22:24:28 -040080 console.log(`VolatileDetailsChanged ${accountId}`);
81 const account = this.getAccount(accountId);
82 if (!account) {
83 console.log(`Unknown account ${accountId}`);
84 return;
85 }
86 account.volatileDetails = details;
87 },
simon06527b02022-10-01 15:01:47 -040088 IncomingAccountMessage: (accountId: string, from: Account, message: Message) => {
idillon-sfl9d956ab2022-10-20 16:33:24 -040089 console.log(`Received message: ${accountId} ${from} ${message}`);
simond47ef9e2022-09-28 22:24:28 -040090 /*
Adrien Béraud6ecaa402021-04-06 17:37:25 -040091 if (parser.validate(message["text/plain"]) === true) {
Adrien Béraude74741b2021-04-19 13:22:54 -040092 console.log(message["text/plain"])
Adrien Béraud6ecaa402021-04-06 17:37:25 -040093 } else {
94
Adrien Béraude74741b2021-04-19 13:22:54 -040095 user = connectedUsers[accountId]
Adrien Béraud6ecaa402021-04-06 17:37:25 -040096 console.log(user.socketId)
Adrien Béraude74741b2021-04-19 13:22:54 -040097 io.to(user.socketId).emit('receivedMessage', message["text/plain"])
98 //io.emit('receivedMessage', message["text/plain"])
Adrien Béraud6ecaa402021-04-06 17:37:25 -040099 }*/
simond47ef9e2022-09-28 22:24:28 -0400100 },
simon06527b02022-10-01 15:01:47 -0400101 RegistrationStateChanged: (accountId: string, state: RegistrationState, code: number, detail: string) => {
simond47ef9e2022-09-28 22:24:28 -0400102 console.log('RegistrationStateChanged: ' + accountId + ' ' + state + ' ' + code + ' ' + detail);
simon06527b02022-10-01 15:01:47 -0400103 const account: Account | undefined = this.getAccount(accountId);
simond47ef9e2022-09-28 22:24:28 -0400104 if (account) {
105 account.registrationState = state;
106 } else {
107 console.log(`Unknown account ${accountId}`);
simon06527b02022-10-01 15:01:47 -0400108 return;
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400109 }
simond47ef9e2022-09-28 22:24:28 -0400110 const ctx = this.tempAccounts[accountId];
111 if (ctx) {
112 if (state === 'REGISTERED') {
simon06527b02022-10-01 15:01:47 -0400113 account.details = JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails;
simond47ef9e2022-09-28 22:24:28 -0400114 ctx.resolve(accountId);
115 delete this.tempAccounts[accountId];
116 } else if (state === 'ERROR_AUTH') {
117 this.dring.removeAccount(accountId);
118 ctx.reject(state);
119 delete this.tempAccounts[accountId];
120 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400121 }
simond47ef9e2022-09-28 22:24:28 -0400122 },
simon06527b02022-10-01 15:01:47 -0400123 RegisteredNameFound: (accountId: string, state: number, address: string, name: string) => {
simond47ef9e2022-09-28 22:24:28 -0400124 console.log(`RegisteredNameFound: ${accountId} ${state} ${address} ${name}`);
125 let lookups;
126 if (accountId) {
127 const account = this.getAccount(accountId);
128 if (!account) {
129 console.log(`Unknown account ${accountId}`);
130 return;
131 }
simon73ef58d2022-10-27 00:25:55 -0400132 if (state === 0) {
simond47ef9e2022-09-28 22:24:28 -0400133 const contact = account.getContactFromCache(address);
134 if (!contact.isRegisteredNameResolved()) contact.setRegisteredName(name);
135 }
136 lookups = account.lookups;
137 } else {
138 lookups = this.lookups;
139 }
140 let index = lookups.length - 1;
141 while (index >= 0) {
142 const lookup = lookups[index];
143 if ((lookup.address && lookup.address === address) || (lookup.name && lookup.name === name)) {
144 lookup.resolve({ address, name, state });
145 lookups.splice(index, 1);
146 }
147 index -= 1;
148 }
149 },
simon06527b02022-10-01 15:01:47 -0400150 NameRegistrationEnded: (accountId: string, state: number, name: string) => {
simond47ef9e2022-09-28 22:24:28 -0400151 console.log(`NameRegistrationEnded: ${accountId} ${state} ${name}`);
152 const account = this.getAccount(accountId);
153 if (account) {
154 if (state === 0) account.volatileDetails['Account.registeredName'] = name;
155 if (account.registeringName) {
156 account.registeringName.resolve(state);
157 delete account.registeringName;
158 }
159 } else {
160 console.log(`Unknown account ${accountId}`);
161 }
162 },
163 // Conversations
simon06527b02022-10-01 15:01:47 -0400164 ConversationReady: (accountId: string, conversationId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400165 console.log(`conversationReady: ${accountId} ${conversationId}`);
166 const account = this.getAccount(accountId);
Adrien Béraud150b4782021-04-21 19:40:59 -0400167 if (!account) {
simond47ef9e2022-09-28 22:24:28 -0400168 console.log(`Unknown account ${accountId}`);
169 return;
Adrien Béraud150b4782021-04-21 19:40:59 -0400170 }
simond47ef9e2022-09-28 22:24:28 -0400171 let conversation = account.getConversation(conversationId);
172 if (!conversation) {
173 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId));
174 members.forEach((member) => (member.contact = account.getContactFromCache(member.uri)));
175 conversation = new Conversation(conversationId, accountId, members);
176 account.addConversation(conversation);
177 }
178 },
simon06527b02022-10-01 15:01:47 -0400179 ConversationRemoved: (accountId: string, conversationId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400180 console.log(`conversationRemoved: ${accountId} ${conversationId}`);
181 const account = this.getAccount(accountId);
182 if (!account) {
183 console.log(`Unknown account ${accountId}`);
184 return;
185 }
186 account.removeConversation(conversationId);
187 },
simon06527b02022-10-01 15:01:47 -0400188 ConversationLoaded: (id: number, accountId: string, conversationId: string, messages: Message[]) => {
simond47ef9e2022-09-28 22:24:28 -0400189 console.log(`conversationLoaded: ${accountId} ${conversationId}`);
190 const account = this.getAccount(accountId);
191 if (!account) {
192 console.log(`Unknown account ${accountId}`);
193 return;
194 }
195 const conversation = account.getConversation(conversationId);
196 if (conversation) {
197 //conversation.addLoadedMessages(messages)
198 const request = conversation.requests[id];
199 if (request) {
200 request.resolve(messages);
201 }
202 }
203 },
simon06527b02022-10-01 15:01:47 -0400204 MessageReceived: (accountId: string, conversationId: string, message: Message) => {
simond47ef9e2022-09-28 22:24:28 -0400205 console.log(`messageReceived: ${accountId} ${conversationId}`);
206 console.log(message);
207 const account = this.getAccount(accountId);
208 if (!account) {
209 console.log(`Unknown account ${accountId}`);
210 return;
211 }
212 const conversation = account.getConversation(conversationId);
213 if (conversation) {
214 conversation.addMessage(message);
215 if (onMessage) onMessage(account, conversation, message);
216 }
217 },
simon06527b02022-10-01 15:01:47 -0400218 ConversationRequestReceived: (accountId: string, conversationId: string, message: Message) => {
simond47ef9e2022-09-28 22:24:28 -0400219 console.log(`conversationRequestReceived: ${accountId} ${conversationId}`);
220 const account = this.getAccount(accountId);
221 if (!account) {
222 console.log(`Unknown account ${accountId}`);
223 return;
224 }
225 },
simon06527b02022-10-01 15:01:47 -0400226 ConversationMemberEvent: (accountId: string, conversationId: string, memberUri: string, event: number) => {
simond47ef9e2022-09-28 22:24:28 -0400227 console.log(`conversationMemberEvent: ${accountId} ${conversationId}`);
228 const account = this.getAccount(accountId);
229 if (!account) {
230 console.log(`Unknown account ${accountId}`);
231 return;
232 }
233 },
simon06527b02022-10-01 15:01:47 -0400234 OnConversationError: (accountId: string, conversationId: string, code: number, what: string) => {
simond47ef9e2022-09-28 22:24:28 -0400235 console.log(`onConversationError: ${accountId} ${conversationId}`);
236 const account = this.getAccount(accountId);
237 if (!account) {
238 console.log(`Unknown account ${accountId}`);
239 return;
240 }
241 },
242 // Calls
simon06527b02022-10-01 15:01:47 -0400243 StateChange: (callId: string, state: string, code: number) => {
244 console.log(`CallStateChange: ${callId} ${state} ${code}`);
simond47ef9e2022-09-28 22:24:28 -0400245 },
simon06527b02022-10-01 15:01:47 -0400246 IncomingCall: (accountId: string, callId: string, peerUri: string) => {
simond47ef9e2022-09-28 22:24:28 -0400247 console.log(`IncomingCall: ${accountId} ${callId} ${peerUri}`);
248 },
simon06527b02022-10-01 15:01:47 -0400249 ConferenceCreated: (confId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400250 console.log(`ConferenceCreated: ${confId}`);
251 },
simon06527b02022-10-01 15:01:47 -0400252 ConferenceChanged: (accountId: string, confId: string, state: string) => {
simond47ef9e2022-09-28 22:24:28 -0400253 console.log(`ConferenceChanged: ${confId}`);
254 },
simon06527b02022-10-01 15:01:47 -0400255 ConferenceRemoved: (confId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400256 console.log(`ConferenceRemoved: ${confId}`);
257 },
simon06527b02022-10-01 15:01:47 -0400258 OnConferenceInfosUpdated: (confId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400259 console.log(`onConferenceInfosUpdated: ${confId}`);
260 },
261 });
262
263 JamiDaemon.vectToJs(this.dring.getAccountList()).forEach((accountId) => {
264 const account = new Account(
265 accountId,
simon06527b02022-10-01 15:01:47 -0400266 JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails,
267 JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId)) as VolatileDetails
simond47ef9e2022-09-28 22:24:28 -0400268 );
269
simon06527b02022-10-01 15:01:47 -0400270 account.contacts = JamiDaemon.vectMapToJs(this.dring.getContacts(accountId)) as Contact[];
simond47ef9e2022-09-28 22:24:28 -0400271
272 JamiDaemon.vectToJs(this.dring.getConversations(accountId)).forEach((conversationId) => {
273 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId));
274 console.log('\n\nXMEMBERS: ', members);
275 members.forEach((member) => {
276 member.contact = account.getContactFromCache(member.uri);
277 if (!member.contact.isRegisteredNameResolved()) {
278 if (!member.uri) return;
279 console.log(`lookupAddress ${accountId} ${member.uri}`, member);
280 member.contact.setRegisteredName(
simon06527b02022-10-01 15:01:47 -0400281 new Promise((resolve: (value: LookupResolveValue) => void, reject) =>
282 account.lookups.push({ address: member.uri, resolve, reject })
283 ).then((result) => {
simon73ef58d2022-10-27 00:25:55 -0400284 if (result.state === 0) return result.name;
285 else if (result.state === 1) return undefined;
simon06527b02022-10-01 15:01:47 -0400286 else return null;
287 })
simond47ef9e2022-09-28 22:24:28 -0400288 );
289 this.dring.lookupAddress(accountId, '', member.uri);
290 }
291 });
292 const conversation = new Conversation(conversationId, accountId, members);
simon06527b02022-10-01 15:01:47 -0400293 conversation.infos = JamiDaemon.mapToJs(this.dring.conversationInfos(accountId, conversationId));
simond47ef9e2022-09-28 22:24:28 -0400294 account.addConversation(conversation);
295 });
simond47ef9e2022-09-28 22:24:28 -0400296
297 this.accounts.push(account);
298 });
299 }
300
simon06527b02022-10-01 15:01:47 -0400301 addAccount(accountConfig: AccountConfig) {
simond47ef9e2022-09-28 22:24:28 -0400302 const params = this.accountDetailsToNative(accountConfig);
303 params.set('Account.type', 'RING');
simon06527b02022-10-01 15:01:47 -0400304 return new Promise<string>((resolve, reject) => {
simond47ef9e2022-09-28 22:24:28 -0400305 const accountId = this.dring.addAccount(params);
306 this.tempAccounts[accountId] = { resolve, reject };
307 });
308 }
309
simon06527b02022-10-01 15:01:47 -0400310 getDevices(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400311 return JamiDaemon.mapToJs(this.dring.getKnownRingDevices(accountId));
312 }
313
simon06527b02022-10-01 15:01:47 -0400314 getAccount(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400315 for (let i = 0; i < this.accounts.length; i++) {
316 const account = this.accounts[i];
317 if (account.getId() === accountId) return account;
Adrien Béraud150b4782021-04-21 19:40:59 -0400318 }
simond47ef9e2022-09-28 22:24:28 -0400319 return undefined;
320 }
321 getAccountList() {
322 return this.accounts;
323 }
simon06527b02022-10-01 15:01:47 -0400324 registerName(accountId: string, password: string, name: string) {
simond47ef9e2022-09-28 22:24:28 -0400325 return new Promise((resolve, reject) => {
326 if (!name) return reject(new Error('Invalid name'));
327 const account = this.getAccount(accountId);
328 if (!account) return reject(new Error("Can't find account"));
329 if (account.registeringName) return reject(new Error('Username already being registered'));
330 if (this.dring.registerName(accountId, password, name)) {
331 account.registeringName = { name, resolve, reject };
332 }
333 });
334 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400335
simon06527b02022-10-01 15:01:47 -0400336 getConversation(accountId: string, conversationId: string) {
simond47ef9e2022-09-28 22:24:28 -0400337 const account = this.getAccount(accountId);
338 if (account) return account.getConversation(conversationId);
339 return null;
340 }
simon06527b02022-10-01 15:01:47 -0400341 getAccountDetails(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400342 return JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId));
343 }
simon06527b02022-10-01 15:01:47 -0400344 setAccountDetails(accountId: string, details: AccountDetails) {
simond47ef9e2022-09-28 22:24:28 -0400345 this.dring.setAccountDetails(accountId, this.mapToNative(details));
346 }
347 getAudioOutputDeviceList() {
348 return JamiDaemon.vectToJs(this.dring.getAudioOutputDeviceList());
349 }
simon06527b02022-10-01 15:01:47 -0400350 getVolume(deviceName: string): number {
simond47ef9e2022-09-28 22:24:28 -0400351 return this.dring.getVolume(deviceName);
352 }
simon06527b02022-10-01 15:01:47 -0400353 setVolume(deviceName: string, volume: number) {
simond47ef9e2022-09-28 22:24:28 -0400354 return this.dring.setVolume(deviceName, volume);
355 }
356
simon06527b02022-10-01 15:01:47 -0400357 lookupName(accountId: string, name: string) {
simond47ef9e2022-09-28 22:24:28 -0400358 const p = new Promise((resolve, reject) => {
359 if (accountId) {
360 const account = this.getAccount(accountId);
361 if (!account) {
362 reject(new Error("Can't find account"));
363 } else {
364 account.lookups.push({ name, resolve, reject });
365 }
366 } else {
367 this.lookups.push({ name, resolve, reject });
368 }
369 });
370 this.dring.lookupName(accountId || '', '', name);
371 return p;
372 }
373
simon06527b02022-10-01 15:01:47 -0400374 lookupAddress(accountId: string, address: string) {
simond47ef9e2022-09-28 22:24:28 -0400375 console.log(`lookupAddress ${accountId} ${address}`);
376 const p = new Promise((resolve, reject) => {
377 if (accountId) {
378 const account = this.getAccount(accountId);
379 if (!account) {
380 reject(new Error("Can't find account"));
381 } else {
382 account.lookups.push({ address, resolve, reject });
383 }
384 } else {
385 this.lookups.push({ address, resolve, reject });
386 }
387 });
388 this.dring.lookupAddress(accountId || '', '', address);
389 return p;
390 }
391
392 stop() {
393 this.dring.fini();
394 }
395
simon06527b02022-10-01 15:01:47 -0400396 addContact(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400397 this.dring.addContact(accountId, contactId);
398 const details = JamiDaemon.mapToJs(this.dring.getContactDetails(accountId, contactId));
399 if (details.conversationId) {
400 const account = this.getAccount(accountId);
401 if (account) {
402 let conversation = account.getConversation(details.conversationId);
403 if (!conversation) {
404 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, details.conversationId));
405 members.forEach((member) => (member.contact = account.getContactFromCache(member.uri)));
406 conversation = new Conversation(details.conversationId, accountId, members);
407 account.addConversation(conversation);
408 }
409 }
Adrien Béraud4e287b92021-04-24 16:15:56 -0400410 }
simond47ef9e2022-09-28 22:24:28 -0400411 return details;
412 }
Adrien Béraud150b4782021-04-21 19:40:59 -0400413
simon06527b02022-10-01 15:01:47 -0400414 removeContact(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400415 //bool ban false
416 this.dring.removeContact(accountId, contactId, false);
417 }
418
simon06527b02022-10-01 15:01:47 -0400419 blockContact(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400420 //bool ban true
421 this.dring.removeContact(accountId, contactId, true);
422 }
423
simon06527b02022-10-01 15:01:47 -0400424 getContactDetails(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400425 return JamiDaemon.mapToJs(this.dring.getContactDetails(accountId, contactId));
426 }
427
simon06527b02022-10-01 15:01:47 -0400428 getDefaultModerators(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400429 const account = this.getAccount(accountId);
430 if (!account) {
431 console.log(`Unknown account ${accountId}`);
432 return {};
Adrien Béraud150b4782021-04-21 19:40:59 -0400433 }
simond47ef9e2022-09-28 22:24:28 -0400434 return JamiDaemon.vectToJs(this.dring.getDefaultModerators(accountId)).map((contactId) =>
435 account.getContactFromCache(contactId)
436 );
437 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400438
simon06527b02022-10-01 15:01:47 -0400439 addDefaultModerator(accountId: string, uri: string) {
simond47ef9e2022-09-28 22:24:28 -0400440 this.dring.setDefaultModerator(accountId, uri, true);
441 }
Adrien Béraud5e9e19b2021-04-22 01:38:53 -0400442
simon06527b02022-10-01 15:01:47 -0400443 removeDefaultModerator(accountId: string, uri: string) {
simond47ef9e2022-09-28 22:24:28 -0400444 this.dring.setDefaultModerator(accountId, uri, false);
445 }
Adrien Béraud4e287b92021-04-24 16:15:56 -0400446
simon06527b02022-10-01 15:01:47 -0400447 sendMessage(accountId: string, conversationId: string, message: string) {
simond47ef9e2022-09-28 22:24:28 -0400448 this.dring.sendMessage(accountId, conversationId, message, '');
449 }
Adrien Béraud4e287b92021-04-24 16:15:56 -0400450
simon06527b02022-10-01 15:01:47 -0400451 loadMessages(accountId: string, conversationId: string, fromMessage?: string) {
simond47ef9e2022-09-28 22:24:28 -0400452 const account = this.getAccount(accountId);
453 if (!account) throw new Error('Unknown account');
454 const conversation = account.getConversation(conversationId);
455 if (!conversation) throw new Error(`Unknown conversation ${conversationId}`);
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400456
simond47ef9e2022-09-28 22:24:28 -0400457 return new Promise((resolve, reject) => {
458 if (!conversation.requests) conversation.requests = {};
459 const requestId = this.dring.loadConversationMessages(accountId, conversationId, fromMessage || '', 32);
460 conversation.requests[requestId] = { resolve, reject };
461 });
462 }
463
simon06527b02022-10-01 15:01:47 -0400464 boolToStr(bool: boolean) {
simond47ef9e2022-09-28 22:24:28 -0400465 return bool ? 'true' : 'false';
466 }
467
simon06527b02022-10-01 15:01:47 -0400468 accountDetailsToNative(account: AccountConfig) {
simond47ef9e2022-09-28 22:24:28 -0400469 const params = new this.dring.StringMap();
470 if (account.managerUri) params.set('Account.managerUri', account.managerUri);
471 if (account.managerUsername) params.set('Account.managerUsername', account.managerUsername);
472 if (account.archivePassword) {
473 params.set('Account.archivePassword', account.archivePassword);
474 } /* else {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400475 console.log("archivePassword required")
Adrien Béraude74741b2021-04-19 13:22:54 -0400476 return
Adrien Béraud88a52442021-04-26 12:11:41 -0400477 }*/
simond47ef9e2022-09-28 22:24:28 -0400478 if (account.alias) params.set('Account.alias', account.alias);
479 if (account.displayName) params.set('Account.displayName', account.displayName);
480 if (account.enable !== undefined) params.set('Account.enable', this.boolToStr(account.enable));
481 if (account.autoAnswer !== undefined) params.set('Account.autoAnswer', this.boolToStr(account.autoAnswer));
482 if (account.autoAnswer !== undefined) params.set('Account.autoAnswer', this.boolToStr(account.autoAnswer));
483 if (account.ringtonePath) params.set('Account.ringtonePath', account.ringtonePath);
484 if (account.ringtoneEnabled !== undefined)
485 params.set('Account.ringtoneEnabled', this.boolToStr(account.ringtoneEnabled));
486 if (account.videoEnabled !== undefined) params.set('Account.videoEnabled', this.boolToStr(account.videoEnabled));
487 if (account.useragent) {
488 params.set('Account.useragent', account.useragent);
489 params.set('Account.hasCustomUserAgent', 'TRUE');
490 } else {
491 params.set('Account.hasCustomUserAgent', 'FALSE');
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400492 }
simond47ef9e2022-09-28 22:24:28 -0400493 if (account.audioPortMin) params.set('Account.audioPortMin', account.audioPortMin);
494 if (account.audioPortMax) params.set('Account.audioPortMax', account.audioPortMax);
495 if (account.videoPortMin) params.set('Account.videoPortMin', account.videoPortMin);
496 if (account.videoPortMax) params.set('Account.videoPortMax', account.videoPortMax);
497 if (account.localInterface) params.set('Account.localInterface', account.localInterface);
498 if (account.publishedSameAsLocal !== undefined)
499 params.set('Account.publishedSameAsLocal', this.boolToStr(account.publishedSameAsLocal));
500 if (account.localPort) params.set('Account.localPort', account.localPort);
501 if (account.publishedPort) params.set('Account.publishedPort', account.publishedPort);
502 if (account.rendezVous !== undefined) params.set('Account.rendezVous', this.boolToStr(account.rendezVous));
503 if (account.upnpEnabled !== undefined) params.set('Account.upnpEnabled', this.boolToStr(account.upnpEnabled));
504 return params;
505 }
simon06527b02022-10-01 15:01:47 -0400506 static vectToJs(vect: any) {
simond47ef9e2022-09-28 22:24:28 -0400507 const len = vect.size();
508 const outputArr = new Array(len);
509 for (let i = 0; i < len; i++) outputArr[i] = vect.get(i);
510 return outputArr;
511 }
simon06527b02022-10-01 15:01:47 -0400512 static mapToJs(m: any): Record<string, any> {
513 const outputObj: Record<string, any> = {};
simond47ef9e2022-09-28 22:24:28 -0400514 JamiDaemon.vectToJs(m.keys()).forEach((k) => (outputObj[k] = m.get(k)));
515 return outputObj;
516 }
simon06527b02022-10-01 15:01:47 -0400517 static vectMapToJs(vectMap: any) {
simond47ef9e2022-09-28 22:24:28 -0400518 const len = vectMap.size();
519 const outputArr = new Array(len);
520 for (let i = 0; i < len; i++) outputArr[i] = JamiDaemon.mapToJs(vectMap.get(i));
521 return outputArr;
522 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400523
simon06527b02022-10-01 15:01:47 -0400524 mapToNative(map: any) {
simond47ef9e2022-09-28 22:24:28 -0400525 const ret = new this.dring.StringMap();
526 for (const [key, value] of Object.entries(map)) ret.set(key, value);
527 return ret;
528 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400529}
530
simond47ef9e2022-09-28 22:24:28 -0400531export default JamiDaemon;