-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7cd314a
commit 9b0700d
Showing
39 changed files
with
2,336 additions
and
625 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
**/*.js | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Retrieves chats with messages | ||
* @param {boolean} newOnly boolean | ||
* @param {Function} done callback | ||
*/ | ||
export async function getAllChatsWithMessages(newOnly, done) { | ||
const x = []; | ||
if (newOnly) { | ||
x.push( | ||
WAPI.getAllChatsWithNewMsg().map((c) => WAPI.getChat(c.id._serialized)) | ||
); | ||
} else { | ||
x.push(WAPI.getAllChatIds().map((c) => WAPI.getChat(c))); | ||
} | ||
const result = (await Promise.all(x)).flatMap((x) => x); | ||
return JSON.stringify(result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Retrieves all groups | ||
* @param {Function} done callback | ||
* @returns {Array} List of chats | ||
*/ | ||
export function getAllGroups(done) { | ||
const groups = window.Store.Chat.filter((chat) => chat.isGroup); | ||
|
||
if (done !== undefined) done(groups); | ||
return groups; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { getAllChatsWithNewMessages } from "./get-chats-with-new-messages"; | ||
import { getAllChatsWithNewMessages } from './get-chats-with-new-messages'; | ||
|
||
/** | ||
* Retrieves all new messages | ||
* TODO: Test, seems to be written incorrectly | ||
*/ | ||
export const getAllNewMessages = function() { | ||
export const getAllNewMessages = function () { | ||
return JSON.stringify( | ||
getAllChatsWithNewMessages() | ||
.map(c => WAPI.getChat(c.id._serialized)) | ||
.map(c => c.msgs._models.filter(x => x.isNewMsg)) || [] | ||
.map((c) => WAPI.getChat(c.id._serialized)) | ||
.map((c) => c.msgs._models.filter((x) => x.isNewMsg)) || [] | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getAllChatsWithNewMessages } from './get-chats-with-new-messages'; | ||
|
||
/** | ||
* Retrieves undread messages | ||
* x.ack === -1 | ||
* TODO: Test this fn, seems incorrect, should not be async | ||
*/ | ||
export const getAllUnreadMessages = async function () { | ||
return JSON.stringify( | ||
getAllChatsWithNewMessages() | ||
.map((c) => WAPI.getChat(c.id._serialized)) | ||
.map((c) => c.msgs._models.filter((x) => x.ack === -1)) | ||
.flatMap((x) => x) || [] | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Retrieves a chat by given id | ||
* @param {string} id | ||
* @param {Function} done optional callback | ||
*/ | ||
export function getChatById(id, done) { | ||
let found = WAPI.getChat(id); | ||
if (found) { | ||
found = WAPI._serializeChatObj(found); | ||
} else { | ||
found = false; | ||
} | ||
|
||
if (done !== undefined) done(found); | ||
return found; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Retrieves chat by its name | ||
* @param {string} name Chat name | ||
* @param {Function} done callback | ||
*/ | ||
export function getChatByName(name, done) { | ||
const found = window.Store.Chat.find((chat) => chat.name === name); | ||
if (done !== undefined) done(found); | ||
return found; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Retrieves chat by its id | ||
* @param {*} id Id of the chat | ||
* @param {*} done Callback | ||
* @returns {Chat} object | ||
*/ | ||
export function getChat(id, done) { | ||
id = typeof id == 'string' ? id : id._serialized; | ||
const found = window.Store.Chat.get(id); | ||
if (found) | ||
found.sendMessage = found.sendMessage | ||
? found.sendMessage | ||
: function () { | ||
return window.Store.sendMessage.apply(this, arguments); | ||
}; | ||
if (done !== undefined) done(found); | ||
return found; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Generates group invite link | ||
* @param {string} chatId | ||
*/ | ||
export async function getGroupInviteLink(chatId) { | ||
var chat = Store.Chat.get(chatId); | ||
if (!chat.isGroup) return false; | ||
await Store.GroupInvite.queryGroupInviteCode(chat); | ||
return `https://chat.whatsapp.com/${chat.inviteCode}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function getNewId() { | ||
var text = ''; | ||
var possible = | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
|
||
for (var i = 0; i < 20; i++) | ||
text += possible.charAt(Math.floor(Math.random() * possible.length)); | ||
return text; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Retrieves satus | ||
* @param {string} to '[email protected]' | ||
* | ||
* TODO: Test this function | ||
*/ | ||
export async function getStatus(id) { | ||
return await Store.MyStatus.getStatus(id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Retrieves unread messages from chat and mark them as read as a regular UX | ||
* @param {string} id Chat id | ||
* @param {boolean} includeMe Include user client messages | ||
* @param {boolean} includeNotifications Include notifications | ||
* @param {Function} done | ||
*/ | ||
export function getUnreadMessagesInChat( | ||
id, | ||
includeMe, | ||
includeNotifications, | ||
done | ||
) { | ||
// get chat and its messages | ||
let chat = WAPI.getChat(id); | ||
let messages = chat.msgs._models; | ||
|
||
// initialize result list | ||
let output = []; | ||
|
||
// look for unread messages, newest is at the end of array | ||
for (let i = messages.length - 1; i >= 0; i--) { | ||
// system message: skip it | ||
if (i === 'remove') { | ||
continue; | ||
} | ||
|
||
// get message | ||
let messageObj = messages[i]; | ||
|
||
// found a read message: stop looking for others | ||
if ( | ||
typeof messageObj.isNewMsg !== 'boolean' || | ||
messageObj.isNewMsg === false | ||
) { | ||
continue; | ||
} else { | ||
messageObj.isNewMsg = false; | ||
// process it | ||
let message = WAPI.processMessageObj( | ||
messageObj, | ||
includeMe, | ||
includeNotifications | ||
); | ||
|
||
// save processed message on result list | ||
if (message) output.push(message); | ||
} | ||
} | ||
// callback was passed: run it | ||
if (done !== undefined) done(output); | ||
// return result list | ||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
export { sendMessage } from './send-message'; | ||
export { sendMessage2 } from './send-message2'; | ||
export { createGroup } from './create-group'; | ||
export { leaveGroup } from './leave-group'; | ||
export { getAllChats } from './get-all-chats'; | ||
export { getAllChatIds } from './get-all-chats-ids'; | ||
export { getAllContacts } from './get-all-contacts'; | ||
export { getMyContacts } from './get-my-contacts'; | ||
export { getAllNewMessages } from './get-all-new-messages'; | ||
export { getAllUnreadMessages } from './get-all-unread-messages'; | ||
export { getAllChatsWithNewMessages } from './get-chats-with-new-messages'; | ||
export { getContact } from './get-contact'; | ||
export { getAllChats } from './get-all-chats'; | ||
export { getMyContacts } from './get-my-contacts'; | ||
export { hasUndreadMessages } from './has-unread-messages'; | ||
export { getAllChatsWithNewMessages } from './get-chats-with-new-messages'; | ||
export { getAllChatIds } from './get-all-chats-ids'; | ||
export { getAllNewMessages } from './get-all-new-messages'; | ||
export { leaveGroup } from './leave-group'; | ||
export { sendMessage } from './send-message'; | ||
export { sendMessage2 } from './send-message2'; | ||
export { getAllChatsWithMessages } from './get-all-chats-with-messages'; | ||
export { getAllGroups } from './get-all-groups'; | ||
export { sendChatstate } from './send-chat-state'; | ||
export { getChat } from './get-chat'; | ||
export { getStatus } from './get-status'; | ||
export { getChatByName } from './get-chat-by-name'; | ||
export { sendMessageWithThumb } from './send-message-with-thumb'; | ||
export { getGroupInviteLink } from './get-group-invite-link'; | ||
export { getNewId } from './get-new-id'; | ||
export { getChatById } from './get-chat-by-id'; | ||
export { getUnreadMessagesInChat } from './get-unread-messages-in-chat'; | ||
export { processMessageObj } from './process-message-object'; | ||
export { loadChatEarlierMessages } from './load-earlier-chat-messages'; | ||
export { | ||
loadAllEarlierMessages, | ||
asyncLoadAllEarlierMessages, | ||
} from './load-all-earlier-chat-messages'; | ||
export { isLoggedIn } from './is-logged-in'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* @param {Function} done Optional callback | ||
* @returns {boolean} true if logged in, false otherwise | ||
*/ | ||
export function isLoggedIn(done) { | ||
// Contact always exists when logged in | ||
const isLogged = | ||
window.Store.Contact && window.Store.Contact.checksum !== undefined; | ||
|
||
if (done !== undefined) done(isLogged); | ||
return isLogged; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
/** | ||
* Leaves group | ||
* @param {string} groupId The group id | ||
* @returns Promise | ||
*/ | ||
export function leaveGroup(groupId) { | ||
groupId = typeof groupId == "string" ? groupId : groupId._serialized; | ||
export async function leaveGroup(groupId) { | ||
groupId = typeof groupId == 'string' ? groupId : groupId._serialized; | ||
var group = WAPI.getChat(groupId); | ||
return group.sendExit(); | ||
}; | ||
return Store.GroupActions.sendExitGroup(group); | ||
} |
Oops, something went wrong.