mirror of
https://github.com/Vendicated/Vencord.git
synced 2025-02-24 15:35:11 +00:00
Add support to hide attachments automatically
This patch includes support to hide attachments with the following configuration: - Comma separated list of User IDs - Comma separated list of domains/hostnames - A toggle to enable/disable the automatic hiding This also fixes the issue: - Fixes the issue where forwarded messages with attachments are not detected
This commit is contained in:
parent
8fccda4a24
commit
35ad3fa2a1
4 changed files with 239 additions and 36 deletions
|
@ -17,14 +17,16 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { get, set } from "@api/DataStore";
|
import { get, set } from "@api/DataStore";
|
||||||
|
import { definePluginSettings } from "@api/Settings";
|
||||||
import { ImageInvisible, ImageVisible } from "@components/Icons";
|
import { ImageInvisible, ImageVisible } from "@components/Icons";
|
||||||
import { Devs } from "@utils/constants";
|
import { Devs } from "@utils/constants";
|
||||||
import definePlugin from "@utils/types";
|
import definePlugin, { OptionType } from "@utils/types";
|
||||||
import { ChannelStore } from "@webpack/common";
|
import { ChannelStore } from "@webpack/common";
|
||||||
import { MessageSnapshot } from "@webpack/types";
|
|
||||||
|
import { ILoadMessagesSuccessPayload, IMessage, IMessageCreatePayload, IMessageUpdatePayload } from "./types";
|
||||||
|
import { isStringEmpty } from "./utils";
|
||||||
|
|
||||||
let style: HTMLStyleElement;
|
let style: HTMLStyleElement;
|
||||||
|
|
||||||
const KEY = "HideAttachments_HiddenIds";
|
const KEY = "HideAttachments_HiddenIds";
|
||||||
|
|
||||||
let hiddenMessages: Set<string> = new Set();
|
let hiddenMessages: Set<string> = new Set();
|
||||||
|
@ -34,46 +36,14 @@ const getHiddenMessages = () => get(KEY).then(set => {
|
||||||
});
|
});
|
||||||
const saveHiddenMessages = (ids: Set<string>) => set(KEY, ids);
|
const saveHiddenMessages = (ids: Set<string>) => set(KEY, ids);
|
||||||
|
|
||||||
export default definePlugin({
|
/**
|
||||||
name: "HideAttachments",
|
* Update CSS
|
||||||
description: "Hide attachments and Embeds for individual messages via hover button",
|
*/
|
||||||
authors: [Devs.Ven],
|
const buildCss = async () => {
|
||||||
|
const elements = [...hiddenMessages]
|
||||||
|
.map(x => `#message-accessories-${x}`)
|
||||||
|
.join(",");
|
||||||
|
|
||||||
renderMessagePopoverButton(msg) {
|
|
||||||
// @ts-ignore - discord-types lags behind discord.
|
|
||||||
const hasAttachmentsInShapshots = msg.messageSnapshots.some(
|
|
||||||
(snapshot: MessageSnapshot) => snapshot?.message.attachments.length
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!msg.attachments.length && !msg.embeds.length && !msg.stickerItems.length && !hasAttachmentsInShapshots) return null;
|
|
||||||
|
|
||||||
const isHidden = hiddenMessages.has(msg.id);
|
|
||||||
|
|
||||||
return {
|
|
||||||
label: isHidden ? "Show Attachments" : "Hide Attachments",
|
|
||||||
icon: isHidden ? ImageVisible : ImageInvisible,
|
|
||||||
message: msg,
|
|
||||||
channel: ChannelStore.getChannel(msg.channel_id),
|
|
||||||
onClick: () => this.toggleHide(msg.id)
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
async start() {
|
|
||||||
style = document.createElement("style");
|
|
||||||
style.id = "VencordHideAttachments";
|
|
||||||
document.head.appendChild(style);
|
|
||||||
|
|
||||||
await getHiddenMessages();
|
|
||||||
await this.buildCss();
|
|
||||||
},
|
|
||||||
|
|
||||||
stop() {
|
|
||||||
style.remove();
|
|
||||||
hiddenMessages.clear();
|
|
||||||
},
|
|
||||||
|
|
||||||
async buildCss() {
|
|
||||||
const elements = [...hiddenMessages].map(id => `#message-accessories-${id}`).join(",");
|
|
||||||
style.textContent = `
|
style.textContent = `
|
||||||
:is(${elements}) :is([class*="embedWrapper"], [class*="clickableSticker"]) {
|
:is(${elements}) :is([class*="embedWrapper"], [class*="clickableSticker"]) {
|
||||||
/* important is not necessary, but add it to make sure bad themes won't break it */
|
/* important is not necessary, but add it to make sure bad themes won't break it */
|
||||||
|
@ -85,14 +55,177 @@ export default definePlugin({
|
||||||
font-size: 80%;
|
font-size: 80%;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
},
|
};
|
||||||
|
|
||||||
async toggleHide(id: string) {
|
/**
|
||||||
|
* Toggle attachment/embed hiding
|
||||||
|
*/
|
||||||
|
const toggleHide = async (id: string): Promise<void> => {
|
||||||
const ids = await getHiddenMessages();
|
const ids = await getHiddenMessages();
|
||||||
if (!ids.delete(id))
|
if (!ids.delete(id))
|
||||||
ids.add(id);
|
ids.add(id);
|
||||||
|
|
||||||
await saveHiddenMessages(ids);
|
await saveHiddenMessages(ids);
|
||||||
await this.buildCss();
|
await buildCss();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the message should be blocked according to user ID filter
|
||||||
|
* @param {Message} payload The message to be checked
|
||||||
|
* @param {string[]} userFilters List of user IDs to be checked
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
const shouldHideByUserIdFilter = (payload: IMessage, userFilters: string[]): boolean => {
|
||||||
|
for (const id of userFilters) {
|
||||||
|
if (payload.author.id === id) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the message should be blocked according to domain list filter
|
||||||
|
* @param {Message} payload The message to be checked
|
||||||
|
* @param {string[]} domainList List of domains to be checked
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
const shouldHideByDomainListFilter = (payload: IMessage, domainList: string[]): boolean => {
|
||||||
|
if (payload.embeds.length <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const embed of payload.embeds) {
|
||||||
|
if (!embed.url) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const domain of domainList) {
|
||||||
|
const host = URL.parse(embed.url)?.hostname ?? "";
|
||||||
|
if (host.indexOf(domain) >= 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks and hides the attachment/embed
|
||||||
|
* @param {Message} message The message to check
|
||||||
|
* @param {object} store The configuration values
|
||||||
|
*/
|
||||||
|
const checkAndHide = async (message: IMessage, store: typeof settings.store): Promise<void> => {
|
||||||
|
if (!store.enableAutoHideAttachments) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hiddenMessages.has(message.id)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const userFilters = isStringEmpty(store.filterUserList)
|
||||||
|
? []
|
||||||
|
: store.filterUserList.split(",");
|
||||||
|
if (shouldHideByUserIdFilter(message, userFilters)) {
|
||||||
|
await toggleHide(message.id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const domainFilters = isStringEmpty(store.filterDomainList)
|
||||||
|
? []
|
||||||
|
: store.filterDomainList.split(",");
|
||||||
|
if (shouldHideByDomainListFilter(message, domainFilters)) {
|
||||||
|
await toggleHide(message.id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forwarded messages
|
||||||
|
// Limitation: User filters don't work on this one.
|
||||||
|
if (Array.isArray(message.message_snapshots)) {
|
||||||
|
for (const snapshot of message.message_snapshots!) {
|
||||||
|
if (shouldHideByDomainListFilter(snapshot.message, domainFilters)) {
|
||||||
|
await toggleHide(message.id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const settings = definePluginSettings({
|
||||||
|
enableAutoHideAttachments: {
|
||||||
|
type: OptionType.BOOLEAN,
|
||||||
|
description: "Enable auto hide attachments",
|
||||||
|
default: false,
|
||||||
|
restartNeeded: false
|
||||||
|
},
|
||||||
|
filterUserList: {
|
||||||
|
type: OptionType.STRING,
|
||||||
|
description: "Comma separated list of User IDs to automatically hide their attachments/embeds",
|
||||||
|
default: "",
|
||||||
|
restartNeeded: true
|
||||||
|
},
|
||||||
|
filterDomainList: {
|
||||||
|
type: OptionType.STRING,
|
||||||
|
description: "Comma separated list of domains to automatically hide their embeds.",
|
||||||
|
default: "",
|
||||||
|
restartNeeded: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default definePlugin({
|
||||||
|
name: "HideAttachments",
|
||||||
|
description: "Hide attachments and Embeds for individual messages via hover button",
|
||||||
|
authors: [Devs.Ven, Devs.aiko],
|
||||||
|
|
||||||
|
settings,
|
||||||
|
|
||||||
|
renderMessagePopoverButton(msg: IMessage) {
|
||||||
|
const hasAttachmentsInSnapshots = !Array.isArray(msg.message_snapshots);
|
||||||
|
if (!msg.attachments.length && !msg.embeds.length && !msg.stickerItems.length && !hasAttachmentsInSnapshots) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isHidden = hiddenMessages.has(msg.id);
|
||||||
|
|
||||||
|
return {
|
||||||
|
label: isHidden ? "Show Attachments" : "Hide Attachments",
|
||||||
|
icon: isHidden ? ImageVisible : ImageInvisible,
|
||||||
|
message: msg,
|
||||||
|
channel: ChannelStore.getChannel(msg.channel_id),
|
||||||
|
onClick: () => toggleHide(msg.id)
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
flux: {
|
||||||
|
async LOAD_MESSAGES_SUCCESS(payload: ILoadMessagesSuccessPayload) {
|
||||||
|
for (const message of payload.messages) {
|
||||||
|
await checkAndHide(message, settings.store);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async MESSAGE_CREATE({ message }: IMessageCreatePayload) {
|
||||||
|
await checkAndHide(message, settings.store);
|
||||||
|
},
|
||||||
|
|
||||||
|
async MESSAGE_UPDATE({ message }: IMessageUpdatePayload) {
|
||||||
|
await checkAndHide(message, settings.store);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async start() {
|
||||||
|
style = document.createElement("style");
|
||||||
|
style.id = "VencordHideAttachments";
|
||||||
|
document.head.appendChild(style);
|
||||||
|
|
||||||
|
await getHiddenMessages();
|
||||||
|
await buildCss();
|
||||||
|
},
|
||||||
|
|
||||||
|
stop() {
|
||||||
|
style.remove();
|
||||||
|
hiddenMessages.clear();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
44
src/plugins/hideAttachments/types.ts
Normal file
44
src/plugins/hideAttachments/types.ts
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* Vencord, a modification for Discord's desktop app
|
||||||
|
* Copyright (c) 2022 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 { Message } from "discord-types/general";
|
||||||
|
|
||||||
|
export interface ILoadMessagesSuccessPayload {
|
||||||
|
channelId: string;
|
||||||
|
messages: Array<Message>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IMessage extends Message {
|
||||||
|
message_reference?: {
|
||||||
|
type: number;
|
||||||
|
channel_id: string;
|
||||||
|
message_id: string;
|
||||||
|
guild_id: string;
|
||||||
|
},
|
||||||
|
message_snapshots?: {
|
||||||
|
message: Message;
|
||||||
|
}[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IMessageCreatePayload {
|
||||||
|
message: IMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IMessageUpdatePayload {
|
||||||
|
message: IMessage;
|
||||||
|
}
|
22
src/plugins/hideAttachments/utils.ts
Normal file
22
src/plugins/hideAttachments/utils.ts
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
/*
|
||||||
|
* Vencord, a modification for Discord's desktop app
|
||||||
|
* Copyright (c) 2022 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function isStringEmpty (str: string) {
|
||||||
|
if (!str) return false;
|
||||||
|
return str.trim().length === 0;
|
||||||
|
}
|
|
@ -579,6 +579,10 @@ export const Devs = /* #__PURE__*/ Object.freeze({
|
||||||
name: "jamesbt365",
|
name: "jamesbt365",
|
||||||
id: 158567567487795200n,
|
id: 158567567487795200n,
|
||||||
},
|
},
|
||||||
|
aiko: {
|
||||||
|
name: "kima_riiiiiii",
|
||||||
|
id: 366434327761911808n
|
||||||
|
}
|
||||||
} satisfies Record<string, Dev>);
|
} satisfies Record<string, Dev>);
|
||||||
|
|
||||||
// iife so #__PURE__ works correctly
|
// iife so #__PURE__ works correctly
|
||||||
|
|
Loading…
Add table
Reference in a new issue