forked from danielcardeenas/sulla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.layer.ts
142 lines (130 loc) Β· 4.17 KB
/
group.layer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { Page } from 'puppeteer';
import { Contact, GroupCreation, Id } from '../model';
import { RetrieverLayer } from './retriever.layer';
declare module WAPI {
const leaveGroup: (groupId: string) => any;
const getGroupParticipantIDs: (groupId: string) => Id[];
const getGroupInviteLink: (chatId: string) => Promise<string>;
const createGroup: (
groupName: string,
contactId: string | string[]
) => GroupCreation;
const removeParticipant: (groupId: string, contactId: string) => void;
const addParticipant: (groupId: string, contactId: string) => void;
const promoteParticipant: (groupId: string, contactId: string) => void;
const demoteParticipant: (groupId: string, contactId: string) => void;
const getGroupAdmins: (groupId: string) => Contact[];
}
export class GroupLayer extends RetrieverLayer {
constructor(page: Page) {
super(page);
}
/**
* Removes the host device from the group
* @param groupId group id
*/
public async leaveGroup(groupId: string) {
return this.page.evaluate((groupId) => WAPI.leaveGroup(groupId), groupId);
}
/**
* Retrieves group members as [Id] objects
* @param groupId group id
*/
public async getGroupMembersIds(groupId: string): Promise<Id[]> {
return this.page.evaluate(
(groupId: string) => WAPI.getGroupParticipantIDs(groupId),
groupId
);
}
/**
* Returns group members [Contact] objects
* @param groupId
*/
public async getGroupMembers(groupId: string) {
const membersIds = await this.getGroupMembersIds(groupId);
const actions = membersIds.map((memberId) => {
return this.getContact(memberId._serialized);
});
return Promise.all(actions);
}
/**
* Generates group-invite link
* @param chatId
* @returns Invitation link
*/
public async getGroupInviteLink(chatId: string) {
return await this.page.evaluate(
(chatId) => WAPI.getGroupInviteLink(chatId),
chatId
);
}
/**
* Creates a new chat group
* @param groupName Group name
* @param contacts Contacts that should be added.
*/
public async createGroup(groupName: string, contacts: string | string[]) {
return await this.page.evaluate(
({ groupName, contacts }) => WAPI.createGroup(groupName, contacts),
{ groupName, contacts }
);
}
/**
* Removes participant from group
* @param groupId Chat id ('0000000000-00000000@g.us')
* @param participantId Participant id'000000000000@c.us'
*/
public async removeParticipant(groupId: string, participantId: string) {
return await this.page.evaluate(
({ groupId, participantId }) =>
WAPI.removeParticipant(groupId, participantId),
{ groupId, participantId }
);
}
/**
* Adds participant to Group
* @param groupId Chat id ('0000000000-00000000@g.us')
* @param participantId Participant id'000000000000@c.us'
*/
public async addParticipant(groupId: string, participantId: string) {
return await this.page.evaluate(
({ groupId, participantId }) =>
WAPI.addParticipant(groupId, participantId),
{ groupId, participantId }
);
}
/**
* Promotes participant as Admin in given group
* @param groupId Chat id ('0000000000-00000000@g.us')
* @param participantId Participant id'000000000000@c.us'
*/
public async promoteParticipant(groupId: string, participantId: string) {
return await this.page.evaluate(
({ groupId, participantId }) =>
WAPI.promoteParticipant(groupId, participantId),
{ groupId, participantId }
);
}
/**
* Demotes admin privileges of participant
* @param groupId Chat id ('0000000000-00000000@g.us')
* @param participantId Participant id'000000000000@c.us'
*/
public async demoteParticipant(groupId: string, participantId: string) {
return await this.page.evaluate(
({ groupId, participantId }) =>
WAPI.demoteParticipant(groupId, participantId),
{ groupId, participantId }
);
}
/**
* Retrieves group admins
* @param chatId Group/Chat id ('0000000000-00000000@g.us')
*/
public async getGroupAdmins(chatId: string) {
return await this.page.evaluate(
(chatId) => WAPI.getGroupAdmins(chatId),
chatId
);
}
}