This repository was archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessagePreviewAccessory.js
More file actions
154 lines (133 loc) · 4.85 KB
/
MessagePreviewAccessory.js
File metadata and controls
154 lines (133 loc) · 4.85 KB
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
const { getModule, getModuleByDisplayName, React, constants: Constants } = require("powercord/webpack");
const MESSAGE_LINK_REGEX =
/https?:\/\/(?:\w+\.)?discord(?:app)?\.com\/channels\/(\d{17,19}|@me)\/(\d{17,19})\/(\d{17,19})/g;
const CHANNEL_PATH_REGEX = /\/channels\/(\d+|@me)(?:\/)?(\d+)(?:\/)(\d+)/;
const {default: ChannelMessage} = getModule(
(x) => x?.default?.type?.displayName == "ChannelMessage",
false
);
const Embed = getModuleByDisplayName("Embed", false);
const HTTP = getModule(["get", "put"], false);
const ChannelPathHelper = getModule(["isAccessibleChannelOrThreadPath"], false);
const MessageConstructor = getModule(["createMessageRecord"], false);
const {getGuildIconURL} = getModule(["getGuildIconURL"], false);
const {renderMaskedLinkComponent} = getModule(["renderMaskedLinkComponent"], false);
const GuildStore = getModule(["getGuild"], false);
const ChannelStore = getModule(["getChannel", "getDMUserIds"], false);
const MessageStore = getModule(["getMessage", "getMessages"], false);
const UserSettingsStore = getModule(["MessageDisplayCompact"], false);
const SearchResultClasses = getModule([
"searchResult",
"message",
"buttonsContainer"
], false);
const fetchedAttempted = new Set();
const manualMessageCache = new Map();
module.exports = function MessagePreviewAccessory(props) {
const {message} = props;
const messageLinks = message.content.match(MESSAGE_LINK_REGEX);
const [, dummyState] = React.useState();
const forceUpdate = React.useCallback(() => dummyState({}), []);
if (messageLinks) {
const elements = [];
for (const link of messageLinks) {
const channelPath = link.match(CHANNEL_PATH_REGEX)[0];
const parsedChannelPath =
ChannelPathHelper.tryParseChannelPath(channelPath);
if (parsedChannelPath.messageId == message.id) continue;
const referencedGuild =
parsedChannelPath.guildId == "@me"
? null
: GuildStore.getGuild(parsedChannelPath.guildId);
const referencedChannel = ChannelStore.getChannel(
parsedChannelPath.channelId
);
const referencedMessage =
MessageStore.getMessage(
parsedChannelPath.channelId,
parsedChannelPath.messageId
) || manualMessageCache.get(parsedChannelPath.messageId);
if (
!referencedMessage &&
!fetchedAttempted.has(parsedChannelPath.messageId)
) {
HTTP.get({
url: Constants.Endpoints.MESSAGES(parsedChannelPath.channelId),
query: {
limit: 1,
around: parsedChannelPath.messageId,
},
retries: 2,
oldFormErrors: true,
}).then((res) => {
manualMessageCache.set(
parsedChannelPath.messageId,
MessageConstructor.createMessageRecord(res.body[0])
);
forceUpdate();
});
fetchedAttempted.add(parsedChannelPath.messageId);
}
if (
!referencedMessage ||
!referencedChannel ||
(!referencedGuild && parsedChannelPath.guildId != "@me")
)
continue;
const recursion = referencedMessage.content.match(MESSAGE_LINK_REGEX);
if (recursion) {
let endRecurse = false;
for (const sublink of recursion) {
const subPath = sublink.match(CHANNEL_PATH_REGEX)[0];
const parsedSubPath = ChannelPathHelper.tryParseChannelPath(subPath);
if (parsedSubPath.messageId == message.id) {
endRecurse = true;
break;
}
}
if (endRecurse) continue;
}
const icon =
parsedChannelPath.guildId == "@me"
? null
: getGuildIconURL({
id: referencedGuild.id,
icon: referencedGuild.icon,
size: 24,
});
elements.push(
React.createElement(Embed, {
embed: {
rawDescription: "",
author: {
name:
parsedChannelPath.guildId == "@me"
? "Direct Message"
: `${referencedGuild.name} - #${referencedChannel.name}`,
iconProxyURL: icon,
},
},
renderLinkComponent: renderMaskedLinkComponent,
renderDescription: () =>
React.createElement(
"div",
{
className: SearchResultClasses.message,
key: referencedMessage.id,
},
React.createElement(ChannelMessage, {
id: "message-link-preview-" + referencedMessage.id,
message: referencedMessage,
channel: referencedChannel,
animateAvatar: false,
subscribeToComponentDispatch: false,
compact: UserSettingsStore.MessageDisplayCompact.getSetting(),
})
),
})
);
}
return elements;
}
return null;
}