RelationShipNotifier: Support multiple users (#944)
This commit is contained in:
parent
99a7d78e9b
commit
7e96b5dcfb
@ -19,7 +19,7 @@
|
|||||||
import { FluxEvents } from "@webpack/types";
|
import { FluxEvents } from "@webpack/types";
|
||||||
|
|
||||||
import { onChannelDelete, onGuildDelete, onRelationshipRemove } from "./functions";
|
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>>> = {
|
export const FluxHandlers: Partial<Record<FluxEvents, Array<(data: any) => void>>> = {
|
||||||
GUILD_CREATE: [syncGuilds],
|
GUILD_CREATE: [syncGuilds],
|
||||||
@ -28,7 +28,8 @@ export const FluxHandlers: Partial<Record<FluxEvents, Array<(data: any) => void>
|
|||||||
CHANNEL_DELETE: [onChannelDelete],
|
CHANNEL_DELETE: [onChannelDelete],
|
||||||
RELATIONSHIP_ADD: [syncFriends],
|
RELATIONSHIP_ADD: [syncFriends],
|
||||||
RELATIONSHIP_UPDATE: [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) {
|
export function forEachEvent(fn: (event: FluxEvents, handler: (data: any) => void) => void) {
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
import { DataStore, Notices } from "@api/index";
|
import { DataStore, Notices } from "@api/index";
|
||||||
import { showNotification } from "@api/Notifications";
|
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 settings from "./settings";
|
||||||
import { ChannelType, RelationshipType, SimpleGroupChannel, SimpleGuild } from "./types";
|
import { ChannelType, RelationshipType, SimpleGroupChannel, SimpleGuild } from "./types";
|
||||||
@ -30,11 +30,20 @@ const friends = {
|
|||||||
requests: [] as string[]
|
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() {
|
export async function syncAndRunChecks() {
|
||||||
|
await runMigrations();
|
||||||
const [oldGuilds, oldGroups, oldFriends] = await DataStore.getMany([
|
const [oldGuilds, oldGroups, oldFriends] = await DataStore.getMany([
|
||||||
"relationship-notifier-guilds",
|
guildsKey(),
|
||||||
"relationship-notifier-groups",
|
groupsKey(),
|
||||||
"relationship-notifier-friends"
|
friendsKey()
|
||||||
]) as [Map<string, SimpleGuild> | undefined, Map<string, SimpleGroupChannel> | undefined, Record<"friends" | "requests", string[]> | undefined];
|
]) as [Map<string, SimpleGuild> | undefined, Map<string, SimpleGroupChannel> | undefined, Record<"friends" | "requests", string[]> | undefined];
|
||||||
|
|
||||||
await Promise.all([syncGuilds(), syncGroups(), syncFriends()]);
|
await Promise.all([syncGuilds(), syncGroups(), syncFriends()]);
|
||||||
@ -104,7 +113,7 @@ export async function syncGuilds() {
|
|||||||
iconURL: icon && `https://cdn.discordapp.com/icons/${id}/${icon}.png`
|
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) {
|
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() {
|
export async function syncFriends() {
|
||||||
@ -145,5 +154,5 @@ export async function syncFriends() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await DataStore.set("relationship-notifier-friends", friends);
|
await DataStore.set(friendsKey(), friends);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user