-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
db.ts
56 lines (48 loc) · 1.45 KB
/
db.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import config from "./env.ts";
import { connect } from "redis";
const redis = await connect({
hostname: config.REDIS_URI.split(":")[0],
port: config.REDIS_URI.split(":")[1],
password: config.REDIS_PASSWORD,
});
export async function getAllFeeds() {
return JSON.parse(await redis.get("FEEDS") || "{}");
}
export async function addFeed(chatID: number, channelID: string) {
const allFeeds = await getAllFeeds();
if (!allFeeds[channelID]) {
allFeeds[channelID] = [];
}
let res = false;
if (!allFeeds[channelID].includes(chatID)) {
allFeeds[channelID].push(chatID);
res = true;
}
await redis.set("FEEDS", JSON.stringify(allFeeds));
return res;
}
export async function removeFeed(chatID: number, channelID: string) {
const allFeeds = await getAllFeeds();
if (allFeeds[channelID]) {
const newList = [];
for (const id of allFeeds[channelID]) {
if (id != chatID) {
newList.push(id);
}
}
allFeeds[channelID] = newList;
}
await redis.set("FEEDS", JSON.stringify(allFeeds));
}
export async function getAllLatest() {
return JSON.parse(await redis.get("LATEST") || "{}");
}
export async function storeLatest(channelID: string, latest: string) {
const allLatest = await getAllLatest();
allLatest[channelID] = latest;
await redis.set("LATEST", JSON.stringify(allLatest));
}
export async function getLatest(channelID: string) {
const allLatest = await getAllLatest();
return allLatest[channelID];
}