2018-08-18 19:51:28 +03:00
|
|
|
import uuid from "uuid/v4"; // tslint:disable-line
|
|
|
|
import moment from "moment-timezone";
|
2018-10-26 06:41:20 +03:00
|
|
|
import { ArchiveEntry } from "./entities/ArchiveEntry";
|
|
|
|
import { getRepository, Repository } from "typeorm";
|
|
|
|
import { BaseRepository } from "./BaseRepository";
|
2018-11-24 18:39:17 +02:00
|
|
|
import { formatTemplateString, trimLines } from "../utils";
|
|
|
|
import { SavedMessage } from "./entities/SavedMessage";
|
|
|
|
import { Channel, Guild, User } from "eris";
|
2018-08-18 19:51:28 +03:00
|
|
|
|
|
|
|
const DEFAULT_EXPIRY_DAYS = 30;
|
|
|
|
|
2018-11-24 18:39:17 +02:00
|
|
|
const MESSAGE_ARCHIVE_HEADER_FORMAT = trimLines(`
|
|
|
|
Server: {guild.name} ({guild.id})
|
|
|
|
`);
|
2018-12-22 13:06:40 +02:00
|
|
|
const MESSAGE_ARCHIVE_MESSAGE_FORMAT =
|
|
|
|
"[#{channel.name}] [{user.id}] [{timestamp}] {user.username}#{user.discriminator}: {content}{attachments}";
|
2018-11-24 18:39:17 +02:00
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
export class GuildArchives extends BaseRepository {
|
|
|
|
protected archives: Repository<ArchiveEntry>;
|
2018-08-18 19:51:28 +03:00
|
|
|
|
|
|
|
constructor(guildId) {
|
2018-10-26 06:41:20 +03:00
|
|
|
super(guildId);
|
|
|
|
this.archives = getRepository(ArchiveEntry);
|
2018-08-18 19:51:28 +03:00
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
// Clean expired archives at start and then every hour
|
|
|
|
this.deleteExpiredArchives();
|
|
|
|
setInterval(() => this.deleteExpiredArchives(), 1000 * 60 * 60);
|
2018-08-18 19:51:28 +03:00
|
|
|
}
|
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
private deleteExpiredArchives() {
|
|
|
|
this.archives
|
|
|
|
.createQueryBuilder()
|
2018-11-24 14:01:06 +02:00
|
|
|
.where("guild_id = :guild_id", { guild_id: this.guildId })
|
2019-01-13 17:29:26 +02:00
|
|
|
.andWhere("expires_at IS NOT NULL")
|
2018-11-24 14:01:06 +02:00
|
|
|
.andWhere("expires_at <= NOW()")
|
2018-10-26 06:41:20 +03:00
|
|
|
.delete()
|
|
|
|
.execute();
|
|
|
|
}
|
2018-08-18 19:51:28 +03:00
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
async find(id: string): Promise<ArchiveEntry> {
|
|
|
|
return this.archives.findOne({
|
|
|
|
where: { id },
|
|
|
|
relations: this.getRelations()
|
|
|
|
});
|
2018-08-18 19:51:28 +03:00
|
|
|
}
|
|
|
|
|
2019-01-13 17:29:26 +02:00
|
|
|
async makePermanent(id: string): Promise<void> {
|
|
|
|
await this.archives.update(
|
|
|
|
{ id },
|
|
|
|
{
|
|
|
|
expires_at: null
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
/**
|
|
|
|
* @returns ID of the created entry
|
|
|
|
*/
|
|
|
|
async create(body: string, expiresAt: moment.Moment = null): Promise<string> {
|
2018-08-18 19:51:28 +03:00
|
|
|
if (!expiresAt) {
|
|
|
|
expiresAt = moment().add(DEFAULT_EXPIRY_DAYS, "days");
|
|
|
|
}
|
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
const result = await this.archives.insert({
|
2018-08-18 19:51:28 +03:00
|
|
|
guild_id: this.guildId,
|
|
|
|
body,
|
|
|
|
expires_at: expiresAt.format("YYYY-MM-DD HH:mm:ss")
|
|
|
|
});
|
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
return result.identifiers[0].id;
|
2018-08-18 19:51:28 +03:00
|
|
|
}
|
2018-11-24 18:39:17 +02:00
|
|
|
|
2018-12-22 13:06:40 +02:00
|
|
|
createFromSavedMessages(savedMessages: SavedMessage[], guild: Guild, expiresAt = null) {
|
2018-11-24 18:39:17 +02:00
|
|
|
if (expiresAt == null) expiresAt = moment().add(DEFAULT_EXPIRY_DAYS, "days");
|
|
|
|
|
2018-12-22 13:06:40 +02:00
|
|
|
const headerStr = formatTemplateString(MESSAGE_ARCHIVE_HEADER_FORMAT, { guild });
|
2018-11-24 18:39:17 +02:00
|
|
|
const msgLines = savedMessages.map(msg => {
|
2018-12-22 13:06:40 +02:00
|
|
|
const channel = guild.channels.get(msg.channel_id);
|
|
|
|
const user = { ...msg.data.author, id: msg.user_id };
|
|
|
|
|
2018-11-24 18:39:17 +02:00
|
|
|
return formatTemplateString(MESSAGE_ARCHIVE_MESSAGE_FORMAT, {
|
|
|
|
id: msg.id,
|
2018-12-22 13:06:40 +02:00
|
|
|
timestamp: moment(msg.posted_at).format("YYYY-MM-DD HH:mm:ss"),
|
2018-11-24 18:39:17 +02:00
|
|
|
content: msg.data.content,
|
2018-12-22 13:06:40 +02:00
|
|
|
user,
|
|
|
|
channel
|
2018-11-24 18:39:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const messagesStr = msgLines.join("\n");
|
|
|
|
|
2019-01-13 17:29:26 +02:00
|
|
|
return this.create([headerStr, messagesStr].join("\n\n"), expiresAt);
|
2018-11-24 18:39:17 +02:00
|
|
|
}
|
2019-01-15 04:03:04 +02:00
|
|
|
|
|
|
|
getUrl(baseUrl, archiveId) {
|
|
|
|
return baseUrl ? `${baseUrl}/archives/${archiveId}` : `Archive ID: ${archiveId}`;
|
|
|
|
}
|
2018-08-18 19:51:28 +03:00
|
|
|
}
|