103 lines
3.3 KiB
JavaScript
103 lines
3.3 KiB
JavaScript
const axios = require("axios");
|
|
|
|
// For pushing updates from bbj
|
|
module.exports = class BBJ {
|
|
constructor(client, alias, source, channels, interval) {
|
|
this.name = "BBJ";
|
|
this.client = client;
|
|
this.alias = alias;
|
|
this.source = source;
|
|
this.channels = channels;
|
|
this.interval = interval;
|
|
|
|
const task_memories = this.client.memory.tasks;
|
|
task_memories[this.alias] = task_memories[this.alias] || {};
|
|
this.memory = task_memories[this.alias];
|
|
}
|
|
|
|
start() {
|
|
this.memory.status = "running";
|
|
this.memory.timestamp = Date.now();
|
|
this.memory.known = {};
|
|
this.fetch(this.cache.bind(this));
|
|
this.job = setInterval(this.run.bind(this), this.interval);
|
|
}
|
|
|
|
stop(ack) {
|
|
clearInterval(this.job);
|
|
this.client.say(ack, `Stopping task "${this.alias}"`);
|
|
}
|
|
|
|
run() {
|
|
this.fetch(this.mirror.bind(this));
|
|
}
|
|
|
|
cache(item) {
|
|
this.memory.known[item.thread_id] = item.last_mod;
|
|
}
|
|
|
|
mirror(item) {
|
|
const thread_id = item.thread_id;
|
|
const last_mod = this.memory.known[thread_id];
|
|
if (last_mod === item.last_mod) return;
|
|
|
|
this.memory.known[thread_id] = item.last_mod;
|
|
|
|
this.get_thread(thread_id, (thread) => {
|
|
const title = thread.data.title;
|
|
const replies = thread.data.reply_count;
|
|
const messages = thread.data.messages;
|
|
const usermap = thread.usermap;
|
|
const reply = messages[replies];
|
|
const author = reply.author;
|
|
const username = usermap[author].user_name;
|
|
const body = reply.body.replace(/>>\d\n\n/, "").replace(/\n/, " ");
|
|
const client = `https://bbj.tilde.team/index.php`;
|
|
const link = `${client}?thread_id=${thread_id}`;
|
|
this.channels.forEach((channel) => {
|
|
const response = `"${title}" (${username}) : ${body} <${link}>`;
|
|
this.client.say(channel, `[${this.alias}] ${response}`);
|
|
});
|
|
});
|
|
// const use = item[this.use].replace(/(<\/?[^>]+>)|\\n/g, "");
|
|
// const post = `${use} <${item.guid}>`;
|
|
// const response = `[${this.alias}] New submission: ${post}`;
|
|
}
|
|
|
|
handle_errors(response) {
|
|
if (response.error) {
|
|
const err = `<${response.code}> ${response.description}`;
|
|
console.error(`${this.name}::error: [axios] ${err}`);
|
|
return false;
|
|
} else {
|
|
return response;
|
|
}
|
|
}
|
|
|
|
get_thread(id, callback) {
|
|
axios.post(`${this.source}/thread_load`, {
|
|
"thread_id": id
|
|
}).then((res) => {
|
|
const response = this.handle_errors(res.data);
|
|
if (response) {
|
|
callback(response);
|
|
}
|
|
}).catch((error) => {
|
|
console.error(`${this.name}::error: [axios] ${error}`);
|
|
});
|
|
}
|
|
|
|
fetch(callback) {
|
|
axios.get(`${this.source}/thread_index`).then((res) => {
|
|
const response = this.handle_errors(res.data);
|
|
if (response) {
|
|
const threads = response.data;
|
|
threads.forEach((thread) => {
|
|
callback(thread);
|
|
});
|
|
}
|
|
}).catch((error) => {
|
|
console.error(`${this.name}::error: [axios] ${error}`);
|
|
});
|
|
}
|
|
} |