Skip to content

Commit

Permalink
fix: serialize message
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Oct 29, 2024
1 parent 33a5a1b commit eb29768
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 182 deletions.
2 changes: 1 addition & 1 deletion src/webpack/@types/API.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ interface API {
* Set message information!
* @param {object} msg mensagens
*/
serializeMessageObj: (msg: object) => void;
serializeMessageObj: (msg: object) => Promise<void>;
}

declare global {
Expand Down
25 changes: 9 additions & 16 deletions src/webpack/api/layes/on-wpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,29 @@ export class onMod {
});

window.interfaceChange(getData());
}).catch((e) => { console.log(e); });

}).catch(() => { });
}

public newMessage() {
this.page.evaluate(() => {
let isHeroEqual = {};
window.Store.Msg.on('add', (newMessage: any) => {
if (!Object.is(isHeroEqual, newMessage)) {
isHeroEqual = newMessage;
if (newMessage && newMessage.isNewMsg) {
window.newMessage(API.serializeMessageObj(newMessage));
}
window.Store.Msg.on('add', async (newMessage: any) => {
if (newMessage && newMessage.isNewMsg) {
const serialize = await API.serializeMessageObj(newMessage)
window.newMessage(serialize);
}
});
}).catch(() => { });
}

public newOnAck() {
this.page.evaluate(() => {
let isHeroEqual = {};
window.Store.Msg.on('change:ack', (newOnAck: any) => {
if (!Object.is(isHeroEqual, newOnAck)) {
isHeroEqual = newOnAck;
if (newOnAck) {
window.newOnAck(newOnAck);
}
if (newOnAck) {
window.newOnAck(newOnAck);
}
});
}).catch(() => { });
}

}
23 changes: 20 additions & 3 deletions src/webpack/assets/functions/help/get-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {string} chatId Contact number
* @returns {(Object|undefined)}
*/
export function getChat(chatId) {
export const getChat = async (chatId) => {

if (!chatId) return false;

Expand All @@ -15,9 +15,26 @@ export function getChat(chatId) {
const ConstructChat = new window.Store.UserConstructor(chatId, {
intentionallyUsePrivateConstructor: !0,
});
found = window.Store.Chat.find(ConstructChat) || false;
const chatWid = new Store.WidFactory.createWid(chatId);
await Store.Chat.add(
{
createdLocally: true,
id: chatWid,
},
{
merge: true,
}

);
found = Store.Chat.find(ConstructChat) || false;
}
}

if (found) {
found.sendMessage = found.sendMessage
? found.sendMessage
: function () {
return window.Store.sendMessage.apply(this, arguments);
};
}
return found;
}
53 changes: 28 additions & 25 deletions src/webpack/assets/serializers/serialize-chat.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
/**
* Serializes a chat object
* @param rawChat Chat object
* @returns {Chat}
*/
export const serializeChatObj = (obj) => {
if (obj == undefined) {
return null;
}
return Object.assign(window.API.serializeRawObj(obj), {
kind: obj.kind,
isGroup: obj.isGroup,
contact: obj['contact']
? window.API.serializeContactObj(obj['contact'])
: null,
groupMetadata: obj['groupMetadata']
? window.API.serializeRawObj(obj['groupMetadata'])
: null,
presence: obj['presence']
? window.API.serializeRawObj(obj['presence'])
: null,
msgs: null,
isOnline: obj.__x_presence.attributes.isOnline || null,
lastSeen:
obj &&
obj.previewMessage &&
obj.previewMessage.__x_ephemeralStartTimestamp
? obj.previewMessage.__x_ephemeralStartTimestamp * 1000
: null,
});
};
if (obj == undefined) {
return null;
}
return Object.assign(window.API.serializeRawObj(obj), {
kind: obj?.kind,
isGroup: obj?.isGroup,
contact: obj?.contact
? window.API.serializeContactObj(obj?.contact)
: null,
groupMetadata: obj?.groupMetadata
? window.API.serializeRawObj(obj?.groupMetadata)
: null,
presence: obj?.presence
? window.API.serializeRawObj(obj?.presence)
: null,
msgs: null,
tcToken: null,
isOnline: obj?.__x_presence?.attributes?.isOnline || null,
lastSeen: obj?.previewMessage?.__x_ephemeralStartTimestamp
? obj.previewMessage.__x_ephemeralStartTimestamp * 1000
: null,
});
};
49 changes: 23 additions & 26 deletions src/webpack/assets/serializers/serialize-contact.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
/**
* Function to serialize contact object
* @param {*} obj
* @returns
*/
export const serializeContactObj = (obj) => {
if (obj === undefined) {
if (obj == undefined) {
return null;
}

const serializedObj = window.API.serializeRawObj(obj);

if (!obj.profilePicThumb && obj.id && window.Store.ProfilePicThumb) {
obj.profilePicThumb = window.Store.ProfilePicThumb.get(obj.id);
if (!obj.profilePicThumb && obj.id && Store.ProfilePicThumb) {
obj.profilePicThumb = Store.ProfilePicThumb.get(obj.id);
}

return {
...serializedObj,
formattedName: obj.formattedName,
displayName: obj.displayName,
formattedShortName: obj.formattedShortName,
formattedShortNameWithNonBreakingSpaces:
obj.formattedShortNameWithNonBreakingSpaces,
isHighLevelVerified: obj.isHighLevelVerified,
isMe: obj.isMe,
mentionName: obj.mentionName,
notifyName: obj.notifyName,
isMyContact: obj.isMyContact,
isPSA: obj.isPSA,
isUser: obj?.isUser ?? obj.id?.server === 'c.us' ? true : false,
isVerified: obj.isVerified,
isWAContact: obj.isWAContact,
profilePicThumbObj: obj.profilePicThumb
? API.serializeProfilePicThumb(obj.profilePicThumb)
return Object.assign(window.API.serializeRawObj(obj), {
revisionNumber: obj?.revisionNumber,
is_contact: obj?.isAddressBookContact,
profilePicThumbObj: obj?.profilePicThumb
? API.serializeProfilePicThumb(obj?.profilePicThumb)
: {},
statusMute: obj.statusMute,
msgs: null,
};
isBusiness: obj?.isBusiness,
isEnterprise: obj?.isEnterprise,
isSmb: obj?.isSmb,
labels: obj?.labels,
locale: obj?.locale,
name: obj?.name,
pendingAction: obj?.pendingAction,
shortName: obj?.shortName,
verifiedName: obj?.verifiedName,
});
};
163 changes: 101 additions & 62 deletions src/webpack/assets/serializers/serialize-message.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,109 @@
export const serializeMessageObj = (obj) => {
/**
* Function to serialize a message object
* @param {*} obj
* @param {*} groupInfo
* @returns
*/
export const serializeMessageObj = async (obj, groupInfo = true) => {
if (obj === undefined) {
return null;
}
const _chat = obj['chat'] ? await API.serializeChatObj(obj['chat']) : {};
let objGroup = null;
if (groupInfo) {
let chats = await API.getAllChats();
objGroup =
obj?.to?.server === 'g.us' || obj?.from?.server === 'g.us'
? chats.find((chat) => chat?.id?._serialized === obj?.from?._serialized)
.contact
: null;
}

const serializedObj = window.API.serializeRawObj(obj);

const chat = obj.chat ? API.serializeChatObj(obj.chat) : {};
if (obj.quotedMsg) obj.quotedMsgObj();
const serializeAds = obj?.ctwaContext
? {
conversionSource: obj?.ctwaContext?.conversionSource,
description: obj?.ctwaContext?.description,
isSuspiciousLink: obj?.ctwaContext?.isSuspiciousLink,
mediaType: obj?.ctwaContext?.mediaType,
mediaUrl: obj?.ctwaContext?.mediaUrl,
sourceUrl: obj?.ctwaContext?.sourceUrl,
thumbnail: obj?.ctwaContext?.thumbnail,
thumbnailUrl: obj?.ctwaContext?.thumbnailUrl,
title: obj?.ctwaContext?.title,
}
: undefined;

return {
...serializedObj,
id: obj.id._serialized,
from: obj.from._serialized,
quotedParticipant: obj.quotedParticipant ? obj.quotedParticipant._serialized : undefined,
author: obj.author ? obj.author._serialized : undefined,
chatId: obj.id && obj.id.remote ? obj.id.remote : obj.chatId && obj.chatId._serialized ? obj.chatId._serialized : undefined,
to: obj.to ? obj.to._serialized : undefined,
isSentByMe: obj.isSentByMe,
fromMe: obj.id.fromMe,
sender: obj.senderObj ? API.serializeContactObj(obj.senderObj) : null,
timestamp: obj.t,
content: obj.body,
body: obj.body,
isGroupMsg: obj.isGroupMsg,
isLink: obj.isLink,
isMMS: obj.isMMS,
isMedia: obj.isMedia,
isNotification: obj.isNotification,
isPSA: obj.isPSA,
type: obj.type,
chat: chat,
isOnline: chat.isOnline,
lastSeen: chat.lastSeen,
quotedMsgObj: obj.quotedMsg,
quotedStanzaId: obj.quotedStanzaID ? obj.quotedStanzaID : undefined,
mediaData: window.API.serializeRawObj(obj.mediaData),
caption: obj.caption,
deprecatedMms3Url: obj.deprecatedMms3Url,
directPath: obj.directPath,
encFilehash: obj.encFilehash,
filehash: obj.filehash,
filename: obj.filename,
mimetype: obj.mimetype,
clientUrl: obj.clientUrl,
mediaKey: obj.mediaKey,
size: obj.size,
t: obj.t,
isNewMsg: obj.isNewMsg,
linkPreview: obj.linkPreview,
text: obj.text,
height: obj.height,
width: obj.width,
self: obj.self,
initialPageSize: obj.initialPageSize,
lat: obj.lat ? obj.lat : undefined,
lng: obj.lng ? obj.lng : undefined,
ack: obj.ack,
scanLengths: undefined,
scansSidecar: undefined,
streamingSidecar: undefined,
waveform: undefined,
replyButtons: undefined,
dynamicReplyButtons: undefined,
buttons: undefined,
hydratedButtons: undefined,
isGroupMsg: obj?.to?.server === 'g.us' || obj?.from?.server === 'g.us',
groupInfo: (obj?.to?.server === 'g.us' || obj?.from?.server === 'g.us') ? chats.find((chat) => chat.id._serialized === obj.from._serialized).contact : null
...window.API.serializeRawObj(obj),
id: obj?.id?._serialized,
msgKey: obj?.id,
from: obj?.from?._serialized,
quotedParticipant: obj?.quotedParticipant?._serialized
? obj?.quotedParticipant?._serialized
: undefined,
author: obj?.author?._serialized ? obj?.author?._serialized : undefined,
chatId: obj?.id?.remote
? obj?.id?.remote
: obj?.chatId?._serialized
? obj?.chatId?._serialized
: undefined,
to: obj?.to?._serialized ? obj?.to?._serialized : undefined,
fromMe: obj?.id?.fromMe,
sender: obj?.senderObj
? await API.serializeContactObj(obj?.senderObj)
: null,
timestamp: obj?.t,
description: obj?.description,
content: obj?.body,
body: obj?.body,
isLink: obj?.isLink,
isMMS: obj?.isMMS,
isMedia: obj?.isMedia,
isNotification: obj?.isNotification,
isPSA: obj?.isPSA,
type: obj?.type,
chat: _chat,
isOnline: _chat?.isOnline,
lastSeen: _chat?.lastSeen,
quotedMsgObj: obj?.quotedMsg,
quotedStanzaId: obj?.quotedStanzaID ? obj?.quotedStanzaID : undefined,
mediaData: window.API.serializeRawObj(obj?.mediaData),
caption: obj?.caption,
deprecatedMms3Url: obj?.deprecatedMms3Url,
directPath: obj?.directPath,
encFilehash: obj?.encFilehash,
filehash: obj?.filehash,
filename: obj?.filename,
mimetype: obj?.mimetype,
clientUrl: obj?.clientUrl,
mediaKey: obj?.mediaKey,
size: obj?.size,
t: obj?.t,
isNewMsg: obj?.isNewMsg,
linkPreview: obj?.linkPreview,
text: obj?.text,
height: obj?.height,
width: obj?.width,
self: obj?.self,
initialPageSize: obj?.initialPageSize,
lat: obj?.lat ? obj.lat : undefined,
lng: obj?.lng ? obj.lng : undefined,
ack: obj?.ack,
scanLengths: null,
scansSidecar: null,
streamingSidecar: null,
waveform: null,
replyButtons: obj?.replyButtons,
dynamicReplyButtons: obj?.dynamicReplyButtons,
buttons: obj?.buttons,
hydratedButtons: obj?.hydratedButtons,
subtype: obj?.subtype,
thumbnail: obj?.thumbnail,
isGroupMsg:
obj?.to?.server === 'g.us' || obj?.from?.server === 'g.us' ? true : false,
groupInfo: objGroup,
title: obj?.title,
ctwaContext: serializeAds,
messageSecret: obj?.messageSecret && undefined,
};
};
Loading

0 comments on commit eb29768

Please sign in to comment.