Skip to content

Commit

Permalink
fix: Fixed WPP.chat.getMessageById (fix #728)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Nov 1, 2022
1 parent dc14a5e commit 1a806b9
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/chat/functions/getMessageById.ts
Expand Up @@ -19,6 +19,7 @@ import Debug from 'debug';
import { assertGetChat } from '../../assert';
import { WPPError } from '../../util';
import { MsgKey, MsgModel, MsgStore } from '../../whatsapp';
import { getSearchContext } from '../../whatsapp/functions';

const debug = Debug('WA-JS:message:getMessageById');

Expand Down Expand Up @@ -63,7 +64,7 @@ export async function getMessageById(

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

msg = chat.msgs.get(msgKey) || result.collection.get(msgKey);
Expand Down
3 changes: 2 additions & 1 deletion src/chat/functions/openChatAt.ts
Expand Up @@ -16,6 +16,7 @@

import { assertFindChat, assertWid } from '../../assert';
import { Cmd, Wid } from '../../whatsapp';
import { getSearchContext } from '../../whatsapp/functions';
import { getMessageById } from '.';

/**
Expand All @@ -38,7 +39,7 @@ export async function openChatAt(

const msg = await getMessageById(messageId);

const result = chat.getSearchContext(msg);
const result = getSearchContext(chat, msg);

return await Cmd.openChatAt(chat, result);
}
56 changes: 56 additions & 0 deletions src/whatsapp/functions/getSearchContext.ts
@@ -0,0 +1,56 @@
/*!
* 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 * as webpack from '../../webpack';
import { exportModule } from '../exportModule';
import { MsgKey, MsgLoad } from '../misc';
import { ChatModel, MsgModel } from '../models';

/**
* @whatsapp 738599 >= 2.2242.5
*/
export declare function getSearchContext(
chat: ChatModel,
msg: MsgModel | MsgKey,
options?: {
isQuotedMsgAvailable: boolean;
}
): {
collection: MsgLoad;
msg?: MsgModel;
key?: MsgKey;
highlightMsg: true;
};

exportModule(
exports,
{
getSearchContext: 'getSearchContext',
},
(m) => m.getSearchContext
);

webpack.injectFallbackModule('getSearchContext', {
getSearchContext: (
chat: ChatModel,
msg: MsgModel | MsgKey,
options?: {
isQuotedMsgAvailable: boolean;
}
) => {
return chat.getSearchContext?.(msg, options);
},
});
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Expand Up @@ -33,6 +33,7 @@ export * from './genMinimalLinkPreview';
export * from './getFanOutList';
export * from './getGroupSenderKeyList';
export * from './getOrGenerate';
export * from './getSearchContext';
export * from './groupParticipants';
export * from './handleAck';
export * from './isAuthenticated';
Expand Down
3 changes: 2 additions & 1 deletion src/whatsapp/misc/Cmd.ts
Expand Up @@ -16,6 +16,7 @@

import { ChatModel } from '..';
import { exportModule } from '../exportModule';
import { getSearchContext } from '../functions';
import { MsgModel } from '../models';
import { EventEmitter } from '.';

Expand Down Expand Up @@ -72,7 +73,7 @@ export declare class CmdClass extends EventEmitter {
_openChat(e?: any, t?: any): void;
openChatAt(
chat: ChatModel,
context: ReturnType<ChatModel['getSearchContext']>
context: ReturnType<typeof getSearchContext>
): Promise<boolean>;
openChatFromUnread(chat: ChatModel): Promise<boolean>;
openChatBottom(chat: ChatModel): Promise<boolean>;
Expand Down
2 changes: 1 addition & 1 deletion src/whatsapp/models/ChatModel.ts
Expand Up @@ -139,7 +139,7 @@ export declare class ChatModel extends ModelChatBase {
loadEarlierMsgs(e?: any, t?: any): any;
isMostRecentCMC(e?: any): boolean;
loadRecentMsgs(e?: any): any;
getSearchContext(
getSearchContext?(
msg: MsgModel | MsgKey,
options?: {
isQuotedMsgAvailable: boolean;
Expand Down

0 comments on commit 1a806b9

Please sign in to comment.