Verified Commit 827d3413 authored by Chocobozzz's avatar Chocobozzz
Browse files

Support updatedAt for auto mute plugin

parent 60437570
......@@ -15,6 +15,7 @@ This plugin expects the following JSON format from public blocklists:
data: {
value: string
action?: 'add' | 'remove' // Default is 'add'
updatedAt?: string // ISO 8601
}[]
}
```
......@@ -29,6 +30,10 @@ For example:
},
{
value: 'root@peertube.cpy.re'
},
{
value: 'chocobozzz@peertube2.cpy.re',
updatedAt: '2020-05-07T14:42:48.954Z'
}
]
}
......@@ -51,3 +56,8 @@ For example, to revert `peertube.cpy.re` from the blocklist, update the JSON:
]
}
```
The purpose of the `updatedAt` field is to not override admin mutes/unmutes:
* Plugin auto mutes of account A with an `updatedAt: '2020-05-07T14:42:48.954Z'`
* Admin thinks this account is fine so it unumutes account A
* On another check, the plugin won't re-mute the account A because of the `updatedAt` is before the last check
......@@ -11,6 +11,7 @@ const store = {
async function register ({
settingsManager,
storageManager,
peertubeHelpers,
registerSetting
}) {
......@@ -36,10 +37,10 @@ async function register ({
const settings = await settingsManager.getSettings([ 'check-seconds-interval', 'blocklist-urls' ])
await load(peertubeHelpers, settings['blocklist-urls'], settings['check-seconds-interval'])
await load(peertubeHelpers, storageManager, settings['blocklist-urls'], settings['check-seconds-interval'])
settingsManager.onSettingsChange(settings => {
load(peertubeHelpers, settings['blocklist-urls'], settings['check-seconds-interval'])
load(peertubeHelpers, storageManager, settings['blocklist-urls'], settings['check-seconds-interval'])
.catch(err => logger.error('Cannot load auto mute plugin.', { err }))
})
}
......@@ -55,7 +56,7 @@ module.exports = {
// ############################################################################
async function load (peertubeHelpers, blocklistUrls, checkIntervalSeconds) {
async function load (peertubeHelpers, storageManager, blocklistUrls, checkIntervalSeconds) {
const { logger } = peertubeHelpers
if (store.timeout) clearTimeout(store.timeout)
......@@ -72,17 +73,27 @@ async function load (peertubeHelpers, blocklistUrls, checkIntervalSeconds) {
logger.info('Loaded %d blocklist URLs for auto mute plugin.', store.urls.length, { urls: store.urls })
runLater(peertubeHelpers)
runLater(peertubeHelpers, storageManager)
}
async function runCheck (peertubeHelpers) {
async function runCheck (peertubeHelpers, storageManager) {
const { logger } = peertubeHelpers
if (store.urls.length === 0) return runLater(peertubeHelpers)
if (store.urls.length === 0) return runLater(peertubeHelpers, storageManager)
let lastChecks = await storageManager.getData('last-checks')
if (!lastChecks) lastChecks = {}
const newLastCheck = {}
for (const url of store.urls) {
try {
const { data } = await get(url)
newLastCheck[url] = new Date().toISOString()
const lastCheckTime = lastChecks[url]
? new Date(lastChecks[url]).getTime()
: 0
if (Array.isArray(data.data) === false) {
throw new Error('JSON response is not valid.')
......@@ -91,6 +102,13 @@ async function runCheck (peertubeHelpers) {
for (const entity of data.data) {
if (!entity.value) throw new Error('JSON entity is not valid.')
// We already checked this entity?
if (entity.updatedAt) {
const updatedAtTime = new Date(entity.updatedAt).getTime()
if (updatedAtTime < lastCheckTime) continue
}
if (entity.action === 'remove') await removeEntity(peertubeHelpers, entity.value)
else await addEntity(peertubeHelpers, entity.value)
}
......@@ -99,15 +117,19 @@ async function runCheck (peertubeHelpers) {
}
}
runLater(peertubeHelpers)
await storageManager.storeData('last-checks', newLastCheck)
runLater(peertubeHelpers, storageManager)
}
function runLater (peertubeHelpers) {
function runLater (peertubeHelpers, storageManager) {
const { logger } = peertubeHelpers
logger.debug('Will run auto mute check in %d seconds.', store.checkIntervalSeconds)
store.timeout = setTimeout(() => runCheck(peertubeHelpers), store.checkIntervalSeconds * 1000)
store.timeout = setTimeout(() => {
runCheck(peertubeHelpers, storageManager)
}, store.checkIntervalSeconds * 1000)
}
function get (url) {
......
{
"name": "peertube-plugin-auto-mute",
"version": "0.0.2",
"version": "0.0.3",
"description": "Auto mute plugin for PeerTube",
"engine": {
"peertube": ">=2.2.0"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment