blob: c6c9666aa945a4573b5e5955ca970aeba50f991e [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
simond47ef9e2022-09-28 22:24:28 -040020import { createRequire } from 'module';
21import path from 'path';
simonc7d52452022-09-23 02:09:42 -040022
simon06527b02022-10-01 15:01:47 -040023import Account, { RegistrationState } from './model/Account';
24import AccountDetails, { AccountConfig, VolatileDetails } from './model/AccountDetails';
25import Contact from './model/Contact';
26import Conversation, { Message } from './model/Conversation';
27import { Lookup, LookupResolveValue, PromiseExecutor } from './model/util';
simon07b4eb02022-09-29 17:50:26 -040028
Adrien Béraude74741b2021-04-19 13:22:54 -040029const require = createRequire(import.meta.url);
Adrien Béraud6ecaa402021-04-06 17:37:25 -040030
Adrien Béraud6ecaa402021-04-06 17:37:25 -040031class JamiDaemon {
simon06527b02022-10-01 15:01:47 -040032 private accounts: Account[];
33 private readonly lookups: Lookup[];
34 private readonly tempAccounts: Record<string, PromiseExecutor<string>>;
35 private dring: Record<string, any>;
36
37 constructor(onMessage: (account: Account, conversation: Conversation, message: Message) => void) {
simond47ef9e2022-09-28 22:24:28 -040038 this.accounts = [];
39 this.lookups = [];
simon06527b02022-10-01 15:01:47 -040040 this.tempAccounts = {};
simond47ef9e2022-09-28 22:24:28 -040041 this.dring = require(path.join(process.cwd(), 'jamid.node'));
42 this.dring.init({
43 AccountsChanged: () => {
44 console.log('AccountsChanged');
simon06527b02022-10-01 15:01:47 -040045 const newAccounts: Account[] = [];
simond47ef9e2022-09-28 22:24:28 -040046 JamiDaemon.vectToJs(this.dring.getAccountList()).forEach((accountId) => {
47 for (const account of this.accounts) {
48 if (account.getId() === accountId) {
49 newAccounts.push(account);
50 return;
51 }
52 }
53 newAccounts.push(
54 new Account(
55 accountId,
simon06527b02022-10-01 15:01:47 -040056 JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails,
57 JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId)) as VolatileDetails
simond47ef9e2022-09-28 22:24:28 -040058 )
59 );
60 });
61 this.accounts = newAccounts;
62 },
simon06527b02022-10-01 15:01:47 -040063 AccountDetailsChanged: (accountId: string, details: AccountDetails) => {
simond47ef9e2022-09-28 22:24:28 -040064 console.log(`AccountDetailsChanged ${accountId}`);
65 const account = this.getAccount(accountId);
66 if (!account) {
67 console.log(`Unknown account ${accountId}`);
68 return;
69 }
70 account.details = details;
71 },
simon06527b02022-10-01 15:01:47 -040072 VolatileDetailsChanged: (accountId: string, details: VolatileDetails) => {
simond47ef9e2022-09-28 22:24:28 -040073 console.log(`VolatileDetailsChanged ${accountId}`);
74 const account = this.getAccount(accountId);
75 if (!account) {
76 console.log(`Unknown account ${accountId}`);
77 return;
78 }
79 account.volatileDetails = details;
80 },
simon06527b02022-10-01 15:01:47 -040081 IncomingAccountMessage: (accountId: string, from: Account, message: Message) => {
simond47ef9e2022-09-28 22:24:28 -040082 console.log(`Received message: ${accountId} ${from} ${message['text/plain']}`);
83 /*
Adrien Béraud6ecaa402021-04-06 17:37:25 -040084 if (parser.validate(message["text/plain"]) === true) {
Adrien Béraude74741b2021-04-19 13:22:54 -040085 console.log(message["text/plain"])
Adrien Béraud6ecaa402021-04-06 17:37:25 -040086 } else {
87
Adrien Béraude74741b2021-04-19 13:22:54 -040088 user = connectedUsers[accountId]
Adrien Béraud6ecaa402021-04-06 17:37:25 -040089 console.log(user.socketId)
Adrien Béraude74741b2021-04-19 13:22:54 -040090 io.to(user.socketId).emit('receivedMessage', message["text/plain"])
91 //io.emit('receivedMessage', message["text/plain"])
Adrien Béraud6ecaa402021-04-06 17:37:25 -040092 }*/
simond47ef9e2022-09-28 22:24:28 -040093 },
simon06527b02022-10-01 15:01:47 -040094 RegistrationStateChanged: (accountId: string, state: RegistrationState, code: number, detail: string) => {
simond47ef9e2022-09-28 22:24:28 -040095 console.log('RegistrationStateChanged: ' + accountId + ' ' + state + ' ' + code + ' ' + detail);
simon06527b02022-10-01 15:01:47 -040096 const account: Account | undefined = this.getAccount(accountId);
simond47ef9e2022-09-28 22:24:28 -040097 if (account) {
98 account.registrationState = state;
99 } else {
100 console.log(`Unknown account ${accountId}`);
simon06527b02022-10-01 15:01:47 -0400101 return;
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400102 }
simond47ef9e2022-09-28 22:24:28 -0400103 const ctx = this.tempAccounts[accountId];
104 if (ctx) {
105 if (state === 'REGISTERED') {
simon06527b02022-10-01 15:01:47 -0400106 account.details = JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails;
simond47ef9e2022-09-28 22:24:28 -0400107 ctx.resolve(accountId);
108 delete this.tempAccounts[accountId];
109 } else if (state === 'ERROR_AUTH') {
110 this.dring.removeAccount(accountId);
111 ctx.reject(state);
112 delete this.tempAccounts[accountId];
113 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400114 }
simond47ef9e2022-09-28 22:24:28 -0400115 },
simon06527b02022-10-01 15:01:47 -0400116 RegisteredNameFound: (accountId: string, state: number, address: string, name: string) => {
simond47ef9e2022-09-28 22:24:28 -0400117 console.log(`RegisteredNameFound: ${accountId} ${state} ${address} ${name}`);
118 let lookups;
119 if (accountId) {
120 const account = this.getAccount(accountId);
121 if (!account) {
122 console.log(`Unknown account ${accountId}`);
123 return;
124 }
125 if (state == 0) {
126 const contact = account.getContactFromCache(address);
127 if (!contact.isRegisteredNameResolved()) contact.setRegisteredName(name);
128 }
129 lookups = account.lookups;
130 } else {
131 lookups = this.lookups;
132 }
133 let index = lookups.length - 1;
134 while (index >= 0) {
135 const lookup = lookups[index];
136 if ((lookup.address && lookup.address === address) || (lookup.name && lookup.name === name)) {
137 lookup.resolve({ address, name, state });
138 lookups.splice(index, 1);
139 }
140 index -= 1;
141 }
142 },
simon06527b02022-10-01 15:01:47 -0400143 NameRegistrationEnded: (accountId: string, state: number, name: string) => {
simond47ef9e2022-09-28 22:24:28 -0400144 console.log(`NameRegistrationEnded: ${accountId} ${state} ${name}`);
145 const account = this.getAccount(accountId);
146 if (account) {
147 if (state === 0) account.volatileDetails['Account.registeredName'] = name;
148 if (account.registeringName) {
149 account.registeringName.resolve(state);
150 delete account.registeringName;
151 }
152 } else {
153 console.log(`Unknown account ${accountId}`);
154 }
155 },
156 // Conversations
simon06527b02022-10-01 15:01:47 -0400157 ConversationReady: (accountId: string, conversationId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400158 console.log(`conversationReady: ${accountId} ${conversationId}`);
159 const account = this.getAccount(accountId);
Adrien Béraud150b4782021-04-21 19:40:59 -0400160 if (!account) {
simond47ef9e2022-09-28 22:24:28 -0400161 console.log(`Unknown account ${accountId}`);
162 return;
Adrien Béraud150b4782021-04-21 19:40:59 -0400163 }
simond47ef9e2022-09-28 22:24:28 -0400164 let conversation = account.getConversation(conversationId);
165 if (!conversation) {
166 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId));
167 members.forEach((member) => (member.contact = account.getContactFromCache(member.uri)));
168 conversation = new Conversation(conversationId, accountId, members);
169 account.addConversation(conversation);
170 }
171 },
simon06527b02022-10-01 15:01:47 -0400172 ConversationRemoved: (accountId: string, conversationId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400173 console.log(`conversationRemoved: ${accountId} ${conversationId}`);
174 const account = this.getAccount(accountId);
175 if (!account) {
176 console.log(`Unknown account ${accountId}`);
177 return;
178 }
179 account.removeConversation(conversationId);
180 },
simon06527b02022-10-01 15:01:47 -0400181 ConversationLoaded: (id: number, accountId: string, conversationId: string, messages: Message[]) => {
simond47ef9e2022-09-28 22:24:28 -0400182 console.log(`conversationLoaded: ${accountId} ${conversationId}`);
183 const account = this.getAccount(accountId);
184 if (!account) {
185 console.log(`Unknown account ${accountId}`);
186 return;
187 }
188 const conversation = account.getConversation(conversationId);
189 if (conversation) {
190 //conversation.addLoadedMessages(messages)
191 const request = conversation.requests[id];
192 if (request) {
193 request.resolve(messages);
194 }
195 }
196 },
simon06527b02022-10-01 15:01:47 -0400197 MessageReceived: (accountId: string, conversationId: string, message: Message) => {
simond47ef9e2022-09-28 22:24:28 -0400198 console.log(`messageReceived: ${accountId} ${conversationId}`);
199 console.log(message);
200 const account = this.getAccount(accountId);
201 if (!account) {
202 console.log(`Unknown account ${accountId}`);
203 return;
204 }
205 const conversation = account.getConversation(conversationId);
206 if (conversation) {
207 conversation.addMessage(message);
208 if (onMessage) onMessage(account, conversation, message);
209 }
210 },
simon06527b02022-10-01 15:01:47 -0400211 ConversationRequestReceived: (accountId: string, conversationId: string, message: Message) => {
simond47ef9e2022-09-28 22:24:28 -0400212 console.log(`conversationRequestReceived: ${accountId} ${conversationId}`);
213 const account = this.getAccount(accountId);
214 if (!account) {
215 console.log(`Unknown account ${accountId}`);
216 return;
217 }
218 },
simon06527b02022-10-01 15:01:47 -0400219 ConversationMemberEvent: (accountId: string, conversationId: string, memberUri: string, event: number) => {
simond47ef9e2022-09-28 22:24:28 -0400220 console.log(`conversationMemberEvent: ${accountId} ${conversationId}`);
221 const account = this.getAccount(accountId);
222 if (!account) {
223 console.log(`Unknown account ${accountId}`);
224 return;
225 }
226 },
simon06527b02022-10-01 15:01:47 -0400227 OnConversationError: (accountId: string, conversationId: string, code: number, what: string) => {
simond47ef9e2022-09-28 22:24:28 -0400228 console.log(`onConversationError: ${accountId} ${conversationId}`);
229 const account = this.getAccount(accountId);
230 if (!account) {
231 console.log(`Unknown account ${accountId}`);
232 return;
233 }
234 },
235 // Calls
simon06527b02022-10-01 15:01:47 -0400236 StateChange: (callId: string, state: string, code: number) => {
237 console.log(`CallStateChange: ${callId} ${state} ${code}`);
simond47ef9e2022-09-28 22:24:28 -0400238 },
simon06527b02022-10-01 15:01:47 -0400239 IncomingCall: (accountId: string, callId: string, peerUri: string) => {
simond47ef9e2022-09-28 22:24:28 -0400240 console.log(`IncomingCall: ${accountId} ${callId} ${peerUri}`);
241 },
simon06527b02022-10-01 15:01:47 -0400242 ConferenceCreated: (confId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400243 console.log(`ConferenceCreated: ${confId}`);
244 },
simon06527b02022-10-01 15:01:47 -0400245 ConferenceChanged: (accountId: string, confId: string, state: string) => {
simond47ef9e2022-09-28 22:24:28 -0400246 console.log(`ConferenceChanged: ${confId}`);
247 },
simon06527b02022-10-01 15:01:47 -0400248 ConferenceRemoved: (confId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400249 console.log(`ConferenceRemoved: ${confId}`);
250 },
simon06527b02022-10-01 15:01:47 -0400251 OnConferenceInfosUpdated: (confId: string) => {
simond47ef9e2022-09-28 22:24:28 -0400252 console.log(`onConferenceInfosUpdated: ${confId}`);
253 },
254 });
255
256 JamiDaemon.vectToJs(this.dring.getAccountList()).forEach((accountId) => {
257 const account = new Account(
258 accountId,
simon06527b02022-10-01 15:01:47 -0400259 JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails,
260 JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId)) as VolatileDetails
simond47ef9e2022-09-28 22:24:28 -0400261 );
262
simon06527b02022-10-01 15:01:47 -0400263 account.contacts = JamiDaemon.vectMapToJs(this.dring.getContacts(accountId)) as Contact[];
simond47ef9e2022-09-28 22:24:28 -0400264
265 JamiDaemon.vectToJs(this.dring.getConversations(accountId)).forEach((conversationId) => {
266 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId));
267 console.log('\n\nXMEMBERS: ', members);
268 members.forEach((member) => {
269 member.contact = account.getContactFromCache(member.uri);
270 if (!member.contact.isRegisteredNameResolved()) {
271 if (!member.uri) return;
272 console.log(`lookupAddress ${accountId} ${member.uri}`, member);
273 member.contact.setRegisteredName(
simon06527b02022-10-01 15:01:47 -0400274 new Promise((resolve: (value: LookupResolveValue) => void, reject) =>
275 account.lookups.push({ address: member.uri, resolve, reject })
276 ).then((result) => {
277 if (result.state == 0) return result.name;
278 else if (result.state == 1) return undefined;
279 else return null;
280 })
simond47ef9e2022-09-28 22:24:28 -0400281 );
282 this.dring.lookupAddress(accountId, '', member.uri);
283 }
284 });
285 const conversation = new Conversation(conversationId, accountId, members);
simon06527b02022-10-01 15:01:47 -0400286 conversation.infos = JamiDaemon.mapToJs(this.dring.conversationInfos(accountId, conversationId));
simond47ef9e2022-09-28 22:24:28 -0400287 account.addConversation(conversation);
288 });
simond47ef9e2022-09-28 22:24:28 -0400289
290 this.accounts.push(account);
291 });
292 }
293
simon06527b02022-10-01 15:01:47 -0400294 addAccount(accountConfig: AccountConfig) {
simond47ef9e2022-09-28 22:24:28 -0400295 const params = this.accountDetailsToNative(accountConfig);
296 params.set('Account.type', 'RING');
simon06527b02022-10-01 15:01:47 -0400297 return new Promise<string>((resolve, reject) => {
simond47ef9e2022-09-28 22:24:28 -0400298 const accountId = this.dring.addAccount(params);
299 this.tempAccounts[accountId] = { resolve, reject };
300 });
301 }
302
simon06527b02022-10-01 15:01:47 -0400303 getDevices(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400304 return JamiDaemon.mapToJs(this.dring.getKnownRingDevices(accountId));
305 }
306
simon06527b02022-10-01 15:01:47 -0400307 getAccount(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400308 for (let i = 0; i < this.accounts.length; i++) {
309 const account = this.accounts[i];
310 if (account.getId() === accountId) return account;
Adrien Béraud150b4782021-04-21 19:40:59 -0400311 }
simond47ef9e2022-09-28 22:24:28 -0400312 return undefined;
313 }
314 getAccountList() {
315 return this.accounts;
316 }
simon06527b02022-10-01 15:01:47 -0400317 registerName(accountId: string, password: string, name: string) {
simond47ef9e2022-09-28 22:24:28 -0400318 return new Promise((resolve, reject) => {
319 if (!name) return reject(new Error('Invalid name'));
320 const account = this.getAccount(accountId);
321 if (!account) return reject(new Error("Can't find account"));
322 if (account.registeringName) return reject(new Error('Username already being registered'));
323 if (this.dring.registerName(accountId, password, name)) {
324 account.registeringName = { name, resolve, reject };
325 }
326 });
327 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400328
simon06527b02022-10-01 15:01:47 -0400329 getConversation(accountId: string, conversationId: string) {
simond47ef9e2022-09-28 22:24:28 -0400330 const account = this.getAccount(accountId);
331 if (account) return account.getConversation(conversationId);
332 return null;
333 }
simon06527b02022-10-01 15:01:47 -0400334 getAccountDetails(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400335 return JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId));
336 }
simon06527b02022-10-01 15:01:47 -0400337 setAccountDetails(accountId: string, details: AccountDetails) {
simond47ef9e2022-09-28 22:24:28 -0400338 this.dring.setAccountDetails(accountId, this.mapToNative(details));
339 }
340 getAudioOutputDeviceList() {
341 return JamiDaemon.vectToJs(this.dring.getAudioOutputDeviceList());
342 }
simon06527b02022-10-01 15:01:47 -0400343 getVolume(deviceName: string): number {
simond47ef9e2022-09-28 22:24:28 -0400344 return this.dring.getVolume(deviceName);
345 }
simon06527b02022-10-01 15:01:47 -0400346 setVolume(deviceName: string, volume: number) {
simond47ef9e2022-09-28 22:24:28 -0400347 return this.dring.setVolume(deviceName, volume);
348 }
349
simon06527b02022-10-01 15:01:47 -0400350 lookupName(accountId: string, name: string) {
simond47ef9e2022-09-28 22:24:28 -0400351 const p = new Promise((resolve, reject) => {
352 if (accountId) {
353 const account = this.getAccount(accountId);
354 if (!account) {
355 reject(new Error("Can't find account"));
356 } else {
357 account.lookups.push({ name, resolve, reject });
358 }
359 } else {
360 this.lookups.push({ name, resolve, reject });
361 }
362 });
363 this.dring.lookupName(accountId || '', '', name);
364 return p;
365 }
366
simon06527b02022-10-01 15:01:47 -0400367 lookupAddress(accountId: string, address: string) {
simond47ef9e2022-09-28 22:24:28 -0400368 console.log(`lookupAddress ${accountId} ${address}`);
369 const p = new Promise((resolve, reject) => {
370 if (accountId) {
371 const account = this.getAccount(accountId);
372 if (!account) {
373 reject(new Error("Can't find account"));
374 } else {
375 account.lookups.push({ address, resolve, reject });
376 }
377 } else {
378 this.lookups.push({ address, resolve, reject });
379 }
380 });
381 this.dring.lookupAddress(accountId || '', '', address);
382 return p;
383 }
384
385 stop() {
386 this.dring.fini();
387 }
388
simon06527b02022-10-01 15:01:47 -0400389 addContact(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400390 this.dring.addContact(accountId, contactId);
391 const details = JamiDaemon.mapToJs(this.dring.getContactDetails(accountId, contactId));
392 if (details.conversationId) {
393 const account = this.getAccount(accountId);
394 if (account) {
395 let conversation = account.getConversation(details.conversationId);
396 if (!conversation) {
397 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, details.conversationId));
398 members.forEach((member) => (member.contact = account.getContactFromCache(member.uri)));
399 conversation = new Conversation(details.conversationId, accountId, members);
400 account.addConversation(conversation);
401 }
402 }
Adrien Béraud4e287b92021-04-24 16:15:56 -0400403 }
simond47ef9e2022-09-28 22:24:28 -0400404 return details;
405 }
Adrien Béraud150b4782021-04-21 19:40:59 -0400406
simon06527b02022-10-01 15:01:47 -0400407 removeContact(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400408 //bool ban false
409 this.dring.removeContact(accountId, contactId, false);
410 }
411
simon06527b02022-10-01 15:01:47 -0400412 blockContact(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400413 //bool ban true
414 this.dring.removeContact(accountId, contactId, true);
415 }
416
simon06527b02022-10-01 15:01:47 -0400417 getContactDetails(accountId: string, contactId: string) {
simond47ef9e2022-09-28 22:24:28 -0400418 return JamiDaemon.mapToJs(this.dring.getContactDetails(accountId, contactId));
419 }
420
simon06527b02022-10-01 15:01:47 -0400421 getDefaultModerators(accountId: string) {
simond47ef9e2022-09-28 22:24:28 -0400422 const account = this.getAccount(accountId);
423 if (!account) {
424 console.log(`Unknown account ${accountId}`);
425 return {};
Adrien Béraud150b4782021-04-21 19:40:59 -0400426 }
simond47ef9e2022-09-28 22:24:28 -0400427 return JamiDaemon.vectToJs(this.dring.getDefaultModerators(accountId)).map((contactId) =>
428 account.getContactFromCache(contactId)
429 );
430 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400431
simon06527b02022-10-01 15:01:47 -0400432 addDefaultModerator(accountId: string, uri: string) {
simond47ef9e2022-09-28 22:24:28 -0400433 this.dring.setDefaultModerator(accountId, uri, true);
434 }
Adrien Béraud5e9e19b2021-04-22 01:38:53 -0400435
simon06527b02022-10-01 15:01:47 -0400436 removeDefaultModerator(accountId: string, uri: string) {
simond47ef9e2022-09-28 22:24:28 -0400437 this.dring.setDefaultModerator(accountId, uri, false);
438 }
Adrien Béraud4e287b92021-04-24 16:15:56 -0400439
simon06527b02022-10-01 15:01:47 -0400440 sendMessage(accountId: string, conversationId: string, message: string) {
simond47ef9e2022-09-28 22:24:28 -0400441 this.dring.sendMessage(accountId, conversationId, message, '');
442 }
Adrien Béraud4e287b92021-04-24 16:15:56 -0400443
simon06527b02022-10-01 15:01:47 -0400444 loadMessages(accountId: string, conversationId: string, fromMessage?: string) {
simond47ef9e2022-09-28 22:24:28 -0400445 const account = this.getAccount(accountId);
446 if (!account) throw new Error('Unknown account');
447 const conversation = account.getConversation(conversationId);
448 if (!conversation) throw new Error(`Unknown conversation ${conversationId}`);
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400449
simond47ef9e2022-09-28 22:24:28 -0400450 return new Promise((resolve, reject) => {
451 if (!conversation.requests) conversation.requests = {};
452 const requestId = this.dring.loadConversationMessages(accountId, conversationId, fromMessage || '', 32);
453 conversation.requests[requestId] = { resolve, reject };
454 });
455 }
456
simon06527b02022-10-01 15:01:47 -0400457 boolToStr(bool: boolean) {
simond47ef9e2022-09-28 22:24:28 -0400458 return bool ? 'true' : 'false';
459 }
460
simon06527b02022-10-01 15:01:47 -0400461 accountDetailsToNative(account: AccountConfig) {
simond47ef9e2022-09-28 22:24:28 -0400462 const params = new this.dring.StringMap();
463 if (account.managerUri) params.set('Account.managerUri', account.managerUri);
464 if (account.managerUsername) params.set('Account.managerUsername', account.managerUsername);
465 if (account.archivePassword) {
466 params.set('Account.archivePassword', account.archivePassword);
467 } /* else {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400468 console.log("archivePassword required")
Adrien Béraude74741b2021-04-19 13:22:54 -0400469 return
Adrien Béraud88a52442021-04-26 12:11:41 -0400470 }*/
simond47ef9e2022-09-28 22:24:28 -0400471 if (account.alias) params.set('Account.alias', account.alias);
472 if (account.displayName) params.set('Account.displayName', account.displayName);
473 if (account.enable !== undefined) params.set('Account.enable', this.boolToStr(account.enable));
474 if (account.autoAnswer !== undefined) params.set('Account.autoAnswer', this.boolToStr(account.autoAnswer));
475 if (account.autoAnswer !== undefined) params.set('Account.autoAnswer', this.boolToStr(account.autoAnswer));
476 if (account.ringtonePath) params.set('Account.ringtonePath', account.ringtonePath);
477 if (account.ringtoneEnabled !== undefined)
478 params.set('Account.ringtoneEnabled', this.boolToStr(account.ringtoneEnabled));
479 if (account.videoEnabled !== undefined) params.set('Account.videoEnabled', this.boolToStr(account.videoEnabled));
480 if (account.useragent) {
481 params.set('Account.useragent', account.useragent);
482 params.set('Account.hasCustomUserAgent', 'TRUE');
483 } else {
484 params.set('Account.hasCustomUserAgent', 'FALSE');
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400485 }
simond47ef9e2022-09-28 22:24:28 -0400486 if (account.audioPortMin) params.set('Account.audioPortMin', account.audioPortMin);
487 if (account.audioPortMax) params.set('Account.audioPortMax', account.audioPortMax);
488 if (account.videoPortMin) params.set('Account.videoPortMin', account.videoPortMin);
489 if (account.videoPortMax) params.set('Account.videoPortMax', account.videoPortMax);
490 if (account.localInterface) params.set('Account.localInterface', account.localInterface);
491 if (account.publishedSameAsLocal !== undefined)
492 params.set('Account.publishedSameAsLocal', this.boolToStr(account.publishedSameAsLocal));
493 if (account.localPort) params.set('Account.localPort', account.localPort);
494 if (account.publishedPort) params.set('Account.publishedPort', account.publishedPort);
495 if (account.rendezVous !== undefined) params.set('Account.rendezVous', this.boolToStr(account.rendezVous));
496 if (account.upnpEnabled !== undefined) params.set('Account.upnpEnabled', this.boolToStr(account.upnpEnabled));
497 return params;
498 }
simon06527b02022-10-01 15:01:47 -0400499 static vectToJs(vect: any) {
simond47ef9e2022-09-28 22:24:28 -0400500 const len = vect.size();
501 const outputArr = new Array(len);
502 for (let i = 0; i < len; i++) outputArr[i] = vect.get(i);
503 return outputArr;
504 }
simon06527b02022-10-01 15:01:47 -0400505 static mapToJs(m: any): Record<string, any> {
506 const outputObj: Record<string, any> = {};
simond47ef9e2022-09-28 22:24:28 -0400507 JamiDaemon.vectToJs(m.keys()).forEach((k) => (outputObj[k] = m.get(k)));
508 return outputObj;
509 }
simon06527b02022-10-01 15:01:47 -0400510 static vectMapToJs(vectMap: any) {
simond47ef9e2022-09-28 22:24:28 -0400511 const len = vectMap.size();
512 const outputArr = new Array(len);
513 for (let i = 0; i < len; i++) outputArr[i] = JamiDaemon.mapToJs(vectMap.get(i));
514 return outputArr;
515 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400516
simon06527b02022-10-01 15:01:47 -0400517 mapToNative(map: any) {
simond47ef9e2022-09-28 22:24:28 -0400518 const ret = new this.dring.StringMap();
519 for (const [key, value] of Object.entries(map)) ret.set(key, value);
520 return ret;
521 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400522}
523
simond47ef9e2022-09-28 22:24:28 -0400524export default JamiDaemon;