feat(plugin): Read all notifications button (#217)

Co-authored-by: Ven <vendicated@riseup.net>
This commit is contained in:
Kareem Olim 2022-11-19 15:54:48 +02:00 committed by GitHub
parent 8f2c247f27
commit 5ce2dc1bb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 186 additions and 2 deletions

55
src/api/ServerList.ts Normal file
View file

@ -0,0 +1,55 @@
/*
* 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 Logger from "../utils/Logger";
const logger = new Logger("ServerListAPI");
export enum ServerListRenderPosition {
Above,
In,
}
const renderFunctionsAbove = new Set<Function>();
const renderFunctionsIn = new Set<Function>();
function getRenderFunctions(position: ServerListRenderPosition) {
return position === ServerListRenderPosition.Above ? renderFunctionsAbove : renderFunctionsIn;
}
export function addServerListElement(position: ServerListRenderPosition, renderFunction: Function) {
getRenderFunctions(position).add(renderFunction);
}
export function removeServerListElement(position: ServerListRenderPosition, renderFunction: Function) {
getRenderFunctions(position).delete(renderFunction);
}
export const renderAll = (position: ServerListRenderPosition) => {
const ret: Array<JSX.Element> = [];
for (const renderFunction of getRenderFunctions(position)) {
try {
ret.unshift(renderFunction());
} catch (e) {
logger.error("Failed to render server list element:", e);
}
}
return ret;
};

View file

@ -22,6 +22,7 @@ import * as $DataStore from "./DataStore";
import * as $MessageAccessories from "./MessageAccessories"; import * as $MessageAccessories from "./MessageAccessories";
import * as $MessageEventsAPI from "./MessageEvents"; import * as $MessageEventsAPI from "./MessageEvents";
import * as $Notices from "./Notices"; import * as $Notices from "./Notices";
import * as $ServerList from "./ServerList";
/** /**
* An API allowing you to listen to Message Clicks or run your own logic * An API allowing you to listen to Message Clicks or run your own logic
@ -62,5 +63,9 @@ const MessageAccessories = $MessageAccessories;
* An API allowing you to add badges to user profiles * An API allowing you to add badges to user profiles
*/ */
const Badges = $Badges; const Badges = $Badges;
/**
* An API allowing you to add custom elements to the server list
*/
const ServerList = $ServerList;
export { Badges, Commands, DataStore, MessageAccessories, MessageEvents, Notices }; export { Badges, Commands, DataStore, MessageAccessories, MessageEvents, Notices, ServerList };

View file

@ -0,0 +1,42 @@
/*
* 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 { Devs } from "../utils/constants";
import definePlugin from "../utils/types";
export default definePlugin({
name: "ServerListAPI",
authors: [Devs.kemo],
description: "Api required for plugins that modify the server list",
patches: [
{
find: "Messages.DISCODO_DISABLED",
replacement: {
match: /(Messages\.DISCODO_DISABLED\);return)(.*?homeIcon.*?)(\}function)/,
replace: "$1[$2].concat(Vencord.Api.ServerList.renderAll(Vencord.Api.ServerList.ServerListRenderPosition.Above))$3"
}
},
{
find: "Messages.SERVERS",
replacement: {
match: /(Messages\.SERVERS,children:)(.+?default:return null\}\}\)\))/,
replace: "$1Vencord.Api.ServerList.renderAll(Vencord.Api.ServerList.ServerListRenderPosition.In).concat($2)"
}
}
]
});

View file

@ -0,0 +1,71 @@
/*
* 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 { addServerListElement, removeServerListElement, ServerListRenderPosition } from "../api/ServerList";
import { Devs } from "../utils/constants";
import definePlugin from "../utils/types";
import { Button, FluxDispatcher, GuildChannelStore, GuildStore, React, ReadStateStore } from "../webpack/common";
function onClick() {
const channels: Array<any> = [];
Object.values(GuildStore.getGuilds()).forEach(guild => {
GuildChannelStore.getChannels(guild.id).SELECTABLE.forEach((c: { channel: { id: string; }; }) => {
if (!ReadStateStore.hasUnread(c.channel.id)) return;
channels.push({
channelId: c.channel.id,
// messageId: c.channel?.lastMessageId,
messageId: ReadStateStore.lastMessageId(c.channel.id),
readStateType: 0
});
});
});
FluxDispatcher.dispatch({
type: "BULK_ACK",
context: "APP",
channels: channels
});
}
const ReadAllButton = () => (
<Button
onClick={onClick}
size={Button.Sizes.MIN}
color={Button.Colors.BRAND}
style={{ marginTop: "2px", marginBottom: "8px", marginLeft: "9px" }}
>Read all</Button>
);
export default definePlugin({
name: "ReadAllNotificationsButton",
description: "Read all server notifications with a single button click!",
authors: [Devs.kemo],
dependencies: ["ServerListAPI"],
renderReadAllButton: () => <ReadAllButton />,
start() {
addServerListElement(ServerListRenderPosition.In, this.renderReadAllButton);
},
stop() {
removeServerListElement(ServerListRenderPosition.In, this.renderReadAllButton);
}
});

View file

@ -136,5 +136,9 @@ export const Devs = Object.freeze({
KraXen72: { KraXen72: {
name: "KraXen72", name: "KraXen72",
id: 379304073515499530n id: 379304073515499530n
},
kemo: {
name: "kemo",
id: 299693897859465228n
} }
}); });

View file

@ -18,7 +18,7 @@
import { Guild } from "discord-types/general"; import { Guild } from "discord-types/general";
import { ChannelStore, GuildStore,SelectedChannelStore } from "../webpack/common"; import { ChannelStore, GuildStore, PrivateChannelsStore, SelectedChannelStore } from "../webpack/common";
export function getCurrentChannel() { export function getCurrentChannel() {
return ChannelStore.getChannel(SelectedChannelStore.getChannelId()); return ChannelStore.getChannel(SelectedChannelStore.getChannelId());
@ -27,3 +27,7 @@ export function getCurrentChannel() {
export function getCurrentGuild(): Guild | undefined { export function getCurrentGuild(): Guild | undefined {
return GuildStore.getGuild(getCurrentChannel()?.guild_id); return GuildStore.getGuild(getCurrentChannel()?.guild_id);
} }
export function openPrivateChannel(userId: string) {
PrivateChannelsStore.openPrivateChannel(userId);
}

View file

@ -34,6 +34,9 @@ export const ReactDOM: typeof import("react-dom") = lazyWebpack(filters.byProps(
export const MessageStore = lazyWebpack(filters.byProps("getRawMessages")) as Omit<Stores.MessageStore, "getMessages"> & { getMessages(chanId: string): any; }; export const MessageStore = lazyWebpack(filters.byProps("getRawMessages")) as Omit<Stores.MessageStore, "getMessages"> & { getMessages(chanId: string): any; };
export const PermissionStore = lazyWebpack(filters.byProps("can", "getGuildPermissions")); export const PermissionStore = lazyWebpack(filters.byProps("can", "getGuildPermissions"));
export const PrivateChannelsStore = lazyWebpack(filters.byProps("openPrivateChannel"));
export const GuildChannelStore = lazyWebpack(filters.byProps("getChannels"));
export const ReadStateStore = lazyWebpack(filters.byProps("lastMessageId"));
export let GuildStore: Stores.GuildStore; export let GuildStore: Stores.GuildStore;
export let UserStore: Stores.UserStore; export let UserStore: Stores.UserStore;
export let SelectedChannelStore: Stores.SelectedChannelStore; export let SelectedChannelStore: Stores.SelectedChannelStore;