1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2023 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { DataStore, Notices } from "@api/index";
import { showNotification } from "@api/Notifications";
import { ChannelStore, GuildMemberStore, GuildStore, RelationshipStore, UserStore, UserUtils } from "@webpack/common";
import settings from "./settings";
import { ChannelType, RelationshipType, SimpleGroupChannel, SimpleGuild } from "./types";
const guilds = new Map<string, SimpleGuild>();
const groups = new Map<string, SimpleGroupChannel>();
const friends = {
friends: [] 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() {
await runMigrations();
const [oldGuilds, oldGroups, oldFriends] = await DataStore.getMany([
guildsKey(),
groupsKey(),
friendsKey()
]) as [Map<string, SimpleGuild> | undefined, Map<string, SimpleGroupChannel> | undefined, Record<"friends" | "requests", string[]> | undefined];
await Promise.all([syncGuilds(), syncGroups(), syncFriends()]);
if (settings.store.offlineRemovals) {
if (settings.store.groups && oldGroups?.size) {
for (const [id, group] of oldGroups) {
if (!groups.has(id))
notify(`You are no longer in the group ${group.name}.`, group.iconURL);
}
}
if (settings.store.servers && oldGuilds?.size) {
for (const [id, guild] of oldGuilds) {
if (!guilds.has(id))
notify(`You are no longer in the server ${guild.name}.`, guild.iconURL);
}
}
if (settings.store.friends && oldFriends?.friends.length) {
for (const id of oldFriends.friends) {
if (friends.friends.includes(id)) continue;
const user = await UserUtils.fetchUser(id).catch(() => void 0);
if (user)
notify(`You are no longer friends with ${user.tag}.`, user.getAvatarURL(undefined, undefined, false));
}
}
if (settings.store.friendRequestCancels && oldFriends?.requests?.length) {
for (const id of oldFriends.requests) {
if (friends.requests.includes(id)) continue;
const user = await UserUtils.fetchUser(id).catch(() => void 0);
if (user)
notify(`Friend request from ${user.tag} has been revoked.`, user.getAvatarURL(undefined, undefined, false));
}
}
}
}
export function notify(text: string, icon?: string) {
if (settings.store.notices)
Notices.showNotice(text, "OK", () => Notices.popNotice());
showNotification({
title: "Relationship Notifier",
body: text,
icon
});
}
export function getGuild(id: string) {
return guilds.get(id);
}
export function deleteGuild(id: string) {
guilds.delete(id);
syncGuilds();
}
export async function syncGuilds() {
guilds.clear();
const me = UserStore.getCurrentUser().id;
for (const [id, { name, icon }] of Object.entries(GuildStore.getGuilds())) {
if (GuildMemberStore.isMember(id, me))
guilds.set(id, {
id,
name,
iconURL: icon && `https://cdn.discordapp.com/icons/${id}/${icon}.png`
});
}
await DataStore.set(guildsKey(), guilds);
}
export function getGroup(id: string) {
return groups.get(id);
}
export function deleteGroup(id: string) {
groups.delete(id);
syncGroups();
}
export async function syncGroups() {
groups.clear();
for (const { type, id, name, rawRecipients, icon } of ChannelStore.getSortedPrivateChannels()) {
if (type === ChannelType.GROUP_DM)
groups.set(id, {
id,
name: name || rawRecipients.map(r => r.username).join(", "),
iconURL: icon && `https://cdn.discordapp.com/channel-icons/${id}/${icon}.png`
});
}
await DataStore.set(groupsKey(), groups);
}
export async function syncFriends() {
friends.friends = [];
friends.requests = [];
const relationShips = RelationshipStore.getRelationships();
for (const id in relationShips) {
switch (relationShips[id]) {
case RelationshipType.FRIEND:
friends.friends.push(id);
break;
case RelationshipType.FRIEND_REQUEST:
friends.requests.push(id);
break;
}
}
await DataStore.set(friendsKey(), friends);
}
|