allow for publishing entire front instead
Signed-off-by: bee! <pascalyoung03@gmail.com>
This commit is contained in:
parent
032f4fa753
commit
1564a110e0
4 changed files with 73 additions and 3 deletions
|
|
@ -9,3 +9,4 @@ pk_token= "PLURALKIT_TOKEN"
|
|||
heartbeat=4500000
|
||||
max_workers=1
|
||||
silence_connections=true
|
||||
full_swap=false
|
||||
|
|
@ -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. |
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
8
index.js
8
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue