Skip to content

Commit

Permalink
feat: Added WPP.status.updateParticipants function
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Feb 17, 2023
1 parent 9b2dbbe commit 1cc99a3
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/status/functions/index.ts
Expand Up @@ -21,3 +21,4 @@ export { sendRawStatus, SendStatusOptions } from './sendRawStatus';
export { sendReadStatus } from './sendReadStatus';
export { sendTextStatus, TextStatusOptions } from './sendTextStatus';
export { sendVideoStatus, VideoStatusOptions } from './sendVideoStatus';
export { updateParticipants } from './updateParticipants';
74 changes: 26 additions & 48 deletions src/status/functions/sendRawStatus.ts
Expand Up @@ -17,23 +17,15 @@
import { assertWid } from '../../assert';
import * as Chat from '../../chat';
import * as webpack from '../../webpack';
import {
ContactStore,
MsgKey,
ParticipantModel,
UserPrefs,
WidFactory,
} from '../../whatsapp';
import { ContactStore, functions, MsgKey, UserPrefs } from '../../whatsapp';
import { wrapModuleFunction } from '../../whatsapp/exportModule';
import {
createMsgProtobuf,
encryptAndSendGroupMsg,
encryptAndSendMsg,
markForgetSenderKey,
randomHex,
updateParticipants,
} from '../../whatsapp/functions';
import { defaultSendStatusOptions } from '..';
import { defaultSendStatusOptions, updateParticipants } from '..';
import { postSendStatus } from './postSendStatus';

export interface SendStatusOptions {
Expand Down Expand Up @@ -70,41 +62,6 @@ export async function sendRawStatus(
return result;
}

let isForgot = false;

async function updateStatusGroup() {
const myContacts = ContactStore.getModelsArray()
.filter((c) => c.isMyContact && !c.isContactBlocked)
.filter((c) => c.notifyName && !c.isMe)
.map((c) => c.id);

myContacts.push(UserPrefs.getMaybeMeUser());

const participants = myContacts.map(
(id) =>
new ParticipantModel({
id,
isAdmin: false,
isSuperAdmin: false,
})
);

const group = WidFactory.createWid('status@broadcast');

await updateParticipants({
group,
participants,
version: Date.now(),
isOffline: false,
});

if (!isForgot) {
isForgot = true;

await markForgetSenderKey(group, myContacts);
}
}

webpack.onInjected(() => {
// allow to send backgroundColor, textColor and font for status
wrapModuleFunction(createMsgProtobuf, (func, ...args) => {
Expand All @@ -123,8 +80,8 @@ webpack.onInjected(() => {
result.extendedTextMessage.font = msg.font;
}

result.inviteLinkGroupTypeV2 = 0;
result.previewType = 0;
result.extendedTextMessage.inviteLinkGroupTypeV2 = 0;
result.extendedTextMessage.previewType = 0;
}

return result;
Expand All @@ -144,7 +101,28 @@ webpack.onInjected(() => {
throw error;
}

await updateStatusGroup();
if (localStorage.getItem('wpp-status-participants') !== 'custom') {
const myContacts = ContactStore.getModelsArray()
.filter((c) => c.isMyContact && !c.isContactBlocked)
.filter((c) => c.notifyName && !c.isMe)
.filter((c) => !c.id.equals(UserPrefs.getMaybeMeUser()))
.map((c) => c.id);

await updateParticipants(myContacts);

localStorage.setItem('wpp-status-participants', 'contacts');
}

const participants = await functions.getParticipants(msg.to);

if (!participants || participants.participants.length === 0) {
throw new Error('empty participants for status@broadcast');
}

await functions.markForgetSenderKey(
msg.to,
participants.participants.map(assertWid)
);

let c;
if (msg.asMms) {
Expand Down
66 changes: 66 additions & 0 deletions src/status/functions/updateParticipants.ts
@@ -0,0 +1,66 @@
/*!
* Copyright 2021 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 { assertWid } from '../../assert';
import {
functions,
MsgKey,
ParticipantModel,
UserPrefs,
Wid,
WidFactory,
} from '../../whatsapp';

export interface SendStatusOptions {
waitForAck?: boolean;
messageId?: string | MsgKey;
}

/**
* Define a custom list of participants to send the status message
*
* @example
* ```javascript
* await WPP.status.updateParticipants(['123@c.us', '456@c.us']);
* ```
*/
export async function updateParticipants(ids: (string | Wid)[]): Promise<void> {
const wids = ids
.map(assertWid)
.filter((c) => !c.equals(UserPrefs.getMaybeMeUser()));

// wids.push(UserPrefs.getMaybeMeUser());

const participants = wids.map(
(id) =>
new ParticipantModel({
id,
isAdmin: false,
isSuperAdmin: false,
})
);

const group = WidFactory.createWid('status@broadcast');

await functions.updateParticipants({
group,
participants,
version: Date.now(),
isOffline: false,
});

localStorage.setItem('wpp-status-participants', 'custom');
}
40 changes: 40 additions & 0 deletions src/whatsapp/functions/getParticipants.ts
@@ -0,0 +1,40 @@
/*!
* Copyright 2021 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 { exportModule } from '../exportModule';
import { Wid } from '../misc';

/**
* @whatsapp 951974 >= 2.2222.8
* @whatsapp 318615 >= 2.2224.7
*/
export declare function getParticipants(group: Wid): Promise<null | {
admins: string[];
deviceSyncComplete?: any;
groupId: string;
participants: string[];
pastParticipants: string[];
rotateKey: boolean;
senderKey: Map<string, boolean>;
}>;

exportModule(
exports,
{
getParticipants: 'getParticipants',
},
(m) => m.getParticipants && m.addParticipants
);
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Expand Up @@ -37,6 +37,7 @@ export * from './getFanOutList';
export * from './getGroupSenderKeyList';
export * from './getGroupSizeLimit';
export * from './getHistorySyncProgress';
export * from './getParticipants';
export * from './getQuotedMsgObj';
export * from './getReactions';
export * from './getSearchContext';
Expand Down

0 comments on commit 1cc99a3

Please sign in to comment.