121 lines
4.9 KiB
TypeScript
121 lines
4.9 KiB
TypeScript
import * as electronRemote from '@electron/remote';
|
|
const electronConsole = electronRemote.getGlobal('console') as Console;
|
|
import Logger from '../../logger/logger';
|
|
const LOG = Logger.create(__filename, electronConsole);
|
|
|
|
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> {
|
|
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); })();
|
|
}
|
|
|
|
async handleMembersAdded(addedMembers: Member[]): Promise<void> {
|
|
LOG.debug(addedMembers.length + ' personal members added');
|
|
await this.db.addMembers(this.guildId, addedMembers);
|
|
}
|
|
async handleMembersChanged(changedMembers: Member[]): Promise<void> {
|
|
LOG.debug(changedMembers.length + ' personal members changed');
|
|
await this.db.updateMembers(this.guildId, changedMembers);
|
|
}
|
|
async handleMembersDeleted(deletedMembers: Member[]): Promise<void> {
|
|
LOG.debug(deletedMembers.length + ' personal members deleted');
|
|
await this.db.deleteMembers(this.guildId, deletedMembers);
|
|
}
|
|
|
|
async handleChannelsAdded(addedChannels: Channel[]): Promise<void> {
|
|
LOG.debug(addedChannels.length + ' personal channels added');
|
|
await this.db.addChannels(this.guildId, addedChannels);
|
|
}
|
|
async handleChannelsChanged(changedChannels: Channel[]): Promise<void> {
|
|
LOG.debug(changedChannels.length + ' personal channels changed');
|
|
await this.db.updateChannels(this.guildId, changedChannels);
|
|
}
|
|
async handleChannelsDeleted(deletedChannels: Channel[]): Promise<void> {
|
|
LOG.debug(deletedChannels.length + ' personal channels deleted');
|
|
await this.db.deleteChannels(this.guildId, deletedChannels);
|
|
}
|
|
|
|
async handleMessagesAdded(addedMessages: Message[]): Promise<void> {
|
|
LOG.debug(addedMessages.length + ' personal messages added');
|
|
await this.db.addMessages(this.guildId, addedMessages);
|
|
}
|
|
async handleMessagesChanged(changedMessages: Message[]): Promise<void> {
|
|
LOG.debug(changedMessages.length + ' personal messages changed');
|
|
await this.db.updateMessages(this.guildId, changedMessages);
|
|
}
|
|
async handleMessagesDeleted(deletedMessages: Message[]): Promise<void> {
|
|
LOG.debug(deletedMessages.length + ' personal messages deleted');
|
|
await this.db.deleteMessages(this.guildId, deletedMessages);
|
|
}
|
|
|
|
async handleResourceAdded(addedResource: Resource): Promise<void> {
|
|
//LOG.debug('personal resoprces added', { addedResource });
|
|
await this.db.addResources(this.guildId, [ addedResource ]);
|
|
}
|
|
async handleResourceChanged(changedResource: Resource): Promise<void> {
|
|
//LOG.debug('personal resoprces changed', { changedResource });
|
|
await this.db.updateResources(this.guildId, [ changedResource ]);
|
|
}
|
|
async handleResourceDeleted(deletedResource: Resource): Promise<void> {
|
|
//LOG.debug('personal resoprces deleted', { deletedResource });
|
|
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
|
|
}
|
|
}
|