Skip to content

Commit

Permalink
fix: Fixed getMessageById from non loaded chat
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Dec 8, 2020
1 parent 62e83ee commit 600ace1
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 26 deletions.
16 changes: 11 additions & 5 deletions src/lib/wapi/functions/delete-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
export function deleteMessages(chatId, messageArray, onlyLocal, done) {
import { getMessageById } from './get-message-by-id';

export async function deleteMessages(chatId, messageArray, onlyLocal, done) {
const userId = new Store.WidFactory.createWid(chatId);
const conversation = WAPI.getChat(userId);
if (!conversation) {
Expand All @@ -66,11 +68,15 @@ export function deleteMessages(chatId, messageArray, onlyLocal, done) {
messageArray = [messageArray];
}

let messagesToDelete = messageArray
.map((msgId) =>
typeof msgId == 'string' ? window.Store.Msg.get(msgId) : msgId
let messagesToDelete = (
await Promise.all(
messageArray.map(async (msgId) =>
typeof msgId == 'string'
? await getMessageById(msgId, null, false)
: msgId
)
)
.filter((x) => x);
).filter((x) => x);
if (messagesToDelete.length == 0) return true;
let jobs = onlyLocal
? [conversation.sendDeleteMsgs(messagesToDelete, conversation)]
Expand Down
4 changes: 3 additions & 1 deletion src/lib/wapi/functions/download-media.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
import { getMessageById } from './get-message-by-id';

export async function downloadMedia(messageId) {
const msg = Store.Msg.get(messageId);
const msg = await getMessageById(messageId, null, false);

if (!msg) {
throw {
Expand Down
22 changes: 13 additions & 9 deletions src/lib/wapi/functions/forward-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,23 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
import { getMessageById } from './get-message-by-id';

export async function forwardMessages(to, messages, skipMyMessages) {
if (!Array.isArray(messages)) {
messages = [messages];
}
const toForward = messages
.map((msg) => {
if (typeof msg === 'string') {
return window.Store.Msg.get(msg);
} else {
return window.Store.Msg.get(msg.id);
}
})
.filter((msg) => (skipMyMessages ? !msg.__x_isSentByMe : true));
const toForward = (
await Promise.all(
messages.map(async (msg) => {
if (typeof msg === 'string') {
return await getMessageById(msg, null, false);
} else {
return await getMessageById(msg.id, null, false);
}
})
)
).filter((msg) => (skipMyMessages ? !msg.__x_isSentByMe : true));

// const userId = new window.Store.UserConstructor(to);
const conversation = window.Store.Chat.get(to);
Expand Down
55 changes: 48 additions & 7 deletions src/lib/wapi/functions/get-message-by-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,57 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
export function getMessageById(id, done) {
export async function getMessageById(id, done, serialize = true) {
//Parse message ID
const key = window.Store.MsgKey.fromString(id);

if (!key) {
return false;
}

// Check message is loaded in store
let msg = window.Store.Msg.get(key);

if (!msg) {
// Get chat of message
const chat = window.Store.Chat.get(key.remote);
if (!chat) {
return false;
}

//If not message not found, load latest messages of chat
await chat.loadEarlierMsgs();
msg = window.Store.Msg.get(key);

if (!msg) {
// If not found, load messages around the message ID
const context = chat.getSearchContext(key);
if (
context &&
context.collection &&
context.collection.loadAroundPromise
) {
await context.collection.loadAroundPromise;
}
msg = window.Store.Msg.get(key);
}
}

if (!msg) {
return false;
}

let result = false;
try {
let msg = window.Store.Msg.get(id);
if (msg) {

if (serialize) {
try {
result = WAPI.processMessageObj(msg, true, true);
}
} catch (err) {}
} catch (err) {}
} else {
result = msg;
}

if (done !== undefined) {
if (typeof done === 'function') {
done(result);
} else {
return result;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/wapi/functions/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
import { getMessageById } from './get-message-by-id';

export async function reply(chatId, content, quotedMessageId) {
const chat = Store.Chat.get(chatId);

let quotedMsgOptions = {};
if (quotedMessageId) {
let quotedMessage = window.Store.Msg.get(quotedMessageId);
let quotedMessage = await getMessageById(quotedMessageId, null, false);
if (quotedMessage && quotedMessage.canReply()) {
quotedMsgOptions = quotedMessage.msgContextInfo(chat);
}
Expand Down
7 changes: 6 additions & 1 deletion src/lib/wapi/functions/sendMessageOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
import { getMessageById } from './get-message-by-id';

/**
* Send message with options
Expand All @@ -73,7 +74,11 @@ export async function sendMessageOptions(chatId, content, options = {}) {

let quotedMsgOptions = {};
if (options.quotedMessageId) {
let quotedMessage = window.Store.Msg.get(options.quotedMessageId);
let quotedMessage = await getMessageById(
options.quotedMessageId,
null,
false
);
if (quotedMessage && quotedMessage.canReply()) {
quotedMsgOptions = quotedMessage.msgContextInfo(chat);
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ interface WAPI {
contactId: string,
messageId: string[] | string,
onlyLocal: boolean
) => any;
) => Promise<boolean>;
demoteParticipant: (groupId: string, contactId: string | string[]) => void;
downloadFile: (data: string) => Promise<string | boolean>;
downloadMedia: (messageId: string) => Promise<string>;
Expand Down Expand Up @@ -119,7 +119,7 @@ interface WAPI {
getGroupParticipantIDs: (groupId: string) => Id[];
getHost: () => HostDevice;
getListMute: (type?: string) => object;
getMessageById: (messageId: string) => Message;
getMessageById: (messageId: string) => Promise<Message>;
getNumberProfile: (contactId: string) => WhatsappProfile;
getProfilePicFromServer: (chatId: string) => string;
getSessionTokenBrowser: (removePath?: boolean) => tokenSession;
Expand Down

0 comments on commit 600ace1

Please sign in to comment.