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-02 07:17:02 +00:00
|
|
|
// TEST_MODE can be turned on or off to prevent communications with the Apollo iMessage Server running on your moder Mac
|
|
|
|
const TEST_MODE = true
|
|
|
|
|
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,
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021-11-27 07:45:53 +00:00
|
|
|
const MAX_WIDTH = 304
|
|
|
|
const SPACE_WIDTH = widthFor12ptFont[32]
|
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)
|
|
|
|
|
|
|
|
console.log(`chats`)
|
|
|
|
console.log(friendlyNameStrings)
|
|
|
|
|
|
|
|
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`},
|
|
|
|
{chatter: `me`, text: `https://github.com/CamHenlin/MessagesForMacintosh https://github.com/CamHenlin/MessagesForMacintosh`},
|
|
|
|
{chatter: `friend 3`, text: `i'm just catching up`},
|
|
|
|
{chatter: `friend 3`, text: `nobody chat for a minute`},
|
|
|
|
{chatter: `friend 2`, text: `hang on`},
|
|
|
|
{chatter: `friend 1`, text: `no`}
|
|
|
|
]
|
|
|
|
|
|
|
|
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`},
|
|
|
|
{friendlyName: `restaurant`, name: `restaurant`}
|
|
|
|
]
|
|
|
|
|
|
|
|
if (TEST_MODE) {
|
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
|
|
|
|
TEST_MESSAGES = TEST_MESSAGES.concat({chatter: `friend 1`, text: Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 64)})
|
|
|
|
}, 10000)
|
|
|
|
}
|
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
class iMessageClient {
|
|
|
|
|
|
|
|
async getMessages (chatId, page) {
|
|
|
|
|
2022-01-02 07:17:02 +00:00
|
|
|
if (TEST_MODE) {
|
|
|
|
|
|
|
|
return splitMessages(TEST_MESSAGES)
|
|
|
|
}
|
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
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) {
|
|
|
|
|
|
|
|
console.log(`error with apollo query`)
|
|
|
|
console.log(error)
|
|
|
|
|
|
|
|
result = {
|
|
|
|
data: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
return splitMessages(messages)
|
|
|
|
}
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
async hasNewMessagesInChat (chatId) {
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
let currentLastMessageOutput = `${lastMessageOutput}`
|
|
|
|
let messageOutput = await this.getMessages(chatId, 0)
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
return (currentLastMessageOutput !== messageOutput).toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
async hasNewMessages (chatId) {
|
|
|
|
|
|
|
|
let currentLastMessageOutput = `${lastMessageOutput}`
|
|
|
|
let messageOutput = await this.getChats(chatId, 0)
|
2021-09-21 06:10:57 +00:00
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
return (currentLastMessageOutput !== messageOutput).toString()
|
|
|
|
}
|
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
|
|
|
|
|
|
|
return splitMessages(TEST_MESSAGES)
|
|
|
|
}
|
|
|
|
|
2021-11-27 07:45:53 +00:00
|
|
|
console.log(`send message`)
|
|
|
|
console.log(chatId)
|
|
|
|
console.log(message)
|
|
|
|
|
2021-12-05 06:27:50 +00:00
|
|
|
let result
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
result = await client.query({
|
|
|
|
query: gql`query sendMessage {
|
|
|
|
sendMessage(chatId: "${chatId}", message: "${message}") {
|
|
|
|
chatter
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.log(`error with apollo query`)
|
|
|
|
console.log(error)
|
|
|
|
|
|
|
|
result = {
|
|
|
|
data: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2021-11-17 07:40:49 +00:00
|
|
|
return splitMessages(messages)
|
2021-09-21 06:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getChats () {
|
|
|
|
|
2022-01-02 07:17:02 +00:00
|
|
|
if (TEST_MODE) {
|
|
|
|
|
|
|
|
return parseChatsToFriendlyNameString(TEST_CHATS)
|
|
|
|
}
|
|
|
|
|
2021-12-05 06:27:50 +00:00
|
|
|
let result
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
result = await client.query({
|
|
|
|
query: gql`query getChats {
|
|
|
|
getChats {
|
|
|
|
name
|
|
|
|
friendlyName
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.log(`error with apollo query`)
|
|
|
|
console.log(error)
|
|
|
|
|
|
|
|
result = {
|
|
|
|
data: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 06:10:57 +00:00
|
|
|
|
|
|
|
let chats = result.data.getChats
|
|
|
|
|
2022-01-02 07:17:02 +00:00
|
|
|
return parseChatsToFriendlyNameString(chats)
|
2021-09-11 06:12:19 +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-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`
|
|
|
|
}
|
|
|
|
|
|
|
|
return `success`
|
|
|
|
}
|
2021-09-11 06:12:19 +00:00
|
|
|
}
|
2021-09-21 06:10:57 +00:00
|
|
|
|
|
|
|
module.exports = iMessageClient
|
|
|
|
|