Add update notifications

master
Dragory 2019-06-09 17:31:17 +03:00
parent 907cb1956f
commit b9133da4da
8 changed files with 154 additions and 1 deletions

View File

@ -96,5 +96,6 @@ These go in `config.json`. See also `config.example.json`.
|threadTimestamps|false|Whether to show custom timestamps in threads, in addition to Discord's own timestamps. Logs always have accurate timestamps, regardless of this setting.|
|typingProxy|false|If enabled, any time a user is typing to modmail in their DMs, the modmail thread will show the bot as "typing"|
|typingProxyReverse|false|If enabled, any time a moderator is typing in a modmail thread, the user will see the bot "typing" in their DMs|
|updateNotifications|true|Whether to automatically check for bot updates and notify about them in new threads|
|url|None|URL to use for attachment and log links. Defaults to `IP:PORT`|
|useNicknames|false|If set to true, mod replies will use their nickname (on the inbox server) instead of their username|

View File

@ -0,0 +1,14 @@
exports.up = async function(knex, Promise) {
if (! await knex.schema.hasTable('updates')) {
await knex.schema.createTable('updates', table => {
table.string('available_version', 16).nullable();
table.dateTime('last_checked').nullable();
});
}
};
exports.down = async function(knex, Promise) {
if (await knex.schema.hasTable('updates')) {
await knex.schema.dropTable('updates');
}
};

View File

@ -10,7 +10,10 @@
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "./node_modules/.bin/eslint ./src"
},
"author": "",
"repository": {
"type": "git",
"url": "https://github.com/Dragory/modmailbot"
},
"dependencies": {
"eris": "github:abalabahaha/eris#dev",
"humanize-duration": "^3.12.1",

View File

@ -91,6 +91,8 @@ const defaultConfig = {
"categoryAutomation": {},
"updateNotifications": true,
"port": 8890,
"url": null,

View File

@ -9,6 +9,7 @@ const bot = require('../bot');
const knex = require('../knex');
const config = require('../config');
const utils = require('../utils');
const updates = require('./updates');
const Thread = require('./Thread');
const {THREAD_STATUS} = require('./constants');
@ -235,6 +236,13 @@ async function createNewThreadForUser(user, quiet = false, ignoreRequirements =
await newThread.postSystemMessage(infoHeader);
if (config.updateNotifications) {
const availableUpdate = await updates.getAvailableUpdate();
if (availableUpdate) {
await newThread.postNonLogMessage(`📣 New bot version available (${availableUpdate})`);
}
}
// If there were errors sending a response to the user, note that
if (responseMessageError) {
await newThread.postSystemMessage(`**NOTE:** Could not send auto-response to the user. The error given was: \`${responseMessageError.message}\``);

113
src/data/updates.js Normal file
View File

@ -0,0 +1,113 @@
const url = require('url');
const https = require('https');
const moment = require('moment');
const knex = require('../knex');
const config = require('../config');
const UPDATE_CHECK_FREQUENCY = 12; // In hours
let updateCheckPromise = null;
async function initUpdatesTable() {
const row = await knex('updates').first();
if (! row) {
await knex('updates').insert({
available_version: null,
last_checked: null,
});
}
}
/**
* Update current and available versions in the database.
* Only works when `repository` in package.json is set to a GitHub repository
* @returns {Promise<void>}
*/
async function refreshVersions() {
await initUpdatesTable();
const { last_checked } = await knex('updates').first();
// Only refresh available version if it's been more than UPDATE_CHECK_FREQUENCY since our last check
if (last_checked != null && last_checked > moment.utc().subtract(UPDATE_CHECK_FREQUENCY, 'hours').format('YYYY-MM-DD HH:mm:ss')) return;
const packageJson = require('../../package.json');
const repositoryUrl = packageJson.repository && packageJson.repository.url;
if (! repositoryUrl) return;
const parsedUrl = url.parse(repositoryUrl);
if (parsedUrl.hostname !== 'github.com') return;
const [, owner, repo] = parsedUrl.pathname.split('/');
if (! owner || ! repo) return;
const apiUrl = `https://api.github.com/repos/${owner}/${repo}/tags`;
https.get(
apiUrl,
{
headers: {
'User-Agent': `Modmailbot (https://github.com/dragory/modmailbot) (${packageJson.version})`
}
},
async res => {
if (res.statusCode !== 200) {
await knex('updates').update({
last_checked: moment.utc().format('YYYY-MM-DD HH:mm:ss')
});
console.warn(`[WARN] Got status code ${res.statusCode} when checking for available updates`);
return;
}
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', async () => {
const parsed = JSON.parse(data);
let latestVersion = parsed[0].name;
await knex('updates').update({
available_version: latestVersion,
last_checked: moment.utc().format('YYYY-MM-DD HH:mm:ss')
});
});
}
);
}
/**
* @param {String} a Version string, e.g. "2.20.0"
* @param {String} b Version string, e.g. "2.20.0"
* @returns {Number} 1 if version a is larger than b, -1 is version a is smaller than b, 0 if they are equal
*/
function compareVersions(a, b) {
const aParts = a.split('.');
const bParts = b.split('.');
for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) {
let aPart = parseInt((aParts[i] || '0').match(/\d+/)[0] || '0', 10);
let bPart = parseInt((bParts[i] || '0').match(/\d+/)[0] || '0', 10);
if (aPart > bPart) return 1;
if (aPart < bPart) return -1;
}
return 0;
}
async function getAvailableUpdate() {
await initUpdatesTable();
const packageJson = require('../../package.json');
const currentVersion = packageJson.version;
const { available_version: availableVersion } = await knex('updates').first();
if (availableVersion == null) return null;
if (currentVersion == null) return availableVersion;
const versionDiff = compareVersions(currentVersion, availableVersion);
if (versionDiff === -1) return availableVersion;
return null;
}
async function refreshVersionsLoop() {
await refreshVersions();
setTimeout(refreshVersionsLoop, UPDATE_CHECK_FREQUENCY * 60 * 60 * 1000);
}
module.exports = {
getAvailableUpdate,
startVersionRefreshLoop: refreshVersionsLoop
};

View File

@ -6,6 +6,7 @@ const {messageQueue} = require('./queue');
const utils = require('./utils');
const blocked = require('./data/blocked');
const threads = require('./data/threads');
const updates = require('./data/updates');
const reply = require('./modules/reply');
const close = require('./modules/close');
@ -196,6 +197,8 @@ module.exports = {
await idModule(bot);
await alert(bot);
updates.startVersionRefreshLoop();
// Connect to Discord
console.log('Connecting to Discord...');
await bot.connect();

View File

@ -3,6 +3,8 @@ const fs = require('fs');
const {promisify} = require('util');
const utils = require("../utils");
const threadUtils = require("../threadUtils");
const updates = require('../data/updates');
const config = require('../config');
const access = promisify(fs.access);
const readFile = promisify(fs.readFile);
@ -42,6 +44,13 @@ module.exports = bot => {
response += ` (${commitHash.slice(0, 7)})`;
}
if (config.updateNotifications) {
const availableUpdate = await updates.getAvailableUpdate();
if (availableUpdate) {
response += ` (version ${availableUpdate} available)`;
}
}
utils.postSystemMessageWithFallback(msg.channel, thread, response);
});
};