Fix some inconsistencies between DB drivers, also fixing scheduled closes on MySQL/MariaDB (again)
parent
7ae8d375b1
commit
434dab59ee
|
@ -1,3 +1,5 @@
|
|||
const utils = require("../utils");
|
||||
|
||||
/**
|
||||
* @property {String} trigger
|
||||
* @property {String} body
|
||||
|
@ -7,7 +9,7 @@
|
|||
*/
|
||||
class Snippet {
|
||||
constructor(props) {
|
||||
Object.assign(this, props);
|
||||
utils.setDataModelProps(this, props);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ const {THREAD_MESSAGE_TYPE, THREAD_STATUS} = require('./constants');
|
|||
*/
|
||||
class Thread {
|
||||
constructor(props) {
|
||||
Object.assign(this, props);
|
||||
utils.setDataModelProps(this, props);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -92,7 +92,7 @@ class Thread {
|
|||
});
|
||||
|
||||
// The string type check is due to a knex bug, see https://github.com/tgriesser/knex/issues/1276
|
||||
if (this.scheduled_close_at && typeof this.scheduled_close_at === 'string') {
|
||||
if (this.scheduled_close_at) {
|
||||
await this.cancelScheduledClose();
|
||||
await this.postSystemMessage(`Cancelling scheduled closing of this thread due to new reply`);
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ class Thread {
|
|||
});
|
||||
|
||||
// The string type check is due to a knex bug, see https://github.com/tgriesser/knex/issues/1276
|
||||
if (this.scheduled_close_at && typeof this.scheduled_close_at === 'string') {
|
||||
if (this.scheduled_close_at) {
|
||||
await this.cancelScheduledClose();
|
||||
await this.postSystemMessage({
|
||||
content: `<@!${this.scheduled_close_id}> Thread that was scheduled to be closed got a new reply. Cancelling.`,
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
const utils = require("../utils");
|
||||
|
||||
/**
|
||||
* @property {Number} id
|
||||
* @property {String} thread_id
|
||||
|
@ -11,7 +13,7 @@
|
|||
*/
|
||||
class ThreadMessage {
|
||||
constructor(props) {
|
||||
Object.assign(this, props);
|
||||
utils.setDataModelProps(this, props);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -257,7 +257,7 @@ addInboxServerCommand('close', async (msg, args, thread) => {
|
|||
if (args[0] === 'cancel') {
|
||||
// Cancel timed close
|
||||
// The string type check is due to a knex bug, see https://github.com/tgriesser/knex/issues/1276
|
||||
if (thread.scheduled_close_at && typeof thread.scheduled_close_at === 'string') {
|
||||
if (thread.scheduled_close_at) {
|
||||
await thread.cancelScheduledClose();
|
||||
thread.postSystemMessage(`Cancelled scheduled closing`);
|
||||
}
|
||||
|
|
25
src/utils.js
25
src/utils.js
|
@ -231,6 +231,29 @@ function postSystemMessageWithFallback(channel, thread, text) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A normalized way to set props in data models, fixing some inconsistencies between different DB drivers in knex
|
||||
* @param {Object} target
|
||||
* @param {Object} props
|
||||
*/
|
||||
function setDataModelProps(target, props) {
|
||||
for (const prop in props) {
|
||||
if (! props.hasOwnProperty(prop)) continue;
|
||||
// DATETIME fields are always returned as Date objects in MySQL/MariaDB
|
||||
if (props[prop] instanceof Date) {
|
||||
// ...even when NULL, in which case the date's set to unix epoch
|
||||
if (props[prop].getUTCFullYear() === 1970) {
|
||||
target[prop] = null;
|
||||
} else {
|
||||
// Set the value as a string in the same format it's returned in SQLite
|
||||
target[prop] = moment.utc(props[prop]).format('YYYY-MM-DD HH:mm:ss');
|
||||
}
|
||||
} else {
|
||||
target[prop] = props[prop];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
BotError,
|
||||
|
||||
|
@ -257,4 +280,6 @@ module.exports = {
|
|||
|
||||
chunk,
|
||||
trimAll,
|
||||
|
||||
setDataModelProps,
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue