Skip to content

Commit

Permalink
fix: Migrated Chat and Msg stores to WA-JS
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Nov 28, 2021
1 parent 5cd98d0 commit 68e7a54
Show file tree
Hide file tree
Showing 34 changed files with 161 additions and 184 deletions.
22 changes: 12 additions & 10 deletions src/lib/wapi/business/send-message-with-buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
*/

window.WAPI.sendButtons = async function (chatId) {
var chat = Store.Chat.get(chatId);
var chat = WPP.whatsapp.ChatStore.get(chatId);
var tempMsg = Object.create(chat.msgs.filter((msg) => msg.__x_isSentByMe)[0]);
// var tempMsg = Object.create(Store.Msg.models.filter(msg => msg.to._serialized===chatId&&msg.__x_isSentByMe&& msg.type=='chat' && !msg.quotedStanzaID)[0])
// var tempMsg = Object.create(WPP.whatsapp.MsgStore.models.filter(msg => msg.to._serialized===chatId&&msg.__x_isSentByMe&& msg.type=='chat' && !msg.quotedStanzaID)[0])
var t2 = Object.create(
Store.Msg.filter((x) => (x.type == 'template') & !x.id.fromMe)[0]
WPP.whatsapp.MsgStore.filter(
(x) => (x.type == 'template') & !x.id.fromMe
)[0]
);
var newId = WPP.chat.generateMessageID(chatId);
delete tempMsg.hasTemplateButtons;
Expand Down Expand Up @@ -107,7 +109,7 @@ window.WAPI.sendButtons = async function (chatId) {
chat.addQueue
.enqueue(
Store.MessageUtils.appendMessage(chat, tempMsg).then(() => {
var e = Store.Msg.add(tempMsg)[0];
var e = WPP.whatsapp.MsgStore.add(tempMsg)[0];
console.log('e ', e);
if (e) {
return e.waitForPrep().then(() => {
Expand Down Expand Up @@ -171,7 +173,7 @@ window.WAPI.sendButtons = async function (chatId) {
}),
retryOn5xx: !0,
resendGuard: function (_) {
var t = Store.Msg.get(e.id);
var t = WPP.whatsapp.MsgStore.get(e.id);
console.log('in resend', _);
return 'protocol' === e.type || (t && t.id.equals(e.id));
},
Expand All @@ -191,9 +193,9 @@ window.WAPI.sendButtons = async function (chatId) {
};

window.WAPI.sendButtons2 = async function (chatId) {
var chat = Store.Chat.get(chatId);
var chat = WPP.whatsapp.ChatStore.get(chatId);
var tempMsg = Object.create(
Store.Msg.models.filter(
WPP.whatsapp.MsgStore.models.filter(
(msg) =>
msg.to._serialized === chatId &&
msg.__x_isSentByMe &&
Expand All @@ -202,7 +204,7 @@ window.WAPI.sendButtons2 = async function (chatId) {
)[0]
);
var t2 = Object.create(
Store.Msg.models.filter(
WPP.whatsapp.MsgStore.models.filter(
(msg) =>
msg.to._serialized === chatId &&
msg.__x_isSentByMe &&
Expand Down Expand Up @@ -278,7 +280,7 @@ window.WAPI.sendButtons2 = async function (chatId) {
chat.addQueue
.enqueue(
Store.MessageUtils.appendMessage(chat, tempMsg).then(() => {
var e = Store.Msg.add(tempMsg)[0];
var e = WPP.whatsapp.MsgStore.add(tempMsg)[0];
console.log('e ', e);
if (e) {
return e.waitForPrep().then(() => {
Expand Down Expand Up @@ -334,7 +336,7 @@ window.WAPI.sendButtons2 = async function (chatId) {
onDrop: s.wrap(reject),
retryOn5xx: !0,
resendGuard: function (_) {
var t = Store.Msg.get(e.id);
var t = WPP.whatsapp.MsgStore.get(e.id);
return 'protocol' === e.type || (t && t.id.equals(e.id));
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/business/send-payment-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ window.WAPI.sendPaymentRequest = async function (
currency,
noteMessage
) {
var chat = Store.Chat.get(chatId);
var chat = WPP.whatsapp.ChatStore.get(chatId);
var tempMsg = Object.create(chat.msgs.filter((msg) => msg.__x_isSentByMe)[0]);
var newId = WPP.chat.generateMessageID(chatId);
var extend = {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/wapi/functions/check-send-exist.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export async function sendExist(chatId, returnChat = true, Send = true) {
let chat = await window.WAPI.getChat(chatId);

if (!chat && chatId === 'status@broadcast') {
chat = new Store.Chat.modelClass({
chat = new WPP.whatsapp.ChatStore.modelClass({
id: WPP.whatsapp.WidFactory.createWid('status@broadcast'),
});
Store.Chat.add(chat);
WPP.whatsapp.ChatStore.add(chat);
chat = await window.WAPI.getChat(chatId); // Fix some methods
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/forward-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function forwardMessages(chatId, messagesId, skipMyMessages) {
}

await WAPI.sendExist(chatId);
const chat = window.Store.Chat.get(chatId);
const chat = WPP.whatsapp.ChatStore.get(chatId);
if (!chat) {
throw {
error: true,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/get-all-chats-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export const getAllChatIds = function (done) {
const chatIds = window.Store.Chat.map(
const chatIds = WPP.whatsapp.ChatStore.map(
(chat) => chat.id._serialized || chat.id
);

Expand Down
4 changes: 3 additions & 1 deletion src/lib/wapi/functions/get-all-chats.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/

export const getAllChats = function (done) {
const chats = window.Store.Chat.map((chat) => WAPI._serializeChatObj(chat));
const chats = WPP.whatsapp.ChatStore.map((chat) =>
WAPI._serializeChatObj(chat)
);

if (done !== undefined) done(chats);
return chats;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/get-all-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export function getAllGroups(done) {
const groups = window.Store.Chat.filter((chat) => chat.isGroup);
const groups = WPP.whatsapp.ChatStore.filter((chat) => chat.isGroup);

if (done !== undefined) done(groups);
return groups;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/get-chat-by-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export function getChatByName(name, done) {
const found = window.Store.Chat.find((chat) => chat.name === name);
const found = WPP.whatsapp.ChatStore.find((chat) => chat.name === name);
if (done !== undefined) done(found);
return found;
}
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/get-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function getChat(id) {
return false;
}
id = typeof id == 'string' ? id : id._serialized;
const found = Store.Chat.get(id);
const found = WPP.whatsapp.ChatStore.get(id);
if (found) {
found.sendMessage = found.sendMessage
? found.sendMessage
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/get-chats-with-new-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { hasUndreadMessages } from './has-unread-messages';

export const getAllChatsWithNewMessages = function (done) {
const chats = window.Store.Chat.filter(hasUndreadMessages).map((chat) =>
const chats = WPP.whatsapp.ChatStore.filter(hasUndreadMessages).map((chat) =>
WAPI._serializeChatObj(chat)
);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/get-group-invite-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export async function getGroupInviteLink(chatId) {
var chat = Store.Chat.get(chatId);
var chat = WPP.whatsapp.ChatStore.get(chatId);
if (!chat) {
throw {
error: true,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/get-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function getMessages(chatId, params = {}, serialize = true) {
}

const result = msgs
.map((m) => new Store.Msg.modelClass(m))
.map((m) => new WPP.whatsapp.MsgStore.modelClass(m))
.map((m) => WAPI.processMessageObj(m, true, true));

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/get-unread-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function getUnreadMessages(
useUnreadCount,
done
) {
const chats = window.Store.Chat.models;
const chats = WPP.whatsapp.ChatStore.models;
const output = [];

for (const chat in chats) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/load-all-earlier-chat-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function loadAllEarlierMessages(id, done) {

// improve load speed
try {
await Store.Msg.findQuery({
await WPP.whatsapp.MsgStore.findQuery({
remote: chat.id,
count: -1,
});
Expand Down
4 changes: 2 additions & 2 deletions src/lib/wapi/functions/open-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export async function openChat(chatId) {
const chat = Store.Chat.get(chatId);
const chat = WPP.whatsapp.ChatStore.get(chatId);
const result = Store.Cmd.default.openChatBottom(chat);
return result;
}
Expand All @@ -29,7 +29,7 @@ export async function openChat(chatId) {
* @returns {{wasVisible: boolean, alignAt: string}}: {wasVisible: false, alignAt: "center"}
*/
export async function openChatAt(chatId, messageId) {
const chat = Store.Chat.get(chatId);
const chat = WPP.whatsapp.ChatStore.get(chatId);
const atMessage = chat.msgs.models.find((model) => model.id.id === messageId);
const args = {
collection: chat.msgs,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/revoke-invite-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export async function revokeGroupInviteLink(chatId) {
var chat = Store.Chat.get(chatId);
var chat = WPP.whatsapp.ChatStore.get(chatId);
if (!chat) {
throw {
error: true,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/send-chat-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
export async function sendChatstate(state, chatId) {
state = parseInt(state, 10);

const chat = window.Store.Chat.get(chatId);
const chat = WPP.whatsapp.ChatStore.get(chatId);

if (!chat) {
throw {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/send-message-with-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export async function sendMessageWithTags(to, body) {
var chat = to.id ? to : Store.Chat.get(to);
var chat = to.id ? to : WPP.whatsapp.ChatStore.get(to);
var chatId = chat.id._serialized;
var msgIveSent = chat.msgs.filter((msg) => msg.__x_isSentByMe)[0];
if (!msgIveSent) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/set-group-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export async function setGroupDescription(groupId, description) {
const chat = Store.Chat.get(groupId);
const chat = WPP.whatsapp.ChatStore.get(groupId);

if (!chat) {
throw {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/set-group-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export async function setGroupProperty(groupId, property, value) {
const chat = Store.Chat.get(groupId);
const chat = WPP.whatsapp.ChatStore.get(groupId);

if (!chat) {
throw {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/set-group-subject.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export async function setGroupSubject(groupId, title) {
const chat = Store.Chat.get(groupId);
const chat = WPP.whatsapp.ChatStore.get(groupId);

if (!chat) {
throw {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/set-temporary-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export async function setTemporaryMessages(chatId, value) {
const chat = Store.Chat.get(chatId);
const chat = WPP.whatsapp.ChatStore.get(chatId);

if (!chat) {
throw {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/wapi/functions/simulate-typing.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

export async function startTyping(chatId) {
const chat = window.Store.Chat.get(chatId);
const chat = WPP.whatsapp.ChatStore.get(chatId);
if (!chat) {
throw {
error: true,
Expand All @@ -33,7 +33,7 @@ export async function startTyping(chatId) {
* @param {string} chatId
*/
export async function stopTyping(chatId) {
const chat = window.Store.Chat.get(chatId);
const chat = WPP.whatsapp.ChatStore.get(chatId);
if (!chat) {
throw {
error: true,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/star-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function starMessages(messagesId, star) {
let count = 0;
// Star Messages
for (const chatId in messagesPerChat) {
const chat = Store.Chat.get(chatId);
const chat = WPP.whatsapp.ChatStore.get(chatId);
const messages = messagesPerChat[chatId];

count += await chat
Expand Down
34 changes: 16 additions & 18 deletions src/lib/wapi/listeners/add-all-new-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@

export function allNewMessagesListener() {
window.WAPI.allNewMessagesListener = (callback) =>
window.WAPI.waitForStore(['Chat', 'Msg'], () => {
window.Store.Msg.on('add', (newMessage) => {
if (newMessage && newMessage.isNewMsg) {
setTimeout(
() => {
let message = window.WAPI.processMessageObj(
newMessage,
true,
false
);
if (message) {
callback(message);
}
},
newMessage.body ? 0 : 2000
);
}
});
WPP.whatsapp.MsgStore.on('add', (newMessage) => {
if (newMessage && newMessage.isNewMsg) {
setTimeout(
() => {
let message = window.WAPI.processMessageObj(
newMessage,
true,
false
);
if (message) {
callback(message);
}
},
newMessage.body ? 0 : 2000
);
}
});
}
42 changes: 20 additions & 22 deletions src/lib/wapi/listeners/add-on-added-to-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,27 @@ export function addOnAddedToGroup() {
* @returns {boolean}
*/
window.WAPI.onAddedToGroup = function (callback) {
window.WAPI.waitForStore('Msg', () => {
window.Store.Msg.on('add', (message) => {
/**
* Mensagem precisa ser:
* - Nova
* - É uma notificação
* - Do tipo de eventos de grupo
* - Evento de adicionado no grupo
*/
if (
message.isNewMsg &&
message.isNotification &&
message.type === 'gp2' &&
message.subtype === 'add'
) {
try {
const data = WAPI._serializeChatObj(message.chat);
callback(data);
} catch (error) {
console.error(error);
}
WPP.whatsapp.MsgStore.on('add', (message) => {
/**
* Mensagem precisa ser:
* - Nova
* - É uma notificação
* - Do tipo de eventos de grupo
* - Evento de adicionado no grupo
*/
if (
message.isNewMsg &&
message.isNotification &&
message.type === 'gp2' &&
message.subtype === 'add'
) {
try {
const data = WAPI._serializeChatObj(message.chat);
callback(data);
} catch (error) {
console.error(error);
}
});
}
});
return true;
};
Expand Down
Loading

0 comments on commit 68e7a54

Please sign in to comment.