forked from wppconnect-team/wa-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa53282
commit ec7364d
Showing
4 changed files
with
199 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/*! | ||
* Copyright 2023 WPPConnect Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { WPPError } from '../../util'; | ||
import { CallModel, CallStore, websocket } from '../../whatsapp'; | ||
import { CALL_STATES } from '../../whatsapp/enums'; | ||
|
||
/** | ||
* End a outcoming call | ||
* | ||
* @example | ||
* ```javascript | ||
* // End any outcoming call | ||
* WPP.call.endCall(); | ||
* | ||
* // End specific call id | ||
* WPP.call.endCall(callId); | ||
* | ||
* // End any outcoming call | ||
* WPP.on('call.outcoming_call', (call) => { | ||
* WPP.call.endCall(call.id); | ||
* }); | ||
* ``` | ||
* | ||
* @param {string} callId The call ID, empty to end the first one | ||
* @return {[type]} [return description] | ||
*/ | ||
export async function endCall(callId?: string): Promise<boolean> { | ||
const callOut = [ | ||
CALL_STATES.ACTIVE, | ||
CALL_STATES.OUTGOING_CALLING, | ||
CALL_STATES.OUTGOING_RING, | ||
]; | ||
|
||
let call: CallModel | undefined = undefined; | ||
|
||
if (callId) { | ||
call = CallStore.get(callId); | ||
} else { | ||
// First outcoming ring or call group | ||
call = CallStore.findFirst( | ||
(c) => callOut.includes(c.getState()) || c.isGroup | ||
); | ||
} | ||
|
||
if (!call) { | ||
throw new WPPError( | ||
'call_not_found', | ||
`Call ${callId || '<empty>'} not found`, | ||
{ | ||
callId, | ||
} | ||
); | ||
} | ||
|
||
if (!callOut.includes(call.getState()) && !call.isGroup) { | ||
throw new WPPError( | ||
'call_is_not_outcoming_calling', | ||
`Call ${callId || '<empty>'} is not outcoming calling`, | ||
{ | ||
callId, | ||
state: call.getState(), | ||
} | ||
); | ||
} | ||
|
||
if (!call.peerJid.isGroupCall()) { | ||
await websocket.ensureE2ESessions([call.peerJid]); | ||
} | ||
|
||
const node = websocket.smax( | ||
'call', | ||
{ | ||
to: call.peerJid.toString({ legacy: true }), | ||
id: websocket.generateId(), | ||
}, | ||
[ | ||
websocket.smax( | ||
'terminate', | ||
{ | ||
'call-id': call.id, | ||
'call-creator': call.peerJid.toString({ legacy: true }), | ||
// count: '0', | ||
}, | ||
null | ||
), | ||
] | ||
); | ||
|
||
await websocket.sendSmaxStanza(node); | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/*! | ||
* Copyright 2023 WPPConnect Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { functions, multidevice, websocket, Wid } from '../../whatsapp'; | ||
|
||
export async function prepareDestionation(wids: Wid[]) { | ||
const fanList = await functions.getFanOutList({ wids }); | ||
await websocket.ensureE2ESessions(fanList); | ||
|
||
let shouldHaveIdentity = false; | ||
const destination = await Promise.all( | ||
fanList.map(async (wid) => { | ||
const encKey = self.crypto.getRandomValues(new Uint8Array(32)).buffer; | ||
|
||
const { type, ciphertext } = await functions.encryptMsgProtobuf(wid, 0, { | ||
call: { | ||
callKey: new Uint8Array(encKey), | ||
}, | ||
}); | ||
|
||
shouldHaveIdentity = shouldHaveIdentity || type === 'pkmsg'; | ||
|
||
return websocket.smax( | ||
'to', | ||
{ | ||
jid: wid.toString({ legacy: true }), | ||
}, | ||
[ | ||
websocket.smax( | ||
'enc', | ||
{ | ||
v: '2', | ||
type: type, | ||
count: '0', | ||
}, | ||
ciphertext | ||
), | ||
] | ||
); | ||
}) | ||
); | ||
|
||
const content: websocket.WapNode[] = []; | ||
|
||
content.push(websocket.smax('destination', {}, destination)); | ||
|
||
if (shouldHaveIdentity) { | ||
const identity = await multidevice.adv.getADVEncodedIdentity(); | ||
content.push(websocket.smax('device-identity', undefined, identity)); | ||
} | ||
|
||
return content; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters