QuickReply: correctly handle new & deleted messages (#3473)

This commit is contained in:
Vending Machine 2025-06-08 19:45:52 +02:00 committed by GitHub
parent a386736dcc
commit 6d47a340b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 63 additions and 57 deletions

View file

@ -21,6 +21,7 @@ import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types"; import definePlugin, { OptionType } from "@utils/types";
import { findByPropsLazy } from "@webpack"; import { findByPropsLazy } from "@webpack";
import { FluxDispatcher, PermissionsBits, PermissionStore, UserStore, WindowStore } from "@webpack/common"; import { FluxDispatcher, PermissionsBits, PermissionStore, UserStore, WindowStore } from "@webpack/common";
import NoReplyMentionPlugin from "plugins/noReplyMention";
const MessageActions = findByPropsLazy("deleteMessage", "startEditMessage"); const MessageActions = findByPropsLazy("deleteMessage", "startEditMessage");
const EditStore = findByPropsLazy("isEditing", "isEditingAny"); const EditStore = findByPropsLazy("isEditing", "isEditingAny");
@ -92,9 +93,8 @@ export default definePlugin({
if (msg.hasFlag(EPHEMERAL)) return; if (msg.hasFlag(EPHEMERAL)) return;
const isShiftPress = event.shiftKey && !settings.store.requireModifier; const isShiftPress = event.shiftKey && !settings.store.requireModifier;
const NoReplyMention = Vencord.Plugins.plugins.NoReplyMention as any as typeof import("../noReplyMention").default; const shouldMention = Vencord.Plugins.isPluginEnabled(NoReplyMentionPlugin.name)
const shouldMention = Vencord.Plugins.isPluginEnabled("NoReplyMention") ? NoReplyMentionPlugin.shouldMention(msg, isShiftPress)
? NoReplyMention.shouldMention(msg, isShiftPress)
: !isShiftPress; : !isShiftPress;
FluxDispatcher.dispatch({ FluxDispatcher.dispatch({

View file

@ -16,19 +16,20 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { definePluginSettings, Settings } from "@api/Settings"; import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types"; import definePlugin, { OptionType } from "@utils/types";
import { findByPropsLazy } from "@webpack"; import { findByPropsLazy } from "@webpack";
import { ChannelStore, ComponentDispatch, FluxDispatcher as Dispatcher, MessageStore, PermissionsBits, PermissionStore, SelectedChannelStore, UserStore } from "@webpack/common"; import { ChannelStore, ComponentDispatch, FluxDispatcher as Dispatcher, MessageActions, MessageStore, PermissionsBits, PermissionStore, SelectedChannelStore, UserStore } from "@webpack/common";
import { Message } from "discord-types/general"; import { Message } from "discord-types/general";
import NoBlockedMessagesPlugin from "plugins/noBlockedMessages";
import NoReplyMentionPlugin from "plugins/noReplyMention";
const Kangaroo = findByPropsLazy("jumpToMessage");
const RelationshipStore = findByPropsLazy("getRelationships", "isBlocked"); const RelationshipStore = findByPropsLazy("getRelationships", "isBlocked");
const isMac = navigator.platform.includes("Mac"); // bruh const isMac = navigator.platform.includes("Mac"); // bruh
let replyIdx = -1; let currentlyReplyingId: string | null = null;
let editIdx = -1; let currentlyEditingId: string | null = null;
const enum MentionOptions { const enum MentionOptions {
@ -69,36 +70,29 @@ export default definePlugin({
flux: { flux: {
DELETE_PENDING_REPLY() { DELETE_PENDING_REPLY() {
replyIdx = -1; currentlyReplyingId = null;
}, },
MESSAGE_END_EDIT() { MESSAGE_END_EDIT() {
editIdx = -1; currentlyEditingId = null;
},
CHANNEL_SELECT() {
currentlyReplyingId = null;
currentlyEditingId = null;
}, },
MESSAGE_START_EDIT: onStartEdit, MESSAGE_START_EDIT: onStartEdit,
CREATE_PENDING_REPLY: onCreatePendingReply CREATE_PENDING_REPLY: onCreatePendingReply
} }
}); });
function calculateIdx(messages: Message[], id: string) { function onStartEdit({ messageId, _isQuickEdit }: any) {
const idx = messages.findIndex(m => m.id === id);
return idx === -1
? idx
: messages.length - idx - 1;
}
function onStartEdit({ channelId, messageId, _isQuickEdit }: any) {
if (_isQuickEdit) return; if (_isQuickEdit) return;
currentlyEditingId = messageId;
const meId = UserStore.getCurrentUser().id;
const messages = MessageStore.getMessages(channelId)._array.filter(m => m.author.id === meId);
editIdx = calculateIdx(messages, messageId);
} }
function onCreatePendingReply({ message, _isQuickReply }: { message: Message; _isQuickReply: boolean; }) { function onCreatePendingReply({ message, _isQuickReply }: { message: Message; _isQuickReply: boolean; }) {
if (_isQuickReply) return; if (_isQuickReply) return;
replyIdx = calculateIdx(MessageStore.getMessages(message.channel_id)._array, message.id); currentlyReplyingId = message.id;
} }
const isCtrl = (e: KeyboardEvent) => isMac ? e.metaKey : e.ctrlKey; const isCtrl = (e: KeyboardEvent) => isMac ? e.metaKey : e.ctrlKey;
@ -123,10 +117,10 @@ function jumpIfOffScreen(channelId: string, messageId: string) {
const vh = Math.max(document.documentElement.clientHeight, window.innerHeight); const vh = Math.max(document.documentElement.clientHeight, window.innerHeight);
const rect = element.getBoundingClientRect(); const rect = element.getBoundingClientRect();
const isOffscreen = rect.bottom < 200 || rect.top - vh >= -200; const isOffscreen = rect.bottom < 150 || rect.top - vh >= -150;
if (isOffscreen) { if (isOffscreen) {
Kangaroo.jumpToMessage({ MessageActions.jumpToMessage({
channelId, channelId,
messageId, messageId,
flash: false, flash: false,
@ -137,43 +131,48 @@ function jumpIfOffScreen(channelId: string, messageId: string) {
function getNextMessage(isUp: boolean, isReply: boolean) { function getNextMessage(isUp: boolean, isReply: boolean) {
let messages: Array<Message & { deleted?: boolean; }> = MessageStore.getMessages(SelectedChannelStore.getChannelId())._array; let messages: Array<Message & { deleted?: boolean; }> = MessageStore.getMessages(SelectedChannelStore.getChannelId())._array;
if (!isReply) { // we are editing so only include own
const meId = UserStore.getCurrentUser().id; const meId = UserStore.getCurrentUser().id;
messages = messages.filter(m => m.author.id === meId); const hasNoBlockedMessages = Vencord.Plugins.isPluginEnabled(NoBlockedMessagesPlugin.name);
}
if (Vencord.Plugins.isPluginEnabled("NoBlockedMessages")) { messages = messages.filter(m => {
messages = messages.filter(m => !RelationshipStore.isBlocked(m.author.id)); if (m.deleted) return false;
} if (!isReply && m.author.id !== meId) return false; // editing only own messages
if (hasNoBlockedMessages && NoBlockedMessagesPlugin.shouldIgnoreMessage(m)) return false;
const mutate = (i: number) => isUp return true;
? Math.min(messages.length - 1, i + 1) });
: Math.max(-1, i - 1);
const findNextNonDeleted = (i: number) => { const findNextNonDeleted = (id: string | null) => {
do { if (id === null) return messages[messages.length - 1];
i = mutate(i);
} while (i !== -1 && messages[messages.length - i - 1]?.deleted === true); const idx = messages.findIndex(m => m.id === id);
return i; if (idx === -1) return messages[messages.length - 1];
const i = isUp ? idx - 1 : idx + 1;
return messages[i] ?? null;
}; };
let i: number; if (isReply) {
if (isReply) const msg = findNextNonDeleted(currentlyReplyingId);
replyIdx = i = findNextNonDeleted(replyIdx); currentlyReplyingId = msg?.id ?? null;
else return msg;
editIdx = i = findNextNonDeleted(editIdx); } else {
const msg = findNextNonDeleted(currentlyEditingId);
return i === - 1 ? undefined : messages[messages.length - i - 1]; currentlyEditingId = msg?.id ?? null;
return msg;
}
} }
function shouldMention(message) { function shouldMention(message: Message) {
const { enabled, userList, shouldPingListed } = Settings.plugins.NoReplyMention;
const shouldPing = !enabled || (shouldPingListed === userList.includes(message.author.id));
switch (settings.store.shouldMention) { switch (settings.store.shouldMention) {
case MentionOptions.NO_REPLY_MENTION_PLUGIN: return shouldPing; case MentionOptions.NO_REPLY_MENTION_PLUGIN:
case MentionOptions.DISABLED: return false; if (!Vencord.Plugins.isPluginEnabled(NoReplyMentionPlugin.name)) return true;
default: return true; return NoReplyMentionPlugin.shouldMention(message, false);
case MentionOptions.DISABLED:
return false;
default:
return true;
} }
} }
@ -181,13 +180,16 @@ function shouldMention(message) {
function nextReply(isUp: boolean) { function nextReply(isUp: boolean) {
const currChannel = ChannelStore.getChannel(SelectedChannelStore.getChannelId()); const currChannel = ChannelStore.getChannel(SelectedChannelStore.getChannelId());
if (currChannel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, currChannel)) return; if (currChannel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, currChannel)) return;
const message = getNextMessage(isUp, true); const message = getNextMessage(isUp, true);
if (!message) if (!message) {
return void Dispatcher.dispatch({ return void Dispatcher.dispatch({
type: "DELETE_PENDING_REPLY", type: "DELETE_PENDING_REPLY",
channelId: SelectedChannelStore.getChannelId(), channelId: SelectedChannelStore.getChannelId(),
}); });
}
const channel = ChannelStore.getChannel(message.channel_id); const channel = ChannelStore.getChannel(message.channel_id);
const meId = UserStore.getCurrentUser().id; const meId = UserStore.getCurrentUser().id;
@ -199,6 +201,7 @@ function nextReply(isUp: boolean) {
showMentionToggle: !channel.isPrivate() && message.author.id !== meId, showMentionToggle: !channel.isPrivate() && message.author.id !== meId,
_isQuickReply: true _isQuickReply: true
}); });
ComponentDispatch.dispatchToLastSubscribed("TEXTAREA_FOCUS"); ComponentDispatch.dispatchToLastSubscribed("TEXTAREA_FOCUS");
jumpIfOffScreen(channel.id, message.id); jumpIfOffScreen(channel.id, message.id);
} }
@ -209,11 +212,13 @@ function nextEdit(isUp: boolean) {
if (currChannel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, currChannel)) return; if (currChannel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, currChannel)) return;
const message = getNextMessage(isUp, false); const message = getNextMessage(isUp, false);
if (!message) if (!message) {
return Dispatcher.dispatch({ return Dispatcher.dispatch({
type: "MESSAGE_END_EDIT", type: "MESSAGE_END_EDIT",
channelId: SelectedChannelStore.getChannelId() channelId: SelectedChannelStore.getChannelId()
}); });
}
Dispatcher.dispatch({ Dispatcher.dispatch({
type: "MESSAGE_START_EDIT", type: "MESSAGE_START_EDIT",
channelId: message.channel_id, channelId: message.channel_id,
@ -221,5 +226,6 @@ function nextEdit(isUp: boolean) {
content: message.content, content: message.content,
_isQuickEdit: true _isQuickEdit: true
}); });
jumpIfOffScreen(message.channel_id, message.id); jumpIfOffScreen(message.channel_id, message.id);
} }