allow for publishing entire front instead

Signed-off-by: bee! <pascalyoung03@gmail.com>
This commit is contained in:
bee! 2023-09-24 13:32:14 -07:00
parent 032f4fa753
commit 1564a110e0
No known key found for this signature in database
GPG key ID: A350C9117C864EB7
4 changed files with 73 additions and 3 deletions

View file

@ -8,4 +8,5 @@ userId="abcd1234"
pk_token= "PLURALKIT_TOKEN" pk_token= "PLURALKIT_TOKEN"
heartbeat=4500000 heartbeat=4500000
max_workers=1 max_workers=1
silence_connections=true silence_connections=true
full_swap=false

View file

@ -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. | | 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. | | 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. | | 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. |

View file

@ -106,6 +106,69 @@ async function determineAction(eventData, frontData = []) {
return action 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) { async function insertFront(member) {
// get current fronters and add new fronter // get current fronters and add new fronter
let fronters = await getPKFronters() let fronters = await getPKFronters()
@ -275,6 +338,7 @@ module.exports = {
getPKFronters, getPKFronters,
findPrimary, findPrimary,
determineAction, determineAction,
swapFront,
insertFront, insertFront,
removeFront, removeFront,
updateCustomStatus, updateCustomStatus,

View file

@ -3,7 +3,7 @@ dotenv.config()
const { Config, System } = require('simplyapi') const { Config, System } = require('simplyapi')
const { Util } = require('simplyapi') const { Util } = require('simplyapi')
const { initializeCache, determineAction, insertFront, removeFront, updateCustomStatus } = require('./dataManager') const { initializeCache, determineAction, swapFront, insertFront, removeFront, updateCustomStatus } = require('./dataManager')
const { const {
isMainThread, isMainThread,
@ -95,8 +95,12 @@ update = async (data) => {
await Util.asyncForEach(data.results, async (o) => { await Util.asyncForEach(data.results, async (o) => {
let system = new System(Config) let system = new System(Config)
let member = await system.getMemberById(o.content.member) let member = await system.getMemberById(o.content.member)
let swap = Config.full_swap
// insert // insert
if (o.operationType == "insert") { if (swap) {
swapFront()
}
else if (o.operationType == "insert") {
insertFront(member) insertFront(member)
} }
else { else {