Skip to content

Instantly share code, notes, and snippets.

@noerw
Created April 18, 2021 21:06
Show Gist options
  • Save noerw/d100f0fbf656e3a9988200f90f3b5a65 to your computer and use it in GitHub Desktop.
Save noerw/d100f0fbf656e3a9988200f90f3b5a65 to your computer and use it in GitHub Desktop.
etherpad-lite: drop version history of old pads
#!/bin/env node
const fetch = require('node-fetch')
const { toQueryString } = require('@norwin/javascript-is-bad')
async function etherRequest (instance, func, apikey, params = {}) {
const query = toQueryString(params)
const url = `${instance}/api/1.2.15/${func}?apikey=${apikey}&${query}`
const response = await fetch(url)
if (response.status !== 200)
throw new Error(`GET ${url} failed with ${response.status}`)
const result = await response.json()
if (result.message !== 'ok')
throw new Error(`GET ${url} failed with '${result.message}'`)
return result.data
}
async function getPads (instance, apikey) {
const { padIDs } = await etherRequest(instance, 'listAllPads', apikey)
return padIDs
}
async function checkShouldDelete (instance, apikey, padID) {
const { revisions } = await etherRequest(instance, 'getRevisionsCount', apikey, { padID })
if (revisions < 100)
return false
const { lastEdited } = await etherRequest(instance, 'getLastEdited', apikey, { padID })
const oneMonth = 34214794813
const edit = new Date(lastEdited)
if (new Date() - new Date(lastEdited) < oneMonth)
return false
return true
}
async function deletePadHistory (instance, apikey, pad) {
// TODO: delete pads that have no changed content ever?
await etherRequest(instance, 'copyPadWithoutHistory', apikey, {
sourceID: pad,
destinationID: `${pad}_backup`,
force: true,
})
await etherRequest(instance, 'movePad', apikey, {
sourceID: `${pad}_backup`,
destinationID: pad,
force: true,
})
}
async function main () {
const instance = process.argv[2]
const apikey = process.argv[3]
if (!instance || !apikey)
throw Error(`usage: ${process.argv[1]} <instance base URL> <apikey>`)
const pads = await getPads(instance, apikey)
console.log(`found ${pads.length} pads`)
for (const p of pads) {
console.log(`processing ${p}`)
try {
if (await checkShouldDelete(instance, apikey, p)) {
console.log(`deleting history of ${p}`)
await deletePadHistory(instance, apikey, p)
} else {
console.log(`skipping ${p} (newer than 1 month or less than 100 revisions)`)
}
} catch (err) {
console.error(err)
}
}
}
main().catch(e => console.error(e))
@noerw
Copy link
Author

noerw commented Apr 18, 2021

Also helpful:

use etherpad-lite;
delete from store where `key` like "sessionstorage:%";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment