Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(noBlockedMessages): Allow hiding of replies to blocked messages #3035

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
60 changes: 46 additions & 14 deletions src/plugins/noBlockedMessages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,32 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { Settings } from "@api/Settings";
import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants";
import { runtimeHashMessageKey } from "@utils/intlHash";
import { Logger } from "@utils/Logger";
import definePlugin, { OptionType } from "@utils/types";
import { findByPropsLazy } from "@webpack";
import { i18n } from "@webpack/common";
import { i18n, MessageStore } from "@webpack/common";
import { Message } from "discord-types/general";

const RelationshipStore = findByPropsLazy("getRelationships", "isBlocked");

const settings = definePluginSettings({
ignoreBlockedMessages: {
description: "Completely ignores (recent) incoming messages from blocked users (locally).",
type: OptionType.BOOLEAN,
default: false,
restartNeeded: true,
},
hideRepliesToBlockedMessages: {
description: "Hides replies to blocked messages.",
type: OptionType.BOOLEAN,
default: false,
restartNeeded: false,
},
});

interface MessageDeleteProps {
// Internal intl message for BLOCKED_MESSAGE_COUNT
collapsedReason: () => any;
Expand All @@ -35,7 +50,9 @@ interface MessageDeleteProps {
export default definePlugin({
name: "NoBlockedMessages",
description: "Hides all blocked messages from chat completely.",
authors: [Devs.rushii, Devs.Samu],
authors: [Devs.rushii, Devs.Samu, Devs.Elvyra],
settings,

patches: [
{
find: "#{intl::BLOCKED_MESSAGES_HIDE}",
Expand All @@ -51,22 +68,37 @@ export default definePlugin({
'"ReadStateStore"'
].map(find => ({
find,
predicate: () => Settings.plugins.NoBlockedMessages.ignoreBlockedMessages === true,
predicate: () => settings.store.ignoreBlockedMessages === true,
replacement: [
{
match: /(?<=MESSAGE_CREATE:function\((\i)\){)/,
replace: (_, props) => `if($self.isBlocked(${props}.message))return;`
replace: (_, props) => `if($self.isBlocked(${props}.message)||$self.isReplyToBlocked(${props}.message))return;`
}
]
}))
],
options: {
ignoreBlockedMessages: {
description: "Completely ignores (recent) incoming messages from blocked users (locally).",
type: OptionType.BOOLEAN,
default: false,
restartNeeded: true,
})),
{
find: ".messageListItem",
replacement: [
{
match: /CUSTOM_GIFT.*?=(?=\(0,\i.jsx\)\(\i.FocusRing)/,
replace: "$&!$self.isReplyToBlocked(arguments[0].message)&&",
}
],
},
],

isReplyToBlocked(message: Message) {
if (!settings.store.hideRepliesToBlockedMessages) return false;
try {
const { messageReference } = message;
if (!messageReference) return false;

const replyMessage = MessageStore.getMessage(messageReference.channel_id, messageReference.message_id);

return replyMessage ? this.isBlocked(replyMessage) : false;
} catch (e) {
new Logger("NoBlockedMessages").error("Failed to check if referenced message is blocked:", e);
}
},

isBlocked(message: Message) {
Expand All @@ -81,7 +113,7 @@ export default definePlugin({
try {
return props.collapsedReason() === i18n.t[runtimeHashMessageKey("BLOCKED_MESSAGE_COUNT")]();
} catch (e) {
console.error(e);
new Logger("NoBlockedMessages").error("Failed to hide blocked message:", e);
}
return false;
}
Expand Down