From 1564a110e02357cf1c4d26ab6b1fee6ac2506651 Mon Sep 17 00:00:00 2001 From: bee! Date: Sun, 24 Sep 2023 13:32:14 -0700 Subject: [PATCH] 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 {