|
@@ -1,21 +1,23 @@
|
|
|
import TurndownService, { Options } from "turndown";
|
|
|
-import { db } from "../db";
|
|
|
import interval from "interval-promise";
|
|
|
import client from "../client";
|
|
|
import sha1 from "sha1";
|
|
|
import * as path from "path";
|
|
|
import * as fs from "fs";
|
|
|
-import { ObjectChain } from "lodash";
|
|
|
|
|
|
import { IAggregator, NewsPostItem, INewsPostData } from "./aggregators/aggregator";
|
|
|
import { ICommand } from "./command";
|
|
|
import { RichEmbed, TextChannel, Message, Channel } from "discord.js";
|
|
|
+import { getRepository } from "typeorm";
|
|
|
+import { KnownChannel } from "../entity/KnownChannel";
|
|
|
+import { AggroNewsItem } from "../entity/AggroNewsItem";
|
|
|
|
|
|
const UPDATE_INTERVAL = 5;
|
|
|
const MAX_PREVIEW_LENGTH = 300;
|
|
|
|
|
|
const aggregators : IAggregator[] = [];
|
|
|
-const aggregateChannelID = db.get("aggregateChannel").value() as string;
|
|
|
+let aggregateChannelID : string = null;
|
|
|
+const AGGREGATOR_MANAGER_CHANNEL = "aggregatorManager";
|
|
|
|
|
|
// TODO: Run BBCode converter instead
|
|
|
const turndown = new TurndownService();
|
|
@@ -66,16 +68,24 @@ function clipText(text: string) {
|
|
|
|
|
|
// TODO: Replace with proper forum implementation
|
|
|
async function addNewsItem(item: NewsPostItem) {
|
|
|
- let aggrItems = db.get("aggregatedItemsCache") as ObjectChain<any>;
|
|
|
+ let repo = getRepository(AggroNewsItem);
|
|
|
|
|
|
- if(aggrItems.has(item.id).value()) {
|
|
|
- let postedItem = aggrItems.get(item.id).value() as INewsPostData;
|
|
|
+ let newsItem = await repo.findOne({
|
|
|
+ where: { feedName: item.feedId, newsId: item.newsId }
|
|
|
+ });
|
|
|
|
|
|
+ if(newsItem) {
|
|
|
// No changes, skip
|
|
|
- if(postedItem.hash == item.hash)
|
|
|
+ if(newsItem.hash == item.hash)
|
|
|
return;
|
|
|
else
|
|
|
- await deleteCacheMessage(postedItem.cacheMessageId);
|
|
|
+ await deleteCacheMessage(newsItem.editMessageId);
|
|
|
+ } else {
|
|
|
+ newsItem = repo.create({
|
|
|
+ newsId: item.newsId,
|
|
|
+ feedName: item.feedId,
|
|
|
+ hash: item.hash
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
let ch = client.channels.get(aggregateChannelID);
|
|
@@ -97,12 +107,9 @@ async function addNewsItem(item: NewsPostItem) {
|
|
|
}
|
|
|
})) as Message;
|
|
|
|
|
|
- aggrItems.set(item.id, {
|
|
|
- hash: item.hash,
|
|
|
- cacheMessageId: msg.id,
|
|
|
- postedMessageId: null
|
|
|
- });
|
|
|
- db.write();
|
|
|
+ newsItem.editMessageId = msg.id;
|
|
|
+
|
|
|
+ await repo.save(newsItem);
|
|
|
}
|
|
|
|
|
|
async function deleteCacheMessage(messageId: string) {
|
|
@@ -148,7 +155,14 @@ function initAggregators() {
|
|
|
}
|
|
|
|
|
|
export default {
|
|
|
- onStart : () => {
|
|
|
+ onStart : async () => {
|
|
|
+ let repo = getRepository(KnownChannel);
|
|
|
+
|
|
|
+ let ch = await repo.findOne({
|
|
|
+ where: { channelType: AGGREGATOR_MANAGER_CHANNEL }
|
|
|
+ });
|
|
|
+ aggregateChannelID = ch.channelId;
|
|
|
+
|
|
|
initAggregators();
|
|
|
interval(checkFeeds, UPDATE_INTERVAL * 60 * 1000);
|
|
|
}
|