2021-09-21 06:10:57 +00:00
|
|
|
require('cross-fetch/polyfill')
|
|
|
|
const ApolloClient = require('apollo-boost').ApolloClient;
|
|
|
|
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache;
|
|
|
|
const createHttpLink = require('apollo-link-http').createHttpLink;
|
|
|
|
const gql = require('graphql-tag')
|
|
|
|
|
2022-01-07 06:09:39 +00:00
|
|
|
// TEST_MODE can be turned on or off to prevent communications with the Apollo iMessage Server running on your modern Mac
|
2022-01-03 07:36:23 +00:00
|
|
|
const TEST_MODE = false
|
2022-01-26 07:54:08 +00:00
|
|
|
const DEBUG = false
|
2022-02-08 08:00:52 +00:00
|
|
|
let lastMessageFromSerialPortTime
|
2022-01-02 07:17:02 +00:00
|
|
|
|
2021-09-21 06:10:57 +00:00
|
|
|
const defaultOptions = {
|
|
|
|
watchQuery: {
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'ignore',
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
fetchPolicy: 'no-cache',
|
2021-10-14 06:16:11 +00:00
|
|
|
errorPolicy: 'ignore',
|
2021-09-21 06:10:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-14 06:16:11 +00:00
|
|
|
let client
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
const widthFor12ptFont = [
|
|
|
|
0,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
8,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
0,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
11,
|
|
|
|
11,
|
|
|
|
9,
|
|
|
|
11,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
4,
|
|
|
|
6,
|
|
|
|
7,
|
|
|
|
10,
|
|
|
|
7,
|
|
|
|
11,
|
|
|
|
10,
|
|
|
|
3,
|
|
|
|
5,
|
|
|
|
5,
|
|
|
|
7,
|
|
|
|
7,
|
|
|
|
4,
|
|
|
|
7,
|
|
|
|
4,
|
|
|
|
7,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
4,
|
|
|
|
4,
|
|
|
|
6,
|
|
|
|
8,
|
|
|
|
6,
|
|
|
|
8,
|
|
|
|
11,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
7,
|
|
|
|
7,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
6,
|
|
|
|
7,
|
|
|
|
9,
|
|
|
|
7,
|
|
|
|
12,
|
|
|
|
9,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
7,
|
|
|
|
6,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
12,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
5,
|
|
|
|
7,
|
|
|
|
5,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
6,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
7,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
6,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
4,
|
|
|
|
6,
|
|
|
|
8,
|
|
|
|
4,
|
|
|
|
12,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
6,
|
|
|
|
7,
|
|
|
|
6,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
12,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
8,
|
|
|
|
5,
|
|
|
|
5,
|
|
|
|
5,
|
|
|
|
8,
|
|
|
|
8
|
|
|
|
]
|
|
|
|
|
2022-01-07 06:09:39 +00:00
|
|
|
// this is tied to mac_main.c's message window max width
|
2021-11-27 07:45:53 +00:00
|
|
|
const MAX_WIDTH = 304
|
|
|
|
const SPACE_WIDTH = widthFor12ptFont[32]
|
2022-01-11 08:00:31 +00:00
|
|
|
let canStart = false
|
2022-01-26 07:54:08 +00:00
|
|
|
let hasNewMessages = false
|
2021-11-17 07:40:49 +00:00
|
|
|
|
|
|
|
const getNextWordLength = (word) => {
|
|
|
|
|
|
|
|
let currentWidth = 0
|
|
|
|
|
|
|
|
for (const char of word.split(``)) {
|
|
|
|
|
|
|
|
let currentCharWidth = widthFor12ptFont[char.charCodeAt()]
|
2021-11-27 07:45:53 +00:00
|
|
|
|
|
|
|
if (isNaN(currentCharWidth)) {
|
|
|
|
|
|
|
|
currentCharWidth = 1;
|
|
|
|
}
|
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
currentWidth += currentCharWidth
|
|
|
|
}
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2022-01-02 07:17:02 +00:00
|
|
|
return currentWidth
|
2021-11-17 07:40:49 +00:00
|
|
|
}
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
const shortenText = (text) => {
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
let outputText = ``
|
|
|
|
let currentWidth = 0
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
for (const word of text.split(` `)) {
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
let currentWordWidth = getNextWordLength(word)
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
if (currentWidth + currentWordWidth + SPACE_WIDTH > MAX_WIDTH) {
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
outputText = `${outputText}ENDLASTMESSAGE`
|
|
|
|
currentWidth = 0
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
// okay, but what if the word itself is greater than max width?
|
|
|
|
if (currentWordWidth > MAX_WIDTH) {
|
2021-11-17 07:40:49 +00:00
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
let splitWordWidth = 0
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
for (const char of word.split(``)) {
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
let currentCharWidth = widthFor12ptFont[char.charCodeAt()]
|
2021-10-14 06:16:11 +00:00
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
if (isNaN(currentCharWidth)) {
|
|
|
|
|
|
|
|
currentCharWidth = 1;
|
|
|
|
}
|
2021-10-14 06:16:11 +00:00
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
if (splitWordWidth + currentCharWidth > MAX_WIDTH) {
|
2021-11-17 07:40:49 +00:00
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
outputText = `${outputText}ENDLASTMESSAGE`
|
|
|
|
splitWordWidth = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
splitWordWidth += currentCharWidth
|
|
|
|
outputText = `${outputText}${char}`
|
2021-11-17 07:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
currentWidth += splitWordWidth
|
|
|
|
|
|
|
|
continue
|
2021-09-21 06:10:57 +00:00
|
|
|
}
|
2021-11-17 07:40:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
currentWidth += currentWordWidth + SPACE_WIDTH
|
2021-11-17 07:40:49 +00:00
|
|
|
outputText = `${outputText} ${word}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputText
|
|
|
|
}
|
|
|
|
|
2021-12-05 06:27:50 +00:00
|
|
|
const MAX_ROWS = 16
|
2021-11-27 07:45:53 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
const splitMessages = (messages) => {
|
|
|
|
|
|
|
|
let firstMessage = true
|
|
|
|
|
|
|
|
if (!messages) {
|
|
|
|
|
2021-12-05 06:27:50 +00:00
|
|
|
return `no messages ENDLASTMESSAGE`
|
2021-11-17 07:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const message of messages) {
|
|
|
|
|
|
|
|
if (firstMessage) {
|
|
|
|
|
|
|
|
let tempMessageOutput = `${message.chatter}: ${message.text}`
|
|
|
|
|
|
|
|
tempMessageOutput = shortenText(tempMessageOutput)
|
|
|
|
messageOutput = tempMessageOutput
|
|
|
|
} else {
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
let tempMessageOutput = `${message.chatter}: ${message.text}`
|
|
|
|
|
|
|
|
tempMessageOutput = shortenText(tempMessageOutput)
|
|
|
|
messageOutput = `${messageOutput}ENDLASTMESSAGE${tempMessageOutput}`
|
2021-09-21 06:10:57 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
firstMessage = false
|
2021-09-21 06:10:57 +00:00
|
|
|
}
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
|
|
|
|
if (messageOutput.split(`ENDLASTMESSAGE`).length > MAX_ROWS) {
|
|
|
|
|
|
|
|
messageOutput = messageOutput.split(`ENDLASTMESSAGE`)
|
|
|
|
|
|
|
|
let newMessageOutput = []
|
|
|
|
|
|
|
|
for (let i = messageOutput.length; i > messageOutput.length - MAX_ROWS; i--) {
|
|
|
|
|
|
|
|
newMessageOutput.unshift(messageOutput[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
messageOutput = newMessageOutput.join(`ENDLASTMESSAGE`)
|
|
|
|
}
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
lastMessageOutput = messageOutput
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
return messageOutput
|
|
|
|
}
|
|
|
|
|
2022-01-02 07:17:02 +00:00
|
|
|
const parseChatsToFriendlyNameString = (chats) => {
|
|
|
|
|
|
|
|
let friendlyNameStrings = ``
|
|
|
|
|
|
|
|
if (!chats) {
|
|
|
|
|
|
|
|
return ``
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let chat of chats) {
|
|
|
|
|
|
|
|
friendlyNameStrings = `${friendlyNameStrings},${chat.friendlyName.replace(/,/g, '')}`
|
|
|
|
}
|
|
|
|
|
|
|
|
friendlyNameStrings = friendlyNameStrings.substring(1, friendlyNameStrings.length)
|
|
|
|
|
|
|
|
return friendlyNameStrings
|
|
|
|
}
|
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
let lastMessageOutput
|
|
|
|
|
2022-01-03 06:00:22 +00:00
|
|
|
let TEST_MESSAGES = [
|
2022-01-02 07:17:02 +00:00
|
|
|
{chatter: `friend 1`, text: `my super fun text message`},
|
|
|
|
{chatter: `me`, text: `some cool old thing I said earlier`},
|
|
|
|
{chatter: `friend 2`, text: `this message is not relevant to the conversation! not at all :(`},
|
|
|
|
{chatter: `friend 1`, text: `i watch star wars in reverse order`},
|
2022-02-08 08:00:52 +00:00
|
|
|
{chatter: `me`, text: `https://github.com/CamHenlin/MessagesForMacintosh`},
|
|
|
|
{chatter: `friend 3`, text: `old computers are fun`}
|
2022-01-02 07:17:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
const TEST_CHATS = [
|
|
|
|
{friendlyName: `my group chat 1`, name: `my group chat 1`},
|
|
|
|
{friendlyName: `friend 1`, name: `friend 1`},
|
|
|
|
{friendlyName: `friend 4`, name: `friend 4`},
|
|
|
|
{friendlyName: `boss`, name: `boss`},
|
|
|
|
{friendlyName: `friend 3`, name: `friend 3`},
|
2022-02-08 08:00:52 +00:00
|
|
|
{friendlyName: `restaurant`, name: `restaurant`},
|
|
|
|
{friendlyName: `work`, name: `work`}
|
2022-01-02 07:17:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
if (TEST_MODE) {
|
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
// setInterval(() => {
|
2022-01-02 07:17:02 +00:00
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
// let chatWords = Math.floor(Math.random() * 20) + 1
|
|
|
|
// let chatMessage = ``
|
|
|
|
|
|
|
|
// for (let chatWord = 0; chatWord < chatWords; chatWord++) {
|
|
|
|
|
|
|
|
// chatMessage = `${chatMessage} ${Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, Math.floor(Math.random() * 16) + 1)}`
|
|
|
|
// }
|
|
|
|
|
|
|
|
// TEST_MESSAGES = TEST_MESSAGES.concat({chatter: `friend 1`, text: chatMessage})
|
|
|
|
// }, 10000)
|
2022-01-02 07:17:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
let storedArgsAndResults = {
|
|
|
|
getMessages: {
|
|
|
|
args: {},
|
|
|
|
output: {}
|
|
|
|
},
|
|
|
|
hasNewMessagesInChat: {
|
|
|
|
args: {},
|
|
|
|
output: {}
|
|
|
|
},
|
|
|
|
getChats: {
|
|
|
|
args: {},
|
|
|
|
output: {}
|
|
|
|
},
|
|
|
|
getChatCounts: {
|
|
|
|
args: {},
|
|
|
|
output: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is our private interface, meant to communicate with our GraphQL server and fill caches
|
|
|
|
// we want everything cached as much as possible to cut down on perceived perf issues on the
|
|
|
|
// classic Macintosh end
|
|
|
|
class iMessageGraphClientClass {
|
2021-11-17 07:40:49 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
async getMessages (chatId, page, fromInterval) {
|
2021-11-17 07:40:49 +00:00
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
storedArgsAndResults.getMessages.args = {
|
|
|
|
chatId,
|
|
|
|
page
|
|
|
|
}
|
|
|
|
|
2022-01-02 07:17:02 +00:00
|
|
|
if (TEST_MODE) {
|
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
let currentLastMessageOutput = `${lastMessageOutput}`
|
|
|
|
|
|
|
|
storedArgsAndResults.getMessages.output = splitMessages(TEST_MESSAGES)
|
|
|
|
|
|
|
|
if (!hasNewMessages && fromInterval) {
|
|
|
|
|
|
|
|
hasNewMessages = currentLastMessageOutput !== storedArgsAndResults.getMessages.output
|
|
|
|
}
|
|
|
|
|
|
|
|
return storedArgsAndResults.getMessages.output
|
2022-01-02 07:17:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
|
|
|
|
console.log(`get messages for chat ID: ${chatId}`)
|
|
|
|
}
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2021-12-05 06:27:50 +00:00
|
|
|
let result
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
result = await client.query({
|
|
|
|
query: gql`query getMessages {
|
|
|
|
getMessages(chatId: "${chatId}", page: "${page}") {
|
|
|
|
chatter
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
console.log(`getMessages: error with apollo query`)
|
2021-12-05 06:27:50 +00:00
|
|
|
console.log(error)
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
return
|
2021-12-05 06:27:50 +00:00
|
|
|
}
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
let messages = result.data.getMessages
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
let currentLastMessageOutput = `${lastMessageOutput}`
|
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
storedArgsAndResults.getMessages.output = splitMessages(messages)
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
if (!hasNewMessages && fromInterval) {
|
|
|
|
|
|
|
|
hasNewMessages = currentLastMessageOutput !== storedArgsAndResults.getMessages.output
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
if (hasNewMessages) {
|
|
|
|
|
|
|
|
console.log(`got new message. previous message was:`)
|
|
|
|
console.log(currentLastMessageOutput)
|
|
|
|
console.log(`new message set is:`)
|
|
|
|
console.log(storedArgsAndResults.getMessages.output)
|
|
|
|
}
|
2022-01-11 08:00:31 +00:00
|
|
|
}
|
2021-11-27 07:45:53 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
async hasNewMessagesInChat () {
|
|
|
|
|
|
|
|
if (!hasNewMessages) {
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
return `false`
|
|
|
|
} else {
|
|
|
|
|
|
|
|
hasNewMessages = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return `true`
|
2021-11-17 07:40:49 +00:00
|
|
|
}
|
2021-10-14 06:16:11 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
async sendMessage (chatId, message) {
|
2021-10-14 06:16:11 +00:00
|
|
|
|
2022-01-02 07:17:02 +00:00
|
|
|
if (TEST_MODE) {
|
|
|
|
|
2022-01-03 06:00:22 +00:00
|
|
|
TEST_MESSAGES = TEST_MESSAGES.concat({chatter: `me`, text: message})
|
2022-01-02 07:17:02 +00:00
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
storedArgsAndResults.getMessages.output = splitMessages(TEST_MESSAGES)
|
|
|
|
|
|
|
|
return storedArgsAndResults.getMessages.output
|
2022-01-02 07:17:02 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 06:27:50 +00:00
|
|
|
let result
|
|
|
|
|
|
|
|
try {
|
2022-01-26 07:54:08 +00:00
|
|
|
|
2022-02-05 07:30:50 +00:00
|
|
|
message = encodeURIComponent(message)
|
2022-01-26 07:54:08 +00:00
|
|
|
|
2021-12-05 06:27:50 +00:00
|
|
|
result = await client.query({
|
|
|
|
query: gql`query sendMessage {
|
|
|
|
sendMessage(chatId: "${chatId}", message: "${message}") {
|
|
|
|
chatter
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
console.log(`sendMessage: error with apollo query`)
|
2021-12-05 06:27:50 +00:00
|
|
|
console.log(error)
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
return
|
2021-12-05 06:27:50 +00:00
|
|
|
}
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
let messages = result.data.sendMessage
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2022-02-05 07:30:50 +00:00
|
|
|
storedArgsAndResults.getMessages.output = splitMessages(messages)
|
|
|
|
|
|
|
|
return storedArgsAndResults.getMessages.output
|
2021-09-21 06:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getChats () {
|
2022-01-11 08:00:31 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
|
|
|
|
if (DEBUG) {
|
|
|
|
|
|
|
|
console.log(`getChats`)
|
|
|
|
}
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2022-01-02 07:17:02 +00:00
|
|
|
if (TEST_MODE) {
|
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
storedArgsAndResults.getChats.output = parseChatsToFriendlyNameString(TEST_CHATS)
|
|
|
|
|
|
|
|
return
|
2022-01-02 07:17:02 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 06:27:50 +00:00
|
|
|
let result
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
result = await client.query({
|
|
|
|
query: gql`query getChats {
|
|
|
|
getChats {
|
|
|
|
name
|
|
|
|
friendlyName
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
console.log(`getChats: error with apollo query`)
|
2021-12-05 06:27:50 +00:00
|
|
|
console.log(error)
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
return
|
2021-12-05 06:27:50 +00:00
|
|
|
}
|
2021-09-21 06:10:57 +00:00
|
|
|
|
|
|
|
let chats = result.data.getChats
|
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
storedArgsAndResults.getChats.output = parseChatsToFriendlyNameString(chats)
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
|
|
|
|
console.log(`getChats complete`)
|
|
|
|
console.log(storedArgsAndResults.getChats.output)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2021-09-11 06:12:19 +00:00
|
|
|
}
|
2021-10-14 06:16:11 +00:00
|
|
|
|
2022-01-07 06:09:39 +00:00
|
|
|
async getChatCounts () {
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
|
|
|
|
console.log(`getChatCounts`)
|
|
|
|
}
|
2022-01-10 06:57:49 +00:00
|
|
|
|
2022-01-07 06:09:39 +00:00
|
|
|
if (TEST_MODE) {
|
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
storedArgsAndResults.getChatCounts.output = parseChatsToFriendlyNameString(TEST_CHATS)
|
|
|
|
|
|
|
|
return
|
2022-01-07 06:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let result
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
result = await client.query({
|
|
|
|
query: gql`query getChatCounts {
|
|
|
|
getChatCounts {
|
|
|
|
friendlyName
|
|
|
|
count
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
console.log(`getChatCounts: error with apollo query`)
|
2022-01-07 06:09:39 +00:00
|
|
|
console.log(error)
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
return
|
2022-01-07 06:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let chats = result.data.getChatCounts
|
|
|
|
|
|
|
|
if (!chats) {
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
return
|
2022-01-07 06:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let friendlyNameStrings = ``
|
|
|
|
|
|
|
|
if (chats.length === 0) {
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
return
|
2022-01-07 06:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let chat of chats) {
|
|
|
|
|
|
|
|
friendlyNameStrings = `${friendlyNameStrings},${chat.friendlyName.replace(/,/g, '')}:::${chat.count}`
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove trailing comma
|
|
|
|
friendlyNameStrings = friendlyNameStrings.substring(1, friendlyNameStrings.length)
|
2022-01-26 07:54:08 +00:00
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
storedArgsAndResults.getChatCounts.output = friendlyNameStrings
|
2022-01-26 07:54:08 +00:00
|
|
|
|
|
|
|
if (DEBUG) {
|
|
|
|
|
|
|
|
console.log(`got chat counts`)
|
|
|
|
console.log(friendlyNameStrings)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2022-01-07 06:09:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 06:16:11 +00:00
|
|
|
setIPAddress (IPAddress) {
|
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
console.log(`instantiate apolloclient with uri ${IPAddress}:4000/`)
|
|
|
|
|
2022-01-02 07:17:02 +00:00
|
|
|
if (TEST_MODE) {
|
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
canStart = true
|
|
|
|
|
2022-01-03 06:00:22 +00:00
|
|
|
return `success`
|
2022-01-02 07:17:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 06:16:11 +00:00
|
|
|
try {
|
|
|
|
|
|
|
|
client = new ApolloClient({
|
|
|
|
uri: `${IPAddress}:4000/`,
|
|
|
|
cache: new InMemoryCache(),
|
|
|
|
link: new createHttpLink({
|
|
|
|
uri: `${IPAddress}:4000/`
|
|
|
|
}),
|
|
|
|
defaultOptions
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
console.log(`error instantiating the ApolloClient`)
|
|
|
|
console.log(err)
|
|
|
|
|
|
|
|
return `failure`
|
|
|
|
}
|
|
|
|
|
2022-01-08 05:44:56 +00:00
|
|
|
console.log(`return success`)
|
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
canStart = true
|
|
|
|
|
2021-10-14 06:16:11 +00:00
|
|
|
return `success`
|
|
|
|
}
|
2021-09-11 06:12:19 +00:00
|
|
|
}
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
let iMessageGraphClient = new iMessageGraphClientClass()
|
|
|
|
|
|
|
|
// provide the public interface
|
|
|
|
class iMessageClient {
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
|
|
|
|
// kick off an update interval
|
2022-02-08 08:00:52 +00:00
|
|
|
const updateInterval = setInterval(async () => {
|
2022-01-11 08:00:31 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
let intervalDate = new Date().toISOString()
|
|
|
|
|
|
|
|
console.log(`${intervalDate}: run interval`)
|
2022-01-11 08:00:31 +00:00
|
|
|
|
|
|
|
if (!canStart) {
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
console.log(`${intervalDate}: can't start yet`)
|
2022-01-11 08:00:31 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2022-01-26 07:54:08 +00:00
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
if (new Date() - lastMessageFromSerialPortTime > 30000) {
|
|
|
|
|
|
|
|
console.log(`${intervalDate}: no serial comms for 30 seconds, unloading interval`)
|
|
|
|
|
|
|
|
clearInterval(updateInterval)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-05 07:30:50 +00:00
|
|
|
if (DEBUG) {
|
2022-01-26 07:54:08 +00:00
|
|
|
|
|
|
|
console.log(`${intervalDate}: running...`)
|
2022-02-05 07:30:50 +00:00
|
|
|
}
|
2022-01-26 07:54:08 +00:00
|
|
|
|
|
|
|
try {
|
2022-01-11 08:00:31 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
if (Object.keys(storedArgsAndResults.getMessages.args).length > 0) {
|
|
|
|
|
|
|
|
console.log(`${intervalDate}: interval: get messages for ${storedArgsAndResults.getMessages.args.chatId}`)
|
|
|
|
await iMessageGraphClient.getMessages(storedArgsAndResults.getMessages.args.chatId, storedArgsAndResults.getMessages.args.page, true)
|
|
|
|
}
|
2022-01-11 08:00:31 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
console.log(`${intervalDate}: interval: getchats`)
|
|
|
|
await iMessageGraphClient.getChats()
|
|
|
|
console.log(`${intervalDate}: interval: getchatcounts`)
|
|
|
|
await iMessageGraphClient.getChatCounts()
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.log(`${intervalDate}: caught error when running interval`)
|
|
|
|
console.log(error)
|
2022-01-11 08:00:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 07:30:50 +00:00
|
|
|
if (DEBUG) {
|
2022-01-26 07:54:08 +00:00
|
|
|
|
|
|
|
console.log(`${intervalDate}: complete!`)
|
2022-02-05 07:30:50 +00:00
|
|
|
}
|
2022-01-26 07:54:08 +00:00
|
|
|
}, 3000)
|
2022-01-11 08:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getMessages (chatId, page) {
|
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
lastMessageFromSerialPortTime = new Date()
|
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
console.log(`iMessageClient.getMessages`)
|
|
|
|
|
|
|
|
if (storedArgsAndResults.getMessages.args.chatId !== chatId || storedArgsAndResults.getMessages.args.page !== page) {
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
await iMessageGraphClient.getMessages(chatId, page, false)
|
2022-01-11 08:00:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
console.log(`iMessageClient.getMessages, return:`)
|
|
|
|
console.log(storedArgsAndResults.getMessages.output)
|
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
return storedArgsAndResults.getMessages.output
|
|
|
|
}
|
|
|
|
|
|
|
|
async hasNewMessagesInChat (chatId) {
|
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
lastMessageFromSerialPortTime = new Date()
|
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
console.log(`iMessageClient.hasNewMessagesInChat`)
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
let returnValue = await iMessageGraphClient.hasNewMessagesInChat(chatId)
|
2022-01-11 08:00:31 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
console.log(`iMessageClient.hasNewMessagesInChat, return:`)
|
|
|
|
console.log(returnValue)
|
2022-01-11 08:00:31 +00:00
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
return returnValue
|
2022-01-11 08:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async sendMessage (chatId, message) {
|
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
lastMessageFromSerialPortTime = new Date()
|
|
|
|
|
2022-02-05 07:30:50 +00:00
|
|
|
console.log(`iMessageClient.sendMessage(${chatId}, ${message})`)
|
|
|
|
|
|
|
|
const messages = await iMessageGraphClient.sendMessage(chatId, message)
|
2022-01-11 08:00:31 +00:00
|
|
|
|
2022-02-05 07:30:50 +00:00
|
|
|
return messages
|
2022-01-11 08:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getChats () {
|
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
lastMessageFromSerialPortTime = new Date()
|
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
console.log(`iMessageClient.getChats`)
|
|
|
|
|
|
|
|
if (Object.keys(storedArgsAndResults.getChats.output).length === 0) {
|
|
|
|
|
|
|
|
await iMessageGraphClient.getChats()
|
|
|
|
}
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
console.log(`iMessageClient.getChats, return:`)
|
|
|
|
console.log(storedArgsAndResults.getChats.output)
|
|
|
|
|
2022-01-11 08:00:31 +00:00
|
|
|
return storedArgsAndResults.getChats.output
|
|
|
|
}
|
|
|
|
|
|
|
|
getChatCounts () {
|
|
|
|
|
2022-02-08 08:00:52 +00:00
|
|
|
lastMessageFromSerialPortTime = new Date()
|
|
|
|
|
2022-01-26 07:54:08 +00:00
|
|
|
console.log(`iMessageClient.getChatCounts, prestored return:`)
|
|
|
|
console.log(storedArgsAndResults.getChatCounts.output)
|
2022-01-11 08:00:31 +00:00
|
|
|
|
|
|
|
return storedArgsAndResults.getChatCounts.output
|
|
|
|
}
|
|
|
|
|
|
|
|
setIPAddress (IPAddress) {
|
|
|
|
|
|
|
|
console.log(`iMessageClient.setIPAddress`)
|
|
|
|
|
|
|
|
return iMessageGraphClient.setIPAddress(IPAddress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 06:10:57 +00:00
|
|
|
module.exports = iMessageClient
|
|
|
|
|