blob: d40cc631174f5c58ddcd99af757516478f6e46f8 [file] [log] [blame]
Adrien Béraud6ecaa402021-04-06 17:37:25 -04001/*
2 * Copyright (c) 2017-2021 Savoir-faire Linux Inc.
3 *
4 * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
5 * Author: Asad Salman <me@asad.co>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
simond47ef9e2022-09-28 22:24:28 -040020'use strict';
Adrien Béraud947e8792021-04-15 18:32:44 -040021
simond47ef9e2022-09-28 22:24:28 -040022import { createRequire } from 'module';
23import path from 'path';
simonc7d52452022-09-23 02:09:42 -040024
simon06527b02022-10-01 15:01:47 -040025import Account, { RegistrationState } from './model/Account';
26import AccountDetails, { AccountConfig, VolatileDetails } from './model/AccountDetails';
27import Contact from './model/Contact';
28import Conversation, { Message } from './model/Conversation';
29import { Lookup, LookupResolveValue, PromiseExecutor } from './model/util';
simon07b4eb02022-09-29 17:50:26 -040030
Adrien Béraude74741b2021-04-19 13:22:54 -040031const require = createRequire(import.meta.url);
Adrien Béraud6ecaa402021-04-06 17:37:25 -040032
Adrien Béraud6ecaa402021-04-06 17:37:25 -040033class JamiDaemon {
simon06527b02022-10-01 15:01:47 -040034 private accounts: Account[];
35 private readonly lookups: Lookup[];
36 private readonly tempAccounts: Record<string, PromiseExecutor<string>>;
37 private dring: Record<string, any>;
38
39 constructor(onMessage: (account: Account, conversation: Conversation, message: Message) => void) {
simond47ef9e2022-09-28 22:24:28 -040040 this.accounts = [];
41 this.lookups = [];
simon06527b02022-10-01 15:01:47 -040042 this.tempAccounts = {};
simond47ef9e2022-09-28 22:24:28 -040043 this.dring = require(path.join(process.cwd(), 'jamid.node'));
44 this.dring.init({
45 AccountsChanged: () => {
46 console.log('AccountsChanged');
simon06527b02022-10-01 15:01:47 -040047 const newAccounts: Account[] = [];
simond47ef9e2022-09-28 22:24:28 -040048 JamiDaemon.vectToJs(this.dring.getAccountList()).forEach((accountId) => {
49 for (const account of this.accounts) {
50 if (account.getId() === accountId) {
51 newAccounts.push(account);
52 return;
53 }
54 }
55 newAccounts.push(
56 new Account(
57 accountId,
simon06527b02022-10-01 15:01:47 -040058 JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails,
59 JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId)) as VolatileDetails
simond47ef9e2022-09-28 22:24:28 -040060 )
61 );
62 });
63 this.accounts = newAccounts;
64 },
simon06527b02022-10-01 15:01:47 -040065 AccountDetailsChanged: (accountId: string, details: AccountDetails) => {
simond47ef9e2022-09-28 22:24:28 -040066 console.log(`AccountDetailsChanged ${accountId}`);
67 const account = this.getAccount(accountId);
68 if (!account) {
69 console.log(`Unknown account ${accountId}`);
70 return;
71 }
72 account.details = details;
73 },
simon06527b02022-10-01 15:01:47 -040074 VolatileDetailsChanged: (accountId: string, details: VolatileDetails) => {
simond47ef9e2022-09-28 22:24:28 -040075 console.log(`VolatileDetailsChanged ${accountId}`);
76 const account = this.getAccount(accountId);
77 if (!account) {
78 console.log(`Unknown account ${accountId}`);
79 return;
80 }
81 account.volatileDetails = details;
82 },
simon06527b02022-10-01 15:01:47 -040083 IncomingAccountMessage: (accountId: string, from: Account, message: Message) => {
simond47ef9e2022-09-28 22:24:28 -040084 console.log(`Received message: ${accountId} ${from} ${message['text/plain']}`);
85 /*
Adrien Béraud6ecaa402021-04-06 17:37:25 -040086 if (parser.validate(message["text/plain"]) === true) {
Adrien Béraude74741b2021-04-19 13:22:54 -040087 console.log(message["text/plain"])
Adrien Béraud6ecaa402021-04-06 17:37:25 -040088 } else {
89
Adrien Béraude74741b2021-04-19 13:22:54 -040090 user = connectedUsers[accountId]
Adrien Béraud6ecaa402021-04-06 17:37:25 -040091 console.log(user.socketId)
Adrien Béraude74741b2021-04-19 13:22:54 -040092 io.to(user.socketId).emit('receivedMessage', message["text/plain"])
93 //io.emit('receivedMessage', message["text/plain"])
Adrien Béraud6ecaa402021-04-06 17:37:25 -040094 }*/
simond47ef9e2022-09-28 22:24:28 -040095 },
simon06527b02022-10-01 15:01:47 -040096 RegistrationStateChanged: (accountId: string, state: RegistrationState, code: number, detail: string) => {
simond47ef9e2022-09-28 22:24:28 -040097 console.log('RegistrationStateChanged: ' + accountId + ' ' + state + ' ' + code + ' ' + detail);
simon06527b02022-10-01 15:01:47 -040098 const account: Account | undefined = this.getAccount(accountId);
simond47ef9e2022-09-28 22:24:28 -040099 if (account) {
100 account.registrationState = state;
101 } else {
102 console.log(`Unknown account ${accountId}`);
simon06527b02022-10-01 15:01:47 -0400103 return;
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400104 }
simond47ef9e2022-09-28 22:24:28 -0400105 const ctx = this.tempAccounts[accountId];
106 if (ctx) {
107 if (state === 'REGISTERED') {
simon06527b02022-10-01 15:01:47 -0400108 account.details = JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails;
simond47ef9e2022-09-28 22:24:28 -0400109 ctx.resolve(accountId);
110 delete this.tempAccounts[accountId];
111 } else if (state === 'ERROR_AUTH') {
112 this.dring.removeAccount(accountId);
113 ctx.reject(state);
114 delete this.tempAccounts[accountId];
115 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400116 }
simond47ef9e2022-09-28 22:24:28 -0400117 },
simon06527b02022-10-01 15:01:47 -0400118 RegisteredNameFound: (accountId: string, state: number, address: string, name: string) => {
simond47ef9e2022-09-28 22:24:28 -0400119 console.log(`RegisteredNameFound: ${accountId} ${state} ${address} ${name}`);
120 let lookups;
121 if (accountId) {
122 const account = this.getAccount(accountId);
123 if (!account) {
124 console.log(`Unknown account ${accountId}`);
125 return;
126 }
127 if (state == 0) {
128 const contact = account.getContactFromCache(address);
129 if (!contact.isRegisteredNameResolved()) contact.setRegisteredName(name);
130 }
131 lookups = account.lookups;
132 } else {
133 lookups = this.lookups;
134 }
135 let index = lookups.length - 1;
136 while (index >= 0) {
137 const lookup = lookups[index];
138 if ((lookup.address && lookup.address === address) || (lookup.name && lookup.name === name)) {
139 lookup.resolve({ address, name, state });
140 lookups.splice(index, 1);
141 }
142 index -= 1;
143 }
144 },
simon06527b02022-10-01 15:01:47 -0400145 NameRegistrationEnded: (accountId: string, state: number, name: string) => {
simond47ef9e2022-09-28 22:24:28 -0400146 console.log(`NameRegistrationEnded: ${accountId} ${state} ${name}`);
147 const account = this.getAccount(accountId);
148 if (account) {
149 if (state === 0) account.volatileDetails['Account.registeredName'] = name;
150 if (account.registeringName) {
151 account.registeringName.resolve(state);
152 delete account.registeringName;
153 }
154 } else {
155 console.log(`Unknown account ${accountId}`);
156 }
157 },
158 // Conversations
simon06527b02022-10-01 15:01:47 -0400159 ConversationReady: (accountId: string, conversationId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400160 console.log(`conversationReady: ${accountId} ${conversationId}`);
161 const account = this.getAccount(accountId);
Adrien Béraud150b4782021-04-21 19:40:59 -0400162 if (!account) {
simond47ef9e2022-09-28 22:24:28 -0400163 console.log(`Unknown account ${accountId}`);
164 return;
Adrien Béraud150b4782021-04-21 19:40:59 -0400165 }
simond47ef9e2022-09-28 22:24:28 -0400166 let conversation = account.getConversation(conversationId);
167 if (!conversation) {
168 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId));
169 members.forEach((member) => (member.contact = account.getContactFromCache(member.uri)));
170 conversation = new Conversation(conversationId, accountId, members);
171 account.addConversation(conversation);
172 }
173 },
simon06527b02022-10-01 15:01:47 -0400174 ConversationRemoved: (accountId: string, conversationId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400175 console.log(`conversationRemoved: ${accountId} ${conversationId}`);
176 const account = this.getAccount(accountId);
177 if (!account) {
178 console.log(`Unknown account ${accountId}`);
179 return;
180 }
181 account.removeConversation(conversationId);
182 },
simon06527b02022-10-01 15:01:47 -0400183 ConversationLoaded: (id: number, accountId: string, conversationId: string, messages: Message[]) => {
simond47ef9e2022-09-28 22:24:28 -0400184 console.log(`conversationLoaded: ${accountId} ${conversationId}`);
185 const account = this.getAccount(accountId);
186 if (!account) {
187 console.log(`Unknown account ${accountId}`);
188 return;
189 }
190 const conversation = account.getConversation(conversationId);
191 if (conversation) {
192 //conversation.addLoadedMessages(messages)
193 const request = conversation.requests[id];
194 if (request) {
195 request.resolve(messages);
196 }
197 }
198 },
simon06527b02022-10-01 15:01:47 -0400199 MessageReceived: (accountId: string, conversationId: string, message: Message) => {
simond47ef9e2022-09-28 22:24:28 -0400200 console.log(`messageReceived: ${accountId} ${conversationId}`);
201 console.log(message);
202 const account = this.getAccount(accountId);
203 if (!account) {
204 console.log(`Unknown account ${accountId}`);
205 return;
206 }
207 const conversation = account.getConversation(conversationId);
208 if (conversation) {
209 conversation.addMessage(message);
210 if (onMessage) onMessage(account, conversation, message);
211 }
212 },
simon06527b02022-10-01 15:01:47 -0400213 ConversationRequestReceived: (accountId: string, conversationId: string, message: Message) => {
simond47ef9e2022-09-28 22:24:28 -0400214 console.log(`conversationRequestReceived: ${accountId} ${conversationId}`);
215 const account = this.getAccount(accountId);
216 if (!account) {
217 console.log(`Unknown account ${accountId}`);
218 return;
219 }
220 },
simon06527b02022-10-01 15:01:47 -0400221 ConversationMemberEvent: (accountId: string, conversationId: string, memberUri: string, event: number) => {
simond47ef9e2022-09-28 22:24:28 -0400222 console.log(`conversationMemberEvent: ${accountId} ${conversationId}`);
223 const account = this.getAccount(accountId);
224 if (!account) {
225 console.log(`Unknown account ${accountId}`);
226 return;
227 }
228 },
simon06527b02022-10-01 15:01:47 -0400229 OnConversationError: (accountId: string, conversationId: string, code: number, what: string) => {
simond47ef9e2022-09-28 22:24:28 -0400230 console.log(`onConversationError: ${accountId} ${conversationId}`);
231 const account = this.getAccount(accountId);
232 if (!account) {
233 console.log(`Unknown account ${accountId}`);
234 return;
235 }
236 },
237 // Calls
simon06527b02022-10-01 15:01:47 -0400238 StateChange: (callId: string, state: string, code: number) => {
239 console.log(`CallStateChange: ${callId} ${state} ${code}`);
simond47ef9e2022-09-28 22:24:28 -0400240 },
simon06527b02022-10-01 15:01:47 -0400241 IncomingCall: (accountId: string, callId: string, peerUri: string) => {
simond47ef9e2022-09-28 22:24:28 -0400242 console.log(`IncomingCall: ${accountId} ${callId} ${peerUri}`);
243 },
simon06527b02022-10-01 15:01:47 -0400244 ConferenceCreated: (confId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400245 console.log(`ConferenceCreated: ${confId}`);
246 },
simon06527b02022-10-01 15:01:47 -0400247 ConferenceChanged: (accountId: string, confId: string, state: string) => {
simond47ef9e2022-09-28 22:24:28 -0400248 console.log(`ConferenceChanged: ${confId}`);
249 },
simon06527b02022-10-01 15:01:47 -0400250 ConferenceRemoved: (confId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400251 console.log(`ConferenceRemoved: ${confId}`);
252 },
simon06527b02022-10-01 15:01:47 -0400253 OnConferenceInfosUpdated: (confId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400254 console.log(`onConferenceInfosUpdated: ${confId}`);
255 },
256 });
257
258 JamiDaemon.vectToJs(this.dring.getAccountList()).forEach((accountId) => {
259 const account = new Account(
260 accountId,
simon06527b02022-10-01 15:01:47 -0400261 JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails,
262 JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId)) as VolatileDetails
simond47ef9e2022-09-28 22:24:28 -0400263 );
264
simon06527b02022-10-01 15:01:47 -0400265 account.contacts = JamiDaemon.vectMapToJs(this.dring.getContacts(accountId)) as Contact[];
simond47ef9e2022-09-28 22:24:28 -0400266
267 JamiDaemon.vectToJs(this.dring.getConversations(accountId)).forEach((conversationId) => {
268 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId));
269 console.log('\n\nXMEMBERS: ', members);
270 members.forEach((member) => {
271 member.contact = account.getContactFromCache(member.uri);
272 if (!member.contact.isRegisteredNameResolved()) {
273 if (!member.uri) return;
274 console.log(`lookupAddress ${accountId} ${member.uri}`, member);
275 member.contact.setRegisteredName(
simon06527b02022-10-01 15:01:47 -0400276 new Promise((resolve: (value: LookupResolveValue) => void, reject) =>
277 account.lookups.push({ address: member.uri, resolve, reject })
278 ).then((result) => {
279 if (result.state == 0) return result.name;
280 else if (result.state == 1) return undefined;
281 else return null;
282 })
simond47ef9e2022-09-28 22:24:28 -0400283 );
284 this.dring.lookupAddress(accountId, '', member.uri);
285 }
286 });
287 const conversation = new Conversation(conversationId, accountId, members);
simon06527b02022-10-01 15:01:47 -0400288 conversation.infos = JamiDaemon.mapToJs(this.dring.conversationInfos(accountId, conversationId));
simond47ef9e2022-09-28 22:24:28 -0400289 account.addConversation(conversation);
290 });
simond47ef9e2022-09-28 22:24:28 -0400291
292 this.accounts.push(account);
293 });
294 }
295
simon06527b02022-10-01 15:01:47 -0400296 addAccount(accountConfig: AccountConfig) {
simond47ef9e2022-09-28 22:24:28 -0400297 const params = this.accountDetailsToNative(accountConfig);
298 params.set('Account.type', 'RING');
simon06527b02022-10-01 15:01:47 -0400299 return new Promise<string>((resolve, reject) => {
simond47ef9e2022-09-28 22:24:28 -0400300 const accountId = this.dring.addAccount(params);
301 this.tempAccounts[accountId] = { resolve, reject };
302 });
303 }
304
simon06527b02022-10-01 15:01:47 -0400305 getDevices(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400306 return JamiDaemon.mapToJs(this.dring.getKnownRingDevices(accountId));
307 }
308
simon06527b02022-10-01 15:01:47 -0400309 getAccount(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400310 for (let i = 0; i < this.accounts.length; i++) {
311 const account = this.accounts[i];
312 if (account.getId() === accountId) return account;
Adrien Béraud150b4782021-04-21 19:40:59 -0400313 }
simond47ef9e2022-09-28 22:24:28 -0400314 return undefined;
315 }
316 getAccountList() {
317 return this.accounts;
318 }
simon06527b02022-10-01 15:01:47 -0400319 registerName(accountId: string, password: string, name: string) {
simond47ef9e2022-09-28 22:24:28 -0400320 return new Promise((resolve, reject) => {
321 if (!name) return reject(new Error('Invalid name'));
322 const account = this.getAccount(accountId);
323 if (!account) return reject(new Error("Can't find account"));
324 if (account.registeringName) return reject(new Error('Username already being registered'));
325 if (this.dring.registerName(accountId, password, name)) {
326 account.registeringName = { name, resolve, reject };
327 }
328 });
329 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400330
simon06527b02022-10-01 15:01:47 -0400331 getConversation(accountId: string, conversationId: string) {
simond47ef9e2022-09-28 22:24:28 -0400332 const account = this.getAccount(accountId);
333 if (account) return account.getConversation(conversationId);
334 return null;
335 }
simon06527b02022-10-01 15:01:47 -0400336 getAccountDetails(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400337 return JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId));
338 }
simon06527b02022-10-01 15:01:47 -0400339 setAccountDetails(accountId: string, details: AccountDetails) {
simond47ef9e2022-09-28 22:24:28 -0400340 this.dring.setAccountDetails(accountId, this.mapToNative(details));
341 }
342 getAudioOutputDeviceList() {
343 return JamiDaemon.vectToJs(this.dring.getAudioOutputDeviceList());
344 }
simon06527b02022-10-01 15:01:47 -0400345 getVolume(deviceName: string): number {
simond47ef9e2022-09-28 22:24:28 -0400346 return this.dring.getVolume(deviceName);
347 }
simon06527b02022-10-01 15:01:47 -0400348 setVolume(deviceName: string, volume: number) {
simond47ef9e2022-09-28 22:24:28 -0400349 return this.dring.setVolume(deviceName, volume);
350 }
351
simon06527b02022-10-01 15:01:47 -0400352 lookupName(accountId: string, name: string) {
simond47ef9e2022-09-28 22:24:28 -0400353 const p = new Promise((resolve, reject) => {
354 if (accountId) {
355 const account = this.getAccount(accountId);
356 if (!account) {
357 reject(new Error("Can't find account"));
358 } else {
359 account.lookups.push({ name, resolve, reject });
360 }
361 } else {
362 this.lookups.push({ name, resolve, reject });
363 }
364 });
365 this.dring.lookupName(accountId || '', '', name);
366 return p;
367 }
368
simon06527b02022-10-01 15:01:47 -0400369 lookupAddress(accountId: string, address: string) {
simond47ef9e2022-09-28 22:24:28 -0400370 console.log(`lookupAddress ${accountId} ${address}`);
371 const p = new Promise((resolve, reject) => {
372 if (accountId) {
373 const account = this.getAccount(accountId);
374 if (!account) {
375 reject(new Error("Can't find account"));
376 } else {
377 account.lookups.push({ address, resolve, reject });
378 }
379 } else {
380 this.lookups.push({ address, resolve, reject });
381 }
382 });
383 this.dring.lookupAddress(accountId || '', '', address);
384 return p;
385 }
386
387 stop() {
388 this.dring.fini();
389 }
390
simon06527b02022-10-01 15:01:47 -0400391 addContact(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400392 this.dring.addContact(accountId, contactId);
393 const details = JamiDaemon.mapToJs(this.dring.getContactDetails(accountId, contactId));
394 if (details.conversationId) {
395 const account = this.getAccount(accountId);
396 if (account) {
397 let conversation = account.getConversation(details.conversationId);
398 if (!conversation) {
399 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, details.conversationId));
400 members.forEach((member) => (member.contact = account.getContactFromCache(member.uri)));
401 conversation = new Conversation(details.conversationId, accountId, members);
402 account.addConversation(conversation);
403 }
404 }
Adrien Béraud4e287b92021-04-24 16:15:56 -0400405 }
simond47ef9e2022-09-28 22:24:28 -0400406 return details;
407 }
Adrien Béraud150b4782021-04-21 19:40:59 -0400408
simon06527b02022-10-01 15:01:47 -0400409 removeContact(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400410 //bool ban false
411 this.dring.removeContact(accountId, contactId, false);
412 }
413
simon06527b02022-10-01 15:01:47 -0400414 blockContact(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400415 //bool ban true
416 this.dring.removeContact(accountId, contactId, true);
417 }
418
simon06527b02022-10-01 15:01:47 -0400419 getContactDetails(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400420 return JamiDaemon.mapToJs(this.dring.getContactDetails(accountId, contactId));
421 }
422
simon06527b02022-10-01 15:01:47 -0400423 getDefaultModerators(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400424 const account = this.getAccount(accountId);
425 if (!account) {
426 console.log(`Unknown account ${accountId}`);
427 return {};
Adrien Béraud150b4782021-04-21 19:40:59 -0400428 }
simond47ef9e2022-09-28 22:24:28 -0400429 return JamiDaemon.vectToJs(this.dring.getDefaultModerators(accountId)).map((contactId) =>
430 account.getContactFromCache(contactId)
431 );
432 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400433
simon06527b02022-10-01 15:01:47 -0400434 addDefaultModerator(accountId: string, uri: string) {
simond47ef9e2022-09-28 22:24:28 -0400435 this.dring.setDefaultModerator(accountId, uri, true);
436 }
Adrien Béraud5e9e19b2021-04-22 01:38:53 -0400437
simon06527b02022-10-01 15:01:47 -0400438 removeDefaultModerator(accountId: string, uri: string) {
simond47ef9e2022-09-28 22:24:28 -0400439 this.dring.setDefaultModerator(accountId, uri, false);
440 }
Adrien Béraud4e287b92021-04-24 16:15:56 -0400441
simon06527b02022-10-01 15:01:47 -0400442 sendMessage(accountId: string, conversationId: string, message: string) {
simond47ef9e2022-09-28 22:24:28 -0400443 this.dring.sendMessage(accountId, conversationId, message, '');
444 }
Adrien Béraud4e287b92021-04-24 16:15:56 -0400445
simon06527b02022-10-01 15:01:47 -0400446 loadMessages(accountId: string, conversationId: string, fromMessage?: string) {
simond47ef9e2022-09-28 22:24:28 -0400447 const account = this.getAccount(accountId);
448 if (!account) throw new Error('Unknown account');
449 const conversation = account.getConversation(conversationId);
450 if (!conversation) throw new Error(`Unknown conversation ${conversationId}`);
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400451
simond47ef9e2022-09-28 22:24:28 -0400452 return new Promise((resolve, reject) => {
453 if (!conversation.requests) conversation.requests = {};
454 const requestId = this.dring.loadConversationMessages(accountId, conversationId, fromMessage || '', 32);
455 conversation.requests[requestId] = { resolve, reject };
456 });
457 }
458
simon06527b02022-10-01 15:01:47 -0400459 boolToStr(bool: boolean) {
simond47ef9e2022-09-28 22:24:28 -0400460 return bool ? 'true' : 'false';
461 }
462
simon06527b02022-10-01 15:01:47 -0400463 accountDetailsToNative(account: AccountConfig) {
simond47ef9e2022-09-28 22:24:28 -0400464 const params = new this.dring.StringMap();
465 if (account.managerUri) params.set('Account.managerUri', account.managerUri);
466 if (account.managerUsername) params.set('Account.managerUsername', account.managerUsername);
467 if (account.archivePassword) {
468 params.set('Account.archivePassword', account.archivePassword);
469 } /* else {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400470 console.log("archivePassword required")
Adrien Béraude74741b2021-04-19 13:22:54 -0400471 return
Adrien Béraud88a52442021-04-26 12:11:41 -0400472 }*/
simond47ef9e2022-09-28 22:24:28 -0400473 if (account.alias) params.set('Account.alias', account.alias);
474 if (account.displayName) params.set('Account.displayName', account.displayName);
475 if (account.enable !== undefined) params.set('Account.enable', this.boolToStr(account.enable));
476 if (account.autoAnswer !== undefined) params.set('Account.autoAnswer', this.boolToStr(account.autoAnswer));
477 if (account.autoAnswer !== undefined) params.set('Account.autoAnswer', this.boolToStr(account.autoAnswer));
478 if (account.ringtonePath) params.set('Account.ringtonePath', account.ringtonePath);
479 if (account.ringtoneEnabled !== undefined)
480 params.set('Account.ringtoneEnabled', this.boolToStr(account.ringtoneEnabled));
481 if (account.videoEnabled !== undefined) params.set('Account.videoEnabled', this.boolToStr(account.videoEnabled));
482 if (account.useragent) {
483 params.set('Account.useragent', account.useragent);
484 params.set('Account.hasCustomUserAgent', 'TRUE');
485 } else {
486 params.set('Account.hasCustomUserAgent', 'FALSE');
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400487 }
simond47ef9e2022-09-28 22:24:28 -0400488 if (account.audioPortMin) params.set('Account.audioPortMin', account.audioPortMin);
489 if (account.audioPortMax) params.set('Account.audioPortMax', account.audioPortMax);
490 if (account.videoPortMin) params.set('Account.videoPortMin', account.videoPortMin);
491 if (account.videoPortMax) params.set('Account.videoPortMax', account.videoPortMax);
492 if (account.localInterface) params.set('Account.localInterface', account.localInterface);
493 if (account.publishedSameAsLocal !== undefined)
494 params.set('Account.publishedSameAsLocal', this.boolToStr(account.publishedSameAsLocal));
495 if (account.localPort) params.set('Account.localPort', account.localPort);
496 if (account.publishedPort) params.set('Account.publishedPort', account.publishedPort);
497 if (account.rendezVous !== undefined) params.set('Account.rendezVous', this.boolToStr(account.rendezVous));
498 if (account.upnpEnabled !== undefined) params.set('Account.upnpEnabled', this.boolToStr(account.upnpEnabled));
499 return params;
500 }
simon06527b02022-10-01 15:01:47 -0400501 static vectToJs(vect: any) {
simond47ef9e2022-09-28 22:24:28 -0400502 const len = vect.size();
503 const outputArr = new Array(len);
504 for (let i = 0; i < len; i++) outputArr[i] = vect.get(i);
505 return outputArr;
506 }
simon06527b02022-10-01 15:01:47 -0400507 static mapToJs(m: any): Record<string, any> {
508 const outputObj: Record<string, any> = {};
simond47ef9e2022-09-28 22:24:28 -0400509 JamiDaemon.vectToJs(m.keys()).forEach((k) => (outputObj[k] = m.get(k)));
510 return outputObj;
511 }
simon06527b02022-10-01 15:01:47 -0400512 static vectMapToJs(vectMap: any) {
simond47ef9e2022-09-28 22:24:28 -0400513 const len = vectMap.size();
514 const outputArr = new Array(len);
515 for (let i = 0; i < len; i++) outputArr[i] = JamiDaemon.mapToJs(vectMap.get(i));
516 return outputArr;
517 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400518
simon06527b02022-10-01 15:01:47 -0400519 mapToNative(map: any) {
simond47ef9e2022-09-28 22:24:28 -0400520 const ret = new this.dring.StringMap();
521 for (const [key, value] of Object.entries(map)) ret.set(key, value);
522 return ret;
523 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400524}
525
simond47ef9e2022-09-28 22:24:28 -0400526export default JamiDaemon;