cordis/client/webapp/guild-personal-db.ts

121 lines
4.9 KiB
TypeScript
Raw Normal View History

2021-12-01 06:07:48 +00:00
import * as electronRemote from '@electron/remote';
const electronConsole = electronRemote.getGlobal('console') as Console;
import Logger from '../../logger/logger';
const LOG = Logger.create(__filename, electronConsole);
2021-11-21 01:06:50 +00:00
import { AsyncFetchable, AsyncLackable } from "./guild-types";
import { Channel, GuildMetadata, Member, Message, Resource, Token } from "./data-types";
import PersonalDB from "./personal-db";
2021-11-21 01:06:50 +00:00
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
2021-11-21 01:06:50 +00:00
) {}
// Fetched Methods
async fetchMetadata(): Promise<GuildMetadata | null> {
2021-12-01 06:07:48 +00:00
return this.db.fetchGuild(this.guildId);
}
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); })();
}
2021-11-21 01:06:50 +00:00
async handleMembersAdded(addedMembers: Member[]): Promise<void> {
2021-12-01 06:07:48 +00:00
LOG.debug(addedMembers.length + ' personal members added');
await this.db.addMembers(this.guildId, addedMembers);
}
async handleMembersChanged(changedMembers: Member[]): Promise<void> {
2021-12-01 06:07:48 +00:00
LOG.debug(changedMembers.length + ' personal members changed');
await this.db.updateMembers(this.guildId, changedMembers);
}
async handleMembersDeleted(deletedMembers: Member[]): Promise<void> {
2021-12-01 06:07:48 +00:00
LOG.debug(deletedMembers.length + ' personal members deleted');
await this.db.deleteMembers(this.guildId, deletedMembers);
}
2021-11-21 01:06:50 +00:00
async handleChannelsAdded(addedChannels: Channel[]): Promise<void> {
2021-12-01 06:07:48 +00:00
LOG.debug(addedChannels.length + ' personal channels added');
await this.db.addChannels(this.guildId, addedChannels);
}
async handleChannelsChanged(changedChannels: Channel[]): Promise<void> {
2021-12-01 06:07:48 +00:00
LOG.debug(changedChannels.length + ' personal channels changed');
await this.db.updateChannels(this.guildId, changedChannels);
}
async handleChannelsDeleted(deletedChannels: Channel[]): Promise<void> {
2021-12-01 06:07:48 +00:00
LOG.debug(deletedChannels.length + ' personal channels deleted');
await this.db.deleteChannels(this.guildId, deletedChannels);
}
2021-11-21 01:06:50 +00:00
async handleMessagesAdded(addedMessages: Message[]): Promise<void> {
2021-12-01 06:07:48 +00:00
LOG.debug(addedMessages.length + ' personal messages added');
await this.db.addMessages(this.guildId, addedMessages);
}
async handleMessagesChanged(changedMessages: Message[]): Promise<void> {
2021-12-01 06:07:48 +00:00
LOG.debug(changedMessages.length + ' personal messages changed');
await this.db.updateMessages(this.guildId, changedMessages);
}
async handleMessagesDeleted(deletedMessages: Message[]): Promise<void> {
2021-12-01 06:07:48 +00:00
LOG.debug(deletedMessages.length + ' personal messages deleted');
await this.db.deleteMessages(this.guildId, deletedMessages);
}
2021-11-21 01:06:50 +00:00
async handleResourceAdded(addedResource: Resource): Promise<void> {
2021-12-01 06:07:48 +00:00
//LOG.debug('personal resoprces added', { addedResource });
await this.db.addResources(this.guildId, [ addedResource ]);
}
async handleResourceChanged(changedResource: Resource): Promise<void> {
2021-12-01 06:07:48 +00:00
//LOG.debug('personal resoprces changed', { changedResource });
await this.db.updateResources(this.guildId, [ changedResource ]);
}
async handleResourceDeleted(deletedResource: Resource): Promise<void> {
2021-12-01 06:07:48 +00:00
//LOG.debug('personal resoprces deleted', { deletedResource });
await this.db.deleteResources(this.guildId, [ deletedResource ]);
}
2021-11-21 01:06:50 +00:00
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
}
2021-12-02 03:48:24 +00:00
}