Skip to content

Commit

Permalink
fix: Improved participant list for send status
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Jun 26, 2022
1 parent 521c6f2 commit 55cd0a4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 26 deletions.
76 changes: 50 additions & 26 deletions src/status/functions/sendRawStatus.ts
Expand Up @@ -15,15 +15,21 @@
*/

import * as Chat from '../../chat';
import { internalEv } from '../../eventEmitter';
import * as webpack from '../../webpack';
import { ChatStore, ContactStore } from '../../whatsapp';
import {
ChatStore,
ContactStore,
ParticipantModel,
UserPrefs,
WidFactory,
} from '../../whatsapp';
import { wrapModuleFunction } from '../../whatsapp/exportModule';
import {
createMsgProtobuf,
encryptAndSendGroupMsg,
encryptAndSendMsg,
getFanOutList,
getGroupSenderKeyList,
updateParticipants,
} from '../../whatsapp/functions';
import { defaultSendStatusOptions } from '..';

Expand All @@ -39,6 +45,9 @@ export async function sendRawStatus(
...defaultSendStatusOptions,
...options,
};

message.ctwaContext = message.ctwaContext || {};

const result = await Chat.sendRawMessage('status@broadcast', message, {
...options,
createChat: true,
Expand All @@ -51,7 +60,45 @@ export async function sendRawStatus(
return result;
}

async function updateStatusGroup() {
const myContacts = ContactStore.getModelsArray()
.filter((c) => c.isMyContact && !c.isContactBlocked)
.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,
});
}

webpack.onInjected(() => {
let timer: any = null;

ContactStore.on('add remove', () => {
clearTimeout(timer);
timer = setTimeout(updateStatusGroup, 1000);
});

internalEv.on('conn.main_ready', () => {
clearTimeout(timer);
timer = setTimeout(updateStatusGroup, 2000);
});

// allow to send backgroundColor, textColor and font for status
wrapModuleFunction(createMsgProtobuf, (func, ...args) => {
const [msg] = args;
Expand All @@ -73,29 +120,6 @@ webpack.onInjected(() => {
return result;
});

// Return the contact list for status@broadcast
wrapModuleFunction(getGroupSenderKeyList, async (func, ...args) => {
const [wid] = args;

if (!wid.isStatusV3()) {
return await func(...args);
}

const myContacts = ContactStore.getModelsArray()
.filter((c) => c.isMyContact && !c.isContactBlocked)
.map((c) => c.id);

const wids = await getFanOutList({ wids: myContacts });

const result = {
skList: [],
skDistribList: wids,
rotateKey: false,
};

return result;
});

// Force to send as group for broadcast list
wrapModuleFunction(encryptAndSendMsg, async (func, ...args) => {
const [msg] = args;
Expand Down
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Expand Up @@ -55,5 +55,6 @@ export * from './setGroup';
export * from './setPin';
export * from './status';
export * from './typeAttributeFromProtobuf';
export * from './updateParticipants';
export * from './uploadProductImage';
export * from './uploadThumbnail';
37 changes: 37 additions & 0 deletions src/whatsapp/functions/updateParticipants.ts
@@ -0,0 +1,37 @@
/*!
* 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';
import { ParticipantModel } from '../models';

/**
* @whatsapp 951974 >= 2.2222.8
*/
export declare function updateParticipants(params: {
group: Wid;
participants: ParticipantModel[];
version: number;
isOffline: boolean;
}): Promise<any>;

exportModule(
exports,
{
updateParticipants: 'updateParticipants',
},
(m) => m.updateParticipants && m.addParticipants
);

0 comments on commit 55cd0a4

Please sign in to comment.