89 lines
2.5 KiB
JavaScript
Executable File
89 lines
2.5 KiB
JavaScript
Executable File
'use strict';
|
|
|
|
const config = require('config');
|
|
const snoowrap = require('snoowrap');
|
|
const bhttp = require('bhttp');
|
|
|
|
const reddit = new snoowrap(config.reddit);
|
|
|
|
function log(msg) {
|
|
console.log(`${new Date().toISOString()} ${msg}`);
|
|
}
|
|
|
|
async function getWekanActorNames() {
|
|
const wekanLoginRes = await bhttp.post(`${config.wekan.url}/users/login`, {
|
|
username: config.wekan.username,
|
|
password: config.wekan.password,
|
|
});
|
|
|
|
if (!wekanLoginRes.statusCode === 200) {
|
|
throw new Error(`Wekan login failed (${wekanLoginRes.statusCode}): ${wekanLoginRes.body}`);
|
|
}
|
|
|
|
const wekanBoardRes = await bhttp.get(`${config.wekan.url}/api/boards/${config.wekan.boardId}/lists/${config.wekan.listId}/cards`, {
|
|
headers: {
|
|
Authorization: `Bearer ${wekanLoginRes.body.token}`,
|
|
},
|
|
});
|
|
|
|
if (!wekanBoardRes.statusCode === 200) {
|
|
throw new Error(`Wekan card retrieval failed (${wekanBoardRes.statusCode}): ${wekanBoardRes.body}`);
|
|
}
|
|
|
|
const actorNames = wekanBoardRes.body.map((card) => {
|
|
if (!card.title) {
|
|
console.warn('Missing card title', card);
|
|
|
|
return null;
|
|
}
|
|
|
|
const name = card.title.split(/\s+/).slice(0, 2).join(' ');
|
|
|
|
if (name.includes('.')) {
|
|
// allow single name to be delimited
|
|
return name.slice(0, name.indexOf('.'));
|
|
}
|
|
|
|
return name;
|
|
;});
|
|
|
|
return actorNames.filter(Boolean);
|
|
}
|
|
|
|
async function init() {
|
|
log(`Retrieving current configuration from ${config.subreddit}`);
|
|
|
|
const automodConfig = await reddit.getSubreddit(config.subreddit).getWikiPage('config/automoderator').fetch();
|
|
const automodLines = automodConfig.content_md.split('\n');
|
|
const actorLineIndex = automodLines.findIndex((line) => line.includes(config.actorCommentKey)) + 1;
|
|
const actorLine = automodLines[actorLineIndex];
|
|
|
|
const wekanActorNames = await getWekanActorNames();
|
|
const actorNames = [...config.baseActorNames, ...wekanActorNames];
|
|
const newActorLine = `title: ${JSON.stringify(actorNames)}`;
|
|
|
|
if (actorLine !== newActorLine) {
|
|
automodLines[actorLineIndex] = newActorLine;
|
|
|
|
const newConfig = automodLines.join('\n');
|
|
|
|
const result = await reddit.getSubreddit(config.subreddit).getWikiPage('config/automoderator').edit({
|
|
text: newConfig,
|
|
reason: 'Synced kanban actor names',
|
|
});
|
|
|
|
log(`Set ${newActorLine}`);
|
|
}
|
|
|
|
if (config.interval) {
|
|
setTimeout(() => init(), config.interval * 1000);
|
|
|
|
log(`Sync complete, resyncing in ${config.interval} seconds`);
|
|
return;
|
|
}
|
|
|
|
log('Sync complete');
|
|
}
|
|
|
|
init();
|