Merge pull request #2 from padlocks/dev

bug fixes
This commit is contained in:
bee! 2022-03-02 04:31:59 -08:00 committed by GitHub
commit abb464937a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 37 deletions

View file

@ -5,4 +5,5 @@ pk_url="https://api.pluralkit.me/v2"
token="AAAAAAAAAAAAAAAAAAAA" token="AAAAAAAAAAAAAAAAAAAA"
userId="AAAAAAAAAAAAAAAAAAA" userId="AAAAAAAAAAAAAAAAAAA"
pk_token= "AAAAAAAAAAAAAAAA" pk_token= "AAAAAAAAAAAAAAAA"
pk_system="AAAAAAAAAAAAAAAA" pk_system="AAAAAAAAAAAAAAAA"
heartbeat=4500000

View file

@ -80,7 +80,10 @@ generateResponse = async (target, data) => {
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({"members": fronters}), { axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({"members": fronters}), {
headers: pkHeader headers: pkHeader
}) })
.catch(err => console.error(err.toJSON().message)) .catch(err => {
if (err.toJSON().status == 400) unknownError400()
else console.error(err.message)
})
response += '' + member.name + ' was added to the front.' response += '' + member.name + ' was added to the front.'
return return
@ -112,7 +115,10 @@ generateResponse = async (target, data) => {
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({ "members": fronters }), { axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({ "members": fronters }), {
headers: pkHeader headers: pkHeader
}) })
.catch(err => console.error(err.message)) .catch(err => {
if (err.toJSON().status == 400) unknownError400()
else console.error(err.message)
})
response += '' + member.name + ' was removed from the front.' response += '' + member.name + ' was removed from the front.'
break; break;
@ -132,7 +138,10 @@ generateResponse = async (target, data) => {
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({ "members": fronters }), { axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({ "members": fronters }), {
headers: pkHeader headers: pkHeader
}) })
.catch(err => console.error(err.message)) .catch(err => {
if (err.toJSON().status == 400) unknownError400()
else console.error(err.message)
})
response += '' + member.name + ' is now the primary fronter.' response += '' + member.name + ' is now the primary fronter.'
} }
} }
@ -156,6 +165,10 @@ generateResponse = async (target, data) => {
return response return response
} }
unknownError400 = () => {
return
}
unknownTarget = (target) => { unknownTarget = (target) => {
console.log('::SimplyWS:: Unknown update target: ' + target + '\n::SimplyWS:: Full message: ' + e) console.log('::SimplyWS:: Unknown update target: ' + target + '\n::SimplyWS:: Full message: ' + e)
} }
@ -219,14 +232,10 @@ determineAction = async (eventData, frontData = []) => {
} }
// get the difference between cached history and current front // get the difference between cached history and current front
let diff = calculateDiff(cache.frontHistory, frontData) let diff = await calculateDiff(cache.frontHistory, frontData)
// we handle one thing at a time, although this should be expanded since you can modify multiple custom statuses at once // we handle one thing at a time, although this should be expanded since you can modify multiple custom statuses at once
if (diff.length == 1) { if (diff.length == 1) {
// if there's an endTime, it was a removal event if (diff[0].content.customStatus) {
if (diff[0].content.endTime) {
action = 'remove'
}
else if (diff[0].content.customStatus) {
// check if customStatus value is in cache // check if customStatus value is in cache
let foundInCache = Object.keys(cache.frontHistory).filter((key) => { let foundInCache = Object.keys(cache.frontHistory).filter((key) => {
return cache.frontHistory[key] === diff[0].content.customStatus return cache.frontHistory[key] === diff[0].content.customStatus
@ -241,6 +250,12 @@ determineAction = async (eventData, frontData = []) => {
console.error('::SimplyWS:: Unrecognized diff: ' + JSON.stringify(diff)) console.error('::SimplyWS:: Unrecognized diff: ' + JSON.stringify(diff))
} }
} }
else {
// if there's an endTime, it was a removal event
if (eventData.content.endTime && !eventData.content.live) {
action = 'remove'
}
}
return action return action
} }
@ -256,17 +271,20 @@ const transform = require('lodash.transform')
const isEqual = require('lodash.isequal') const isEqual = require('lodash.isequal')
const isArray = require('lodash.isarray') const isArray = require('lodash.isarray')
const isObject = require('lodash.isobject') const isObject = require('lodash.isobject')
calculateDiff = (origObj, newObj) => { const { PassThrough } = require('stream')
changes = (newObj, origObj) => { calculateDiff = async (origObj, newObj) => {
let arrayIndexCounter = 0 return new Promise(function (resolve) {
return transform(newObj, function (result, value, key) { changes = (newObj, origObj) => {
if (!isEqual(value, origObj[key])) { let arrayIndexCounter = 0
let resultKey = isArray(origObj) ? arrayIndexCounter++ : key return transform(newObj, function (result, value, key) {
result[resultKey] = (isObject(value) && isObject(origObj[key])) ? changes(value, origObj[key]) : value if (!isEqual(value, origObj[key])) {
} let resultKey = isArray(origObj) ? arrayIndexCounter++ : key
}) result[resultKey] = (isObject(value) && isObject(origObj[key])) ? changes(value, origObj[key]) : value
} }
return changes(newObj, origObj) })
}
resolve(changes(newObj, origObj))
})
} }
main() main()

View file

@ -17,7 +17,6 @@
"lodash.isequal": "^4.5.0", "lodash.isequal": "^4.5.0",
"lodash.isobject": "^3.0.2", "lodash.isobject": "^3.0.2",
"lodash.transform": "^4.6.0", "lodash.transform": "^4.6.0",
"pkapi.js": "^3.1.0",
"ws": "^8.5.0" "ws": "^8.5.0"
}, },
"optionalDependencies": { "optionalDependencies": {

View file

@ -1,15 +0,0 @@
{
"version": 2,
"builds": [
{
"src": "./index.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/"
}
]
}