Skip to content

Commit

Permalink
feat: addParticipant
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Jul 15, 2022
1 parent 37858be commit ac87235
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/webpack/@types/API.d.ts
Expand Up @@ -2,10 +2,14 @@ interface API {
sendMessage: (to: string, body: String, options: object) => Promise<any>;
interfaceChange: (callback: Function) => void;
newMessage: (callback: Function) => void;
newOnAck: (callback: Function) => void;
newOnAck: (callback: Function) => void;
getAllContacts: () => void;
addChatWapi: () => void;
createGroup: (groupName: string, contacts: string | string[]) => Promise<any>;
addParticipant: (
groupId: string,
contacts: string | string[]
) => Promise<any>;
}

declare global {
Expand All @@ -20,4 +24,3 @@ declare global {
}

export {};

24 changes: 24 additions & 0 deletions src/webpack/api/layes/group.layer.ts
Expand Up @@ -35,4 +35,28 @@ export class GroupLayer extends ListenerLayer {
}
});
}

/**
* Adds participant to Group
* @param groupId Chat id ('0000000000-00000000@g.us')
* @param contacts Participants id '000000000000@c.us'
*/
public async addParticipant(
groupId: string,
contacts: string | string[]
): Promise<any> {
return new Promise(async (resolve, reject) => {
const result = await this.page
.evaluate(
({ groupId, contacts }) => API.addParticipant(groupId, contacts),
{ groupId, contacts }
)
.catch();
if (result.erro == true) {
return reject(result);
} else {
return resolve(result);
}
});
}
}
3 changes: 3 additions & 0 deletions src/webpack/assets/api.js
Expand Up @@ -27,6 +27,7 @@ import {
baseSendMessage,
getAllContacts,
createGroup,
addParticipant
} from './functions';

import {
Expand Down Expand Up @@ -87,6 +88,8 @@ if (typeof window.API === 'undefined') {

// Group
window.API.createGroup = createGroup;
window.API.addParticipant = addParticipant;

// Serialize
window.API.serializeMessageObj = serializeMessageObj;
window.API.serializeChatObj = serializeChatObj;
Expand Down
39 changes: 39 additions & 0 deletions src/webpack/assets/functions/add-participant.js
@@ -0,0 +1,39 @@
export async function addParticipant(groupId, contactsId) {
if (!Array.isArray(contactsId)) {
contactsId = [contactsId];
}
const chatGroup = Store.Chat.get(groupId);
if (!chatGroup) {
return API.scope(
null,
true,
404,
`group not found: ${groupId}`
);
}
const contacts = await Promise.all(contactsId.map((c) => API.sendExist(c)));
const checkErro = contacts.filter((e) => e.erro === true)[0];

if (checkErro && checkErro.status === 404) {
if (!checkErro.erro) {
checkErro.erro = true;
}
return checkErro;
}
try {
await window.Store.Participants.addParticipants(chatGroup, contacts);
return API.scope(
null,
false,
null,
`contact(s) successfully added!`
);
} catch {
return API.scope(
null,
true,
null,
`could not add a participant`
);
}
}
1 change: 1 addition & 0 deletions src/webpack/assets/functions/index.js
@@ -1,3 +1,4 @@
export { sendMessage, baseSendMessage } from './send-message';
export { getAllContacts } from './get-all-contacts';
export { createGroup } from './create-group';
export { addParticipant } from './add-participant';
7 changes: 7 additions & 0 deletions src/webpack/assets/help/filter-object.js
Expand Up @@ -100,4 +100,11 @@ export const filterObjects = [{
type: 'CheckWid',
when: (module) => (module.validateWid ? module : null),
},
{
type: 'Participants',
when: (module) =>
module.addParticipants && module.promoteCommunityParticipants ?
module :
null,
},
];

0 comments on commit ac87235

Please sign in to comment.