Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 90 additions & 4 deletions src/plugins/noBlockedMessages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,46 @@ import { Devs } from "@utils/constants";
import { runtimeHashMessageKey } from "@utils/intlHash";
import { Logger } from "@utils/Logger";
import definePlugin, { OptionType } from "@utils/types";
import { Message } from "@vencord/discord-types";
import { i18n, RelationshipStore } from "@webpack/common";
import { Message, MessageJSON } from "@vencord/discord-types";
import { MessageType } from "@vencord/discord-types/enums";
import { FluxDispatcher, i18n, MessageStore, RelationshipStore, SelectedChannelStore } from "@webpack/common";

interface MessageDeleteProps {
// Internal intl message for BLOCKED_MESSAGE_COUNT
collapsedReason: () => any;
}

let ReplyStore: any;

// Some referenced messages aren't in the MessageStore so this falls back to the ReplyStore cache
function getReferencedMessage(channelId: string, messageId: string) {
const fromMessageStore = MessageStore.getMessage(channelId, messageId);
if (fromMessageStore) return fromMessageStore;

const fromReplyStore = ReplyStore?._channelCaches?.get(channelId)?._cachedMessages?.get(messageId)?.message;
if (fromReplyStore) return fromReplyStore;
}

function rerenderChannelMessages() {
if (!settings.store.hideRepliesToBlockedUsers) return;

try {
const channelId = SelectedChannelStore.getChannelId();
if (!channelId) return;

const messages = MessageStore.getMessages(channelId)?._array ?? [];
for (const message of messages) {
if (message.type !== MessageType.REPLY) continue;
FluxDispatcher.dispatch({
type: "MESSAGE_UPDATE",
message
});
}
} catch (e) {
new Logger("NoBlockedMessages").error("Failed to refresh visible messages:", e);
}
}

// Remove this migration once enough time has passed
migratePluginSetting("NoBlockedMessages", "ignoreBlockedMessages", "ignoreMessages");
const settings = definePluginSettings({
Expand All @@ -43,6 +75,12 @@ const settings = definePluginSettings({
type: OptionType.BOOLEAN,
default: true,
restartNeeded: false
},
hideRepliesToBlockedUsers: {
description: "Hide replies to blocked users",
type: OptionType.BOOLEAN,
default: false,
restartNeeded: false
}
});

Expand Down Expand Up @@ -82,10 +120,44 @@ export default definePlugin({
replace: (_, props) => `if($self.shouldIgnoreMessage(${props}.message))return;`
}
]
},
{
find: "Message must not be a thread starter message",
predicate: () => settings.store.hideRepliesToBlockedUsers,
replacement: {
match: /return (null!=\i\?\(0,\i\.jsx\)\(\i,\{flashKey:\i,[\s\S]*?`bg-flash-\$\{\i\}`\):\i)/,
replace: "return $self.shouldHideReply(arguments[0])?null:$1",
}
},
{
find: "ReferencedMessageStore",
predicate: () => settings.store.hideRepliesToBlockedUsers,
replacement: [
{
match: /_channelCaches=new Map;/,
replace: "$&_=$self.setReplyStore(this);"
}
]
}
],

shouldIgnoreMessage(message: Message) {
flux: {
RELATIONSHIP_ADD() {
rerenderChannelMessages();
},
RELATIONSHIP_UPDATE() {
rerenderChannelMessages();
},
RELATIONSHIP_REMOVE() {
rerenderChannelMessages();
}
},

setReplyStore(store: any) {
ReplyStore = store;
},

shouldIgnoreMessage(message: MessageJSON) {
try {
if (RelationshipStore.isBlocked(message.author.id)) {
return true;
Expand All @@ -107,5 +179,19 @@ export default definePlugin({
new Logger("NoBlockedMessages").error("Failed to check if message should be hidden:", e);
return false;
}
}
},

shouldHideReply({ message }: { message?: Message; }) {
if (!settings.store.hideRepliesToBlockedUsers) return false;
if (!message || message?.type !== MessageType.REPLY) return false;

const ref = message.messageReference;
if (!ref?.channel_id || !ref.message_id) return false;

const referencedMessage = getReferencedMessage(ref.channel_id, ref.message_id);
if (!referencedMessage) return false;

if (RelationshipStore.isBlocked(referencedMessage.author.id)) return true;
return settings.store.applyToIgnoredUsers && RelationshipStore.isIgnored(referencedMessage.author.id);
},
});
4 changes: 4 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,10 @@ export const Devs = /* #__PURE__*/ Object.freeze({
name: "prism",
id: 390884143749136386n,
},
nickoates: {
name: "nickoates",
id: 302915598038335490n,
}
} satisfies Record<string, Dev>);

// iife so #__PURE__ works correctly
Expand Down
Loading