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 { SyncFetchable, SyncLackable } from "./guild-types"; import { GuildMetadata, Member, Channel, Message, Resource, Token } from "./data-types"; import MessageRAMCache from "./message-ram-cache"; import ResourceRAMCache from "./resource-ram-cache"; export default class RAMGuild implements SyncFetchable, SyncLackable { private metadata: GuildMetadata | null = null; private members = new Map(); // id -> member private channels = new Map(); // id -> channel constructor( private readonly recentMessages: MessageRAMCache, private readonly resources: ResourceRAMCache, private readonly guildId: number ) {} getMembers(): Map { return this.members; } getChannels(): Map { return this.channels; } fetchMetadata(): GuildMetadata | null { return this.metadata; } fetchMembers(): Member[] | null { return this.members.size === 0 ? null : Array.from(this.members.values()); } fetchChannels(): Channel[] | null { return this.channels.size === 0 ? null : Array.from(this.channels.values()); } fetchMessagesRecent(channelId: string, number: number): Message[] | null { return this.recentMessages.fetchRecentMessages(this.guildId, channelId, number); } fetchMessagesBefore(channelId: string, messageId: string, number: number): Message[] | null { return null; // not planning on doing this unless someone wants to do a scrolling cache } fetchMessagesAfter(channelId: string, messageId: string, number: number): Message[] | null { return null; // not planning on doing this unless someone wants to do a scrolling cache } fetchResource(resourceId: string): Resource | null { return this.resources.getResource(this.guildId, resourceId); } fetchTokens(): Token[] | null { return null; // TODO: token ram cache } handleMetadataChanged(changedMetaData: GuildMetadata): void { this.metadata = changedMetaData; } handleMembersAdded(addedMembers: Member[]): void { // LOG.debug('RAM members added', { addedMembers }); for (let member of addedMembers) { this.members.set(member.id, member); } } handleMembersChanged(changedMembers: Member[]): void { // LOG.debug('RAM members changed', { changedMembers }); for (let member of changedMembers) { if (this.members.has(member.id)) this.members.set(member.id, member); } } handleMembersDeleted(deletedMembers: Member[]): void { // LOG.debug('RAM members deleted', { deletedMembers }); for (let member of deletedMembers) { this.members.delete(member.id); } } handleChannelsAdded(addedChannels: Channel[]): void { for (let channel of addedChannels) { this.channels.set(channel.id, channel); } } handleChannelsChanged(changedChannels: Channel[]): void { for (let channel of changedChannels) { if (this.channels.has(channel.id)) this.channels.set(channel.id, channel); } } handleChannelsDeleted(deletedChannels: Channel[]): void { for (let channel of deletedChannels) { this.channels.delete(channel.id); } } handleMessagesAdded(addedMessages: Message[]): void { let byChannel = new Map(); for (let message of addedMessages) { if (!byChannel.has(message.channel.id)) { byChannel.set(message.channel.id, [ message ]); } else { byChannel.get(message.channel.id)?.push(message); } } for (let [ channelId, messages ] of byChannel.entries()) { this.recentMessages.upsertMessages(this.guildId, channelId, messages); } } handleMessagesChanged(changedMessages: Message[]): void { let byChannel = new Map(); for (let message of changedMessages) { if (!byChannel.has(message.channel.id)) { byChannel.set(message.channel.id, [ message ]); } else { byChannel.get(message.channel.id)?.push(message); } } for (let [ channelId, messages ] of byChannel.entries()) { this.recentMessages.upsertMessages(this.guildId, channelId, messages); } } handleMessagesDeleted(deletedMessages: Message[]): void { let byChannel = new Map(); for (let message of deletedMessages) { if (!byChannel.has(message.channel.id)) { byChannel.set(message.channel.id, [ message ]); } else { byChannel.get(message.channel.id)?.push(message); } } for (let [ channelId, messages ] of byChannel.entries()) { this.recentMessages.deleteMessages(this.guildId, channelId, messages); } } handleResourceAdded(addedResource: Resource): void { this.resources.putResource(this.guildId, addedResource); } handleResourceChanged(changedResource: Resource): void { // This should be rare/never happen this.resources.putResource(this.guildId, changedResource); } handleResourceDeleted(deletedResource: Resource): void { // do nothing, the cache will garbage collect itself :) // also resource deletion should be pretty rare, especially in a cache return; } handleTokensAdded(addedTokens: Token[]): void { return; // TODO: token ram cache } handleTokensChanged(changedTokens: Token[]): void { return; // TODO: token ram cache } handleTokensDeleted(deletedTokens: Token[]): void { return; // TODO: token ram cache } }