RelationShipNotifier: Support multiple users (#944)

This commit is contained in:
V 2023-04-18 18:52:46 +02:00 committed by GitHub
parent 99a7d78e9b
commit 7e96b5dcfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 9 deletions

@ -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<Record<FluxEvents, Array<(data: any) => void>>> = {
GUILD_CREATE: [syncGuilds],
@ -28,7 +28,8 @@ export const FluxHandlers: Partial<Record<FluxEvents, Array<(data: any) => 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) {

@ -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<string, SimpleGuild> | undefined, Map<string, SimpleGroupChannel> | 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);
}