cache.threads

This commit is contained in:
Skillz4Killz
2021-06-13 13:34:18 +00:00
committed by GitHub
parent ee79d7da78
commit f2177e8ca6
6 changed files with 70 additions and 25 deletions
+39 -18
View File
@@ -6,6 +6,7 @@ import type { DiscordenoMember } from "./structures/member.ts";
import type { DiscordenoMessage } from "./structures/message.ts"; import type { DiscordenoMessage } from "./structures/message.ts";
import type { PresenceUpdate } from "./types/activity/presence_update.ts"; import type { PresenceUpdate } from "./types/activity/presence_update.ts";
import type { Emoji } from "./types/emojis/emoji.ts"; import type { Emoji } from "./types/emojis/emoji.ts";
import { Thread } from "./util/channel_to_thread.ts";
import { Collection } from "./util/collection.ts"; import { Collection } from "./util/collection.ts";
export const cache = { export const cache = {
@@ -35,6 +36,7 @@ export const cache = {
activeGuildIds: new Set<bigint>(), activeGuildIds: new Set<bigint>(),
dispatchedGuildIds: new Set<bigint>(), dispatchedGuildIds: new Set<bigint>(),
dispatchedChannelIds: new Set<bigint>(), dispatchedChannelIds: new Set<bigint>(),
threads: new Collection<bigint, Thread>(),
}; };
function messageSweeper(message: DiscordenoMessage) { function messageSweeper(message: DiscordenoMessage) {
@@ -107,28 +109,43 @@ export let cacheHandlers = {
filter, filter,
}; };
export type TableName = "guilds" | "unavailableGuilds" | "channels" | "messages" | "members" | "presences"; export type TableName = "guilds" | "unavailableGuilds" | "channels" | "messages" | "members" | "presences" | "threads";
function set(table: "guilds", key: bigint, value: DiscordenoGuild): Promise<Collection<bigint, DiscordenoGuild>>; async function set(table: "threads", key: bigint, value: Thread): Promise<Collection<bigint, Thread>>;
function set(table: "channels", key: bigint, value: DiscordenoChannel): Promise<Collection<bigint, DiscordenoChannel>>; async function set(table: "guilds", key: bigint, value: DiscordenoGuild): Promise<Collection<bigint, DiscordenoGuild>>;
function set(table: "messages", key: bigint, value: DiscordenoMessage): Promise<Collection<bigint, DiscordenoMessage>>; async function set(
function set(table: "members", key: bigint, value: DiscordenoMember): Promise<Collection<bigint, DiscordenoMember>>; table: "channels",
function set(table: "presences", key: bigint, value: PresenceUpdate): Promise<Collection<bigint, PresenceUpdate>>; key: bigint,
function set(table: "unavailableGuilds", key: bigint, value: number): Promise<Collection<bigint, number>>; value: DiscordenoChannel
): Promise<Collection<bigint, DiscordenoChannel>>;
async function set(
table: "messages",
key: bigint,
value: DiscordenoMessage
): Promise<Collection<bigint, DiscordenoMessage>>;
async function set(
table: "members",
key: bigint,
value: DiscordenoMember
): Promise<Collection<bigint, DiscordenoMember>>;
async function set(table: "presences", key: bigint, value: PresenceUpdate): Promise<Collection<bigint, PresenceUpdate>>;
async function set(table: "unavailableGuilds", key: bigint, value: number): Promise<Collection<bigint, number>>;
async function set(table: TableName, key: bigint, value: any) { async function set(table: TableName, key: bigint, value: any) {
return cache[table].set(key, value); return cache[table].set(key, value);
} }
function get(table: "guilds", key: bigint): Promise<DiscordenoGuild | undefined>; async function get(table: "threads", key: bigint): Promise<Thread | undefined>;
function get(table: "channels", key: bigint): Promise<DiscordenoChannel | undefined>; async function get(table: "guilds", key: bigint): Promise<DiscordenoGuild | undefined>;
function get(table: "messages", key: bigint): Promise<DiscordenoMessage | undefined>; async function get(table: "channels", key: bigint): Promise<DiscordenoChannel | undefined>;
function get(table: "members", key: bigint): Promise<DiscordenoMember | undefined>; async function get(table: "messages", key: bigint): Promise<DiscordenoMessage | undefined>;
function get(table: "presences", key: bigint): Promise<PresenceUpdate | undefined>; async function get(table: "members", key: bigint): Promise<DiscordenoMember | undefined>;
function get(table: "unavailableGuilds", key: bigint): Promise<number | undefined>; async function get(table: "presences", key: bigint): Promise<PresenceUpdate | undefined>;
async function get(table: "unavailableGuilds", key: bigint): Promise<number | undefined>;
async function get(table: TableName, key: bigint) { async function get(table: TableName, key: bigint) {
return cache[table].get(key); return cache[table].get(key);
} }
function forEach(table: "threads", callback: (value: Thread, key: bigint, map: Map<bigint, Thread>) => unknown): void;
function forEach( function forEach(
table: "guilds", table: "guilds",
callback: (value: DiscordenoGuild, key: bigint, map: Map<bigint, DiscordenoGuild>) => unknown callback: (value: DiscordenoGuild, key: bigint, map: Map<bigint, DiscordenoGuild>) => unknown
@@ -153,23 +170,27 @@ function forEach(table: TableName, callback: (value: any, key: bigint, map: Map<
return cache[table].forEach(callback); return cache[table].forEach(callback);
} }
function filter( async function filter(
table: "threads",
callback: (value: Thread, key: bigint) => boolean
): Promise<Collection<bigint, Thread>>;
async function filter(
table: "guilds", table: "guilds",
callback: (value: DiscordenoGuild, key: bigint) => boolean callback: (value: DiscordenoGuild, key: bigint) => boolean
): Promise<Collection<bigint, DiscordenoGuild>>; ): Promise<Collection<bigint, DiscordenoGuild>>;
function filter( async function filter(
table: "unavailableGuilds", table: "unavailableGuilds",
callback: (value: number, key: bigint) => boolean callback: (value: number, key: bigint) => boolean
): Promise<Collection<bigint, number>>; ): Promise<Collection<bigint, number>>;
function filter( async function filter(
table: "channels", table: "channels",
callback: (value: DiscordenoChannel, key: bigint) => boolean callback: (value: DiscordenoChannel, key: bigint) => boolean
): Promise<Collection<bigint, DiscordenoChannel>>; ): Promise<Collection<bigint, DiscordenoChannel>>;
function filter( async function filter(
table: "messages", table: "messages",
callback: (value: DiscordenoMessage, key: bigint) => boolean callback: (value: DiscordenoMessage, key: bigint) => boolean
): Promise<Collection<bigint, DiscordenoMessage>>; ): Promise<Collection<bigint, DiscordenoMessage>>;
function filter( async function filter(
table: "members", table: "members",
callback: (value: DiscordenoMember, key: bigint) => boolean callback: (value: DiscordenoMember, key: bigint) => boolean
): Promise<Collection<bigint, DiscordenoMember>>; ): Promise<Collection<bigint, DiscordenoMember>>;
+4 -4
View File
@@ -1,14 +1,14 @@
import { eventHandlers } from "../../bot.ts"; import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { Channel } from "../../types/channels/channel.ts"; import { Channel } from "../../types/channels/channel.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { channelToThread } from "../../util/channel_to_thread.ts";
export async function handleThreadCreate(data: DiscordGatewayPayload) { export async function handleThreadCreate(data: DiscordGatewayPayload) {
const payload = data.d as Channel; const payload = data.d as Channel;
const discordenoChannel = await structures.createDiscordenoChannel(payload); const thread = channelToThread(payload);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel); await cacheHandlers.set("threads", thread.id, thread);
eventHandlers.threadCreate?.(discordenoChannel); eventHandlers.threadCreate?.(thread);
} }
+3 -3
View File
@@ -7,12 +7,12 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleThreadDelete(data: DiscordGatewayPayload) { export async function handleThreadDelete(data: DiscordGatewayPayload) {
const payload = data.d as Channel; const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get("channels", snowflakeToBigint(payload.id)); const cachedChannel = await cacheHandlers.get("threads", snowflakeToBigint(payload.id));
if (!cachedChannel) return; if (!cachedChannel) return;
await cacheHandlers.delete("channels", snowflakeToBigint(payload.id)); await cacheHandlers.delete("threads", snowflakeToBigint(payload.id));
cacheHandlers.forEach("messages", (message) => { cacheHandlers.forEach("messages", (message) => {
eventHandlers.debug?.("loop", `Running forEach messages loop in CHANNEL_DELTE file.`); eventHandlers.debug?.("loop", `Running forEach messages loop in THREAD_DELETE file.`);
if (message.channelId === snowflakeToBigint(payload.id)) { if (message.channelId === snowflakeToBigint(payload.id)) {
cacheHandlers.delete("messages", message.id); cacheHandlers.delete("messages", message.id);
} }
+2
View File
@@ -110,6 +110,8 @@ export async function createDiscordenoChannel(data: Channel, guildId?: bigint) {
(Object.keys(rest) as (keyof typeof rest)[]).forEach((key) => { (Object.keys(rest) as (keyof typeof rest)[]).forEach((key) => {
eventHandlers.debug?.("loop", `Running forEach loop in createDiscordenoChannel function.`); eventHandlers.debug?.("loop", `Running forEach loop in createDiscordenoChannel function.`);
if (key === "threadMetadata") return;
props[key] = createNewProp( props[key] = createNewProp(
CHANNEL_SNOWFLAKES.includes(key) ? (rest[key] ? snowflakeToBigint(rest[key] as string) : undefined) : rest[key] CHANNEL_SNOWFLAKES.includes(key) ? (rest[key] ? snowflakeToBigint(rest[key] as string) : undefined) : rest[key]
); );
@@ -6,6 +6,8 @@ export interface ThreadMetadata {
// TODO(threads): channel struct should convert this to a unixx // TODO(threads): channel struct should convert this to a unixx
/** Timestamp when the thread's archive status was last changed, used for calculating recent activity */ /** Timestamp when the thread's archive status was last changed, used for calculating recent activity */
archiveTimestamp: string; archiveTimestamp: string;
/** id of the user that last archived or unarchived the thread */
archiverId?: string;
/** When a thread is locked, only users with `MANAGE_THREADS` can unarchive it */ /** When a thread is locked, only users with `MANAGE_THREADS` can unarchive it */
locked?: boolean; locked?: boolean;
} }
+20
View File
@@ -0,0 +1,20 @@
import { Channel } from "../types/channels/channel.ts";
import { snowflakeToBigint } from "./bigint.ts";
export function channelToThread(channel: Channel) {
return {
id: snowflakeToBigint(channel.id),
channelId: snowflakeToBigint(channel.parentId!),
memberCount: channel.memberCount,
messageCount: channel.messageCount,
archived: channel.threadMetadata?.archived || false,
archiveTimestamp: channel.threadMetadata?.archiveTimestamp
? Date.parse(channel.threadMetadata.archiveTimestamp)
: undefined,
archiverId: channel.threadMetadata?.archiverId ? snowflakeToBigint(channel.threadMetadata.archiverId) : undefined,
autoArchiveDuration: channel.threadMetadata?.autoArchiveDuration || 0,
locked: channel.threadMetadata?.locked || false,
};
}
export type Thread = ReturnType<typeof channelToThread>