Skip to content

Commit

Permalink
fix: forwardMessages return
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Feb 28, 2021
1 parent dbea75d commit 4b3511b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 43 deletions.
86 changes: 54 additions & 32 deletions src/lib/wapi/functions/forward-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export async function forwardMessages(chatId, messages, skipMyMessages) {
if (!Array.isArray(messages)) {
messages = [messages];
}

var toForward = (
await Promise.all(
messages.map(async (msg) => {
Expand All @@ -68,37 +69,58 @@ export async function forwardMessages(chatId, messages, skipMyMessages) {
)
).filter((msg) => (skipMyMessages ? !msg.__x_isSentByMe : true));

if (chat.id) {
await Promise.each(toForward, async (e) => {
let conversation = await window.Store.Chat.get(chatId);
let newId = await window.WAPI.getNewMessageId(chat.id);
let tempMsg = await Object.create(
chat.msgs.filter((msg) => msg.__x_isSentByMe)
)[0];
let toFor = await Object.assign(e);
let extend = {
id: newId,
from: Store.Me.wid,
t: parseInt(new Date().getTime() / 1000),
to: chat.__x_contact.__x_id,
ack: 0,
participant: void 0,
local: !0,
self: 'out',
isNewMsg: !0,
star: !1,
isForwarded: toFor.isForwarded || !toFor.isSentByMe,
forwardedFromWeb: !0,
forwardingScore: 21,
multicast: false,
};
Object.assign(tempMsg, toFor);
Object.assign(tempMsg, extend);
var m = { type: 'forwardMessages' };

return await Store.addAndSendMsgToChat(conversation, tempMsg);
}).then(async (t) => {
let result = await Promise.all(t);
console.log(result);
});
}
return new Promise(async (resolve, reject) => {
if (chat.id) {
var To = await WAPI.getchatId(chat.id);
await Promise.each(toForward, async (e) => {
if (typeof e.erro !== 'undefined' && e.erro === true) {
var obj = WAPI.scope(To, true, null, 'message not found');
Object.assign(obj, m);
reject(obj);
return;
}
let conversation = await window.Store.Chat.get(chatId);
let newId = await window.WAPI.getNewMessageId(chat.id);
let tempMsg = await Object.create(
chat.msgs.filter((msg) => msg.__x_isSentByMe)
)[0];
let toFor = await Object.assign(e);
let extend = {
id: newId,
from: Store.Me.wid,
t: parseInt(new Date().getTime() / 1000),
to: chat.__x_contact.__x_id,
ack: 0,
participant: void 0,
local: !0,
self: 'out',
isNewMsg: !0,
star: !1,
isForwarded: toFor.isForwarded || !toFor.isSentByMe,
forwardedFromWeb: !0,
forwardingScore: 21,
multicast: false,
};

Object.assign(tempMsg, toFor);
Object.assign(tempMsg, extend);

return await Store.addAndSendMsgToChat(conversation, tempMsg);
})
.then(async () => {
var obj = WAPI.scope(To, false, 200, null);
Object.assign(obj, m);
resolve(obj);
})
.catch(() => {
var obj = WAPI.scope(To, true, 404, null);
Object.assign(obj, m);
reject(obj);
});
} else {
reject(chat);
}
});
}
16 changes: 5 additions & 11 deletions src/lib/wapi/functions/get-message-by-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,16 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
export async function getMessageById(id, done, serialize = true) {
//Parse message ID
const key = window.Store.MsgKey.fromString(id);

if (!key) {
return false;
}

export async function getMessageById(key, done, serialize = true) {
// Check message is loaded in store
let msg = window.Store.Msg.get(key);
let erro = { erro: true };

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

//If not message not found, load latest messages of chat
Expand All @@ -89,10 +83,10 @@ export async function getMessageById(id, done, serialize = true) {
}

if (!msg) {
return false;
return erro;
}

let result = false;
let result = erro;

if (serialize) {
try {
Expand Down

0 comments on commit 4b3511b

Please sign in to comment.