Skip to content

Commit

Permalink
fix: Fixed chat.new_message event for status reply (fix #1211)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Jul 7, 2023
1 parent 6ba7540 commit a898151
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 28 deletions.
12 changes: 3 additions & 9 deletions src/chat/events/registerNewMessageEvent.ts
Expand Up @@ -14,11 +14,10 @@
* limitations under the License.
*/

import { getMyUserId } from '../../conn';
import { internalEv } from '../../eventEmitter';
import * as webpack from '../../webpack';
import { ChatStore, MsgKey, MsgModel, MsgStore } from '../../whatsapp';
import { getQuotedMsg } from '../functions/';
import { ChatStore, MsgModel, MsgStore } from '../../whatsapp';
import { getQuotedMsg, getQuotedMsgKey } from '../functions/';

webpack.onInjected(() => register());

Expand Down Expand Up @@ -61,12 +60,7 @@ function register() {
if (typeof MsgModel.prototype.quotedMsgId === 'undefined') {
Object.defineProperty(MsgModel.prototype, 'quotedMsgId', {
get: function () {
const quotedMsgId = new MsgKey({
id: this.quotedStanzaID,
fromMe: getMyUserId()?.equals(this.quotedParticipant) || false,
remote: this.quotedRemoteJid ? this.quotedRemoteJid : this.id.remote,
participant: this.isGroupMsg ? this.quotedParticipant : undefined,
});
const quotedMsgId = getQuotedMsgKey(this);

return quotedMsgId;
},
Expand Down
20 changes: 12 additions & 8 deletions src/chat/functions/getMessageById.ts
Expand Up @@ -18,7 +18,7 @@ import Debug from 'debug';

import { assertGetChat } from '../../assert';
import { WPPError } from '../../util';
import { MsgKey, MsgModel, MsgStore } from '../../whatsapp';
import { MsgKey, MsgModel, MsgStore, StatusV3Store } from '../../whatsapp';
import { getSearchContext } from '../../whatsapp/functions';

const debug = Debug('WA-JS:message:getMessageById');
Expand Down Expand Up @@ -59,15 +59,19 @@ export async function getMessageById(
let msg = MsgStore.get(msgKey);

if (!msg) {
const chat = assertGetChat(msgKey.remote);
msg = chat.msgs.get(msgKey);
if (msgKey.remote.isStatusV3()) {
msg = StatusV3Store.getMyStatus().msgs.get(msgKey);
} else {
const chat = assertGetChat(msgKey.remote);
msg = chat.msgs.get(msgKey);

if (!msg) {
debug(`searching remote message with id ${msgKey.toString()}`);
const result = getSearchContext(chat, msgKey);
await result.collection.loadAroundPromise;
if (!msg) {
debug(`searching remote message with id ${msgKey.toString()}`);
const result = getSearchContext(chat, msgKey);
await result.collection.loadAroundPromise;

msg = chat.msgs.get(msgKey) || result.collection.get(msgKey);
msg = chat.msgs.get(msgKey) || result.collection.get(msgKey);
}
}
}

Expand Down
14 changes: 3 additions & 11 deletions src/chat/functions/getQuotedMsg.ts
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

import { getMyUserId } from '../../conn';
import { WPPError } from '../../util';
import { ChatStore, MsgKey, MsgModel, Wid } from '../../whatsapp';
import { MsgKey, MsgModel } from '../../whatsapp';
import { getMessageById } from './getMessageById';
import { getQuotedMsgKey } from './getQuotedMsgKey';

/**
* Get a quoted message
Expand All @@ -36,15 +36,7 @@ export async function getQuotedMsg(id: string | MsgKey): Promise<MsgModel> {
}
);
}
const chat = msg.id.fromMe
? ChatStore.get(msg.to as Wid)
: ChatStore.get(msg.from as Wid);

const quotedMsgId = new MsgKey({
id: msg.quotedStanzaID,
fromMe: msg.quotedParticipant?._serialized === getMyUserId()?._serialized,
remote: msg.quotedRemoteJid ? msg.quotedRemoteJid : msg.id.remote,
participant: chat?.isGroup ? msg.quotedParticipant : undefined,
});
const quotedMsgId = getQuotedMsgKey(msg);
return await getMessageById(quotedMsgId);
}
50 changes: 50 additions & 0 deletions src/chat/functions/getQuotedMsgKey.ts
@@ -0,0 +1,50 @@
/*!
* Copyright 2023 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 { getMyUserId } from '../../conn';
import { WPPError } from '../../util';
import { MsgKey, MsgModel, Wid } from '../../whatsapp';

/**
* Get a quoted message
*
* @category Chat
*/
export function getQuotedMsgKey(msg: MsgModel): MsgKey {
if (!msg.quotedStanzaID) {
throw new WPPError(
'message_not_have_a_reply',
`Message ${msg.id} does not have a reply`,
{
id: msg.id,
}
);
}

const remote = msg.quotedRemoteJid ? msg.quotedRemoteJid : msg.id.remote;
const fromMe = getMyUserId()?.equals(msg.quotedParticipant) || false;

const quotedMsgId = new MsgKey({
id: msg.quotedStanzaID,
fromMe: fromMe,
remote: remote,
participant:
Wid.isGroup(msg.from!) || Wid.isGroup(msg.to!) || Wid.isStatusV3(remote)
? msg.quotedParticipant
: undefined,
});
return quotedMsgId;
}
1 change: 1 addition & 0 deletions src/chat/functions/index.ts
Expand Up @@ -34,6 +34,7 @@ export { getMessageById } from './getMessageById';
export { getMessages, GetMessagesOptions } from './getMessages';
export { getPlatformFromMessage } from './getPlatformFromMessage';
export { getQuotedMsg } from './getQuotedMsg';
export { getQuotedMsgKey } from './getQuotedMsgKey';
export { getReactions } from './getReactions';
export { getVotes } from './getVotes';
export { ChatListOptions, list } from './list';
Expand Down

0 comments on commit a898151

Please sign in to comment.