Skip to content

Commit

Permalink
Separate serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcardeenas committed Jan 25, 2020
1 parent e2ee4bd commit 54ae3dd
Show file tree
Hide file tree
Showing 13 changed files with 1,844 additions and 1,810 deletions.
2 changes: 2 additions & 0 deletions src/lib/wapi/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { sendMessage } from './send-message';
export { sendMessage2 } from './send-message2';
42 changes: 42 additions & 0 deletions src/lib/wapi/functions/send-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export function sendMessage(id, message, done) {
var chat = WAPI.getChat(id);
if (chat) {
if (done !== undefined) {
chat.sendMessage(message).then(function() {
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

var trials = 0;

function check() {
for (let i = chat.msgs.models.length - 1; i >= 0; i--) {
let msg = chat.msgs.models[i];

if (!msg.senderObj.isMe || msg.body != message) {
continue;
}
done(WAPI._serializeMessageObj(msg));
return True;
}
trials += 1;
// console.log(trials);

if (trials > 30) {
done(true);
return;
}
sleep(500).then(check);
}
check();
});
return true;
} else {
chat.sendMessage(message);
return true;
}
} else {
if (done !== undefined) done(false);
return false;
}
}
20 changes: 20 additions & 0 deletions src/lib/wapi/functions/send-message2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function sendMessage2(id, message, done) {
var chat = WAPI.getChat(id);
if (chat !== undefined) {
try {
if (done !== undefined) {
chat.sendMessage(message).then(function() {
done(true);
});
} else {
chat.sendMessage(message);
}
return true;
} catch (error) {
if (done !== undefined) done(false);
return false;
}
}
if (done !== undefined) done(false);
return false;
}
1 change: 0 additions & 1 deletion src/lib/wapi/jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
"module": "commonjs",
"target": "es6"
},
"exclude": ["node_modules"]
}
5 changes: 5 additions & 0 deletions src/lib/wapi/serializers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { _serializeChatObj } from './serialize-chat';
export { _serializeRawObj } from './serialize-raw';
export { _serializeMessageObj } from './serialize-message';
export { _serializeContactObj } from './serialize-contact';
export { _serializeNumberStatusObj } from './serialize-number-status';
35 changes: 35 additions & 0 deletions src/lib/wapi/serializers/serialize-chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { _serializeRawObj } from './serialize-raw';

/**
* Serializes a chat object
*
* @param rawChat Chat object
* @returns {Chat}
*/
export const _serializeChatObj = obj => {
if (obj == undefined) {
return null;
}

return Object.assign(_serializeRawObj(obj), {
kind: obj.kind,
isGroup: obj.isGroup,
contact: obj["contact"]
? window.WAPI._serializeContactObj(obj["contact"])
: null,
groupMetadata: obj["groupMetadata"]
? _serializeRawObj(obj["groupMetadata"])
: null,
presence: obj["presence"]
? _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
});
};
25 changes: 25 additions & 0 deletions src/lib/wapi/serializers/serialize-contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Serializes contact object
* @param {Contact} obj
*/
export const _serializeContactObj = obj => {
if (obj == undefined) {
return null;
}

return Object.assign(window.WAPI._serializeRawObj(obj), {
formattedName: obj.formattedName,
isHighLevelVerified: obj.isHighLevelVerified,
isMe: obj.isMe,
isMyContact: obj.isMyContact,
isPSA: obj.isPSA,
isUser: obj.isUser,
isVerified: obj.isVerified,
isWAContact: obj.isWAContact,
profilePicThumbObj: obj.profilePicThumb
? WAPI._serializeProfilePicThumb(obj.profilePicThumb)
: {},
statusMute: obj.statusMute,
msgs: null
});
};
31 changes: 31 additions & 0 deletions src/lib/wapi/serializers/serialize-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Serializes chat object
* @param {Chat} obj
*/
export const _serializeMessageObj = obj => {
if (obj == undefined) {
return null;
}
const _chat = WAPI._serializeChatObj(obj["chat"]);
return Object.assign(window.WAPI._serializeRawObj(obj), {
id: obj.id._serialized,
sender: obj["senderObj"]
? WAPI._serializeContactObj(obj["senderObj"])
: null,
timestamp: obj["t"],
content: 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,
chatId: obj.id.remote,
quotedMsgObj: WAPI._serializeMessageObj(obj["_quotedMsgObj"]),
mediaData: window.WAPI._serializeRawObj(obj["mediaData"]),
isOnline: _chat.isOnline,
lastSeen: _chat.lastSeen
});
};
19 changes: 19 additions & 0 deletions src/lib/wapi/serializers/serialize-number-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Serializes number status object
* @param {*} obj
*/
export const _serializeNumberStatusObj = obj => {
if (obj == undefined) {
return null;
}

return Object.assign(
{},
{
id: obj.jid,
status: obj.status,
isBusiness: obj.biz === true,
canReceiveMessage: obj.status === 200
}
);
};
21 changes: 21 additions & 0 deletions src/lib/wapi/serializers/serialize-profile-pic-thumb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Serializes profile pic (thumbnail) object
* @param {*} obj
*/
export const _serializeProfilePicThumb = obj => {
if (obj == undefined) {
return null;
}

return Object.assign(
{},
{
eurl: obj.eurl,
id: obj.id,
img: obj.img,
imgFull: obj.imgFull,
raw: obj.raw,
tag: obj.tag
}
);
};
10 changes: 10 additions & 0 deletions src/lib/wapi/serializers/serialize-raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Serializes object into JSON format safely
* @param {*} obj
*/
export const _serializeRawObj = obj => {
if (obj) {
return obj.toJSON();
}
return {};
};
44 changes: 44 additions & 0 deletions src/lib/wapi/store/get-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { storeObjects } from "./store-objects";

export function getStore(modules) {
let foundCount = 0;
for (let idx in modules) {
if (typeof modules[idx] === "object" && modules[idx] !== null) {
let first = Object.values(modules[idx])[0];
if (typeof first === "object" && first.exports) {
for (let idx2 in modules[idx]) {
let module = modules(idx2);
if (!module) {
continue;
}
storeObjects.forEach(needObj => {
if (!needObj.conditions || needObj.foundedModule) return;
let neededModule = needObj.conditions(module);
if (neededModule !== null) {
foundCount++;
needObj.foundedModule = neededModule;
}
});
if (foundCount == storeObjects.length) {
break;
}
}

let neededStore = storeObjects.find(needObj => needObj.id === "Store");
window.Store = neededStore.foundedModule
? neededStore.foundedModule
: {};
storeObjects.splice(storeObjects.indexOf(neededStore), 1);
storeObjects.forEach(needObj => {
if (needObj.foundedModule) {
window.Store[needObj.id] = needObj.foundedModule;
}
});
window.Store.sendMessage = function(e) {
return window.Store.SendTextMsgToChat(this, ...arguments);
};
return window.Store;
}
}
}
}
Loading

0 comments on commit 54ae3dd

Please sign in to comment.