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')
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
class iMessageClient {
|
|
|
|
|
|
|
|
async getMessages (chatId, page) {
|
|
|
|
|
2021-10-14 06:16:11 +00:00
|
|
|
console.log(`get messages for chat ID: ${chatId}`)
|
2021-09-21 06:10:57 +00:00
|
|
|
|
|
|
|
let result = await client.query({
|
|
|
|
query: gql`query getMessages {
|
|
|
|
getMessages(chatId: "${chatId}", page: "${page}") {
|
|
|
|
chatter
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
})
|
|
|
|
|
|
|
|
let messages = result.data.getMessages
|
|
|
|
let messageOutput = ``
|
|
|
|
const maxPerLine = 20000
|
|
|
|
console.log(`return messages from get messages:`)
|
|
|
|
console.log(messages)
|
|
|
|
|
|
|
|
let firstMessage = true
|
|
|
|
|
|
|
|
if (!messages) {
|
|
|
|
|
|
|
|
return ``
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const message of messages) {
|
|
|
|
|
|
|
|
if (firstMessage) {
|
2021-10-14 06:16:11 +00:00
|
|
|
|
2021-09-21 06:10:57 +00:00
|
|
|
messageOutput = `${message.chatter}: ${message.text}`
|
|
|
|
} else {
|
2021-10-14 06:16:11 +00:00
|
|
|
|
2021-09-21 06:10:57 +00:00
|
|
|
messageOutput = `${messageOutput}ENDLASTMESSAGE${message.chatter}: ${message.text}`
|
|
|
|
}
|
|
|
|
|
|
|
|
firstMessage = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return messageOutput
|
|
|
|
}
|
2021-09-11 06:12:19 +00:00
|
|
|
|
|
|
|
|
2021-09-21 06:10:57 +00:00
|
|
|
async sendMessage (chatId, message) {
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2021-10-14 06:16:11 +00:00
|
|
|
console.log(`send messages for chat ID: ${chatId} ${message}`)
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2021-09-21 06:10:57 +00:00
|
|
|
let result = await client.query({
|
|
|
|
query: gql`query sendMessage {
|
|
|
|
sendMessage(chatId: "${chatId}", message: "${message}") {
|
|
|
|
chatter
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
})
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2021-10-14 06:16:11 +00:00
|
|
|
let messages = result.data.sendMessage
|
2021-09-21 06:10:57 +00:00
|
|
|
let messageOutput = ``
|
|
|
|
const maxPerLine = 20000
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2021-09-21 06:10:57 +00:00
|
|
|
let firstMessage = true
|
2021-09-11 06:12:19 +00:00
|
|
|
|
2021-09-21 06:10:57 +00:00
|
|
|
if (!messages) {
|
|
|
|
|
|
|
|
return ``
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const message of messages) {
|
|
|
|
|
|
|
|
if (firstMessage) {
|
2021-10-14 06:16:11 +00:00
|
|
|
|
|
|
|
messageOutput = `${message.chatter}: ${message.text}`
|
2021-09-21 06:10:57 +00:00
|
|
|
} else {
|
2021-10-14 06:16:11 +00:00
|
|
|
|
|
|
|
messageOutput = `${messageOutput}ENDLASTMESSAGE${message.chatter}: ${message.text}`
|
2021-09-21 06:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
firstMessage = false
|
2021-09-11 06:12:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 06:10:57 +00:00
|
|
|
return messageOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
async getChats () {
|
|
|
|
|
|
|
|
let result = await client.query({
|
|
|
|
query: gql`query getChats {
|
|
|
|
getChats {
|
|
|
|
name
|
|
|
|
friendlyName
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
})
|
|
|
|
|
|
|
|
let chats = result.data.getChats
|
|
|
|
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-09-11 06:12:19 +00:00
|
|
|
}
|
2021-10-14 06:16:11 +00:00
|
|
|
|
|
|
|
setIPAddress (IPAddress) {
|
|
|
|
|
|
|
|
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
|
|
|
|
|