From e696fe9684a69d55f817f5e594e783fff37e2237 Mon Sep 17 00:00:00 2001 From: bee! Date: Wed, 23 Mar 2022 14:27:45 -0700 Subject: [PATCH 01/11] fix typos --- dataManager.js | 2 +- index.js | 4 ++-- package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dataManager.js b/dataManager.js index 56ef124..8901da7 100644 --- a/dataManager.js +++ b/dataManager.js @@ -1,5 +1,5 @@ const axios = require('axios') -const { Config, System, Util } = require('SimplyAPI') +const { Config, System, Util } = require('simplyapi') const pkUrl = Config.pk_url const pkHeader = { diff --git a/index.js b/index.js index db416bd..60a0c78 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,8 @@ const dotenv = require('dotenv') dotenv.config() -const { Config, System } = require('SimplyAPI') -const { Util } = require('SimplyAPI') +const { Config, System } = require('simplyapi') +const { Util } = require('simplyapi') const { initializeCache, determineAction, insertFront, removeFront, updateCustomStatus } = require('./dataManager') let e diff --git a/package.json b/package.json index 5cf0e9c..f3ac2a4 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "lodash.isequal": "^4.5.0", "lodash.isobject": "^3.0.2", "lodash.transform": "^4.6.0", - "simplyapi": "^0.1.1", + "simplyapi": "^0.1.2", "ws": "^8.5.0" }, "optionalDependencies": { From 7df8eedbea9c995113438246d83d2b4df56622d7 Mon Sep 17 00:00:00 2001 From: bee! Date: Wed, 23 Mar 2022 15:05:20 -0700 Subject: [PATCH 02/11] fix api to the current subdomain of the rewrite --- .env_example | 4 ++-- README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.env_example b/.env_example index 44e4dda..decde22 100644 --- a/.env_example +++ b/.env_example @@ -1,7 +1,7 @@ # Compatiplural -url_override="https://api.apparyllis.com" +url_override="https://v2.apparyllis.com" api_version="v1" -socket="wss://api.apparyllis.com/v1/socket" +socket="wss://v2.apparyllis.com/v1/socket" pk_url="https://api.pluralkit.me/v2" token="AAAAAAAAAAAAAAAAAAAA" userId="AAAAAAAAAAAAAAAAAAA" diff --git a/README.md b/README.md index 246a115..537fac0 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ This project already has a Procfile set up, so it's super easy to get started. O These can be set either in the .env file, in terminal, or in the config vars section of Heroku. | Setting | Default | Description | | ---------| ------- | ------------------ | -| url_override | https://api.apparyllis.com | The base URL for all SimplyPlural API requests. Unless you are running your own fork of Simply Plural, you shouldn't change this. | +| url_override | https://v2.apparyllis.com | The base URL for all SimplyPlural API requests. Unless you are running your own fork of Simply Plural, you shouldn't change this. | | api_version | v1 | The target SimplyPlural API version. Unless you are running your own fork of Simply Plural, you shouldn't change this. | -| socket | wss://api.apparyllis.com/v1/socket | The socket URL for SimplyPlural. Unless you are running your own fork of Simply Plural, you shouldn't change this. | +| socket | wss://v2.apparyllis.com/v1/socket | The socket URL for SimplyPlural. Unless you are running your own fork of Simply Plural, you shouldn't change this. | | pk_url | https://api.pluralkit.me/v2 | The base URL for all PluralKit API requests. Unless you are running your own fork of PluralKit, you shouldn't change this. | | token | token_here | Your SimplyPlural account token. As of now, the only permission necessary is the Read permission. | | userId | user_id | Your SimplyPlural account/system ID. You can find it in account info near the bottom. | From 4728714d72842ec167ee0cddd2041ada10530245 Mon Sep 17 00:00:00 2001 From: bee! Date: Mon, 9 May 2022 08:07:44 -0700 Subject: [PATCH 03/11] queue system to fix data malformation with rapid events --- .env_example | 14 ++--- README.md | 6 +-- dataManager.js | 144 +++++++++++++++++++++++++++---------------------- index.js | 53 ++++++++++++++++-- package.json | 3 +- 5 files changed, 139 insertions(+), 81 deletions(-) diff --git a/.env_example b/.env_example index 44e4dda..c83e345 100644 --- a/.env_example +++ b/.env_example @@ -1,10 +1,10 @@ # Compatiplural -url_override="https://api.apparyllis.com" +url_override="https://v2.apparyllis.com" api_version="v1" -socket="wss://api.apparyllis.com/v1/socket" +socket="wss://v2.apparyllis.com/v1/socket" pk_url="https://api.pluralkit.me/v2" -token="AAAAAAAAAAAAAAAAAAAA" -userId="AAAAAAAAAAAAAAAAAAA" -pk_token= "AAAAAAAAAAAAAAAA" -pk_system="AAAAAAAAAAAAAAAA" -heartbeat=4500000 \ No newline at end of file +token="SIMPLYPLURAL_TOKEN" +userId="abcd1234" +pk_token= "PLURALKIT_TOKEN" +heartbeat=4500000 +max_workers=1 \ No newline at end of file diff --git a/README.md b/README.md index 246a115..aed59b5 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,12 @@ This project already has a Procfile set up, so it's super easy to get started. O These can be set either in the .env file, in terminal, or in the config vars section of Heroku. | Setting | Default | Description | | ---------| ------- | ------------------ | -| url_override | https://api.apparyllis.com | The base URL for all SimplyPlural API requests. Unless you are running your own fork of Simply Plural, you shouldn't change this. | +| url_override | https://v2.apparyllis.com | The base URL for all SimplyPlural API requests. Unless you are running your own fork of Simply Plural, you shouldn't change this. | | api_version | v1 | The target SimplyPlural API version. Unless you are running your own fork of Simply Plural, you shouldn't change this. | -| socket | wss://api.apparyllis.com/v1/socket | The socket URL for SimplyPlural. Unless you are running your own fork of Simply Plural, you shouldn't change this. | +| socket | wss://v2.apparyllis.com/v1/socket | The socket URL for SimplyPlural. Unless you are running your own fork of Simply Plural, you shouldn't change this. | | pk_url | https://api.pluralkit.me/v2 | The base URL for all PluralKit API requests. Unless you are running your own fork of PluralKit, you shouldn't change this. | | token | token_here | Your SimplyPlural account token. As of now, the only permission necessary is the Read permission. | | userId | user_id | Your SimplyPlural account/system ID. You can find it in account info near the bottom. | | pk_token | pluralkit_token_here | Your PluralKit token. Get it by using `pk;token`. | -| pk_system | pluralkit_system_id | Your Pluralkit system ID. This can be either your Discord account ID or your 5 letter ID shown by using pk;system. | | heartbeat | 4500000 | The time in miliseconds before the websocket client reconnects to the websocket server. | +| max_workers | 1 | Max number of workers for processing enqueued tasks. This probably shouldn't be changed. | diff --git a/dataManager.js b/dataManager.js index 8901da7..abaf52d 100644 --- a/dataManager.js +++ b/dataManager.js @@ -3,7 +3,7 @@ const { Config, System, Util } = require('simplyapi') const pkUrl = Config.pk_url const pkHeader = { - 'Content-Type': 'application/json', + 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': Config.pk_token } @@ -13,10 +13,6 @@ async function initializeCache() { cache.frontHistory = await system.getFronters() } -function unknownError400() { - return -} - function unknownTarget(target) { console.log('::SimplyWS:: Unknown update target: ' + target + '\n::SimplyWS:: Full message: ' + e) } @@ -25,15 +21,9 @@ function unrecognizedMessage(msg) { console.log('::SimplyWS:: Unrecognized message: ' + msg + '\n::SimplyWS:: Full message: ' + e) } -// async function asyncForEach(array, callback) { -// for (let index = 0; index < array.length; index++) { -// await callback(array[index], index, array) -// } -// } - async function getPKFronters() { let members = [] - let fronters = await axios.get(`${pkUrl}/systems/${Config.pk_system}/fronters`, { + let fronters = await axios.get(`${pkUrl}/systems/@me/fronters`, { headers: pkHeader }) .catch((err) => { @@ -118,9 +108,13 @@ async function determineAction(eventData, frontData = []) { async function insertFront(member) { // get current fronters and add new fronter - let system = new System(Config) let fronters = await getPKFronters() - fronters.push(member.content.pkId) + if (!fronters.includes(member.content.pkId)) { + fronters.push(member.content.pkId) + } else { + console.warn('::SimplyWS:: Member already in fronters: ' + member.content.pkId) + return + } // find the "primary" fronter to move to the first element in the list let primary = await findPrimary() @@ -131,38 +125,57 @@ async function insertFront(member) { } } - // cache front - cache.frontHistory = await system.getFronters() - - // post the new switch - axios.post(`${pkUrl}/systems/${Config.pk_system}/switches`, JSON.stringify({ "members": fronters }), { + // post the new switch + let url = `${pkUrl}/systems/@me/switches` + await axios.post(url, JSON.stringify({ "members": fronters }), { headers: pkHeader }) - .catch(err => { - if (err.toJSON().status == 400) - unknownError400() - else if (err.toJSON().status == 429) + .then(async (res) => { + let front = await getPKFronters() + if (!front.includes(member.content.pkId)) { + console.log('::SimplyWS:: Failed to insert fronter: ' + member.content.pkId) + await insertFront(member) + return + } else { + console.log('::SimplyWS:: ' + member.content.name + ' was added to the front.') + } + }) + .catch(async err => { + let status = err.status || err.toJSON().status + if (status == 400) { + // if the fronter is already in the front, do nothing + return + } + else if (status == 404) { + // member not found + console.error("::SimplyWS:: Could not find member: " + member.content.pkId) + let index = fronters.indexOf(member.content.pkId) + fronters.splice(index, 1) + return + } + else if (status == 429) { // Too many requests - setTimeout(function () { - insertFront(member) + console.warn("::SimplyWS:: Too many requests, waiting to try again.") + let index = fronters.indexOf(member.content.pkId) + fronters.splice(index, 1) + setTimeout(async function () { + await insertFront(member) }, 1000) return + } }) - - let checkFront = await getPKFronters() - if (!checkFront.includes(member.content.pkId)) { - await insertFront(member) - return - } else { - console.log('::SimplyWS:: ' + member.content.name + ' was added to the front.') - } } async function removeFront(member) { - let system = new System(Config) let fronters = await getPKFronters() - let index = fronters.indexOf(member.content.pkId) - fronters.splice(index, 1) + + if (fronters.includes(member.content.pkId)) { + let index = fronters.indexOf(member.content.pkId) + fronters.splice(index, 1) + } else { + console.warn('::SimplyWS:: Member is not in front: ' + member.content.pkId) + return + } // find the "primary" fronter to move to the first element in the list let p = await findPrimary() @@ -173,31 +186,37 @@ async function removeFront(member) { } } - // cache front - cache.frontHistory = await system.getFronters() - - // post the new switch - axios.post(`${pkUrl}/systems/${Config.pk_system}/switches`, JSON.stringify({ "members": fronters }), { + let url = `${pkUrl}/systems/@me/switches` + await axios.post(url, JSON.stringify({ "members": fronters }), { headers: pkHeader }) - .catch(err => { - if (err.toJSON().status == 400) - unknownError400() - else if (err.toJSON().status == 429) + .then(async (res) => { + let front = await getPKFronters() + if (front.includes(member.content.pkId)) { + console.log('::SimplyWS:: Failed to remove fronter: ' + member.content.pkId) + await removeFront(member) + return + } else { + console.log('::SimplyWS:: ' + member.content.name + ' was removed from the front.') + } + }) + .catch(async err => { + let status = err.status || err.toJSON().status + if (status == 400) { + // fronter is already not in front + console.warn("::SimplyWS:: " + member.content.name + " is not in the front.") + return + } + else if (status == 429) { // Too many requests - setTimeout(function () { - removeFront(member) + console.warn("::SimplyWS:: Too many requests, waiting to try again.") + fronters.push(member.content.pkId) + setTimeout(async function () { + await removeFront(member) }, 1000) return + } }) - - let checkFront = await getPKFronters() - if (checkFront.includes(member.content.pkId)) { - await removeFront(member) - return - } else { - console.log('::SimplyWS:: ' + member.content.name + ' was removed from the front.') - } } async function updateCustomStatus(member) { @@ -210,20 +229,18 @@ async function updateCustomStatus(member) { fronters.splice(fronters.indexOf(primary), 1) fronters.unshift(primary) - // cache front - cache.frontHistory = await system.getFronters() - // post the new switch - axios.post(`${pkUrl}/systems/${Config.pk_system}/switches`, JSON.stringify({ "members": fronters }), { + axios.post(`${pkUrl}/systems/@me/switches`, JSON.stringify({ "members": fronters }), { headers: pkHeader }) - .catch(err => { + .catch(async err => { if (err.toJSON().status == 400) unknownError400() else if (err.toJSON().status == 429) - // Too many requests + //Too many requests + console.warn("::SimplyWS:: Too many requests, waiting to try again.") setTimeout(function () { - updateCustomStatus(member) + await updateCustomStatus(member) }, 1000) return }) @@ -236,12 +253,10 @@ async function updateCustomStatus(member) { } } -const { inspect } = require('util') const transform = require('lodash.transform') const isEqual = require('lodash.isequal') const isArray = require('lodash.isarray') const isObject = require('lodash.isobject') -const { PassThrough } = require('stream') async function calculateDiff(origObj, newObj) { return new Promise(function (resolve) { changes = (newObj, origObj) => { @@ -259,7 +274,6 @@ async function calculateDiff(origObj, newObj) { module.exports = { initializeCache, - unknownError400, unknownTarget, unrecognizedMessage, getPKFronters, diff --git a/index.js b/index.js index 60a0c78..21887d2 100644 --- a/index.js +++ b/index.js @@ -5,19 +5,60 @@ const { Config, System } = require('simplyapi') const { Util } = require('simplyapi') const { initializeCache, determineAction, insertFront, removeFront, updateCustomStatus } = require('./dataManager') +const { + isMainThread, + BroadcastChannel, + Worker +} = require('node:worker_threads') + let e -main = async () => { - openWebSocket() +main = () => { + initiateWorkerPool() } -openWebSocket = async () => { +// Queue +const async = require('async') +const queue = async.queue((task, completed) => { + let error = { status: false, message: '' } + update(task.data) + .catch(err => { + error.status = true + error.message = err + }) + completed(error, task) + +}, Config.max_workers) + +initiateWorkerPool = () => { + // Worker Pool + const bc = new BroadcastChannel('plural') + + if (isMainThread) { + openWebSocket() + + bc.onmessage = (event) => { + //console.log('::SimplyWS:: received message from worker') + queue.push(event.data, (error, task) => { + if (error.status) { + console.log(`An error occurred while processing task ${error.message}`) + } + }) + } + for (let n = 0; n < Config.max_workers; n++) + new Worker(__filename) + } +} + +// Socket +openWebSocket = () => { const WebSocketClient = require('./WebsocketClient') - const wss = new WebSocketClient(Config.socket); + const wss = new WebSocketClient(Config.socket) let initialPacket = { "op": "authenticate", "token": Config.token } wss.onOpen = (_) => { wss.send(JSON.stringify(initialPacket)); } wss.onClose = (e) => { console.log('SimplyWS/onClose :: %s', e); e = '' } wss.onError = (e) => { console.log('SimplyWS/onError :: %s', e) } + const bc = new BroadcastChannel('plural') wss.onMessage = (raw) => { e = raw let data = JSON.parse(e) @@ -33,7 +74,8 @@ openWebSocket = async () => { console.log('::SimplyWS:: invalid token, exiting..') process.exit(1) case "update": - update(data) + initializeCache() + bc.postMessage({data: data}) break default: //unrecognizedMessage(data.msg) @@ -42,6 +84,7 @@ openWebSocket = async () => { } } +// Data Processing update = async (data) => { let target = data.target switch (target) { diff --git a/package.json b/package.json index f3ac2a4..7f9115a 100644 --- a/package.json +++ b/package.json @@ -12,13 +12,14 @@ "repository": "github:padlocks/Compatiplural", "dependencies": { "ajv": "^8.10.0", + "async": "^3.2.3", "axios": "^0.26.0", "dotenv": "^16.0.0", "lodash.isarray": "^4.0.0", "lodash.isequal": "^4.5.0", "lodash.isobject": "^3.0.2", "lodash.transform": "^4.6.0", - "simplyapi": "^0.1.2", + "simplyapi": "^0.1.3", "ws": "^8.5.0" }, "optionalDependencies": { From 7e34599116149bcc4e487c5fb7f3aac60b45f48e Mon Sep 17 00:00:00 2001 From: bee! Date: Mon, 9 May 2022 22:49:16 -0700 Subject: [PATCH 04/11] fixes and update simplyapi --- dataManager.js | 5 ++--- package.json | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/dataManager.js b/dataManager.js index abaf52d..0b8ba3a 100644 --- a/dataManager.js +++ b/dataManager.js @@ -240,7 +240,7 @@ async function updateCustomStatus(member) { //Too many requests console.warn("::SimplyWS:: Too many requests, waiting to try again.") setTimeout(function () { - await updateCustomStatus(member) + updateCustomStatus(member) }, 1000) return }) @@ -255,7 +255,6 @@ async function updateCustomStatus(member) { const transform = require('lodash.transform') const isEqual = require('lodash.isequal') -const isArray = require('lodash.isarray') const isObject = require('lodash.isobject') async function calculateDiff(origObj, newObj) { return new Promise(function (resolve) { @@ -263,7 +262,7 @@ async function calculateDiff(origObj, newObj) { let arrayIndexCounter = 0 return transform(newObj, function (result, value, key) { if (!isEqual(value, origObj[key])) { - let resultKey = isArray(origObj) ? arrayIndexCounter++ : key + let resultKey = Array.isArray(origObj) ? arrayIndexCounter++ : key result[resultKey] = (isObject(value) && isObject(origObj[key])) ? changes(value, origObj[key]) : value } }) diff --git a/package.json b/package.json index 7f9115a..1b81ed4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "Compatiplural", - "version": "1.0.0", + "version": "1.1.0", "description": "SimplyPlural -> PluralKit Connectivity", "main": "index.js", "scripts": { @@ -15,11 +15,10 @@ "async": "^3.2.3", "axios": "^0.26.0", "dotenv": "^16.0.0", - "lodash.isarray": "^4.0.0", "lodash.isequal": "^4.5.0", "lodash.isobject": "^3.0.2", "lodash.transform": "^4.6.0", - "simplyapi": "^0.1.3", + "simplyapi": "^0.1.4", "ws": "^8.5.0" }, "optionalDependencies": { From 9d54038ec715b9262a6a5d06de9f76239cfd2895 Mon Sep 17 00:00:00 2001 From: bee! Date: Fri, 13 May 2022 09:39:48 -0700 Subject: [PATCH 05/11] fix customStatus events, option to silence spam messages --- .env_example | 3 ++- README.md | 1 + WebsocketClient.js | 8 ++++---- dataManager.js | 7 ++----- index.js | 8 +++++--- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/.env_example b/.env_example index c83e345..e8c8531 100644 --- a/.env_example +++ b/.env_example @@ -7,4 +7,5 @@ token="SIMPLYPLURAL_TOKEN" userId="abcd1234" pk_token= "PLURALKIT_TOKEN" heartbeat=4500000 -max_workers=1 \ No newline at end of file +max_workers=1 +silence_connections=true \ No newline at end of file diff --git a/README.md b/README.md index aed59b5..d52522d 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,4 @@ These can be set either in the .env file, in terminal, or in the config vars sec | pk_token | pluralkit_token_here | Your PluralKit token. Get it by using `pk;token`. | | heartbeat | 4500000 | The time in miliseconds before the websocket client reconnects to the websocket server. | | max_workers | 1 | Max number of workers for processing enqueued tasks. This probably shouldn't be changed. | +| silence_connections | true | Whether or not to silence the websocket connection and authentication messages. | diff --git a/WebsocketClient.js b/WebsocketClient.js index f10f9f5..bc40b9e 100644 --- a/WebsocketClient.js +++ b/WebsocketClient.js @@ -7,7 +7,7 @@ function WebSocketClient(url) { let connecting = false let backoff = 250 const init = () => { - console.error(`::SimplyWS:: [${timestamp()}] connecting`) + if (!process.env.silence_connections) console.error(`::SimplyWS:: [${timestamp()}] connecting`) connecting = false if (client !== undefined) { client.removeAllListeners() @@ -21,14 +21,14 @@ function WebSocketClient(url) { timeout = setTimeout(() => client.terminate(), process.env.heartbeat || 350000) } client.on('ping', () => { - console.log(`::SimplyWS:: [${timestamp()}] pinged`) + if (!process.env.silence_connections) console.log(`::SimplyWS:: [${timestamp()}] pinged`) heartbeat() }) client.on('open', (e) => { if (typeof this.onOpen === 'function') { this.onOpen() } else { - console.log(`::SimplyWS:: [${timestamp()}] opened`) + if (!process.env.silence_connections) console.log(`::SimplyWS:: [${timestamp()}] opened`) console.log(e) } heartbeat() @@ -37,7 +37,7 @@ function WebSocketClient(url) { if (typeof this.onMessage === 'function') { this.onMessage(e) } else { - console.log(`::SimplyWS:: [${timestamp()}] messaged`) + if (!process.env.silence_connections) console.log(`::SimplyWS:: [${timestamp()}] messaged`) } heartbeat() }) diff --git a/dataManager.js b/dataManager.js index 0b8ba3a..5e844b5 100644 --- a/dataManager.js +++ b/dataManager.js @@ -221,10 +221,9 @@ async function removeFront(member) { async function updateCustomStatus(member) { // find the "primary" fronter to move to the first element in the list - let system = new System(Config) let fronters = await getPKFronters() let primary = await findPrimary() - if (primary && fronters.length > 1) { + if (primary && fronters.length > 1 && (member.content.pkId == primary)) { if (fronters.indexOf(primary) >= 0) { fronters.splice(fronters.indexOf(primary), 1) fronters.unshift(primary) @@ -234,9 +233,7 @@ async function updateCustomStatus(member) { headers: pkHeader }) .catch(async err => { - if (err.toJSON().status == 400) - unknownError400() - else if (err.toJSON().status == 429) + if (err.toJSON().status == 429) //Too many requests console.warn("::SimplyWS:: Too many requests, waiting to try again.") setTimeout(function () { diff --git a/index.js b/index.js index 21887d2..ea23807 100644 --- a/index.js +++ b/index.js @@ -39,6 +39,7 @@ initiateWorkerPool = () => { bc.onmessage = (event) => { //console.log('::SimplyWS:: received message from worker') queue.push(event.data, (error, task) => { + // task completed if (error.status) { console.log(`An error occurred while processing task ${error.message}`) } @@ -59,6 +60,7 @@ openWebSocket = () => { wss.onError = (e) => { console.log('SimplyWS/onError :: %s', e) } const bc = new BroadcastChannel('plural') + let first_auth = true wss.onMessage = (raw) => { e = raw let data = JSON.parse(e) @@ -66,15 +68,15 @@ openWebSocket = () => { switch (data.msg) { case "Successfully authenticated": - console.log('::SimplyWS:: authenticated') + if (!process.env.silence_connections || first_auth) console.log('::SimplyWS:: authenticated') + first_auth = false // cache current front initializeCache() break case "Authentication violation: Token is missing or invalid. Goodbye :)": - console.log('::SimplyWS:: invalid token, exiting..') + console.error('::SimplyWS:: invalid token, exiting..') process.exit(1) case "update": - initializeCache() bc.postMessage({data: data}) break default: From 1564a110e02357cf1c4d26ab6b1fee6ac2506651 Mon Sep 17 00:00:00 2001 From: bee! Date: Sun, 24 Sep 2023 13:32:14 -0700 Subject: [PATCH 06/11] allow for publishing entire front instead Signed-off-by: bee! --- .env_example | 3 ++- README.md | 1 + dataManager.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 8 +++++-- 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/.env_example b/.env_example index e8c8531..894f144 100644 --- a/.env_example +++ b/.env_example @@ -8,4 +8,5 @@ userId="abcd1234" pk_token= "PLURALKIT_TOKEN" heartbeat=4500000 max_workers=1 -silence_connections=true \ No newline at end of file +silence_connections=true +full_swap=false \ No newline at end of file diff --git a/README.md b/README.md index d52522d..b4c458c 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,4 @@ These can be set either in the .env file, in terminal, or in the config vars sec | heartbeat | 4500000 | The time in miliseconds before the websocket client reconnects to the websocket server. | | max_workers | 1 | Max number of workers for processing enqueued tasks. This probably shouldn't be changed. | | silence_connections | true | Whether or not to silence the websocket connection and authentication messages. | +| full_swap | false | Determines whether to completely overwrite PluralKit's front with SimplyPlural's. No individual changes. | diff --git a/dataManager.js b/dataManager.js index 5e844b5..209cd5e 100644 --- a/dataManager.js +++ b/dataManager.js @@ -106,6 +106,69 @@ async function determineAction(eventData, frontData = []) { return action } +async function swapFront() { + let system = new System(Config) + let front = await system.getFronters() + + // start forming new front list + let newFront = [] + for (member of front) { + let m = await system.getMemberById(member.content.member) + + if (m.content.pkId) { + // fronting member pkID has been found + newFront.push(m.content.pkId) + } + } + + // shift primary fronter to first in list + let primary = await findPrimary() + if (primary) { + if (newFront.indexOf(primary) > 0) { + newFront.splice(newFront.indexOf(primary), 1) + newFront.unshift(primary) + } + } + + // post the new switch + let url = `${pkUrl}/systems/@me/switches` + await axios.post(url, JSON.stringify({ "members": newFront }), { + headers: pkHeader + }) + .then(async (res) => { + // check if current front equals the new front + let front = await getPKFronters() + var equal = (front.length == newFront.length) && front.every(function(element, index) { + return element === newFront[index]; + }) + if (!equal) { + console.log('::SimplyWS:: Failed to swap front: ' + newFront) + await swapFront() + return + } else { + console.log('::SimplyWS:: SP\'s front has been published to PK.') + } + }) + .catch(async err => { + let status = err.status || err.toJSON().status + if (status == 400) { + // if the fronter is already in the front, do nothing + return + } + else if (status == 404) { + return + } + else if (status == 429) { + // Too many requests + console.warn("::SimplyWS:: Too many requests, waiting to try again.") + setTimeout(async function () { + await swapFront() + }, 1000) + return + } + }) +} + async function insertFront(member) { // get current fronters and add new fronter let fronters = await getPKFronters() @@ -275,6 +338,7 @@ module.exports = { getPKFronters, findPrimary, determineAction, + swapFront, insertFront, removeFront, updateCustomStatus, diff --git a/index.js b/index.js index ea23807..9c82e08 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ dotenv.config() const { Config, System } = require('simplyapi') const { Util } = require('simplyapi') -const { initializeCache, determineAction, insertFront, removeFront, updateCustomStatus } = require('./dataManager') +const { initializeCache, determineAction, swapFront, insertFront, removeFront, updateCustomStatus } = require('./dataManager') const { isMainThread, @@ -95,8 +95,12 @@ update = async (data) => { await Util.asyncForEach(data.results, async (o) => { let system = new System(Config) let member = await system.getMemberById(o.content.member) + let swap = Config.full_swap // insert - if (o.operationType == "insert") { + if (swap) { + swapFront() + } + else if (o.operationType == "insert") { insertFront(member) } else { From 5e0fe8aa0ce6b91fad31fe3eff41b66314041892 Mon Sep 17 00:00:00 2001 From: bee! Date: Sun, 22 Oct 2023 16:31:10 -0700 Subject: [PATCH 07/11] docker: add dockerfile, format log --- .dockerignore | 2 ++ Dockerfile | 16 ++++++++++++++++ dataManager.js | 36 +++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..5171c54 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +node_modules +npm-debug.log \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1ece624 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM node:18 + +WORKDIR /usr/src/app + +COPY package*.json ./ +RUN npm install + +# If you are building your code for production +# RUN npm ci --omit=dev + +# Bundle app source +COPY . . + +#EXPOSE 8080 + +CMD [ "node", "index.js" ] \ No newline at end of file diff --git a/dataManager.js b/dataManager.js index 209cd5e..ec1cc5f 100644 --- a/dataManager.js +++ b/dataManager.js @@ -52,7 +52,7 @@ async function findPrimary() { if (fronter.content.customStatus) { if (fronter.content.customStatus.toLowerCase().includes("primary")) { let member = await system.getMemberById(fronter.content.member) - resolve(member.content.pkId) + resolve({ name: member.content.name, pkId: member.content.pkId }) found = true } } @@ -112,21 +112,30 @@ async function swapFront() { // start forming new front list let newFront = [] + let frontNames = [] for (member of front) { let m = await system.getMemberById(member.content.member) if (m.content.pkId) { // fronting member pkID has been found newFront.push(m.content.pkId) + frontNames.push(m.content.name) } } // shift primary fronter to first in list let primary = await findPrimary() - if (primary) { - if (newFront.indexOf(primary) > 0) { - newFront.splice(newFront.indexOf(primary), 1) - newFront.unshift(primary) + let primaryPK = primary.pkId + let primaryName = primary.name + if (primaryPK) { + if (newFront.indexOf(primaryPK) > 0) { + newFront.splice(newFront.indexOf(primaryPK), 1) + newFront.unshift(primaryPK) + } + + if (frontNames.indexOf(primaryName) > 0) { + frontNames.splice(frontNames.indexOf(primaryName), 1) + frontNames.unshift(primaryName) } } @@ -146,7 +155,8 @@ async function swapFront() { await swapFront() return } else { - console.log('::SimplyWS:: SP\'s front has been published to PK.') + let formattedNames = frontNames.toString().replace(',',', ') + console.log(`::SimplyWS:: SimplyPlural -> PluralKit: ${formattedNames}`) } }) .catch(async err => { @@ -180,7 +190,7 @@ async function insertFront(member) { } // find the "primary" fronter to move to the first element in the list - let primary = await findPrimary() + let primary = await findPrimary().pkId if (primary) { if (fronters.indexOf(primary) > 0) { fronters.splice(fronters.indexOf(primary), 1) @@ -241,11 +251,11 @@ async function removeFront(member) { } // find the "primary" fronter to move to the first element in the list - let p = await findPrimary() - if (p) { - if (fronters.indexOf(p) > 0) { - fronters.splice(fronters.indexOf(p), 1) - fronters.unshift(p) + let primary = await findPrimary().pkId + if (primary) { + if (fronters.indexOf(primary) > 0) { + fronters.splice(fronters.indexOf(primary), 1) + fronters.unshift(primary) } } @@ -285,7 +295,7 @@ async function removeFront(member) { async function updateCustomStatus(member) { // find the "primary" fronter to move to the first element in the list let fronters = await getPKFronters() - let primary = await findPrimary() + let primary = await findPrimary().pkId if (primary && fronters.length > 1 && (member.content.pkId == primary)) { if (fronters.indexOf(primary) >= 0) { fronters.splice(fronters.indexOf(primary), 1) From 2983fb83e3033640f467ce5e72890030f90d15f9 Mon Sep 17 00:00:00 2001 From: bee! Date: Wed, 19 Jun 2024 21:59:49 -0700 Subject: [PATCH 08/11] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b4c458c..9a8cd91 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ This project already has a Procfile set up, so it's super easy to get started. O These can be set either in the .env file, in terminal, or in the config vars section of Heroku. | Setting | Default | Description | | ---------| ------- | ------------------ | -| url_override | https://v2.apparyllis.com | The base URL for all SimplyPlural API requests. Unless you are running your own fork of Simply Plural, you shouldn't change this. | +| url_override | https://api.apparyllis.com | The base URL for all SimplyPlural API requests. Unless you are running your own fork of Simply Plural, you shouldn't change this. | | api_version | v1 | The target SimplyPlural API version. Unless you are running your own fork of Simply Plural, you shouldn't change this. | -| socket | wss://v2.apparyllis.com/v1/socket | The socket URL for SimplyPlural. Unless you are running your own fork of Simply Plural, you shouldn't change this. | +| socket | wss://api.apparyllis.com/v1/socket | The socket URL for SimplyPlural. Unless you are running your own fork of Simply Plural, you shouldn't change this. | | pk_url | https://api.pluralkit.me/v2 | The base URL for all PluralKit API requests. Unless you are running your own fork of PluralKit, you shouldn't change this. | | token | token_here | Your SimplyPlural account token. As of now, the only permission necessary is the Read permission. | | userId | user_id | Your SimplyPlural account/system ID. You can find it in account info near the bottom. | From 0f00694d2e1669f15c8afe0c4c88f494b41a6df1 Mon Sep 17 00:00:00 2001 From: bee! Date: Wed, 19 Jun 2024 22:00:50 -0700 Subject: [PATCH 09/11] Update .env_example --- .env_example | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.env_example b/.env_example index 894f144..ad0e951 100644 --- a/.env_example +++ b/.env_example @@ -1,7 +1,7 @@ # Compatiplural -url_override="https://v2.apparyllis.com" +url_override="https://api.apparyllis.com" api_version="v1" -socket="wss://v2.apparyllis.com/v1/socket" +socket="wss://api.apparyllis.com/v1/socket" pk_url="https://api.pluralkit.me/v2" token="SIMPLYPLURAL_TOKEN" userId="abcd1234" @@ -9,4 +9,5 @@ pk_token= "PLURALKIT_TOKEN" heartbeat=4500000 max_workers=1 silence_connections=true -full_swap=false \ No newline at end of file +full_swap=false +primary_tag="primary " From a764d7b21ce67c4afa0db0888113968c5ca1a9c6 Mon Sep 17 00:00:00 2001 From: bee! Date: Wed, 19 Jun 2024 22:01:14 -0700 Subject: [PATCH 10/11] Update dataManager.js --- dataManager.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dataManager.js b/dataManager.js index ec1cc5f..83c95bf 100644 --- a/dataManager.js +++ b/dataManager.js @@ -50,7 +50,7 @@ async function findPrimary() { return new Promise(async (resolve) => { await Util.asyncForEach(fronters, async (fronter) => { if (fronter.content.customStatus) { - if (fronter.content.customStatus.toLowerCase().includes("primary")) { + if (fronter.content.customStatus.toLowerCase().includes(Config.primary_tag)) { let member = await system.getMemberById(fronter.content.member) resolve({ name: member.content.name, pkId: member.content.pkId }) found = true @@ -353,4 +353,4 @@ module.exports = { removeFront, updateCustomStatus, calculateDiff -} \ No newline at end of file +} From b33335d18a1287d6af8879f6867f03eb75c6f420 Mon Sep 17 00:00:00 2001 From: Bee Date: Thu, 2 Jan 2025 12:22:37 -0800 Subject: [PATCH 11/11] fix: crash when custom front is activated --- .env_example | 7 ++++--- .gitignore | 2 ++ README.md | 4 ++-- dataManager.js | 7 +++++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.env_example b/.env_example index 894f144..7bf1e2f 100644 --- a/.env_example +++ b/.env_example @@ -1,7 +1,7 @@ # Compatiplural -url_override="https://v2.apparyllis.com" +url_override="https://api.apparyllis.com" api_version="v1" -socket="wss://v2.apparyllis.com/v1/socket" +socket="wss://api.apparyllis.com/v1/socket" pk_url="https://api.pluralkit.me/v2" token="SIMPLYPLURAL_TOKEN" userId="abcd1234" @@ -9,4 +9,5 @@ pk_token= "PLURALKIT_TOKEN" heartbeat=4500000 max_workers=1 silence_connections=true -full_swap=false \ No newline at end of file +full_swap=false +primary_tag="primary" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0befe13..33014e4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ config.json package-lock.json .env .vercel +docker_build.bat +docker_run.bat diff --git a/README.md b/README.md index b4c458c..9a8cd91 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ This project already has a Procfile set up, so it's super easy to get started. O These can be set either in the .env file, in terminal, or in the config vars section of Heroku. | Setting | Default | Description | | ---------| ------- | ------------------ | -| url_override | https://v2.apparyllis.com | The base URL for all SimplyPlural API requests. Unless you are running your own fork of Simply Plural, you shouldn't change this. | +| url_override | https://api.apparyllis.com | The base URL for all SimplyPlural API requests. Unless you are running your own fork of Simply Plural, you shouldn't change this. | | api_version | v1 | The target SimplyPlural API version. Unless you are running your own fork of Simply Plural, you shouldn't change this. | -| socket | wss://v2.apparyllis.com/v1/socket | The socket URL for SimplyPlural. Unless you are running your own fork of Simply Plural, you shouldn't change this. | +| socket | wss://api.apparyllis.com/v1/socket | The socket URL for SimplyPlural. Unless you are running your own fork of Simply Plural, you shouldn't change this. | | pk_url | https://api.pluralkit.me/v2 | The base URL for all PluralKit API requests. Unless you are running your own fork of PluralKit, you shouldn't change this. | | token | token_here | Your SimplyPlural account token. As of now, the only permission necessary is the Read permission. | | userId | user_id | Your SimplyPlural account/system ID. You can find it in account info near the bottom. | diff --git a/dataManager.js b/dataManager.js index ec1cc5f..203df1d 100644 --- a/dataManager.js +++ b/dataManager.js @@ -50,7 +50,7 @@ async function findPrimary() { return new Promise(async (resolve) => { await Util.asyncForEach(fronters, async (fronter) => { if (fronter.content.customStatus) { - if (fronter.content.customStatus.toLowerCase().includes("primary")) { + if (fronter.content.customStatus.toLowerCase().includes(Config.primary_tag)) { let member = await system.getMemberById(fronter.content.member) resolve({ name: member.content.name, pkId: member.content.pkId }) found = true @@ -116,11 +116,14 @@ async function swapFront() { for (member of front) { let m = await system.getMemberById(member.content.member) - if (m.content.pkId) { + if (m.content && m.content.pkId) { // fronting member pkID has been found newFront.push(m.content.pkId) frontNames.push(m.content.name) } + else { + console.warn('::SimplyWS:: System member not found, this may be a custom front which is unsupported.') + } } // shift primary fronter to first in list