Skip to content

Commit

Permalink
feat: Added call.outgoing_call event (wppconnect-team#2018)
Browse files Browse the repository at this point in the history
* feat: Added outgoing_call event
  • Loading branch information
marcelo386 authored Jun 18, 2024
1 parent 2e41648 commit cda7e7a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 8 deletions.
38 changes: 38 additions & 0 deletions src/call/events/eventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,42 @@ export interface CallEventTypes {
*/
peerJid: Wid;
};
/**
* Triggered when you a outgoing call
*
* @example
* ```javascript
* WPP.on('call.outgoing_call', (call) => {
* // Your code
* //Example: End any outgoing call
* WPP.call.endCall(call.id);
* });
* ```
*/
'call.outgoing_call': {
/**
* The call id
*/
id: string;
/**
* Is a call from a group
*/
isGroup: boolean;
/**
* Is call with video
*/
isVideo: boolean;
/**
* timestamp of offer
*/
offerTime: number;
/**
* Wid of sender without device id
*/
sender: Wid;
/**
* Wid of sender with device id
*/
peerJid: Wid;
};
}
1 change: 1 addition & 0 deletions src/call/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
*/

import './registerIncomingCallEvent';
import './registerOutgoingCallEvent';
50 changes: 50 additions & 0 deletions src/call/events/registerOutgoingCallEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*!
* Copyright 2024 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 { internalEv } from '../../eventEmitter';
import * as webpack from '../../webpack';
import { CallModel, CallStore, WidFactory } from '../../whatsapp';
import { CALL_STATES } from '../../whatsapp/enums';

webpack.onInjected(() => register());

function register() {
CallStore.on('add', (call: CallModel) => {
if (call.isGroup) {
internalEv.emit('call.outgoing_call', {
id: call.id,
isGroup: call.isGroup,
isVideo: call.isVideo,
offerTime: call.offerTime,
sender: WidFactory.toChatWid(call.peerJid),
peerJid: call.peerJid,
});
}
});

CallStore.on('change', (call: CallModel) => {
if (call.getState() === CALL_STATES.OUTGOING_RING) {
internalEv.emit('call.outgoing_call', {
id: call.id,
isGroup: call.isGroup,
isVideo: call.isVideo,
offerTime: call.offerTime,
sender: WidFactory.toChatWid(call.peerJid),
peerJid: call.peerJid,
});
}
});
}
16 changes: 8 additions & 8 deletions src/call/functions/end.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2023 WPPConnect Team
* Copyright 2024 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,18 +19,18 @@ import { CallModel, CallStore, websocket } from '../../whatsapp';
import { CALL_STATES } from '../../whatsapp/enums';

/**
* End a outcoming call
* End a outgoing call
*
* @example
* ```javascript
* // End any outcoming call
* // End any outgoing call
* WPP.call.end();
*
* // End specific call id
* WPP.call.end(callId);
*
* // End any outcoming call
* WPP.on('call.outcoming_call', (call) => {
* // End any outgoing call
* WPP.on('call.outgoing_call', (call) => {
* WPP.call.end(call.id);
* });
* ```
Expand All @@ -50,7 +50,7 @@ export async function end(callId?: string): Promise<boolean> {
if (callId) {
call = CallStore.get(callId);
} else {
// First outcoming ring or call group
// First outgoing ring or call group
call = CallStore.findFirst(
(c) => callOut.includes(c.getState()) || c.isGroup
);
Expand All @@ -68,8 +68,8 @@ export async function end(callId?: string): Promise<boolean> {

if (!callOut.includes(call.getState()) && !call.isGroup) {
throw new WPPError(
'call_is_not_outcoming_calling',
`Call ${callId || '<empty>'} is not outcoming calling`,
'call_is_not_outgoing_calling',
`Call ${callId || '<empty>'} is not outgoing calling`,
{
callId,
state: call.getState(),
Expand Down

0 comments on commit cda7e7a

Please sign in to comment.