From 7e96b5dcfbf29dfa0fe6c61e861d8441967289d0 Mon Sep 17 00:00:00 2001 From: V Date: Tue, 18 Apr 2023 18:52:46 +0200 Subject: [PATCH] RelationShipNotifier: Support multiple users (#944) --- src/plugins/relationshipNotifier/events.ts | 5 +++-- src/plugins/relationshipNotifier/utils.ts | 23 +++++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/plugins/relationshipNotifier/events.ts b/src/plugins/relationshipNotifier/events.ts index 1600484f..0c6914d9 100644 --- a/src/plugins/relationshipNotifier/events.ts +++ b/src/plugins/relationshipNotifier/events.ts @@ -19,7 +19,7 @@ import { FluxEvents } from "@webpack/types"; import { onChannelDelete, onGuildDelete, onRelationshipRemove } from "./functions"; -import { syncFriends, syncGroups, syncGuilds } from "./utils"; +import { syncAndRunChecks, syncFriends, syncGroups, syncGuilds } from "./utils"; export const FluxHandlers: Partial void>>> = { GUILD_CREATE: [syncGuilds], @@ -28,7 +28,8 @@ export const FluxHandlers: Partial void> CHANNEL_DELETE: [onChannelDelete], RELATIONSHIP_ADD: [syncFriends], RELATIONSHIP_UPDATE: [syncFriends], - RELATIONSHIP_REMOVE: [syncFriends, onRelationshipRemove] + RELATIONSHIP_REMOVE: [syncFriends, onRelationshipRemove], + CONNECTION_OPEN: [syncAndRunChecks] }; export function forEachEvent(fn: (event: FluxEvents, handler: (data: any) => void) => void) { diff --git a/src/plugins/relationshipNotifier/utils.ts b/src/plugins/relationshipNotifier/utils.ts index 09a329a3..39b7fce7 100644 --- a/src/plugins/relationshipNotifier/utils.ts +++ b/src/plugins/relationshipNotifier/utils.ts @@ -18,7 +18,7 @@ import { DataStore, Notices } from "@api/index"; import { showNotification } from "@api/Notifications"; -import { ChannelStore, GuildStore, RelationshipStore, UserUtils } from "@webpack/common"; +import { ChannelStore, GuildStore, RelationshipStore, UserStore, UserUtils } from "@webpack/common"; import settings from "./settings"; import { ChannelType, RelationshipType, SimpleGroupChannel, SimpleGuild } from "./types"; @@ -30,11 +30,20 @@ const friends = { requests: [] as string[] }; +const guildsKey = () => `relationship-notifier-guilds-${UserStore.getCurrentUser().id}`; +const groupsKey = () => `relationship-notifier-groups-${UserStore.getCurrentUser().id}`; +const friendsKey = () => `relationship-notifier-friends-${UserStore.getCurrentUser().id}`; + +async function runMigrations() { + DataStore.delMany(["relationship-notifier-guilds", "relationship-notifier-groups", "relationship-notifier-friends"]); +} + export async function syncAndRunChecks() { + await runMigrations(); const [oldGuilds, oldGroups, oldFriends] = await DataStore.getMany([ - "relationship-notifier-guilds", - "relationship-notifier-groups", - "relationship-notifier-friends" + guildsKey(), + groupsKey(), + friendsKey() ]) as [Map | undefined, Map | undefined, Record<"friends" | "requests", string[]> | undefined]; await Promise.all([syncGuilds(), syncGroups(), syncFriends()]); @@ -104,7 +113,7 @@ export async function syncGuilds() { iconURL: icon && `https://cdn.discordapp.com/icons/${id}/${icon}.png` }); } - await DataStore.set("relationship-notifier-guilds", guilds); + await DataStore.set(guildsKey(), guilds); } export function getGroup(id: string) { @@ -126,7 +135,7 @@ export async function syncGroups() { }); } - await DataStore.set("relationship-notifier-groups", groups); + await DataStore.set(groupsKey(), groups); } export async function syncFriends() { @@ -145,5 +154,5 @@ export async function syncFriends() { } } - await DataStore.set("relationship-notifier-friends", friends); + await DataStore.set(friendsKey(), friends); }