cordis/client/webapp/guild-personal-db.ts
2021-11-20 19:06:50 -06:00

104 lines
3.9 KiB
TypeScript

import { AsyncFetchable, AsyncLackable } from "./guild-types";
import { Channel, GuildMetadata, Member, Message, Resource, Token } from "./data-types";
import PersonalDB from "./personal-db";
export default class PersonalDBGuild implements AsyncFetchable, AsyncLackable {
constructor(
private readonly db: PersonalDB,
private readonly guildId: number,
private readonly memberId: string, // would be used if we want to cache-update the status/display name of members
) {}
// Fetched Methods
async fetchMetadata(): Promise<GuildMetadata | null> {
// TODO
return null;
}
async fetchMembers(): Promise<Member[] | null> {
return await this.db.fetchMembers(this.guildId);
}
async fetchChannels(): Promise<Channel[] | null> {
return await this.db.fetchChannels(this.guildId);
}
async fetchMessagesRecent(channelId: string, number: number): Promise<Message[] | null> {
return await this.db.fetchMessagesRecent(this.guildId, channelId, number);
}
async fetchMessagesBefore(channelId: string, messageId: string, number: number): Promise<Message[] | null> {
return await this.db.fetchMessagesBefore(this.guildId, channelId, messageId, number);
}
async fetchMessagesAfter(channelId: string, messageId: string, number: number): Promise<Message[] | null> {
return await this.db.fetchMessagesAfter(this.guildId, channelId, messageId, number);
}
async fetchResource(resourceId: string): Promise<Resource | null> {
return await this.db.fetchResource(this.guildId, resourceId);
}
async fetchTokens(): Promise<Token[] | null> {
return null; // personal db currently does not handle tokens
}
// Lacking Methods (resolving differences)
async handleMetadataChanged(changedMetaData: GuildMetadata): Promise<void> {
(async () => { await this.db.updateGuildName(this.guildId, changedMetaData.name); })();
(async () => { await this.db.updateGuildIcon(this.guildId, changedMetaData.iconResourceId); })();
}
async handleMembersAdded(addedMembers: Member[]): Promise<void> {
await this.db.addMembers(this.guildId, addedMembers);
}
async handleMembersChanged(changedMembers: Member[]): Promise<void> {
await this.db.updateMembers(this.guildId, changedMembers);
}
async handleMembersDeleted(deletedMembers: Member[]): Promise<void> {
await this.db.deleteMembers(this.guildId, deletedMembers);
}
async handleChannelsAdded(addedChannels: Channel[]): Promise<void> {
await this.db.addChannels(this.guildId, addedChannels);
}
async handleChannelsChanged(changedChannels: Channel[]): Promise<void> {
await this.db.updateChannels(this.guildId, changedChannels);
}
async handleChannelsDeleted(deletedChannels: Channel[]): Promise<void> {
await this.db.deleteChannels(this.guildId, deletedChannels);
}
async handleMessagesAdded(addedMessages: Message[]): Promise<void> {
await this.db.addMessages(this.guildId, addedMessages);
}
async handleMessagesChanged(changedMessages: Message[]): Promise<void> {
await this.db.updateMessages(this.guildId, changedMessages);
}
async handleMessagesDeleted(deletedMessages: Message[]): Promise<void> {
await this.db.deleteMessages(this.guildId, deletedMessages);
}
async handleResourceAdded(addedResource: Resource): Promise<void> {
await this.db.addResources(this.guildId, [ addedResource ]);
}
async handleResourceChanged(changedResource: Resource): Promise<void> {
await this.db.updateResources(this.guildId, [ changedResource ]);
}
async handleResourceDeleted(deletedResource: Resource): Promise<void> {
await this.db.deleteResources(this.guildId, [ deletedResource ]);
}
async handleTokensAdded(addedTokens: Token[]): Promise<void> {
return; // cache currently does not handle tokens
}
async handleTokensChanged(changedTokens: Token[]): Promise<void> {
return; // cache currently does not handle tokens
}
async handleTokensDeleted(deletedTokens: Token[]): Promise<void> {
return; // cache currently does not handle tokens
}
}