2018-11-24 14:01:06 +02:00
|
|
|
import { Brackets, getRepository, Repository } from "typeorm";
|
2019-05-25 21:25:34 +03:00
|
|
|
import { BaseGuildRepository } from "./BaseGuildRepository";
|
2018-11-24 14:01:06 +02:00
|
|
|
import { ISavedMessageData, SavedMessage } from "./entities/SavedMessage";
|
2018-11-24 14:58:54 +02:00
|
|
|
import { QueuedEventEmitter } from "../QueuedEventEmitter";
|
2018-11-24 14:18:48 +02:00
|
|
|
import { GuildChannel, Message } from "eris";
|
|
|
|
import moment from "moment-timezone";
|
2020-05-28 01:54:38 +03:00
|
|
|
import { DAYS, MINUTES } from "../utils";
|
2020-05-28 02:45:07 +03:00
|
|
|
import { isAPI } from "../globals";
|
2018-11-24 14:01:06 +02:00
|
|
|
|
2020-05-28 01:54:38 +03:00
|
|
|
const CLEANUP_INTERVAL = 5 * MINUTES;
|
2018-11-24 14:01:06 +02:00
|
|
|
|
2020-05-28 01:54:38 +03:00
|
|
|
/**
|
|
|
|
* How long message edits, deletions, etc. will include the original message content.
|
|
|
|
* This is very heavy storage-wise, so keeping it as low as possible is ideal.
|
|
|
|
*/
|
2020-05-28 02:37:14 +03:00
|
|
|
const RETENTION_PERIOD = 1 * DAYS;
|
2018-11-24 14:01:06 +02:00
|
|
|
|
2019-04-20 19:51:26 +03:00
|
|
|
async function cleanup() {
|
|
|
|
const repository = getRepository(SavedMessage);
|
|
|
|
await repository
|
|
|
|
.createQueryBuilder("messages")
|
|
|
|
.where(
|
|
|
|
// Clear deleted messages
|
|
|
|
new Brackets(qb => {
|
|
|
|
qb.where("deleted_at IS NOT NULL");
|
|
|
|
qb.andWhere(`deleted_at <= (NOW() - INTERVAL ${CLEANUP_INTERVAL}000 MICROSECOND)`);
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.orWhere(
|
|
|
|
// Clear old messages
|
|
|
|
new Brackets(qb => {
|
|
|
|
qb.where("is_permanent = 0");
|
|
|
|
qb.andWhere(`posted_at <= (NOW() - INTERVAL ${RETENTION_PERIOD}000 MICROSECOND)`);
|
|
|
|
}),
|
|
|
|
)
|
2020-05-28 02:39:04 +03:00
|
|
|
.limit(50_000) // To avoid long table locks, limit the amount of messages deleted at once
|
2019-04-20 19:51:26 +03:00
|
|
|
.delete()
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
setTimeout(cleanup, CLEANUP_INTERVAL);
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:45:07 +03:00
|
|
|
if (!isAPI()) {
|
|
|
|
// Start first cleanup 30 seconds after startup
|
|
|
|
setTimeout(cleanup, 30 * 1000);
|
|
|
|
}
|
2019-04-20 19:51:26 +03:00
|
|
|
|
2019-05-25 21:25:34 +03:00
|
|
|
export class GuildSavedMessages extends BaseGuildRepository {
|
2018-11-24 14:01:06 +02:00
|
|
|
private messages: Repository<SavedMessage>;
|
2018-11-24 17:59:05 +02:00
|
|
|
protected toBePermanent: Set<string>;
|
|
|
|
|
2018-11-24 14:58:54 +02:00
|
|
|
public events: QueuedEventEmitter;
|
2018-11-24 14:01:06 +02:00
|
|
|
|
|
|
|
constructor(guildId) {
|
|
|
|
super(guildId);
|
|
|
|
this.messages = getRepository(SavedMessage);
|
2018-11-24 14:58:54 +02:00
|
|
|
this.events = new QueuedEventEmitter();
|
2018-11-24 14:01:06 +02:00
|
|
|
|
2018-11-24 17:59:05 +02:00
|
|
|
this.toBePermanent = new Set();
|
2018-11-24 14:01:06 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 14:18:48 +02:00
|
|
|
public msgToSavedMessageData(msg: Message): ISavedMessageData {
|
2018-11-24 14:53:55 +02:00
|
|
|
const data: ISavedMessageData = {
|
2018-11-24 14:18:48 +02:00
|
|
|
author: {
|
|
|
|
username: msg.author.username,
|
2019-04-20 19:24:28 +03:00
|
|
|
discriminator: msg.author.discriminator,
|
2018-11-24 14:18:48 +02:00
|
|
|
},
|
2018-12-22 12:38:52 +02:00
|
|
|
content: msg.content,
|
2019-04-20 19:24:28 +03:00
|
|
|
timestamp: msg.timestamp,
|
2018-11-24 14:18:48 +02:00
|
|
|
};
|
2018-11-24 14:53:55 +02:00
|
|
|
|
|
|
|
if (msg.attachments.length) data.attachments = msg.attachments;
|
|
|
|
if (msg.embeds.length) data.embeds = msg.embeds;
|
|
|
|
|
|
|
|
return data;
|
2018-11-24 14:18:48 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 14:01:06 +02:00
|
|
|
find(id) {
|
2018-11-24 14:33:43 +02:00
|
|
|
return this.messages
|
|
|
|
.createQueryBuilder()
|
|
|
|
.where("guild_id = :guild_id", { guild_id: this.guildId })
|
|
|
|
.andWhere("id = :id", { id })
|
|
|
|
.andWhere("deleted_at IS NULL")
|
|
|
|
.getOne();
|
2018-11-24 14:01:06 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 18:39:17 +02:00
|
|
|
getLatestBotMessagesByChannel(channelId, limit) {
|
2018-11-24 17:12:36 +02:00
|
|
|
return this.messages
|
|
|
|
.createQueryBuilder()
|
|
|
|
.where("guild_id = :guild_id", { guild_id: this.guildId })
|
2018-11-24 18:39:17 +02:00
|
|
|
.andWhere("channel_id = :channel_id", { channel_id: channelId })
|
|
|
|
.andWhere("is_bot = 1")
|
2018-11-24 19:42:51 +02:00
|
|
|
.andWhere("deleted_at IS NULL")
|
2018-11-24 18:39:17 +02:00
|
|
|
.orderBy("id", "DESC")
|
|
|
|
.limit(limit)
|
2018-11-24 17:12:36 +02:00
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
|
2018-11-24 18:39:17 +02:00
|
|
|
getLatestByChannelBeforeId(channelId, beforeId, limit) {
|
|
|
|
return this.messages
|
|
|
|
.createQueryBuilder()
|
|
|
|
.where("guild_id = :guild_id", { guild_id: this.guildId })
|
|
|
|
.andWhere("channel_id = :channel_id", { channel_id: channelId })
|
|
|
|
.andWhere("id < :beforeId", { beforeId })
|
2018-11-24 19:42:51 +02:00
|
|
|
.andWhere("deleted_at IS NULL")
|
2018-11-24 18:39:17 +02:00
|
|
|
.orderBy("id", "DESC")
|
|
|
|
.limit(limit)
|
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
|
|
|
|
getLatestByChannelAndUser(channelId, userId, limit) {
|
|
|
|
return this.messages
|
|
|
|
.createQueryBuilder()
|
|
|
|
.where("guild_id = :guild_id", { guild_id: this.guildId })
|
|
|
|
.andWhere("channel_id = :channel_id", { channel_id: channelId })
|
|
|
|
.andWhere("user_id = :user_id", { user_id: userId })
|
2018-11-24 19:42:51 +02:00
|
|
|
.andWhere("deleted_at IS NULL")
|
2018-11-24 18:39:17 +02:00
|
|
|
.orderBy("id", "DESC")
|
|
|
|
.limit(limit)
|
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
|
|
|
|
getUserMessagesByChannelAfterId(userId, channelId, afterId, limit = null) {
|
|
|
|
let query = this.messages
|
|
|
|
.createQueryBuilder()
|
|
|
|
.where("guild_id = :guild_id", { guild_id: this.guildId })
|
|
|
|
.andWhere("user_id = :user_id", { user_id: userId })
|
|
|
|
.andWhere("channel_id = :channel_id", { channel_id: channelId })
|
2018-11-24 19:42:51 +02:00
|
|
|
.andWhere("id > :afterId", { afterId })
|
|
|
|
.andWhere("deleted_at IS NULL");
|
2018-11-24 18:39:17 +02:00
|
|
|
|
|
|
|
if (limit != null) {
|
|
|
|
query = query.limit(limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return query.getMany();
|
|
|
|
}
|
|
|
|
|
2019-10-11 01:59:56 +03:00
|
|
|
getMultiple(messageIds: string[]): Promise<SavedMessage[]> {
|
|
|
|
return this.messages
|
|
|
|
.createQueryBuilder()
|
|
|
|
.where("guild_id = :guild_id", { guild_id: this.guildId })
|
|
|
|
.andWhere("id IN (:messageIds)", { messageIds })
|
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
|
2018-11-24 14:01:06 +02:00
|
|
|
async create(data) {
|
2018-11-24 17:59:05 +02:00
|
|
|
const isPermanent = this.toBePermanent.has(data.id);
|
|
|
|
if (isPermanent) {
|
|
|
|
data.is_permanent = true;
|
|
|
|
this.toBePermanent.delete(data.id);
|
|
|
|
}
|
|
|
|
|
2018-11-24 14:01:06 +02:00
|
|
|
try {
|
2018-11-24 14:23:10 +02:00
|
|
|
await this.messages.insert(data);
|
2018-11-24 14:01:06 +02:00
|
|
|
} catch (e) {
|
2020-01-12 12:04:20 +02:00
|
|
|
console.warn(e); // tslint:disable-line
|
2018-11-24 14:01:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const inserted = await this.messages.findOne(data.id);
|
|
|
|
this.events.emit("create", [inserted]);
|
2019-01-06 14:39:16 +02:00
|
|
|
this.events.emit(`create:${data.id}`, [inserted]);
|
2018-11-24 14:01:06 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 14:18:48 +02:00
|
|
|
async createFromMsg(msg: Message, overrides = {}) {
|
2019-08-14 10:57:39 +03:00
|
|
|
const existingSavedMsg = await this.find(msg.id);
|
|
|
|
if (existingSavedMsg) return;
|
|
|
|
|
2018-11-24 14:18:48 +02:00
|
|
|
const savedMessageData = this.msgToSavedMessageData(msg);
|
|
|
|
const postedAt = moment.utc(msg.timestamp, "x").format("YYYY-MM-DD HH:mm:ss.SSS");
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
id: msg.id,
|
|
|
|
guild_id: (msg.channel as GuildChannel).guild.id,
|
|
|
|
channel_id: msg.channel.id,
|
|
|
|
user_id: msg.author.id,
|
|
|
|
is_bot: msg.author.bot,
|
|
|
|
data: savedMessageData,
|
2019-04-20 19:24:28 +03:00
|
|
|
posted_at: postedAt,
|
2018-11-24 14:18:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return this.create({ ...data, ...overrides });
|
|
|
|
}
|
|
|
|
|
2018-11-24 14:01:06 +02:00
|
|
|
async markAsDeleted(id) {
|
|
|
|
await this.messages
|
|
|
|
.createQueryBuilder("messages")
|
|
|
|
.update()
|
|
|
|
.set({
|
2019-04-20 19:24:28 +03:00
|
|
|
deleted_at: () => "NOW(3)",
|
2018-11-24 14:01:06 +02:00
|
|
|
})
|
|
|
|
.where("guild_id = :guild_id", { guild_id: this.guildId })
|
|
|
|
.andWhere("id = :id", { id })
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
const deleted = await this.messages.findOne(id);
|
2018-12-22 14:37:41 +02:00
|
|
|
|
|
|
|
if (deleted) {
|
|
|
|
this.events.emit("delete", [deleted]);
|
2019-01-06 14:39:16 +02:00
|
|
|
this.events.emit(`delete:${id}`, [deleted]);
|
2018-12-22 14:37:41 +02:00
|
|
|
}
|
2018-11-24 14:01:06 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 13:06:40 +02:00
|
|
|
/**
|
|
|
|
* Marks the specified messages as deleted in the database (if they weren't already marked before).
|
|
|
|
* If any messages were marked as deleted, also emits the deleteBulk event.
|
|
|
|
*/
|
2018-11-24 18:39:17 +02:00
|
|
|
async markBulkAsDeleted(ids) {
|
2018-12-22 13:06:40 +02:00
|
|
|
const deletedAt = moment().format("YYYY-MM-DD HH:mm:ss.SSS");
|
|
|
|
|
2018-11-24 18:39:17 +02:00
|
|
|
await this.messages
|
|
|
|
.createQueryBuilder()
|
|
|
|
.update()
|
2018-12-22 13:06:40 +02:00
|
|
|
.set({ deleted_at: deletedAt })
|
2018-11-24 18:39:17 +02:00
|
|
|
.where("guild_id = :guild_id", { guild_id: this.guildId })
|
|
|
|
.andWhere("id IN (:ids)", { ids })
|
2018-12-22 13:06:40 +02:00
|
|
|
.andWhere("deleted_at IS NULL")
|
2018-11-24 18:39:17 +02:00
|
|
|
.execute();
|
|
|
|
|
2018-11-24 19:33:29 +02:00
|
|
|
const deleted = await this.messages
|
2018-11-24 18:39:17 +02:00
|
|
|
.createQueryBuilder()
|
|
|
|
.where("id IN (:ids)", { ids })
|
2019-06-22 18:52:24 +03:00
|
|
|
.andWhere("deleted_at = :deletedAt", { deletedAt })
|
2018-11-24 18:39:17 +02:00
|
|
|
.getMany();
|
2018-11-24 19:33:29 +02:00
|
|
|
|
2018-12-22 13:06:40 +02:00
|
|
|
if (deleted.length) {
|
|
|
|
this.events.emit("deleteBulk", [deleted]);
|
|
|
|
}
|
2018-11-24 18:39:17 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 14:18:48 +02:00
|
|
|
async saveEdit(id, newData: ISavedMessageData) {
|
2018-11-24 14:01:06 +02:00
|
|
|
const oldMessage = await this.messages.findOne(id);
|
2018-12-15 23:07:19 +02:00
|
|
|
if (!oldMessage) return;
|
|
|
|
|
2018-11-24 14:01:06 +02:00
|
|
|
const newMessage = { ...oldMessage, data: newData };
|
|
|
|
|
|
|
|
await this.messages.update(
|
|
|
|
{ id },
|
|
|
|
{
|
2019-04-20 19:24:28 +03:00
|
|
|
data: newData,
|
|
|
|
},
|
2018-11-24 14:01:06 +02:00
|
|
|
);
|
|
|
|
|
2018-11-24 17:12:36 +02:00
|
|
|
this.events.emit("update", [newMessage, oldMessage]);
|
2019-01-06 14:39:16 +02:00
|
|
|
this.events.emit(`update:${id}`, [newMessage, oldMessage]);
|
2018-11-24 14:01:06 +02:00
|
|
|
}
|
2018-11-24 14:18:48 +02:00
|
|
|
|
|
|
|
async saveEditFromMsg(msg: Message) {
|
|
|
|
const newData = this.msgToSavedMessageData(msg);
|
|
|
|
return this.saveEdit(msg.id, newData);
|
|
|
|
}
|
2018-11-24 17:59:05 +02:00
|
|
|
|
|
|
|
async setPermanent(id: string) {
|
|
|
|
const savedMsg = await this.find(id);
|
|
|
|
if (savedMsg) {
|
|
|
|
await this.messages.update(
|
|
|
|
{ id },
|
|
|
|
{
|
2019-04-20 19:24:28 +03:00
|
|
|
is_permanent: true,
|
|
|
|
},
|
2018-11-24 17:59:05 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.toBePermanent.add(id);
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 14:39:16 +02:00
|
|
|
|
|
|
|
async onceMessageAvailable(id: string, handler: (msg: SavedMessage) => any, timeout: number = 60 * 1000) {
|
|
|
|
let called = false;
|
|
|
|
let onceEventListener;
|
|
|
|
let timeoutFn;
|
|
|
|
|
|
|
|
const callHandler = async (msg: SavedMessage) => {
|
|
|
|
this.events.off(`create:${id}`, onceEventListener);
|
|
|
|
clearTimeout(timeoutFn);
|
|
|
|
|
|
|
|
if (called) return;
|
|
|
|
called = true;
|
|
|
|
|
|
|
|
await handler(msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
onceEventListener = this.events.once(`create:${id}`, callHandler);
|
|
|
|
timeoutFn = setTimeout(() => {
|
|
|
|
called = true;
|
|
|
|
callHandler(null);
|
|
|
|
}, timeout);
|
|
|
|
|
|
|
|
const messageInDB = await this.find(id);
|
|
|
|
if (messageInDB) {
|
|
|
|
callHandler(messageInDB);
|
|
|
|
}
|
|
|
|
}
|
2018-11-24 14:01:06 +02:00
|
|
|
}
|