Skip to content

Commit

Permalink
fix: Fixed group functions (close #416)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Dec 10, 2020
1 parent 7bff0b2 commit e595f94
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
12 changes: 9 additions & 3 deletions src/lib/wapi/functions/add-participant.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,19 @@ export async function addParticipant(groupId, contactsId) {
}

contactsId = await Promise.all(contactsId.map((c) => WAPI.sendExist(c)));
contactsId = contactsId
.filter((c) => !c.erro && c.isUser)
.map((c) => c.contact);
contactsId = contactsId.filter((c) => !c.erro && c.isUser).map((c) => c.id);

if (!contactsId.length) {
return false;
}

await window.Store.WapQuery.addParticipants(chat.id, contactsId);

const participants = contactsId.map((c) =>
chat.groupMetadata.participants.get(c)
);

await window.Store.Participants.addParticipants(chat, contactsId);

return true;
}
18 changes: 12 additions & 6 deletions src/lib/wapi/functions/demote-participant.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
export async function demoteParticipant(groupId, contactsId, done) {
const chat = Store.Chat.get(groupId);

if (!Array.isArray(contactsId)) {
contactsId = [contactsId];
}
Expand All @@ -62,17 +63,22 @@ export async function demoteParticipant(groupId, contactsId, done) {
contactsId = contactsId
.filter((c) => !c.erro && c.isUser)
.map((c) => chat.groupMetadata.participants.get(c.id))
.filter((c) => typeof c !== 'undefined');
.filter((c) => typeof c !== 'undefined')
.map((c) => c.id);

if (!contactsId.length) {
typeof done === 'function' && done(false);
return false;
}

return window.Store.Participants.demoteParticipants(chat, contactsId).then(
() => {
typeof done === 'function' && done(true);
return true;
}
await window.Store.WapQuery.demoteParticipants(chat.id, contactsId);

const participants = contactsId.map((c) =>
chat.groupMetadata.participants.get(c)
);

await window.Store.Participants.demoteParticipants(chat, participants);

typeof done === 'function' && done(true);
return true;
}
18 changes: 12 additions & 6 deletions src/lib/wapi/functions/promote-participant.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
export async function promoteParticipant(groupId, contactsId, done) {
const chat = Store.Chat.get(groupId);

if (!Array.isArray(contactsId)) {
contactsId = [contactsId];
}
Expand All @@ -62,17 +63,22 @@ export async function promoteParticipant(groupId, contactsId, done) {
contactsId = contactsId
.filter((c) => !c.erro && c.isUser)
.map((c) => chat.groupMetadata.participants.get(c.id))
.filter((c) => typeof c !== 'undefined');
.filter((c) => typeof c !== 'undefined')
.map((c) => c.id);

if (!contactsId.length) {
typeof done === 'function' && done(false);
return false;
}

return window.Store.Participants.promoteParticipants(chat, contactsId).then(
() => {
typeof done === 'function' && done(true);
return true;
}
await window.Store.WapQuery.promoteParticipants(chat.id, contactsId);

const participants = contactsId.map((c) =>
chat.groupMetadata.participants.get(c)
);

await window.Store.Participants.promoteParticipants(chat, participants);

typeof done === 'function' && done(true);
return true;
}
18 changes: 12 additions & 6 deletions src/lib/wapi/functions/remove-participant.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
export async function removeParticipant(groupId, contactsId, done) {
const chat = Store.Chat.get(groupId);

if (!Array.isArray(contactsId)) {
contactsId = [contactsId];
}
Expand All @@ -62,17 +63,22 @@ export async function removeParticipant(groupId, contactsId, done) {
contactsId = contactsId
.filter((c) => !c.erro && c.isUser)
.map((c) => chat.groupMetadata.participants.get(c.id))
.filter((c) => typeof c !== 'undefined');
.filter((c) => typeof c !== 'undefined')
.map((c) => c.id);

if (!contactsId.length) {
typeof done === 'function' && done(false);
return false;
}

return window.Store.Participants.removeParticipants(chat, contactsId).then(
() => {
typeof done === 'function' && done(true);
return true;
}
await window.Store.WapQuery.removeParticipants(chat.id, contactsId);

const participants = contactsId.map((c) =>
chat.groupMetadata.participants.get(c)
);

await window.Store.Participants.removeParticipants(chat, participants);

typeof done === 'function' && done(true);
return true;
}

0 comments on commit e595f94

Please sign in to comment.