Revert "MLEnhanced Update"

This reverts commit 163c35ff87.
This commit is contained in:
thororen1234 2024-11-18 22:15:50 -05:00
parent 163c35ff87
commit b56e03fd2f
34 changed files with 1076 additions and 3010 deletions

View file

@ -1,7 +1,19 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
* 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 "./VencordTab.css";
@ -189,12 +201,12 @@ function EquicordSettings() {
</Forms.FormText>
{Switches.map(
s =>
(s) =>
s && (
<Switch
key={s.key}
value={settings[s.key]}
onChange={v => (settings[s.key] = v)}
onChange={(v) => (settings[s.key] = v)}
note={
s.warning.enabled ? (
<>
@ -277,8 +289,8 @@ function EquicordSettings() {
value: "hud",
},
]}
select={v => (settings.macosVibrancyStyle = v)}
isSelected={v => settings.macosVibrancyStyle === v}
select={(v) => (settings.macosVibrancyStyle = v)}
isSelected={(v) => settings.macosVibrancyStyle === v}
serialize={identity}
/>
</>
@ -324,7 +336,7 @@ function DiscordInviteCard({ invite, image }: DiscordInviteProps) {
<div className={cl("card-buttons")}>
<Button
className="vc-joindiscordbutton vc-settingbuttons"
onClick={async e => {
onClick={async (e) => {
e.preventDefault();
openInviteModal(invite).catch(() =>
showToast("Invalid or expired invite"),

View file

@ -60,7 +60,7 @@ function cleanMessage(msg: CustomMessage) {
}
function openViewRawModal(obj: any, type: string, isMessage?: boolean) {
const key = openModal(props => (
const key = openModal((props) => (
<ErrorBoundary>
<ModalRoot {...props} size={ModalSize.LARGE}>
<ModalHeader>
@ -134,16 +134,16 @@ export default definePlugin({
"Copy/View raw content of any message, channel, or guild, but show in the right click menu.",
authors: [Devs.KingFish, Devs.Ven, Devs.rad, Devs.ImLvna, Devs.Kyuuhachi],
contextMenus: {
"guild-context": makeContextCallback("guild", val =>
"guild-context": makeContextCallback("guild", (val) =>
openViewRawModal(val, "Guild"),
),
"channel-context": makeContextCallback("channel", val =>
"channel-context": makeContextCallback("channel", (val) =>
openViewRawModal(val, "Channel"),
),
"user-context": makeContextCallback("user", val =>
"user-context": makeContextCallback("user", (val) =>
openViewRawModal(val, "User"),
),
message: makeContextCallback("message", val =>
message: makeContextCallback("message", (val) =>
openViewRawModal(cleanMessage(val), "Message", true),
),
},

View file

@ -28,19 +28,19 @@ const settings = definePluginSettings({
},
});
const getPresend = dictionary => {
const getPresend = (dictionary) => {
const presendObject: SendListener = (_, msg) => {
msg.content = msg.content.trim();
if (!msg.content.includes("```") && /\w/.test(msg.content.charAt(0))) {
if (settings.store.autoWordReplacement) {
const re = new RegExp(
`(^|(?<=[^A-Z0-9]+))(${Object.keys(dictionary)
.map(k => k.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
.map((k) => k.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
.join("|")})((?=[^A-Z0-9]+)|$)`,
"gi",
);
if (re !== null) {
msg.content = msg.content.replace(re, match => {
msg.content = msg.content.replace(re, (match) => {
return dictionary[match.toLowerCase()] || match;
});
}
@ -62,7 +62,7 @@ const getPresend = dictionary => {
// Ensure sentences are capitalized after punctuation
if (settings.store.autoCapitalization) {
msg.content = msg.content.replace(/([.!?])\s*(\w)/g, match =>
msg.content = msg.content.replace(/([.!?])\s*(\w)/g, (match) =>
match.toUpperCase(),
);

View file

@ -16,29 +16,260 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Flogger, settings } from ".";
import { addMessageIDB, db, DBMessageStatus, deleteMessagesBulkIDB, getOldestMessagesIDB } from "./db";
import { LoggedMessage, LoggedMessageJSON } from "./types";
import { cleanupMessage } from "./utils";
import { cacheMessageImages } from "./utils/saveImage";
import { createStore } from "@api/DataStore";
import { DataStore } from "@api/index";
import { Settings } from "@api/Settings";
export const addMessage = async (message: LoggedMessage | LoggedMessageJSON, status: DBMessageStatus) => {
if (settings.store.saveImages && status === DBMessageStatus.DELETED)
import { Flogger, Native, settings } from ".";
import { LoggedMessage, LoggedMessageIds, LoggedMessageJSON, LoggedMessages, MessageRecord } from "./types";
import { cleanupMessage, getNative, sortMessagesByDate } from "./utils";
import { cacheMessageImages, deleteMessageImages } from "./utils/saveImage";
export const defaultLoggedMessages = { deletedMessages: {}, editedMessages: {}, };
export const LOGGED_MESSAGES_KEY = "logged-messages-hi";
export const MessageLoggerStore = createStore("MessageLoggerData", "MessageLoggerStore");
// this gets used by the logs modal. logs modal should only use saved messages not messages that are being processed
// also hasMessageInLogs should only check saved messages not the ones that are being processed
export let savedLoggedMessages: LoggedMessages = { ...defaultLoggedMessages };
export let loggedMessages: LoggedMessages = { ...defaultLoggedMessages };
(async () => {
try {
const Native = getNative();
const res = await Native.getLogsFromFs();
if (res != null) {
Flogger.log("Got logged messages from native wont be checking DataStore");
const cleaned = await cleanMessages(res, Native);
loggedMessages = cleaned;
savedLoggedMessages = cleaned;
return;
}
if (IS_WEB) {
Flogger.log("hii. no point in checking DataStore if. we already did up there ^^");
return;
}
const data = await DataStore.get(LOGGED_MESSAGES_KEY, MessageLoggerStore);
if (data == null) {
Flogger.log("No logged messages in DataStore");
return;
}
Flogger.log("Loading logged messages from DataStore and writing to native");
Native.writeLogs(JSON.stringify(data));
loggedMessages = data;
savedLoggedMessages = res;
} catch (error) {
console.error("Error loading logged messages from the store:", error);
}
})();
// api
export const saveLoggedMessages = async () => {
if (settings.store.saveMessages) {
await Native.writeLogs(JSON.stringify(loggedMessages));
}
savedLoggedMessages = loggedMessages;
};
export const addMessage = async (message: LoggedMessage | LoggedMessageJSON, key: keyof LoggedMessageIds, isBulk = false) => {
if (settings.store.saveImages && key === "deletedMessages")
await cacheMessageImages(message);
const finalMessage = cleanupMessage(message);
loggedMessages[message.id] = { message: finalMessage };
await addMessageIDB(finalMessage, status);
if (!loggedMessages[key][message.channel_id])
loggedMessages[key][message.channel_id] = [];
if (settings.store.messageLimit > 0) {
const currentMessageCount = await db.count("messages");
if (currentMessageCount > settings.store.messageLimit) {
const messagesToDelete = currentMessageCount - settings.store.messageLimit;
if (messagesToDelete <= 0 || messagesToDelete >= settings.store.messageLimit) return;
if (!loggedMessages[key][message.channel_id].includes(message.id))
loggedMessages[key][message.channel_id].push(message.id);
const oldestMessages = await getOldestMessagesIDB(messagesToDelete);
// if limit is negative or 0 there is no limit
if (settings.store.messageLimit > 0 && (Object.keys(loggedMessages).length - 2) > settings.store.messageLimit)
await deleteOldestMessageWithoutSaving(loggedMessages);
Flogger.info(`Deleting ${messagesToDelete} oldest messages`);
await deleteMessagesBulkIDB(oldestMessages.map(m => m.message_id));
if (!isBulk)
await saveLoggedMessages();
};
export const removeFromKey = (
message_id: string,
channel_id: string,
loggedMessages: LoggedMessages,
key: keyof LoggedMessageIds,
) => {
if (loggedMessages[key][channel_id]) {
loggedMessages[key][channel_id] = loggedMessages[key][channel_id].filter(msgid => msgid !== message_id);
if (loggedMessages[key][channel_id].length === 0) {
delete loggedMessages[key][channel_id];
}
}
};
function removeLogWithoutSaving(messageId: string, loggedMessages: LoggedMessages) {
const record = loggedMessages[messageId];
if (record) {
const channel_id = record.message?.channel_id;
if (channel_id != null) {
removeFromKey(messageId, channel_id, loggedMessages, "editedMessages");
removeFromKey(messageId, channel_id, loggedMessages, "deletedMessages");
}
delete loggedMessages[messageId];
}
return loggedMessages;
}
export async function removeLogs(ids: string[]) {
for (const msgId of ids) {
removeLogWithoutSaving(msgId, loggedMessages);
}
await saveLoggedMessages();
}
export async function removeLog(id: string) {
const record = loggedMessages[id];
if (record?.message)
deleteMessageImages(record.message);
removeLogWithoutSaving(id, loggedMessages);
await saveLoggedMessages();
}
export async function clearLogs() {
await Native.writeLogs(JSON.stringify(defaultLoggedMessages));
loggedMessages = defaultLoggedMessages;
savedLoggedMessages = defaultLoggedMessages;
}
// utils
export const hasMessageInLogs = (messageId: string) => {
const bruh = Object.values(savedLoggedMessages)
.filter(m => !Array.isArray(m)) as MessageRecord[];
return bruh.find(m => m.message?.id === messageId);
};
export const hasLogs = async () => {
const hasDeletedMessages = Object.keys(loggedMessages.deletedMessages).length > 0;
const hasEditedMessages = Object.keys(loggedMessages.editedMessages).length > 0;
const hasMessages = Object.keys(loggedMessages).filter(m => m !== "editedMessages" && m !== "deletedMessages").length > 0;
if (hasDeletedMessages && hasEditedMessages && hasMessages) return true;
return false;
};
export function findLoggedChannelByMessageIdSync(messageId: string, loggedMessages: LoggedMessages, key: keyof LoggedMessageIds) {
for (const channelId in loggedMessages[key]) {
if (loggedMessages[key][channelId].includes(messageId)) return channelId;
}
return null;
}
export async function findLoggedChannelByMessage(messageId: string, key?: keyof LoggedMessageIds): Promise<[string | null, keyof LoggedMessageIds]> {
if (!key) {
const id1 = findLoggedChannelByMessageIdSync(messageId, loggedMessages, "deletedMessages");
if (id1) return [id1, "deletedMessages"];
const id2 = findLoggedChannelByMessageIdSync(messageId, loggedMessages, "editedMessages");
return [id2, "editedMessages"];
}
return [findLoggedChannelByMessageIdSync(messageId, loggedMessages, key), key];
}
export function getOldestMessage(loggedMessageIds: LoggedMessages) {
const messags = Object.values(loggedMessageIds)
.filter(m => !Array.isArray(m) && m.message != null) as MessageRecord[];
const sortedMessages = messags.sort((a, b) => sortMessagesByDate(a.message.timestamp, b.message.timestamp));
const oldestMessage = sortedMessages[sortedMessages.length - 1];
return oldestMessage ?? null;
}
export function getMessage(channelId: string, messageId: string) {
const messags = Object.values(savedLoggedMessages)
.filter(m => !Array.isArray(m) && m.message != null) as MessageRecord[];
return messags.find(m => m.message.channel_id === channelId && m.message.id === messageId);
}
export async function deleteOldestMessageWithoutSaving(loggedMessages: LoggedMessages) {
const oldestMessage = getOldestMessage(loggedMessages);
if (!oldestMessage || !oldestMessage.message) {
console.warn("couldnt find oldest message. oldestMessage == null || oldestMessage.message == null");
return loggedMessages;
}
const { message } = oldestMessage;
const [channelId, key] = await findLoggedChannelByMessage(message.id);
if (!channelId || !key) {
console.warn("couldnt find oldest message. channelId =", channelId, " key =", key);
return loggedMessages;
}
removeLogWithoutSaving(message.id, loggedMessages);
// console.log("removing", message);
return loggedMessages;
}
async function cleanMessages(loggedMessages: LoggedMessages, _Native: any = Native) {
try {
const cleaned = { ...loggedMessages };
if (IS_WEB) return cleaned;
const messageRecords = Object.values(cleaned)
.filter(m => !Array.isArray(m)) as MessageRecord[];
let hasChanged = false;
for (const messageRecord of messageRecords) {
const { message } = messageRecord;
if (message?.attachments) {
for (const attachment of message.attachments) {
if (attachment.blobUrl) {
hasChanged = true;
delete attachment.blobUrl;
}
}
}
}
if (hasChanged)
await _Native.writeLogs(Settings.plugins.MessageLoggerEnhanced.logsDir, JSON.stringify(cleaned));
return cleaned;
} catch (err) {
Flogger.error("Error cleaning messages:", err);
return loggedMessages;
}
}

View file

@ -1,26 +1,37 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2023 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 { classNameFactory } from "@api/Styles";
import { Flex } from "@components/Flex";
import { InfoIcon } from "@components/Icons";
import { openUserProfile } from "@utils/discord";
import { copyWithToast } from "@utils/misc";
import { closeAllModals, ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal";
import { LazyComponent } from "@utils/react";
import { LazyComponent, useAwaiter } from "@utils/react";
import { find, findByCode, findByCodeLazy } from "@webpack";
import { Alerts, Button, ChannelStore, ContextMenuApi, FluxDispatcher, Menu, NavigationRouter, React, TabBar, Text, TextInput, Tooltip, useMemo, useRef, useState } from "@webpack/common";
import { Alerts, Button, ChannelStore, ContextMenuApi, FluxDispatcher, Menu, NavigationRouter, React, TabBar, Text, TextInput, useCallback, useMemo, useRef, useState } from "@webpack/common";
import { User } from "discord-types/general";
import { clearMessagesIDB, DBMessageRecord, deleteMessageIDB, deleteMessagesBulkIDB } from "../db";
import { settings } from "../index";
import { LoggedMessage, LoggedMessageJSON } from "../types";
import { messageJsonToMessageClass } from "../utils";
import { importLogs } from "../utils/settingsUtils";
import { useMessages } from "./hooks";
import { clearLogs, defaultLoggedMessages, removeLog, removeLogs, savedLoggedMessages } from "../LoggedMessageManager";
import { LoggedMessage, LoggedMessageJSON, LoggedMessages } from "../types";
import { isGhostPinged, messageJsonToMessageClass, sortMessagesByDate } from "../utils";
import { doesMatch, parseQuery } from "../utils/parseQuery";
export interface MessagePreviewProps {
className: string;
@ -56,7 +67,7 @@ const ChildrenAccessories = LazyComponent<ChildrenAccProops>(() => findByCode("c
const cl = classNameFactory("msg-logger-modal-");
export enum LogTabs {
enum LogTabs {
DELETED = "Deleted",
EDITED = "Edited",
GHOST_PING = "Ghost Pinged"
@ -68,14 +79,73 @@ interface Props {
}
export function LogsModal({ modalProps, initalQuery }: Props) {
const [x, setX] = useState(0);
const forceUpdate = () => setX(e => e + 1);
const [logs, _, pending] = useAwaiter(async () => savedLoggedMessages, {
fallbackValue: defaultLoggedMessages as LoggedMessages,
deps: [x]
});
const [currentTab, setCurrentTab] = useState(LogTabs.DELETED);
const [queryEh, setQuery] = useState(initalQuery ?? "");
const [sortNewest, setSortNewest] = useState(settings.store.sortNewest);
const [numDisplayedMessages, setNumDisplayedMessages] = useState(settings.store.messagesToDisplayAtOnceInLogs);
const [numDisplayedMessages, setNumDisplayedMessages] = useState(50);
const contentRef = useRef<HTMLDivElement | null>(null);
const { messages, total, statusTotal, pending, reset } = useMessages(queryEh, currentTab, sortNewest, numDisplayedMessages);
const handleLoadMore = useCallback(() => {
setNumDisplayedMessages(prevNum => prevNum + 50);
}, []);
// Flogger.log(logs, _, pending, contentRef);
// Flogger.time("hi");
const messages: string[][] = currentTab === LogTabs.DELETED || currentTab === LogTabs.GHOST_PING
? Object.values(logs?.deletedMessages ?? {})
: Object.values(logs?.editedMessages ?? {});
const flattendAndfilteredAndSortedMessages = useMemo(() => {
const { success, type, id, negate, query } = parseQuery(queryEh);
if (query === "" && !success) {
const result = messages
.flat()
.filter(m => currentTab === LogTabs.GHOST_PING ? isGhostPinged(logs[m].message!) : true)
.sort(sortMessagesByDate);
return sortNewest ? result : result.reverse();
}
const result = messages
.flat()
.filter(m =>
currentTab === LogTabs.GHOST_PING
? isGhostPinged(logs[m].message)
: true
)
.filter(m =>
logs[m]?.message != null &&
(
success === false
? true
: negate
? !doesMatch(type!, id!, logs[m].message!)
: doesMatch(type!, id!, logs[m].message!)
)
)
.filter(m =>
logs[m]?.message?.content?.toLowerCase()?.includes(query.toLowerCase()) ??
logs[m].message?.editHistory?.map(m => m.content?.toLowerCase()).includes(query.toLowerCase())
)
.sort(sortMessagesByDate);
return sortNewest ? result : result.reverse();
}, [currentTab, logs, queryEh, sortNewest]);
const visibleMessages = flattendAndfilteredAndSortedMessages.slice(0, numDisplayedMessages);
const canLoadMore = numDisplayedMessages < flattendAndfilteredAndSortedMessages.length;
// Flogger.timeEnd("hi");
return (
<ModalRoot className={cl("root")} {...modalProps} size={ModalSize.LARGE}>
<ModalHeader className={cl("header")}>
@ -87,7 +157,7 @@ export function LogsModal({ modalProps, initalQuery }: Props) {
selectedItem={currentTab}
onItemSelect={e => {
setCurrentTab(e);
setNumDisplayedMessages(settings.store.messagesToDisplayAtOnceInLogs);
setNumDisplayedMessages(50);
contentRef.current?.firstElementChild?.scrollTo(0, 0);
// forceUpdate();
}}
@ -118,23 +188,21 @@ export function LogsModal({ modalProps, initalQuery }: Props) {
<ModalContent
className={cl("content")}
>
{messages != null && total === 0 && (
<EmptyLogs
hasQuery={queryEh.length !== 0}
reset={reset}
/>
)}
{!pending && messages != null && (
<LogsContentMemo
visibleMessages={messages}
canLoadMore={messages.length < statusTotal && messages.length >= settings.store.messagesToDisplayAtOnceInLogs}
tab={currentTab}
sortNewest={sortNewest}
reset={reset}
handleLoadMore={() => setNumDisplayedMessages(e => e + settings.store.messagesToDisplayAtOnceInLogs)}
/>
)}
{
pending || logs == null || messages.length === 0
? <EmptyLogs />
: (
<LogsContentMemo
visibleMessages={visibleMessages}
canLoadMore={canLoadMore}
tab={currentTab}
logs={logs}
sortNewest={sortNewest}
forceUpdate={forceUpdate}
handleLoadMore={handleLoadMore}
/>
)
}
</ModalContent>
}
</div>
@ -148,8 +216,8 @@ export function LogsModal({ modalProps, initalQuery }: Props) {
confirmColor: Button.Colors.RED,
cancelText: "Cancel",
onConfirm: async () => {
await clearMessagesIDB();
reset();
await clearLogs();
forceUpdate();
}
})}
@ -159,16 +227,16 @@ export function LogsModal({ modalProps, initalQuery }: Props) {
<Button
style={{ marginRight: "16px" }}
color={Button.Colors.YELLOW}
disabled={messages?.length === 0}
disabled={visibleMessages.length === 0}
onClick={() => Alerts.show({
title: "Clear Logs",
body: `Are you sure you want to clear ${messages.length} logs`,
body: `Are you sure you want to clear ${visibleMessages.length} logs`,
confirmText: "Clear",
confirmColor: Button.Colors.RED,
cancelText: "Cancel",
onConfirm: async () => {
await deleteMessagesBulkIDB(messages.map(e => e.message_id));
reset();
await removeLogs(visibleMessages);
forceUpdate();
}
})}
>
@ -194,27 +262,28 @@ export function LogsModal({ modalProps, initalQuery }: Props) {
}
interface LogContentProps {
logs: LoggedMessages,
sortNewest: boolean;
tab: LogTabs;
visibleMessages: DBMessageRecord[];
visibleMessages: string[];
canLoadMore: boolean;
reset: () => void;
forceUpdate: () => void;
handleLoadMore: () => void;
}
function LogsContent({ visibleMessages, canLoadMore, sortNewest, tab, reset, handleLoadMore }: LogContentProps) {
function LogsContent({ logs, visibleMessages, canLoadMore, sortNewest, tab, forceUpdate, handleLoadMore }: LogContentProps) {
if (visibleMessages.length === 0)
return <NoResults tab={tab} />;
return (
<div className={cl("content-inner")}>
{visibleMessages
.map(({ message }, i) => (
.map((id, i) => (
<LMessage
key={message.id}
log={{ message }}
reset={reset}
isGroupStart={isGroupStart(message, visibleMessages[i - 1]?.message, sortNewest)}
key={id}
log={logs[id] as { message: LoggedMessageJSON; }}
forceUpdate={forceUpdate}
isGroupStart={isGroupStart(logs[id]?.message, logs[visibleMessages[i - 1]]?.message, sortNewest)}
/>
))}
{
@ -261,35 +330,12 @@ function NoResults({ tab }: { tab: LogTabs; }) {
);
}
function EmptyLogs({ hasQuery, reset: forceUpdate }: { hasQuery: boolean; reset: () => void; }) {
function EmptyLogs() {
return (
<div className={cl("empty-logs", "content-inner")} style={{ textAlign: "center" }}>
<Flex flexDirection="column" style={{ position: "relative" }}>
<Text variant="text-lg/normal">
Empty eh
</Text>
{!hasQuery && (
<>
<Tooltip text="ML Enhanced now stores logs in indexeddb. You need to import your old logs from the logs directory. Importing wont overwrite existing logs">
{({ onMouseEnter, onMouseLeave }) => (
<div
className={cl("info-icon")}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
<InfoIcon />
</div>
)}
</Tooltip>
<Button onClick={() => importLogs().then(() => forceUpdate())}>
Import Logs
</Button>
</>
)}
</Flex>
<Text variant="text-lg/normal">
Empty eh
</Text>
</div>
);
@ -298,13 +344,11 @@ function EmptyLogs({ hasQuery, reset: forceUpdate }: { hasQuery: boolean; reset:
interface LMessageProps {
log: { message: LoggedMessageJSON; };
isGroupStart: boolean,
reset: () => void;
forceUpdate: () => void;
}
function LMessage({ log, isGroupStart, reset, }: LMessageProps) {
function LMessage({ log, isGroupStart, forceUpdate, }: LMessageProps) {
const message = useMemo(() => messageJsonToMessageClass(log), [log]);
// console.log(message);
if (!message) return null;
return (
@ -326,6 +370,7 @@ function LMessage({ log, isGroupStart, reset, }: LMessageProps) {
closeAllModals();
}}
/>
<Menu.MenuItem
key="open-user-profile"
id="open-user-profile"
@ -382,7 +427,10 @@ function LMessage({ log, isGroupStart, reset, }: LMessageProps) {
label="Delete Log"
color="danger"
action={() =>
deleteMessageIDB(log.message.id).then(() => reset())
removeLog(log.message.id)
.then(() => {
forceUpdate();
})
}
/>
@ -430,8 +478,6 @@ function isGroupStart(
) {
if (!currentMessage || !previousMessage) return true;
if (currentMessage.id === previousMessage.id) return true;
const [newestMessage, oldestMessage] = sortNewest
? [previousMessage, currentMessage]
: [currentMessage, previousMessage];

View file

@ -1,109 +0,0 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { useEffect, useState } from "@webpack/common";
import { countMessagesByStatusIDB, countMessagesIDB, DBMessageRecord, DBMessageStatus, getDateStortedMessagesByStatusIDB } from "../db";
import { doesMatch, tokenizeQuery } from "../utils/parseQuery";
import { LogTabs } from "./LogsModal";
function useDebouncedValue<T>(value: T, delay: number): T {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}, [value, delay]);
return debouncedValue;
}
// this is so shit
export function useMessages(query: string, currentTab: LogTabs, sortNewest: boolean, numDisplayedMessages: number) {
// only for initial load
const [pending, setPending] = useState(true);
const [messages, setMessages] = useState<DBMessageRecord[]>([]);
const [statusTotal, setStatusTotal] = useState<number>(0);
const [total, setTotal] = useState<number>(0);
const debouncedQuery = useDebouncedValue(query, 300);
useEffect(() => {
countMessagesIDB().then(x => setTotal(x));
}, [pending]);
useEffect(() => {
let isMounted = true;
const loadMessages = async () => {
const status = getStatus(currentTab);
if (debouncedQuery === "") {
const [messages, statusTotal] = await Promise.all([
getDateStortedMessagesByStatusIDB(sortNewest, numDisplayedMessages, status),
countMessagesByStatusIDB(status),
]);
if (isMounted) {
setMessages(messages);
setStatusTotal(statusTotal);
}
setPending(false);
} else {
const allMessages = await getDateStortedMessagesByStatusIDB(sortNewest, Number.MAX_SAFE_INTEGER, status);
const { queries, rest } = tokenizeQuery(debouncedQuery);
const filteredMessages = allMessages.filter(record => {
for (const query of queries) {
const matching = doesMatch(query.key, query.value, record.message);
if (query.negate ? matching : !matching) {
return false;
}
}
return rest.every(r =>
record.message.content.toLowerCase().includes(r.toLowerCase())
);
});
if (isMounted) {
setMessages(filteredMessages);
setStatusTotal(Number.MAX_SAFE_INTEGER);
}
setPending(false);
}
};
loadMessages();
return () => {
isMounted = false;
};
}, [debouncedQuery, sortNewest, numDisplayedMessages, currentTab, pending]);
return { messages, statusTotal, total, pending, reset: () => setPending(true) };
}
function getStatus(currentTab: LogTabs) {
switch (currentTab) {
case LogTabs.DELETED:
return DBMessageStatus.DELETED;
case LogTabs.EDITED:
return DBMessageStatus.EDITED;
default:
return DBMessageStatus.GHOST_PINGED;
}
}

View file

@ -1,199 +0,0 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { LoggedMessageJSON } from "./types";
import { getMessageStatus } from "./utils";
import { DB_NAME, DB_VERSION } from "./utils/constants";
import { DBSchema, IDBPDatabase, openDB } from "./utils/idb";
import { getAttachmentBlobUrl } from "./utils/saveImage";
export enum DBMessageStatus {
DELETED = "DELETED",
EDITED = "EDITED",
GHOST_PINGED = "GHOST_PINGED",
}
export interface DBMessageRecord {
message_id: string;
channel_id: string;
status: DBMessageStatus;
message: LoggedMessageJSON;
}
export interface MLIDB extends DBSchema {
messages: {
key: string;
value: DBMessageRecord;
indexes: {
by_channel_id: string;
by_status: DBMessageStatus;
by_timestamp: string;
by_timestamp_and_message_id: [string, string];
};
};
}
export let db: IDBPDatabase<MLIDB>;
export const cachedMessages = new Map<string, LoggedMessageJSON>();
// this is probably not the best way to do this
async function cacheRecords(records: DBMessageRecord[]) {
for (const r of records) {
cacheRecord(r);
for (const att of r.message.attachments) {
const blobUrl = await getAttachmentBlobUrl(att);
if (blobUrl) {
att.url = blobUrl + "#";
att.proxy_url = blobUrl + "#";
}
}
}
return records;
}
async function cacheRecord(record?: DBMessageRecord | null) {
if (!record) return record;
cachedMessages.set(record.message_id, record.message);
return record;
}
export async function initIDB() {
db = await openDB<MLIDB>(DB_NAME, DB_VERSION, {
upgrade(db) {
const messageStore = db.createObjectStore("messages", { keyPath: "message_id" });
messageStore.createIndex("by_channel_id", "channel_id");
messageStore.createIndex("by_status", "status");
messageStore.createIndex("by_timestamp", "message.timestamp");
messageStore.createIndex("by_timestamp_and_message_id", ["channel_id", "message.timestamp"]);
}
});
}
initIDB();
export async function hasMessageIDB(message_id: string) {
return cachedMessages.has(message_id) || (await db.count("messages", message_id)) > 0;
}
export async function countMessagesIDB() {
return db.count("messages");
}
export async function countMessagesByStatusIDB(status: DBMessageStatus) {
return db.countFromIndex("messages", "by_status", status);
}
export async function getAllMessagesIDB() {
return cacheRecords(await db.getAll("messages"));
}
export async function getMessagesForChannelIDB(channel_id: string) {
return cacheRecords(await db.getAllFromIndex("messages", "by_channel_id", channel_id));
}
export async function getMessageIDB(message_id: string) {
return cacheRecord(await db.get("messages", message_id));
}
export async function getMessagesByStatusIDB(status: DBMessageStatus) {
return cacheRecords(await db.getAllFromIndex("messages", "by_status", status));
}
export async function getOldestMessagesIDB(limit: number) {
return cacheRecords(await db.getAllFromIndex("messages", "by_timestamp", undefined, limit));
}
export async function getDateStortedMessagesByStatusIDB(newest: boolean, limit: number, status: DBMessageStatus) {
const tx = db.transaction("messages", "readonly");
const { store } = tx;
const index = store.index("by_status");
const direction = newest ? "prev" : "next";
const cursor = await index.openCursor(IDBKeyRange.only(status), direction);
if (!cursor) {
console.log("No messages found");
return [];
}
const messages: DBMessageRecord[] = [];
for await (const c of cursor) {
messages.push(c.value);
if (messages.length >= limit) break;
}
return cacheRecords(messages);
}
export async function getMessagesByChannelAndAfterTimestampIDB(channel_id: string, start: string) {
const tx = db.transaction("messages", "readonly");
const { store } = tx;
const index = store.index("by_timestamp_and_message_id");
const cursor = await index.openCursor(IDBKeyRange.bound([channel_id, start], [channel_id, "\uffff"]));
if (!cursor) {
console.log("No messages found in range");
return [];
}
const messages: DBMessageRecord[] = [];
for await (const c of cursor) {
messages.push(c.value);
}
return cacheRecords(messages);
}
export async function addMessageIDB(message: LoggedMessageJSON, status: DBMessageStatus) {
await db.put("messages", {
channel_id: message.channel_id,
message_id: message.id,
status,
message,
});
cachedMessages.set(message.id, message);
}
export async function addMessagesBulkIDB(messages: LoggedMessageJSON[], status?: DBMessageStatus) {
const tx = db.transaction("messages", "readwrite");
const { store } = tx;
await Promise.all([
...messages.map(message => store.add({
channel_id: message.channel_id,
message_id: message.id,
status: status ?? getMessageStatus(message),
message,
})),
tx.done
]);
messages.forEach(message => cachedMessages.set(message.id, message));
}
export async function deleteMessageIDB(message_id: string) {
await db.delete("messages", message_id);
cachedMessages.delete(message_id);
}
export async function deleteMessagesBulkIDB(message_ids: string[]) {
const tx = db.transaction("messages", "readwrite");
const { store } = tx;
await Promise.all([...message_ids.map(id => store.delete(id)), tx.done]);
message_ids.forEach(id => cachedMessages.delete(id));
}
export async function clearMessagesIDB() {
await db.clear("messages");
cachedMessages.clear();
}

View file

@ -1,37 +1,51 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2023 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 const VERSION = "4.0.0";
export const VERSION = "3.0.0";
export const Native = getNative();
import "./styles.css";
import { NavContextMenuPatchCallback } from "@api/ContextMenu";
import { definePluginSettings, Settings } from "@api/Settings";
import ErrorBoundary from "@components/ErrorBoundary";
import { Devs } from "@utils/constants";
import { Logger } from "@utils/Logger";
import definePlugin from "@utils/types";
import definePlugin, { OptionType } from "@utils/types";
import { findByPropsLazy } from "@webpack";
import { FluxDispatcher, MessageStore, React, UserStore } from "@webpack/common";
import { Alerts, Button, FluxDispatcher, Menu, MessageActions, MessageStore, React, Toasts, UserStore } from "@webpack/common";
import { ImageCacheDir, LogsDir } from "./components/FolderSelectInput";
import { OpenLogsButton } from "./components/LogsButton";
import { openLogModal } from "./components/LogsModal";
import * as idb from "./db";
import { addMessage } from "./LoggedMessageManager";
import { addMessage, loggedMessages, MessageLoggerStore, removeLog } from "./LoggedMessageManager";
import * as LoggedMessageManager from "./LoggedMessageManager";
import { settings } from "./settings";
import { FetchMessagesResponse, LoadMessagePayload, LoggedMessage, LoggedMessageJSON, MessageCreatePayload, MessageDeleteBulkPayload, MessageDeletePayload, MessageUpdatePayload } from "./types";
import { cleanUpCachedMessage, cleanupUserObject, getNative, isGhostPinged, mapTimestamp, messageJsonToMessageClass, reAddDeletedMessages } from "./utils";
import { removeContextMenuBindings, setupContextMenuPatches } from "./utils/contextMenu";
import { LoadMessagePayload, LoggedAttachment, LoggedMessage, LoggedMessageJSON, MessageCreatePayload, MessageDeleteBulkPayload, MessageDeletePayload, MessageUpdatePayload } from "./types";
import { addToXAndRemoveFromOpposite, cleanUpCachedMessage, cleanupUserObject, doesBlobUrlExist, getNative, isGhostPinged, ListType, mapEditHistory, messageJsonToMessageClass, reAddDeletedMessages, removeFromX } from "./utils";
import { DEFAULT_IMAGE_CACHE_DIR } from "./utils/constants";
import { shouldIgnore } from "./utils/index";
import { LimitedMap } from "./utils/LimitedMap";
import { doesMatch } from "./utils/parseQuery";
import * as imageUtils from "./utils/saveImage";
import * as ImageManager from "./utils/saveImage/ImageManager";
export { settings };
import { downloadLoggedMessages } from "./utils/settingsUtils";
export const Flogger = new Logger("MessageLoggerEnhanced", "#f26c6c");
@ -45,7 +59,7 @@ const handledMessageIds = new Set();
async function messageDeleteHandler(payload: MessageDeletePayload & { isBulk: boolean; }) {
if (payload.mlDeleted) {
if (settings.store.permanentlyRemoveLogByDefault)
await idb.deleteMessageIDB(payload.id);
await removeLog(payload.id);
return;
}
@ -68,8 +82,6 @@ async function messageDeleteHandler(payload: MessageDeletePayload & { isBulk: bo
message = { ...cacheSentMessages.get(`${payload.channelId},${payload.id}`), deleted: true } as LoggedMessageJSON;
}
const ghostPinged = isGhostPinged(message as any);
if (
shouldIgnore({
channelId: message?.channel_id ?? payload.channelId,
@ -77,7 +89,7 @@ async function messageDeleteHandler(payload: MessageDeletePayload & { isBulk: bo
authorId: message?.author?.id,
bot: message?.bot || message?.author?.bot,
flags: message?.flags,
ghostPinged,
ghostPinged: isGhostPinged(message as any),
isCachedByUs: (message as LoggedMessageJSON).ourCache
})
) {
@ -93,10 +105,7 @@ async function messageDeleteHandler(payload: MessageDeletePayload & { isBulk: bo
if (message == null || message.channel_id == null || !message.deleted) return;
// Flogger.log("ADDING MESSAGE (DELETED)", message);
if (payload.isBulk)
return message;
await addMessage(message, ghostPinged ? idb.DBMessageStatus.GHOST_PINGED : idb.DBMessageStatus.DELETED);
await addMessage(message, "deletedMessages", payload.isBulk ?? false);
}
finally {
handledMessageIds.delete(payload.id);
@ -105,13 +114,10 @@ async function messageDeleteHandler(payload: MessageDeletePayload & { isBulk: bo
async function messageDeleteBulkHandler({ channelId, guildId, ids }: MessageDeleteBulkPayload) {
// is this bad? idk man
const messages = [] as LoggedMessageJSON[];
for (const id of ids) {
const msg = await messageDeleteHandler({ type: "MESSAGE_DELETE", channelId, guildId, id, isBulk: true });
if (msg) messages.push(msg as LoggedMessageJSON);
await messageDeleteHandler({ type: "MESSAGE_DELETE", channelId, guildId, id, isBulk: true });
}
await idb.addMessagesBulkIDB(messages);
await LoggedMessageManager.saveLoggedMessages();
}
async function messageUpdateHandler(payload: MessageUpdatePayload) {
@ -148,7 +154,7 @@ async function messageUpdateHandler(payload: MessageUpdatePayload) {
...(cachedMessage.editHistory ?? []),
{
content: cachedMessage.content,
timestamp: (new Date()).toISOString()
timestamp: new Date().toISOString()
}
]
};
@ -160,7 +166,7 @@ async function messageUpdateHandler(payload: MessageUpdatePayload) {
if (message == null || message.channel_id == null || message.editHistory == null || message.editHistory.length === 0) return;
// Flogger.log("ADDING MESSAGE (EDITED)", message, payload);
await addMessage(message, idb.DBMessageStatus.EDITED);
await addMessage(message, "editedMessages");
}
function messageCreateHandler(payload: MessageCreatePayload) {
@ -180,86 +186,412 @@ function messageCreateHandler(payload: MessageCreatePayload) {
// Flogger.log(`cached\nkey:${payload.message.channel_id},${payload.message.id}\nvalue:`, payload.message);
}
async function processMessageFetch(response: FetchMessagesResponse) {
try {
if (!response.ok || response.body.length === 0) {
Flogger.error("Failed to fetch messages", response);
return;
// also stolen from mlv2
function messageLoadSuccess(payload: LoadMessagePayload) {
const deletedMessages = loggedMessages.deletedMessages[payload.channelId];
const editedMessages = loggedMessages.editedMessages[payload.channelId];
const recordIDs: string[] = [...(deletedMessages || []), ...(editedMessages || [])];
for (let i = 0; i < payload.messages.length; ++i) {
const recievedMessage = payload.messages[i];
const record = loggedMessages[recievedMessage.id];
if (record == null || record.message == null) continue;
if (record.message.editHistory!.length !== 0) {
payload.messages[i].editHistory = record.message.editHistory;
}
const firstMessage = response.body[response.body.length - 1];
// console.time("fetching messages from idb");
const messages = await idb.getMessagesByChannelAndAfterTimestampIDB(firstMessage.channel_id, firstMessage.timestamp);
// console.timeEnd("fetching messages from idb");
if (!messages.length) return;
const deletedMessages = messages.filter(m => m.status === idb.DBMessageStatus.DELETED);
for (const recivedMessage of response.body) {
const record = messages.find(m => m.message_id === recivedMessage.id);
if (record == null) continue;
if (record.message.editHistory && record.message.editHistory.length > 0) {
recivedMessage.editHistory = record.message.editHistory;
}
}
const fetchUser = (id: string) => UserStore.getUser(id) || response.body.find(e => e.author.id === id);
for (let i = 0, len = messages.length; i < len; i++) {
const record = messages[i];
if (!record) continue;
const { message } = record;
for (let j = 0, len2 = message.mentions.length; j < len2; j++) {
const user = message.mentions[j];
const cachedUser = fetchUser((user as any).id || user);
if (cachedUser) (message.mentions[j] as any) = cleanupUserObject(cachedUser);
}
const author = fetchUser(message.author.id);
if (!author) continue;
(message.author as any) = cleanupUserObject(author);
}
response.body.extra = deletedMessages.map(m => m.message);
} catch (e) {
Flogger.error("Failed to fetch messages", e);
}
const fetchUser = (id: string) => UserStore.getUser(id) || payload.messages.find(e => e.author.id === id);
for (let i = 0, len = recordIDs.length; i < len; i++) {
const id = recordIDs[i];
if (!loggedMessages[id]) continue;
const { message } = loggedMessages[id] as { message: LoggedMessageJSON; };
for (let j = 0, len2 = message.mentions.length; j < len2; j++) {
const user = message.mentions[j];
const cachedUser = fetchUser((user as any).id || user);
if (cachedUser) (message.mentions[j] as any) = cleanupUserObject(cachedUser);
}
const author = fetchUser(message.author.id);
if (!author) continue;
(message.author as any) = cleanupUserObject(author);
}
reAddDeletedMessages(payload.messages, deletedMessages, !payload.hasMoreAfter && !payload.isBefore, !payload.hasMoreBefore && !payload.isAfter);
}
export const settings = definePluginSettings({
saveMessages: {
default: true,
type: OptionType.BOOLEAN,
description: "Wether to save the deleted and edited messages.",
},
saveImages: {
type: OptionType.BOOLEAN,
description: "Save deleted messages",
default: false
},
sortNewest: {
default: true,
type: OptionType.BOOLEAN,
description: "Sort logs by newest.",
},
cacheMessagesFromServers: {
default: false,
type: OptionType.BOOLEAN,
description: "Usually message logger only logs from whitelisted ids and dms, enabling this would mean it would log messages from all servers as well. Note that this may cause the cache to exceed its limit, resulting in some messages being missed. If you are in a lot of servers, this may significantly increase the chances of messages being logged, which can result in a large message record and the inclusion of irrelevant messages.",
},
ignoreBots: {
type: OptionType.BOOLEAN,
description: "Whether to ignore messages by bots",
default: false,
onChange() {
// we will be handling the ignoreBots now (enabled or not) so the original messageLogger shouldnt
Settings.plugins.MessageLogger.ignoreBots = false;
}
},
ignoreSelf: {
type: OptionType.BOOLEAN,
description: "Whether to ignore messages by yourself",
default: false,
onChange() {
Settings.plugins.MessageLogger.ignoreSelf = false;
}
},
ignoreMutedGuilds: {
default: false,
type: OptionType.BOOLEAN,
description: "Messages in muted guilds will not be logged. Whitelisted users/channels in muted guilds will still be logged."
},
ignoreMutedCategories: {
default: false,
type: OptionType.BOOLEAN,
description: "Messages in channels belonging to muted categories will not be logged. Whitelisted users/channels in muted guilds will still be logged."
},
ignoreMutedChannels: {
default: false,
type: OptionType.BOOLEAN,
description: "Messages in muted channels will not be logged. Whitelisted users/channels in muted guilds will still be logged."
},
alwaysLogDirectMessages: {
default: true,
type: OptionType.BOOLEAN,
description: "Always log DMs",
},
alwaysLogCurrentChannel: {
default: true,
type: OptionType.BOOLEAN,
description: "Always log current selected channel. Blacklisted channels/users will still be ignored.",
},
permanentlyRemoveLogByDefault: {
default: false,
type: OptionType.BOOLEAN,
description: "Equicord's base MessageLogger remove log button wiil delete logs permanently",
},
hideMessageFromMessageLoggers: {
default: false,
type: OptionType.BOOLEAN,
description: "When enabled, a context menu button will be added to messages to allow you to delete messages without them being logged by other loggers. Might not be safe, use at your own risk."
},
ShowLogsButton: {
default: true,
type: OptionType.BOOLEAN,
description: "Toggle to whenever show the toolbox or not",
restartNeeded: true,
},
hideMessageFromMessageLoggersDeletedMessage: {
default: "redacted eh",
type: OptionType.STRING,
description: "The message content to replace the message with when using the hide message from message loggers feature.",
},
messageLimit: {
default: 200,
type: OptionType.NUMBER,
description: "Maximum number of messages to save. Older messages are deleted when the limit is reached. 0 means there is no limit"
},
imagesLimit: {
default: 100,
type: OptionType.NUMBER,
description: "Maximum number of images to save. Older images are deleted when the limit is reached. 0 means there is no limit"
},
cacheLimit: {
default: 1000,
type: OptionType.NUMBER,
description: "Maximum number of messages to store in the cache. Older messages are deleted when the limit is reached. This helps reduce memory usage and improve performance. 0 means there is no limit",
},
whitelistedIds: {
default: "",
type: OptionType.STRING,
description: "Whitelisted server, channel, or user IDs."
},
blacklistedIds: {
default: "",
type: OptionType.STRING,
description: "Blacklisted server, channel, or user IDs."
},
imageCacheDir: {
type: OptionType.COMPONENT,
description: "Select saved images directory",
component: ErrorBoundary.wrap(ImageCacheDir) as any
},
logsDir: {
type: OptionType.COMPONENT,
description: "Select logs directory",
component: ErrorBoundary.wrap(LogsDir) as any
},
exportLogs: {
type: OptionType.COMPONENT,
description: "Export Logs From IndexedDB",
component: () =>
<Button onClick={downloadLoggedMessages}>
Export Logs
</Button>
},
openLogs: {
type: OptionType.COMPONENT,
description: "Open Logs",
component: () =>
<Button onClick={() => openLogModal()}>
Open Logs
</Button>
},
openImageCacheFolder: {
type: OptionType.COMPONENT,
description: "Opens the image cache directory",
component: () =>
<Button
disabled={
IS_WEB
|| settings.store.imageCacheDir == null
|| settings.store.imageCacheDir === DEFAULT_IMAGE_CACHE_DIR
}
onClick={() => Native.showItemInFolder(settings.store.imageCacheDir)}
>
Open Image Cache Folder
</Button>
},
clearLogs: {
type: OptionType.COMPONENT,
description: "Clear Logs",
component: () =>
<Button
color={Button.Colors.RED}
onClick={() => Alerts.show({
title: "Clear Logs",
body: "Are you sure you want to clear all logs?",
confirmColor: Button.Colors.RED,
confirmText: "Clear",
cancelText: "Cancel",
onConfirm: () => {
LoggedMessageManager.clearLogs();
},
})}
>
Clear Logs
</Button>
},
});
const idFunctions = {
Server: props => props?.guild?.id,
User: props => props?.message?.author?.id || props?.user?.id,
Channel: props => props.message?.channel_id || props.channel?.id
} as const;
type idKeys = keyof typeof idFunctions;
function renderListOption(listType: ListType, IdType: idKeys, props: any) {
const id = idFunctions[IdType](props);
if (!id) return null;
const isBlocked = settings.store[listType].includes(id);
const oppositeListType = listType === "blacklistedIds" ? "whitelistedIds" : "blacklistedIds";
const isOppositeBlocked = settings.store[oppositeListType].includes(id);
const list = listType === "blacklistedIds" ? "Blacklist" : "Whitelist";
const addToList = () => addToXAndRemoveFromOpposite(listType, id);
const removeFromList = () => removeFromX(listType, id);
return (
<Menu.MenuItem
id={`${listType}-${IdType}-${id}`}
label={
isOppositeBlocked
? `Move ${IdType} to ${list}`
: isBlocked ? `Remove ${IdType} From ${list}` : `${list} ${IdType}`
}
action={isBlocked ? removeFromList : addToList}
/>
);
}
function renderOpenLogs(idType: idKeys, props: any) {
const id = idFunctions[idType](props);
if (!id) return null;
return (
<Menu.MenuItem
id={`open-logs-for-${idType.toLowerCase()}`}
label={`Open Logs For ${idType}`}
action={() => openLogModal(`${idType.toLowerCase()}:${id}`)}
/>
);
}
const contextMenuPath: NavContextMenuPatchCallback = (children, props) => {
if (!props) return;
if (!children.some(child => child?.props?.id === "message-logger")) {
children.push(
<Menu.MenuSeparator />,
<Menu.MenuItem
id="message-logger"
label="Message Logger"
>
<Menu.MenuItem
id="open-logs"
label="Open Logs"
action={() => openLogModal()}
/>
{Object.keys(idFunctions).map(IdType => renderOpenLogs(IdType as idKeys, props))}
<Menu.MenuSeparator />
{Object.keys(idFunctions).map(IdType => (
<React.Fragment key={IdType}>
{renderListOption("blacklistedIds", IdType as idKeys, props)}
{renderListOption("whitelistedIds", IdType as idKeys, props)}
</React.Fragment>
))}
{
props.navId === "message"
&& (props.message?.deleted || props.message?.editHistory?.length > 0)
&& (
<>
<Menu.MenuSeparator />
<Menu.MenuItem
id="remove-message"
label={props.message?.deleted ? "Remove Message (Permanent)" : "Remove Message History (Permanent)"}
color="danger"
action={() =>
removeLog(props.message.id)
.then(() => {
if (props.message.deleted) {
FluxDispatcher.dispatch({
type: "MESSAGE_DELETE",
channelId: props.message.channel_id,
id: props.message.id,
mlDeleted: true
});
} else {
props.message.editHistory = [];
}
}).catch(() => Toasts.show({
type: Toasts.Type.FAILURE,
message: "Failed to remove message",
id: Toasts.genId()
}))
}
/>
</>
)
}
{
settings.store.hideMessageFromMessageLoggers
&& props.navId === "message"
&& props.message?.author?.id === UserStore.getCurrentUser().id
&& props.message?.deleted === false
&& (
<>
<Menu.MenuSeparator />
<Menu.MenuItem
id="hide-from-message-loggers"
label="Delete Message (Hide From Message Loggers)"
color="danger"
action={async () => {
await MessageActions.deleteMessage(props.message.channel_id, props.message.id);
MessageActions._sendMessage(props.message.channel_id, {
"content": settings.store.hideMessageFromMessageLoggersDeletedMessage,
"tts": false,
"invalidEmojis": [],
"validNonShortcutEmojis": []
}, { nonce: props.message.id });
}}
/>
</>
)
}
</Menu.MenuItem>
);
}
};
export default definePlugin({
name: "MessageLoggerEnhanced",
authors: [Devs.Aria],
description: "G'day",
description: "logs messages, images, and ghost pings",
dependencies: ["MessageLogger"],
contextMenus: {
"message": contextMenuPath,
"channel-context": contextMenuPath,
"user-context": contextMenuPath,
"guild-context": contextMenuPath,
"gdm-context": contextMenuPath
},
patches: [
{
find: "_tryFetchMessagesCached",
replacement: [
{
match: /(?<=\.get\({url.+?then\()(\i)=>\(/,
replace: "async $1=>(await $self.processMessageFetch($1),"
},
{
match: /(?<=type:"LOAD_MESSAGES_SUCCESS",.{1,100})messages:(\i)/,
replace: "get messages() {return $self.coolReAddDeletedMessages($1, this);}"
}
]
find: '"MessageStore"',
replacement: {
match: /(getOrCreate\(\i\);)(\i=\i\.loadComplete.*?}\),)/,
replace: "$1$self.messageLoadSuccess(arguments[0]);$2"
}
},
{
find: "THREAD_STARTER_MESSAGE?null===",
replacement: {
match: / deleted:\i\.deleted, editHistory:\i\.editHistory,/,
replace: "deleted:$self.getDeleted(...arguments), editHistory:$self.getEdited(...arguments),"
match: /interactionData:null!=.{0,50}.interaction_data/,
replace: "deleted:$self.getDeleted(...arguments), editHistory:$self.getEdited(...arguments)"
}
},
{
find: "toolbar:function",
predicate: () => settings.store.ShowLogsButton,
@ -277,15 +609,20 @@ export default definePlugin({
}
},
// https://regex101.com/r/S3IVGm/1
// fix vidoes failing because there are no thumbnails
// https://regex101.com/r/TMV1vY/1
{
find: ".handleImageLoad)",
find: ".removeMosaicItemHoverButton",
replacement: {
match: /(componentDidMount\(\){)(.{1,150}===(.+?)\.LOADING)/,
replace:
"$1if(this.props?.src?.startsWith('blob:') && this.props?.item?.type === 'VIDEO')" +
"return this.setState({readyState: $3.READY});$2"
match: /(\i=(\i)=>{)(.+?isSingleMosaicItem)/,
replace: "$1 let forceUpdate=Vencord.Util.useForceUpdater();$self.patchAttachments($2,forceUpdate);$3"
}
},
{
find: "handleImageLoad)",
replacement: {
match: /(render\(\){)(.{1,100}zoomThumbnailPlaceholder)/,
replace: "$1$self.checkImage(this);$2"
}
},
@ -331,29 +668,15 @@ export default definePlugin({
];
},
processMessageFetch,
messageLoadSuccess,
store: MessageLoggerStore,
openLogModal,
doesMatch,
reAddDeletedMessages,
LoggedMessageManager,
ImageManager,
imageUtils,
idb,
coolReAddDeletedMessages: (messages: LoggedMessageJSON[] & { extra: LoggedMessageJSON[]; }, payload: LoadMessagePayload) => {
try {
if (messages.extra)
reAddDeletedMessages(messages, messages.extra, !payload.hasMoreAfter && !payload.isBefore, !payload.hasMoreBefore && !payload.isAfter);
}
catch (e) {
Flogger.error("Failed to re-add deleted messages", e);
}
finally {
return messages;
}
},
isDeletedMessage: (id: string) => cacheSentMessages.get(id)?.deleted ?? false,
isDeletedMessage: (id: string) => loggedMessages.deletedMessages[id] != null,
getDeleted(m1, m2) {
const deleted = m2?.deleted;
@ -364,12 +687,53 @@ export default definePlugin({
getEdited(m1, m2) {
const editHistory = m2?.editHistory;
if (editHistory == null && m1?.editHistory != null && m1.editHistory.length > 0)
return m1.editHistory.map(mapTimestamp);
return m1.editHistory.map(mapEditHistory);
return editHistory;
},
attachments: new Map<string, LoggedAttachment>(),
patchAttachments(props: { attachment: LoggedAttachment, message: LoggedMessage; }, forceUpdate: () => void) {
const { attachment, message } = props;
if (!message?.deleted || !LoggedMessageManager.hasMessageInLogs(message.id))
return; // Flogger.log("ignoring", message.id);
if (this.attachments.has(attachment.id))
return props.attachment = this.attachments.get(attachment.id)!; // Flogger.log("blobUrl already exists");
imageUtils.getAttachmentBlobUrl(attachment).then((blobUrl: string | null) => {
if (blobUrl == null) {
Flogger.error("image not found. for message.id =", message.id, blobUrl);
return;
}
Flogger.log("Got blob url for message.id =", message.id, blobUrl);
// we need to copy because changing this will change the attachment for the message in the logs
const attachmentCopy = { ...attachment };
attachmentCopy.oldUrl = attachment.url;
const finalBlobUrl = blobUrl + "#";
attachmentCopy.blobUrl = finalBlobUrl;
attachmentCopy.url = finalBlobUrl;
attachmentCopy.proxy_url = finalBlobUrl;
this.attachments.set(attachment.id, attachmentCopy);
forceUpdate();
});
},
async checkImage(instance: any) {
if (!instance.props.isBad && instance.state?.readyState !== "READY" && instance.props?.src?.startsWith("blob:")) {
if (await doesBlobUrlExist(instance.props.src)) {
Flogger.log("image exists", instance.props.src);
return instance.setState(e => ({ ...e, readyState: "READY" }));
}
instance.props.isBad = true;
}
},
flux: {
"MESSAGE_DELETE": messageDeleteHandler as any,
"MESSAGE_DELETE": messageDeleteHandler,
"MESSAGE_DELETE_BULK": messageDeleteBulkHandler,
"MESSAGE_UPDATE": messageUpdateHandler,
"MESSAGE_CREATE": messageCreateHandler
@ -377,11 +741,10 @@ export default definePlugin({
async start() {
this.oldGetMessage = oldGetMessage = MessageStore.getMessage;
// we have to do this because the original message logger fetches the message from the store now
MessageStore.getMessage = (channelId: string, messageId: string) => {
const MLMessage = idb.cachedMessages.get(messageId);
if (MLMessage) return messageJsonToMessageClass({ message: MLMessage });
const MLMessage = LoggedMessageManager.getMessage(channelId, messageId);
if (MLMessage?.message) return messageJsonToMessageClass(MLMessage);
return this.oldGetMessage(channelId, messageId);
};
@ -391,13 +754,9 @@ export default definePlugin({
const { imageCacheDir, logsDir } = await Native.getSettings();
settings.store.imageCacheDir = imageCacheDir;
settings.store.logsDir = logsDir;
setupContextMenuPatches();
},
stop() {
removeContextMenuBindings();
MessageStore.getMessage = this.oldGetMessage;
}
});

View file

@ -7,15 +7,12 @@
import { readdir, readFile, unlink, writeFile } from "node:fs/promises";
import path from "node:path";
import { DATA_DIR } from "@main/utils/constants";
import { Queue } from "@utils/Queue";
import { dialog, IpcMainInvokeEvent, shell } from "electron";
import { DATA_DIR } from "../../../main/utils/constants";
import { getSettings, saveSettings } from "./settings";
export * from "./updater";
import { LoggedAttachment } from "../types";
import { LOGS_DATA_FILENAME } from "../utils/constants";
import { ensureDirectoryExists, getAttachmentIdFromFilename, sleep } from "./utils";
import { ensureDirectoryExists, getAttachmentIdFromFilename } from "./utils";
export { getSettings };
@ -56,13 +53,7 @@ export async function init(_event: IpcMainInvokeEvent) {
export async function getImageNative(_event: IpcMainInvokeEvent, attachmentId: string): Promise<Uint8Array | Buffer | null> {
const imagePath = nativeSavedImages.get(attachmentId);
if (!imagePath) return null;
try {
return await readFile(imagePath);
} catch (error: any) {
console.error(error);
return null;
}
return await readFile(imagePath);
}
export async function writeImageNative(_event: IpcMainInvokeEvent, filename: string, content: Uint8Array) {
@ -90,11 +81,24 @@ export async function deleteFileNative(_event: IpcMainInvokeEvent, attachmentId:
await unlink(imagePath);
}
const LOGS_DATA_FILENAME = "message-logger-logs.json";
const dataWriteQueue = new Queue();
export async function getLogsFromFs(_event: IpcMainInvokeEvent) {
const logsDir = await getLogsDir();
await ensureDirectoryExists(logsDir);
try {
return JSON.parse(await readFile(path.join(logsDir, LOGS_DATA_FILENAME), "utf-8"));
} catch { }
return null;
}
export async function writeLogs(_event: IpcMainInvokeEvent, contents: string) {
const logsDir = await getLogsDir();
writeFile(path.join(logsDir, LOGS_DATA_FILENAME), contents);
dataWriteQueue.push(() => writeFile(path.join(logsDir, LOGS_DATA_FILENAME), contents));
}
@ -133,68 +137,3 @@ export async function chooseDir(event: IpcMainInvokeEvent, logKey: "logsDir" | "
export async function showItemInFolder(_event: IpcMainInvokeEvent, filePath: string) {
shell.showItemInFolder(filePath);
}
export async function chooseFile(_event: IpcMainInvokeEvent, title: string, filters: Electron.FileFilter[], defaultPath?: string) {
const res = await dialog.showOpenDialog({ title, filters, properties: ["openFile"], defaultPath });
const [path] = res.filePaths;
if (!path) throw Error("Invalid file");
return await readFile(path, "utf-8");
}
// doing it in native because you can only fetch images from the renderer
// other types of files will cause cors issues
export async function downloadAttachment(_event: IpcMainInvokeEvent, attachemnt: LoggedAttachment, attempts = 0, useOldUrl = false): Promise<{ error: string | null; path: string | null; }> {
try {
if (!attachemnt?.url || !attachemnt.oldUrl || !attachemnt?.id || !attachemnt?.fileExtension)
return { error: "Invalid Attachment", path: null };
if (attachemnt.id.match(/[\\/.]/)) {
return { error: "Invalid Attachment ID", path: null };
}
const existingImage = nativeSavedImages.get(attachemnt.id);
if (existingImage)
return {
error: null,
path: existingImage
};
const res = await fetch(useOldUrl ? attachemnt.oldUrl : attachemnt.url);
if (res.status !== 200) {
if (res.status === 404 || res.status === 403)
return { error: `Failed to get attachment ${attachemnt.id} for caching, error code ${res.status}`, path: null };
attempts++;
if (attempts > 3) {
return {
error: `Failed to get attachment ${attachemnt.id} for caching. too many attempts, error code ${res.status}`,
path: null,
};
}
await sleep(1000);
return downloadAttachment(_event, attachemnt, attempts, res.status === 415);
}
const ab = await res.arrayBuffer();
const imageCacheDir = await getImageCacheDir();
await ensureDirectoryExists(imageCacheDir);
const finalPath = path.join(imageCacheDir, `${attachemnt.id}${attachemnt.fileExtension}`);
await writeFile(finalPath, Buffer.from(ab));
nativeSavedImages.set(attachemnt.id, finalPath);
return {
error: null,
path: finalPath
};
} catch (error: any) {
console.error(error);
return { error: error.message, path: null };
}
}

View file

@ -47,4 +47,3 @@ async function getSettingsFilePath() {
return mlSettingsDir;
}

View file

@ -1,134 +0,0 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { execFile as cpExecFile, ExecFileOptions } from "node:child_process";
import { readdir } from "fs/promises";
import { join } from "path";
import { promisify } from "util";
import type { GitResult } from "../types";
import { memoize } from "../utils/memoize";
const execFile = promisify(cpExecFile);
const isFlatpak = process.platform === "linux" && Boolean(process.env.FLATPAK_ID?.includes("discordapp") || process.env.FLATPAK_ID?.includes("Discord"));
if (process.platform === "darwin") process.env.PATH = `/usr/local/bin:${process.env.PATH}`;
const VENCORD_USER_PLUGIN_DIR = join(__dirname, "..", "src", "userplugins");
const getCwd = memoize(async () => {
const dirs = await readdir(VENCORD_USER_PLUGIN_DIR, { withFileTypes: true });
for (const dir of dirs) {
if (!dir.isDirectory()) continue;
const pluginDir = join(VENCORD_USER_PLUGIN_DIR, dir.name);
const files = await readdir(pluginDir);
if (files.includes("LoggedMessageManager.ts")) return join(VENCORD_USER_PLUGIN_DIR, dir.name);
}
return;
});
async function git(...args: string[]): Promise<GitResult> {
const opts: ExecFileOptions = { cwd: await getCwd(), shell: true };
try {
let result;
if (isFlatpak) {
result = await execFile("flatpak-spawn", ["--host", "git", ...args], opts);
} else {
result = await execFile("git", args, opts);
}
return { value: result.stdout.trim(), stderr: result.stderr, ok: true };
} catch (error: any) {
return {
ok: false,
cmd: error.cmd as string,
message: error.stderr as string,
error
};
}
}
export async function update() {
return await git("pull");
}
export async function getCommitHash() {
return await git("rev-parse", "HEAD");
}
export interface GitInfo {
repo: string;
gitHash: string;
}
export async function getRepoInfo(): Promise<GitResult> {
const res = await git("remote", "get-url", "origin");
if (!res.ok) {
return res;
}
const gitHash = await getCommitHash();
if (!gitHash.ok) {
return gitHash;
}
return {
ok: true,
value: {
repo: res.value
.replace(/git@(.+):/, "https://$1/")
.replace(/\.git$/, ""),
gitHash: gitHash.value
}
};
}
export interface Commit {
hash: string;
longHash: string;
message: string;
author: string;
}
export async function getNewCommits(): Promise<GitResult> {
const branch = await git("branch", "--show-current");
if (!branch.ok) {
return branch;
}
const logFormat = "%H;%an;%s";
const branchRange = `HEAD..origin/${branch.value}`;
try {
await git("fetch");
const logOutput = await git("log", `--format="${logFormat}"`, branchRange);
if (!logOutput.ok) {
return logOutput;
}
if (logOutput.value.trim() === "") {
return { ok: true, value: [] };
}
const commitLines = logOutput.value.trim().split("\n");
const commits: Commit[] = commitLines.map(line => {
const [hash, author, ...rest] = line.split(";");
return { longHash: hash, hash: hash.slice(0, 7), author, message: rest.join(";") } satisfies Commit;
});
return { ok: true, value: commits };
} catch (error: any) {
return { ok: false, cmd: error.cmd, message: error.message, error };
}
}

View file

@ -24,5 +24,3 @@ export async function ensureDirectoryExists(cacheDir: string) {
export function getAttachmentIdFromFilename(filename: string) {
return path.parse(filename).name;
}
export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

View file

@ -1,242 +0,0 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { definePluginSettings } from "@api/Settings";
import ErrorBoundary from "@components/ErrorBoundary";
import { OptionType } from "@utils/types";
import { Alerts, Button } from "@webpack/common";
import { Settings } from "Vencord";
import { Native } from ".";
import { ImageCacheDir, LogsDir } from "./components/FolderSelectInput";
import { openLogModal } from "./components/LogsModal";
import { clearMessagesIDB } from "./db";
import { DEFAULT_IMAGE_CACHE_DIR } from "./utils/constants";
import { exportLogs, importLogs } from "./utils/settingsUtils";
export const settings = definePluginSettings({
saveMessages: {
default: true,
type: OptionType.BOOLEAN,
description: "Wether to save the deleted and edited messages.",
},
saveImages: {
type: OptionType.BOOLEAN,
description: "Save deleted attachments.",
default: false
},
sortNewest: {
default: true,
type: OptionType.BOOLEAN,
description: "Sort logs by newest.",
},
cacheMessagesFromServers: {
default: false,
type: OptionType.BOOLEAN,
description: "Usually message logger only logs from whitelisted ids and dms, enabling this would mean it would log messages from all servers as well. Note that this may cause the cache to exceed its limit, resulting in some messages being missed. If you are in a lot of servers, this may significantly increase the chances of messages being logged, which can result in a large message record and the inclusion of irrelevant messages.",
},
autoCheckForUpdates: {
default: true,
type: OptionType.BOOLEAN,
description: "Automatically check for updates on startup.",
},
ignoreBots: {
type: OptionType.BOOLEAN,
description: "Whether to ignore messages by bots",
default: false,
onChange() {
// we will be handling the ignoreBots now (enabled or not) so the original messageLogger shouldnt
Settings.plugins.MessageLogger.ignoreBots = false;
}
},
ignoreSelf: {
type: OptionType.BOOLEAN,
description: "Whether to ignore messages by yourself",
default: false,
onChange() {
Settings.plugins.MessageLogger.ignoreSelf = false;
}
},
ignoreMutedGuilds: {
default: false,
type: OptionType.BOOLEAN,
description: "Messages in muted guilds will not be logged. Whitelisted users/channels in muted guilds will still be logged."
},
ignoreMutedCategories: {
default: false,
type: OptionType.BOOLEAN,
description: "Messages in channels belonging to muted categories will not be logged. Whitelisted users/channels in muted guilds will still be logged."
},
ignoreMutedChannels: {
default: false,
type: OptionType.BOOLEAN,
description: "Messages in muted channels will not be logged. Whitelisted users/channels in muted guilds will still be logged."
},
alwaysLogDirectMessages: {
default: true,
type: OptionType.BOOLEAN,
description: "Always log DMs",
},
alwaysLogCurrentChannel: {
default: true,
type: OptionType.BOOLEAN,
description: "Always log current selected channel. Blacklisted channels/users will still be ignored.",
},
permanentlyRemoveLogByDefault: {
default: false,
type: OptionType.BOOLEAN,
description: "Vencord's base MessageLogger remove log button wiil delete logs permanently",
},
hideMessageFromMessageLoggers: {
default: false,
type: OptionType.BOOLEAN,
description: "When enabled, a context menu button will be added to messages to allow you to delete messages without them being logged by other loggers. Might not be safe, use at your own risk."
},
ShowLogsButton: {
default: true,
type: OptionType.BOOLEAN,
description: "Toggle to whenever show the toolbox or not",
restartNeeded: true,
},
messagesToDisplayAtOnceInLogs: {
default: 100,
type: OptionType.NUMBER,
description: "Number of messages to display at once in logs & number of messages to load when loading more messages in logs.",
},
hideMessageFromMessageLoggersDeletedMessage: {
default: "redacted eh",
type: OptionType.STRING,
description: "The message content to replace the message with when using the hide message from message loggers feature.",
},
messageLimit: {
default: 200,
type: OptionType.NUMBER,
description: "Maximum number of messages to save. Older messages are deleted when the limit is reached. 0 means there is no limit"
},
attachmentSizeLimitInMegabytes: {
default: 12,
type: OptionType.NUMBER,
description: "Maximum size of an attachment in megabytes to save. Attachments larger than this size will not be saved."
},
attachmentFileExtensions: {
default: "png,jpg,jpeg,gif,webp,mp4,webm,mp3,ogg,wav",
type: OptionType.STRING,
description: "Comma separated list of file extensions to save. Attachments with file extensions not in this list will not be saved. Leave empty to save all attachments."
},
cacheLimit: {
default: 1000,
type: OptionType.NUMBER,
description: "Maximum number of messages to store in the cache. Older messages are deleted when the limit is reached. This helps reduce memory usage and improve performance. 0 means there is no limit",
},
whitelistedIds: {
default: "",
type: OptionType.STRING,
description: "Whitelisted server, channel, or user IDs."
},
blacklistedIds: {
default: "",
type: OptionType.STRING,
description: "Blacklisted server, channel, or user IDs."
},
imageCacheDir: {
type: OptionType.COMPONENT,
description: "Select saved images directory",
component: ErrorBoundary.wrap(ImageCacheDir) as any
},
logsDir: {
type: OptionType.COMPONENT,
description: "Select logs directory",
component: ErrorBoundary.wrap(LogsDir) as any
},
importLogs: {
type: OptionType.COMPONENT,
description: "Import Logs From File",
component: () =>
<Button onClick={importLogs}>
Import Logs
</Button>
},
exportLogs: {
type: OptionType.COMPONENT,
description: "Export Logs From IndexedDB",
component: () =>
<Button onClick={exportLogs}>
Export Logs
</Button>
},
openLogs: {
type: OptionType.COMPONENT,
description: "Open Logs",
component: () =>
<Button onClick={() => openLogModal()}>
Open Logs
</Button>
},
openImageCacheFolder: {
type: OptionType.COMPONENT,
description: "Opens the image cache directory",
component: () =>
<Button
disabled={
IS_WEB
|| settings.store.imageCacheDir == null
|| settings.store.imageCacheDir === DEFAULT_IMAGE_CACHE_DIR
}
onClick={() => Native.showItemInFolder(settings.store.imageCacheDir)}
>
Open Image Cache Folder
</Button>
},
clearLogs: {
type: OptionType.COMPONENT,
description: "Clear Logs",
component: () =>
<Button
color={Button.Colors.RED}
onClick={() => Alerts.show({
title: "Clear Logs",
body: "Are you sure you want to clear all logs?",
confirmColor: Button.Colors.RED,
confirmText: "Clear",
cancelText: "Cancel",
onConfirm: () => {
clearMessagesIDB();
},
})}
>
Clear Logs
</Button>
},
});

View file

@ -11,14 +11,6 @@
height: 100%;
}
.msg-logger-modal-info-icon {
position: absolute;
top: -24px;
right: -24px;
color: var(--interactive-normal);
cursor: pointer;
}
.msg-logger-modal-header {
flex-direction: column;
@ -43,11 +35,12 @@
height: 100%;
}
.msg-logger-modal-header > div:has(input) {
.msg-logger-modal-header>div:has(input) {
width: 100%;
}
.msg-logger-modal-tab-bar-item {
margin-right: 32px;
padding-bottom: 16px;
margin-bottom: -2px;
}
@ -62,7 +55,7 @@
color: var(--interactive-normal);
}
:is(.vc-log-toolbox-btn:hover, .vc-log-toolbox-btn[class*="selected"]) svg {
:is(.vc-log-toolbox-btn:hover, .vc-log-toolbox-btn[class*='selected']) svg {
color: var(--interactive-active);
}
@ -83,3 +76,31 @@
margin: 6px;
padding: 4px 8px;
}
.vc-updater-modal-content {
padding: 1rem 2rem;
}
div[class*="messagelogger-deleted"] [class*="contents"] > :is(div, h1, h2, h3, p) {
color: var(--text-normal) !important;
}
/* Markdown title highlighting */
div[class*="messagelogger-deleted"] [class*="contents"] :is(h1, h2, h3) {
color: var(--text-normal) !important;
}
/* Bot "thinking" text highlighting */
div[class*="messagelogger-deleted"] [class*="colorStandard"] {
color: var(--text-normal) !important;
}
/* Embed highlighting */
div[class*="messagelogger-deleted"] article :is(div, span, h1, h2, h3, p) {
color: var(--text-normal) !important;
}
div[class*="messagelogger-deleted"] a {
color: var(--text-link) !important;
text-decoration: underline;
}

View file

@ -24,7 +24,6 @@ export interface LoggedAttachment extends MessageAttachment {
blobUrl?: string;
nativefileSystem?: boolean;
oldUrl?: string;
oldProxyUrl?: string;
}
export type RefrencedMessage = LoggedMessageJSON & { message_id: string; };
@ -92,27 +91,6 @@ export interface LoadMessagePayload {
isStale: boolean;
}
export interface FetchMessagesResponse {
ok: boolean;
headers: Headers;
body: LoggedMessageJSON[] & {
extra?: LoggedMessageJSON[];
};
text: string;
status: number;
}
export interface PatchAttachmentItem {
uniqueId: string;
originalItem: LoggedAttachment;
type: string;
downloadUrl: string;
height: number;
width: number;
spoiler: boolean;
contentType: string;
}
export interface AttachmentData {
messageId: string;
attachmentId: string;

View file

@ -25,7 +25,10 @@ export class LimitedMap<K, V> {
set(key: K, value: V) {
if (settings.store.cacheLimit > 0 && this.map.size >= settings.store.cacheLimit) {
// delete the first entry
this.map.delete(this.map.keys().next().value);
const firstKey = this.map.keys().next().value;
if (firstKey !== undefined) {
this.map.delete(firstKey);
}
}
this.map.set(key, value);
}

View file

@ -33,7 +33,7 @@ export function cleanupMessage(message: any, removeDetails: boolean = true): Log
ret.guildId = ret.guild_id ?? getGuildIdByChannel(ret.channel_id);
ret.embeds = (ret.embeds ?? []).map(cleanupEmbed);
ret.deleted = ret.deleted ?? false;
ret.deletedTimestamp = ret.deleted ? (new Date()).toISOString() : undefined;
ret.deletedTimestamp = ret.deleted ? new Date().toISOString() : undefined;
ret.editHistory = ret.editHistory ?? [];
if (ret.type === 19) {
ret.message_reference = message.message_reference || message.messageReference;

View file

@ -17,7 +17,3 @@
*/
export const DEFAULT_IMAGE_CACHE_DIR = "savedImages";
export const DB_NAME = "MessageLoggerIDB";
export const DB_VERSION = 1;
export const LOGS_DATA_FILENAME = "message-logger-logs.json";

View file

@ -1,171 +0,0 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { addContextMenuPatch, NavContextMenuPatchCallback, removeContextMenuPatch } from "@api/ContextMenu";
import { FluxDispatcher, Menu, MessageActions, React, Toasts, UserStore } from "@webpack/common";
import { openLogModal } from "../components/LogsModal";
import { deleteMessageIDB } from "../db";
import { settings } from "../index";
import { addToXAndRemoveFromOpposite, ListType, removeFromX } from ".";
const idFunctions = {
Server: props => props?.guild?.id,
User: props => props?.message?.author?.id || props?.user?.id,
Channel: props => props.message?.channel_id || props.channel?.id
} as const;
type idKeys = keyof typeof idFunctions;
function renderListOption(listType: ListType, IdType: idKeys, props: any) {
const id = idFunctions[IdType](props);
if (!id) return null;
const isBlocked = settings.store[listType].includes(id);
const oppositeListType = listType === "blacklistedIds" ? "whitelistedIds" : "blacklistedIds";
const isOppositeBlocked = settings.store[oppositeListType].includes(id);
const list = listType === "blacklistedIds" ? "Blacklist" : "Whitelist";
const addToList = () => addToXAndRemoveFromOpposite(listType, id);
const removeFromList = () => removeFromX(listType, id);
return (
<Menu.MenuItem
id={`${listType}-${IdType}-${id}`}
label={
isOppositeBlocked
? `Move ${IdType} to ${list}`
: isBlocked ? `Remove ${IdType} From ${list}` : `${list} ${IdType}`
}
action={isBlocked ? removeFromList : addToList}
/>
);
}
function renderOpenLogs(idType: idKeys, props: any) {
const id = idFunctions[idType](props);
if (!id) return null;
return (
<Menu.MenuItem
id={`open-logs-for-${idType.toLowerCase()}`}
label={`Open Logs For ${idType}`}
action={() => openLogModal(`${idType.toLowerCase()}:${id}`)}
/>
);
}
export const contextMenuPath: NavContextMenuPatchCallback = (children, props) => {
if (!props) return;
if (!children.some(child => child?.props?.id === "message-logger")) {
children.push(
<Menu.MenuSeparator />,
<Menu.MenuItem
id="message-logger"
label="Message Logger"
>
<Menu.MenuItem
id="open-logs"
label="Open Logs"
action={() => openLogModal()}
/>
{Object.keys(idFunctions).map(IdType => renderOpenLogs(IdType as idKeys, props))}
<Menu.MenuSeparator />
{Object.keys(idFunctions).map(IdType => (
<React.Fragment key={IdType}>
{renderListOption("blacklistedIds", IdType as idKeys, props)}
{renderListOption("whitelistedIds", IdType as idKeys, props)}
</React.Fragment>
))}
{
props.navId === "message"
&& (props.message?.deleted || props.message?.editHistory?.length > 0)
&& (
<>
<Menu.MenuSeparator />
<Menu.MenuItem
id="remove-message"
label={props.message?.deleted ? "Remove Message (Permanent)" : "Remove Message History (Permanent)"}
color="danger"
action={() =>
deleteMessageIDB(props.message.id)
.then(() => {
if (props.message.deleted) {
FluxDispatcher.dispatch({
type: "MESSAGE_DELETE",
channelId: props.message.channel_id,
id: props.message.id,
mlDeleted: true
});
} else {
props.message.editHistory = [];
}
}).catch(() => Toasts.show({
type: Toasts.Type.FAILURE,
message: "Failed to remove message",
id: Toasts.genId()
}))
}
/>
</>
)
}
{
settings.store.hideMessageFromMessageLoggers
&& props.navId === "message"
&& props.message?.author?.id === UserStore.getCurrentUser().id
&& props.message?.deleted === false
&& (
<>
<Menu.MenuSeparator />
<Menu.MenuItem
id="hide-from-message-loggers"
label="Delete Message (Hide From Message Loggers)"
color="danger"
action={async () => {
await MessageActions.deleteMessage(props.message.channel_id, props.message.id);
MessageActions._sendMessage(props.message.channel_id, {
"content": settings.store.hideMessageFromMessageLoggersDeletedMessage,
"tts": false,
"invalidEmojis": [],
"validNonShortcutEmojis": []
}, { nonce: props.message.id });
}}
/>
</>
)
}
</Menu.MenuItem>
);
}
};
export const setupContextMenuPatches = () => {
addContextMenuPatch("message", contextMenuPath);
addContextMenuPatch("channel-context", contextMenuPath);
addContextMenuPatch("user-context", contextMenuPath);
addContextMenuPatch("guild-context", contextMenuPath);
addContextMenuPatch("gdm-context", contextMenuPath);
};
export const removeContextMenuBindings = () => {
removeContextMenuPatch("message", contextMenuPath);
removeContextMenuPatch("channel-context", contextMenuPath);
removeContextMenuPatch("user-context", contextMenuPath);
removeContextMenuPatch("guild-context", contextMenuPath);
removeContextMenuPatch("gdm-context", contextMenuPath);
};

View file

@ -0,0 +1,19 @@
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2023 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/>.
*/
//! hi this file is now usless. but ill keep it here just in case some people forgot to remove it from the preload.ts

View file

@ -1,6 +0,0 @@
ISC License (ISC)
Copyright (c) 2016, Jake Archibald <jaffathecake@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

View file

@ -1,78 +0,0 @@
/* eslint-disable simple-header/header */
import { IDBPCursor, IDBPIndex, IDBPObjectStore } from "./entry.js";
import { Func, instanceOfAny } from "./util.js";
import { replaceTraps, reverseTransformCache, unwrap } from "./wrap-idb-value.js";
const advanceMethodProps = ["continue", "continuePrimaryKey", "advance"];
const methodMap: { [s: string]: Func; } = {};
const advanceResults = new WeakMap<IDBPCursor, Promise<IDBPCursor | null>>();
const ittrProxiedCursorToOriginalProxy = new WeakMap<IDBPCursor, IDBPCursor>();
const cursorIteratorTraps: ProxyHandler<any> = {
get(target, prop) {
if (!advanceMethodProps.includes(prop as string)) return target[prop];
let cachedFunc = methodMap[prop as string];
if (!cachedFunc) {
cachedFunc = methodMap[prop as string] = function (
this: IDBPCursor,
...args: any
) {
advanceResults.set(
this,
(ittrProxiedCursorToOriginalProxy.get(this) as any)[prop](...args),
);
};
}
return cachedFunc;
},
};
async function* iterate(
this: IDBPObjectStore | IDBPIndex | IDBPCursor,
...args: any[]
): AsyncIterableIterator<any> {
// tslint:disable-next-line:no-this-assignment
let cursor: typeof this | null = this;
if (!(cursor instanceof IDBCursor)) {
cursor = await (cursor as IDBPObjectStore | IDBPIndex).openCursor(...args);
}
if (!cursor) return;
cursor = cursor as IDBPCursor;
const proxiedCursor = new Proxy(cursor, cursorIteratorTraps);
ittrProxiedCursorToOriginalProxy.set(proxiedCursor, cursor);
// Map this double-proxy back to the original, so other cursor methods work.
reverseTransformCache.set(proxiedCursor, unwrap(cursor));
while (cursor) {
yield proxiedCursor;
// If one of the advancing methods was not called, call continue().
cursor = await (advanceResults.get(proxiedCursor) || cursor.continue());
advanceResults.delete(proxiedCursor);
}
}
function isIteratorProp(target: any, prop: number | string | symbol) {
return (
(prop === Symbol.asyncIterator &&
instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) ||
(prop === "iterate" && instanceOfAny(target, [IDBIndex, IDBObjectStore]))
);
}
replaceTraps(oldTraps => ({
...oldTraps,
get(target, prop, receiver) {
if (isIteratorProp(target, prop)) return iterate;
return oldTraps.get!(target, prop, receiver);
},
has(target, prop) {
return isIteratorProp(target, prop) || oldTraps.has!(target, prop);
},
}));

View file

@ -1,75 +0,0 @@
/* eslint-disable simple-header/header */
import { IDBPDatabase, IDBPIndex } from "./entry.js";
import { Func } from "./util.js";
import { replaceTraps } from "./wrap-idb-value.js";
const readMethods = ["get", "getKey", "getAll", "getAllKeys", "count"];
const writeMethods = ["put", "add", "delete", "clear"];
const cachedMethods = new Map<string, Func>();
function getMethod(
target: any,
prop: string | number | symbol,
): Func | undefined {
if (
!(
target instanceof IDBDatabase &&
!(prop in target) &&
typeof prop === "string"
)
) {
return;
}
if (cachedMethods.get(prop)) return cachedMethods.get(prop);
const targetFuncName: string = prop.replace(/FromIndex$/, "");
const useIndex = prop !== targetFuncName;
const isWrite = writeMethods.includes(targetFuncName);
if (
// Bail if the target doesn't exist on the target. Eg, getAll isn't in Edge.
!(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) ||
!(isWrite || readMethods.includes(targetFuncName))
) {
return;
}
const method = async function (
this: IDBPDatabase,
storeName: string,
...args: any[]
) {
// isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :(
const tx = this.transaction(storeName, isWrite ? "readwrite" : "readonly");
let target:
| typeof tx.store
| IDBPIndex<unknown, string[], string, string, "readwrite" | "readonly"> =
tx.store;
if (useIndex) target = target.index(args.shift());
// Must reject if op rejects.
// If it's a write operation, must reject if tx.done rejects.
// Must reject with op rejection first.
// Must resolve with op value.
// Must handle both promises (no unhandled rejections)
return (
await Promise.all([
(target as any)[targetFuncName](...args),
isWrite && tx.done,
])
)[0];
};
cachedMethods.set(prop, method);
return method;
}
replaceTraps(oldTraps => ({
...oldTraps,
get: (target, prop, receiver) =>
getMethod(target, prop) || oldTraps.get!(target, prop, receiver),
has: (target, prop) =>
!!getMethod(target, prop) || oldTraps.has!(target, prop),
}));

File diff suppressed because it is too large Load diff

View file

@ -1,5 +0,0 @@
/* eslint-disable simple-header/header */
export * from "./entry.js";
import "./database-extras.js";
import "./async-iterators.js";

View file

@ -1,9 +0,0 @@
/* eslint-disable simple-header/header */
export type Constructor = new (...args: any[]) => any;
export type Func = (...args: any[]) => any;
export const instanceOfAny = (
object: any,
constructors: Constructor[],
): boolean => constructors.some(c => object instanceof c);

View file

@ -1,227 +0,0 @@
/* eslint-disable simple-header/header */
import {
IDBPCursor,
IDBPCursorWithValue,
IDBPDatabase,
IDBPIndex,
IDBPObjectStore,
IDBPTransaction,
} from "./entry.js";
import { Constructor, Func, instanceOfAny } from "./util.js";
let idbProxyableTypes: Constructor[];
let cursorAdvanceMethods: Func[];
// This is a function to prevent it throwing up in node environments.
function getIdbProxyableTypes(): Constructor[] {
return (
idbProxyableTypes ||
(idbProxyableTypes = [
IDBDatabase,
IDBObjectStore,
IDBIndex,
IDBCursor,
IDBTransaction,
])
);
}
// This is a function to prevent it throwing up in node environments.
function getCursorAdvanceMethods(): Func[] {
return (
cursorAdvanceMethods ||
(cursorAdvanceMethods = [
IDBCursor.prototype.advance,
IDBCursor.prototype.continue,
IDBCursor.prototype.continuePrimaryKey,
])
);
}
const transactionDoneMap: WeakMap<
IDBTransaction,
Promise<void>
> = new WeakMap();
const transformCache = new WeakMap();
export const reverseTransformCache = new WeakMap();
function promisifyRequest<T>(request: IDBRequest<T>): Promise<T> {
const promise = new Promise<T>((resolve, reject) => {
const unlisten = () => {
request.removeEventListener("success", success);
request.removeEventListener("error", error);
};
const success = () => {
resolve(wrap(request.result as any) as any);
unlisten();
};
const error = () => {
reject(request.error);
unlisten();
};
request.addEventListener("success", success);
request.addEventListener("error", error);
});
// This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
// is because we create many promises from a single IDBRequest.
reverseTransformCache.set(promise, request);
return promise;
}
function cacheDonePromiseForTransaction(tx: IDBTransaction): void {
// Early bail if we've already created a done promise for this transaction.
if (transactionDoneMap.has(tx)) return;
const done = new Promise<void>((resolve, reject) => {
const unlisten = () => {
tx.removeEventListener("complete", complete);
tx.removeEventListener("error", error);
tx.removeEventListener("abort", error);
};
const complete = () => {
resolve();
unlisten();
};
const error = () => {
reject(tx.error || new DOMException("AbortError", "AbortError"));
unlisten();
};
tx.addEventListener("complete", complete);
tx.addEventListener("error", error);
tx.addEventListener("abort", error);
});
// Cache it for later retrieval.
transactionDoneMap.set(tx, done);
}
let idbProxyTraps: ProxyHandler<any> = {
get(target, prop, receiver) {
if (target instanceof IDBTransaction) {
// Special handling for transaction.done.
if (prop === "done") return transactionDoneMap.get(target);
// Make tx.store return the only store in the transaction, or undefined if there are many.
if (prop === "store") {
return receiver.objectStoreNames[1]
? undefined
: receiver.objectStore(receiver.objectStoreNames[0]);
}
}
// Else transform whatever we get back.
return wrap(target[prop]);
},
set(target, prop, value) {
target[prop] = value;
return true;
},
has(target, prop) {
if (
target instanceof IDBTransaction &&
(prop === "done" || prop === "store")
) {
return true;
}
return prop in target;
},
};
export function replaceTraps(
callback: (currentTraps: ProxyHandler<any>) => ProxyHandler<any>,
): void {
idbProxyTraps = callback(idbProxyTraps);
}
function wrapFunction<T extends Func>(func: T): Function {
// Due to expected object equality (which is enforced by the caching in `wrap`), we
// only create one new func per func.
// Cursor methods are special, as the behaviour is a little more different to standard IDB. In
// IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the
// cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense
// with real promises, so each advance methods returns a new promise for the cursor object, or
// undefined if the end of the cursor has been reached.
if (getCursorAdvanceMethods().includes(func)) {
return function (this: IDBPCursor, ...args: Parameters<T>) {
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
// the original object.
func.apply(unwrap(this), args);
return wrap(this.request);
};
}
return function (this: any, ...args: Parameters<T>) {
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
// the original object.
return wrap(func.apply(unwrap(this), args));
};
}
function transformCachableValue(value: any): any {
if (typeof value === "function") return wrapFunction(value);
// This doesn't return, it just creates a 'done' promise for the transaction,
// which is later returned for transaction.done (see idbObjectHandler).
if (value instanceof IDBTransaction) cacheDonePromiseForTransaction(value);
if (instanceOfAny(value, getIdbProxyableTypes()))
return new Proxy(value, idbProxyTraps);
// Return the same value back if we're not going to transform it.
return value;
}
/**
* Enhance an IDB object with helpers.
*
* @param value The thing to enhance.
*/
export function wrap(value: IDBDatabase): IDBPDatabase;
export function wrap(value: IDBIndex): IDBPIndex;
export function wrap(value: IDBObjectStore): IDBPObjectStore;
export function wrap(value: IDBTransaction): IDBPTransaction;
export function wrap(
value: IDBOpenDBRequest,
): Promise<IDBPDatabase | undefined>;
export function wrap<T>(value: IDBRequest<T>): Promise<T>;
export function wrap(value: any): any {
// We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because
// IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.
if (value instanceof IDBRequest) return promisifyRequest(value);
// If we've already transformed this value before, reuse the transformed value.
// This is faster, but it also provides object equality.
if (transformCache.has(value)) return transformCache.get(value);
const newValue = transformCachableValue(value);
// Not all types are transformed.
// These may be primitive types, so they can't be WeakMap keys.
if (newValue !== value) {
transformCache.set(value, newValue);
reverseTransformCache.set(newValue, value);
}
return newValue;
}
/**
* Revert an enhanced IDB object to a plain old miserable IDB one.
*
* Will also revert a promise back to an IDBRequest.
*
* @param value The enhanced object to revert.
*/
interface Unwrap {
(value: IDBPCursorWithValue<any, any, any, any, any>): IDBCursorWithValue;
(value: IDBPCursor<any, any, any, any, any>): IDBCursor;
(value: IDBPDatabase): IDBDatabase;
(value: IDBPIndex<any, any, any, any, any>): IDBIndex;
(value: IDBPObjectStore<any, any, any, any>): IDBObjectStore;
(value: IDBPTransaction<any, any, any>): IDBTransaction;
<T extends any>(value: Promise<IDBPDatabase<T>>): IDBOpenDBRequest;
(value: Promise<IDBPDatabase>): IDBOpenDBRequest;
<T>(value: Promise<T>): IDBRequest<T>;
}
export const unwrap: Unwrap = (value: any): any =>
reverseTransformCache.get(value);

View file

@ -21,6 +21,7 @@ import { findStoreLazy } from "@webpack";
import { ChannelStore, SelectedChannelStore, UserStore } from "@webpack/common";
import { settings } from "../index";
import { loggedMessages } from "../LoggedMessageManager";
import { LoggedMessageJSON } from "../types";
import { findLastIndex, getGuildIdByChannel } from "./misc";
@ -30,9 +31,9 @@ export * from "./misc";
// stolen from mlv2
// https://github.com/1Lighty/BetterDiscordPlugins/blob/master/Plugins/MessageLoggerV2/MessageLoggerV2.plugin.js#L2367
interface Id { id: string, time: number; message?: LoggedMessageJSON; }
interface Id { id: string, time: number; }
export const DISCORD_EPOCH = 14200704e5;
export function reAddDeletedMessages(messages: LoggedMessageJSON[], deletedMessages: LoggedMessageJSON[], channelStart: boolean, channelEnd: boolean) {
export function reAddDeletedMessages(messages: LoggedMessageJSON[], deletedMessages: string[], channelStart: boolean, channelEnd: boolean) {
if (!messages.length || !deletedMessages?.length) return;
const IDs: Id[] = [];
const savedIDs: Id[] = [];
@ -42,11 +43,11 @@ export function reAddDeletedMessages(messages: LoggedMessageJSON[], deletedMessa
IDs.push({ id: id, time: (parseInt(id) / 4194304) + DISCORD_EPOCH });
}
for (let i = 0, len = deletedMessages.length; i < len; i++) {
const record = deletedMessages[i];
const id = deletedMessages[i];
const record = loggedMessages[id];
if (!record) continue;
savedIDs.push({ id: record.id, time: (parseInt(record.id) / 4194304) + DISCORD_EPOCH, message: record });
savedIDs.push({ id: id, time: (parseInt(id) / 4194304) + DISCORD_EPOCH });
}
savedIDs.sort((a, b) => a.time - b.time);
if (!savedIDs.length) return;
const { time: lowestTime } = IDs[IDs.length - 1];
@ -59,10 +60,11 @@ export function reAddDeletedMessages(messages: LoggedMessageJSON[], deletedMessa
reAddIDs.push(...IDs);
reAddIDs.sort((a, b) => b.time - a.time);
for (let i = 0, len = reAddIDs.length; i < len; i++) {
const { id, message } = reAddIDs[i];
const { id } = reAddIDs[i];
if (messages.findIndex(e => e.id === id) !== -1) continue;
if (!message) continue;
messages.splice(i, 0, message);
const record = loggedMessages[id];
if (!record.message) continue;
messages.splice(i, 0, record.message);
}
}

View file

@ -16,11 +16,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { get, set } from "@api/DataStore";
import { PluginNative } from "@utils/types";
import { findByCodeLazy, findLazy } from "@webpack";
import { ChannelStore, moment, UserStore } from "@webpack/common";
import { DBMessageStatus } from "../db";
import { LOGGED_MESSAGES_KEY, MessageLoggerStore } from "../LoggedMessageManager";
import { LoggedMessageJSON } from "../types";
import { DEFAULT_IMAGE_CACHE_DIR } from "./constants";
import { DISCORD_EPOCH } from "./index";
@ -46,14 +47,6 @@ export const hasPingged = (message?: LoggedMessageJSON | { mention_everyone: boo
);
};
export const getMessageStatus = (message: LoggedMessageJSON) => {
if (isGhostPinged(message)) return DBMessageStatus.GHOST_PINGED;
if (message.deleted) return DBMessageStatus.DELETED;
if (message.editHistory?.length) return DBMessageStatus.EDITED;
throw new Error("Unknown message status");
};
export const discordIdToDate = (id: string) => new Date((parseInt(id) / 4194304) + DISCORD_EPOCH);
export const sortMessagesByDate = (timestampA: string, timestampB: string) => {
@ -88,9 +81,8 @@ const getTimestamp = (timestamp: any): Date => {
return new Date(timestamp);
};
export const mapTimestamp = (m: any) => {
if (m.timestamp) m.timestamp = getTimestamp(m.timestamp);
if (m.editedTimestamp) m.editedTimestamp = getTimestamp(m.editedTimestamp);
export const mapEditHistory = (m: any) => {
m.timestamp = getTimestamp(m.timestamp);
return m;
};
@ -100,19 +92,16 @@ export const messageJsonToMessageClass = memoize((log: { message: LoggedMessageJ
if (!log?.message) return null;
const message: any = new MessageClass(log.message);
// @ts-ignore
message.timestamp = getTimestamp(message.timestamp);
const editHistory = message.editHistory?.map(mapTimestamp);
const editHistory = message.editHistory?.map(mapEditHistory);
if (editHistory && editHistory.length > 0) {
message.editHistory = editHistory;
}
if (message.editedTimestamp)
message.editedTimestamp = getTimestamp(message.editedTimestamp);
if (message.firstEditTimestamp)
message.firstEditTimestamp = getTimestamp(message.firstEditTimestamp);
message.author = UserStore.getUser(message.author.id) ?? new AuthorClass(message.author);
message.author = new AuthorClass(message.author);
message.author.nick = message.author.globalName ?? message.author.username;
message.embeds = message.embeds.map(e => sanitizeEmbed(message.channel_id, message.id, e));
@ -120,9 +109,6 @@ export const messageJsonToMessageClass = memoize((log: { message: LoggedMessageJ
if (message.poll)
message.poll.expiry = moment(message.poll.expiry);
if (message.messageSnapshots)
message.messageSnapshots.map(m => mapTimestamp(m.message));
// console.timeEnd("message populate");
return message;
});
@ -144,7 +130,8 @@ export async function doesBlobUrlExist(url: string) {
export function getNative(): PluginNative<typeof import("../native")> {
if (IS_WEB) {
const Native = {
writeLogs: async () => { },
getLogsFromFs: async () => get(LOGGED_MESSAGES_KEY, MessageLoggerStore),
writeLogs: async (logs: string) => set(LOGGED_MESSAGES_KEY, JSON.parse(logs), MessageLoggerStore),
getDefaultNativeImageDir: async () => DEFAULT_IMAGE_CACHE_DIR,
getDefaultNativeDataDir: async () => "",
deleteFileNative: async () => { },
@ -157,12 +144,6 @@ export function getNative(): PluginNative<typeof import("../native")> {
messageLoggerEnhancedUniqueIdThingyIdkMan: async () => { },
showItemInFolder: async () => { },
writeImageNative: async () => { },
getCommitHash: async () => ({ ok: true, value: "" }),
getRepoInfo: async () => ({ ok: true, value: { repo: "", gitHash: "" } }),
getNewCommits: async () => ({ ok: true, value: [] }),
update: async () => ({ ok: true, value: "" }),
chooseFile: async () => "",
downloadAttachment: async () => ({ error: "web", path: null }),
} satisfies PluginNative<typeof import("../native")>;
return Native;

View file

@ -23,19 +23,21 @@ import { getGuildIdByChannel } from "./index";
import { memoize } from "./memoize";
const validIdSearchTypes = ["server", "guild", "channel", "in", "user", "from", "message", "has", "before", "after", "around", "near", "during"] as const;
const validIdSearchTypes = ["server", "guild", "channel", "in", "user", "from", "message"] as const;
type ValidIdSearchTypesUnion = typeof validIdSearchTypes[number];
interface QueryResult {
key: ValidIdSearchTypesUnion;
value: string;
negate: boolean;
success: boolean;
query: string;
type?: ValidIdSearchTypesUnion;
id?: string;
negate?: boolean;
}
export const parseQuery = memoize((query: string = ""): QueryResult | string => {
export const parseQuery = memoize((query: string = ""): QueryResult => {
let trimmedQuery = query.trim();
if (!trimmedQuery) {
return query;
return { success: false, query };
}
let negate = false;
@ -46,30 +48,23 @@ export const parseQuery = memoize((query: string = ""): QueryResult | string =>
const [filter, rest] = trimmedQuery.split(" ", 2);
if (!filter) {
return query;
return { success: false, query };
}
const [type, id] = filter.split(":") as [ValidIdSearchTypesUnion, string];
if (!type || !id || !validIdSearchTypes.includes(type)) {
return query;
return { success: false, query };
}
return {
key: type,
value: id,
success: true,
type,
id,
negate,
query: rest ?? ""
};
});
export const tokenizeQuery = (query: string) => {
const parts = query.split(" ").map(parseQuery);
const queries = parts.filter(p => typeof p !== "string") as QueryResult[];
const rest = parts.filter(p => typeof p === "string") as string[];
return { queries, rest };
};
const linkRegex = /[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/;
export const doesMatch = (type: typeof validIdSearchTypes[number], value: string, message: LoggedMessageJSON) => {
switch (type) {
@ -100,32 +95,6 @@ export const doesMatch = (type: typeof validIdSearchTypes[number], value: string
return guild.id === value
|| guild.name.toLowerCase().includes(value.toLowerCase());
}
case "before":
return new Date(message.timestamp) < new Date(value);
case "after":
return new Date(message.timestamp) > new Date(value);
case "around":
case "near":
case "during":
return Math.abs(new Date(message.timestamp).getTime() - new Date(value).getTime()) < 1000 * 60 * 60 * 24;
case "has": {
switch (value) {
case "attachment":
return message.attachments.length > 0;
case "image":
return message.attachments.some(a => a.content_type?.startsWith("image")) ||
message.embeds.some(e => e.image || e.thumbnail);
case "video":
return message.attachments.some(a => a.content_type?.startsWith("video")) ||
message.embeds.some(e => e.video);
case "embed":
return message.embeds.length > 0;
case "link":
return message.content.match(linkRegex);
default:
return false;
}
}
default:
return false;
}

View file

@ -23,26 +23,23 @@ import {
keys,
set,
} from "@api/DataStore";
import { sleep } from "@utils/misc";
import { LoggedAttachment } from "userplugins/vc-message-logger-enhanced/types";
import { Flogger, Native } from "../..";
import { DEFAULT_IMAGE_CACHE_DIR } from "../constants";
const ImageStore = createStore("MessageLoggerImageData", "MessageLoggerImageStore");
interface IDBSavedImage { attachmentId: string, path: string; }
const idbSavedImages = new Map<string, IDBSavedImage>();
interface IDBSavedImages { attachmentId: string, path: string; }
let idbSavedImages: IDBSavedImages[] = [];
(async () => {
try {
const paths = await keys(ImageStore);
paths.forEach(path => {
const str = path.toString();
if (!str.startsWith(DEFAULT_IMAGE_CACHE_DIR)) return;
idbSavedImages.set(str.split("/")?.[1]?.split(".")?.[0], { attachmentId: str.split("/")?.[1]?.split(".")?.[0], path: str });
});
idbSavedImages = (await keys(ImageStore))
.map(m => {
const str = m.toString();
if (!str.startsWith(DEFAULT_IMAGE_CACHE_DIR)) return null;
return { attachmentId: str.split("/")?.[1]?.split(".")?.[0], path: str };
})
.filter(Boolean) as IDBSavedImages[];
} catch (err) {
Flogger.error("Failed to get idb images", err);
}
@ -51,7 +48,7 @@ const idbSavedImages = new Map<string, IDBSavedImage>();
export async function getImage(attachmentId: string, fileExt?: string | null): Promise<any> {
// for people who have access to native api but some images are still in idb
// also for people who dont have native api
const idbPath = idbSavedImages.get(attachmentId)?.path;
const idbPath = idbSavedImages.find(m => m.attachmentId === attachmentId)?.path;
if (idbPath)
return get(idbPath, ImageStore);
@ -60,23 +57,19 @@ export async function getImage(attachmentId: string, fileExt?: string | null): P
return await Native.getImageNative(attachmentId);
}
export async function downloadAttachment(attachemnt: LoggedAttachment): Promise<string | undefined> {
// file name shouldnt have any query param shinanigans
export async function writeImage(imageCacheDir: string, filename: string, content: Uint8Array): Promise<void> {
if (IS_WEB) {
return await downloadAttachmentWeb(attachemnt);
const path = `${imageCacheDir}/${filename}`;
idbSavedImages.push({ attachmentId: filename.split(".")?.[0], path });
return set(path, content, ImageStore);
}
const { path, error } = await Native.downloadAttachment(attachemnt);
if (error || !path) {
Flogger.error("Failed to download attachment", error, path);
return;
}
return path;
Native.writeImageNative(filename, content);
}
export async function deleteImage(attachmentId: string): Promise<void> {
const idbPath = idbSavedImages.get(attachmentId)?.path;
const idbPath = idbSavedImages.find(m => m.attachmentId === attachmentId)?.path;
if (idbPath)
return await del(idbPath, ImageStore);
@ -85,33 +78,3 @@ export async function deleteImage(attachmentId: string): Promise<void> {
await Native.deleteFileNative(attachmentId);
}
async function downloadAttachmentWeb(attachemnt: LoggedAttachment, attempts = 0) {
if (!attachemnt?.url || !attachemnt?.id || !attachemnt?.fileExtension) {
Flogger.error("Invalid attachment", attachemnt);
return;
}
const res = await fetch(attachemnt.url);
if (res.status !== 200) {
if (res.status === 404 || res.status === 403) return;
attempts++;
if (attempts > 3) {
Flogger.warn(`Failed to get attachment ${attachemnt.id} for caching, error code ${res.status}`);
return;
}
await sleep(1000);
return downloadAttachmentWeb(attachemnt, attempts);
}
const ab = await res.arrayBuffer();
const path = `${DEFAULT_IMAGE_CACHE_DIR}/${attachemnt.id}${attachemnt.fileExtension}`;
// await writeImage(imageCacheDir, `${attachmentId}${fileExtension}`, new Uint8Array(ab));
await set(path, new Uint8Array(ab), ImageStore);
idbSavedImages.set(attachemnt.id, { attachmentId: attachemnt.id, path });
return path;
}

View file

@ -16,12 +16,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { sleep } from "@utils/misc";
import { MessageAttachment } from "discord-types/general";
import { Flogger, settings } from "../..";
import { Flogger, Native, settings } from "../..";
import { LoggedAttachment, LoggedMessage, LoggedMessageJSON } from "../../types";
import { memoize } from "../memoize";
import { deleteImage, downloadAttachment, getImage, } from "./ImageManager";
import { deleteImage, getImage, writeImage, } from "./ImageManager";
export function getFileExtension(str: string) {
const matches = str.match(/(\.[a-zA-Z0-9]+)(?:\?.*)?$/);
@ -30,61 +31,64 @@ export function getFileExtension(str: string) {
return matches[1];
}
export function isAttachmentGoodToCache(attachment: MessageAttachment, fileExtension: string) {
if (attachment.size > settings.store.attachmentSizeLimitInMegabytes * 1024 * 1024) {
Flogger.log("Attachment too large to cache", attachment.filename);
return false;
}
const attachmentFileExtensionsStr = settings.store.attachmentFileExtensions.trim();
if (attachmentFileExtensionsStr === "")
return true;
const allowedFileExtensions = attachmentFileExtensionsStr.split(",");
if (fileExtension.startsWith(".")) {
fileExtension = fileExtension.slice(1);
}
if (!fileExtension || !allowedFileExtensions.includes(fileExtension)) {
Flogger.log("Attachment not in allowed file extensions", attachment.filename);
return false;
}
return true;
export function isImage(url: string) {
return /\.(jpe?g|png|gif|bmp)(\?.*)?$/i.test(url);
}
export function isAttachmentImage(attachment: MessageAttachment) {
return isImage(attachment.filename ?? attachment.url) || (attachment.content_type?.split("/")[0] === "image");
}
function transformAttachmentUrl(messageId: string, attachmentUrl: string) {
const url = new URL(attachmentUrl);
url.searchParams.set("messageId", messageId);
return url.toString();
}
export async function cacheImage(url: string, attachmentIdx: number, attachmentId: string, messageId: string, channelId: string, fileExtension: string | null, attempts = 0) {
const res = await fetch(url);
if (res.status !== 200) {
if (res.status === 404 || res.status === 403) return;
attempts++;
if (attempts > 3) {
Flogger.warn(`Failed to get image ${attachmentId} for caching, error code ${res.status}`);
return;
}
await sleep(1000);
return cacheImage(url, attachmentIdx, attachmentId, messageId, channelId, fileExtension, attempts);
}
const ab = await res.arrayBuffer();
const imageCacheDir = settings.store.imageCacheDir ?? await Native.getDefaultNativeImageDir();
const path = `${imageCacheDir}/${attachmentId}${fileExtension}`;
await writeImage(imageCacheDir, `${attachmentId}${fileExtension}`, new Uint8Array(ab));
return path;
}
export async function cacheMessageImages(message: LoggedMessage | LoggedMessageJSON) {
try {
for (const attachment of message.attachments) {
const fileExtension = getFileExtension(attachment.filename ?? attachment.url) ?? attachment.content_type?.split("/")?.[1] ?? ".png";
if (!isAttachmentGoodToCache(attachment, fileExtension)) {
for (let i = 0; i < message.attachments.length; i++) {
const attachment = message.attachments[i];
if (!isAttachmentImage(attachment)) {
Flogger.log("skipping", attachment.filename);
continue;
}
// apparently proxy urls last longer
attachment.url = transformAttachmentUrl(message.id, attachment.proxy_url);
attachment.oldUrl = attachment.url;
attachment.oldProxyUrl = attachment.proxy_url;
const fileExtension = getFileExtension(attachment.filename ?? attachment.url) ?? attachment.content_type?.split("/")?.[1] ?? ".png";
const path = await cacheImage(attachment.url, i, attachment.id, message.id, message.channel_id, fileExtension);
// only normal urls work if theres a charset in the content type /shrug
if (attachment.content_type?.includes(";")) {
attachment.proxy_url = attachment.url;
} else {
// apparently proxy urls last longer
attachment.url = attachment.proxy_url;
attachment.proxy_url = attachment.url;
}
attachment.fileExtension = fileExtension;
const path = await downloadAttachment(attachment);
if (!path) {
Flogger.error("Failed to cache attachment", attachment);
if (path == null) {
Flogger.error("Failed to save image from attachment. id: ", attachment.id);
continue;
}
attachment.fileExtension = fileExtension;
attachment.path = path;
}

View file

@ -16,78 +16,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { chooseFile as chooseFileWeb } from "@utils/web";
import { Toasts } from "@webpack/common";
import { DataStore } from "@api/index";
import { Native } from "..";
import { addMessagesBulkIDB, DBMessageRecord, getAllMessagesIDB } from "../db";
import { LoggedMessage, LoggedMessageJSON } from "../types";
import { LOGGED_MESSAGES_KEY, MessageLoggerStore } from "../LoggedMessageManager";
async function getLogContents(): Promise<string> {
if (IS_WEB) {
const file = await chooseFileWeb(".json");
return new Promise((resolve, reject) => {
if (!file) return reject("No file selected");
// 99% of this is coppied from src\utils\settingsSync.ts
const reader = new FileReader();
reader.onload = () => resolve(reader.result as string);
reader.onerror = reject;
reader.readAsText(file);
});
}
const settings = await Native.getSettings();
return Native.chooseFile("Logs", [{ extensions: ["json"], name: "logs" }], settings.logsDir);
}
export async function importLogs() {
try {
const content = await getLogContents();
const data = JSON.parse(content) as { messages: DBMessageRecord[]; };
let messages: LoggedMessageJSON[] = [];
if ((data as any).deletedMessages || (data as any).editedMessages) {
messages = Object.values((data as unknown as LoggedMessage)).filter(m => m.message).map(m => m.message) as LoggedMessageJSON[];
} else
messages = data.messages.map(m => m.message);
if (!Array.isArray(messages)) {
throw new Error("Invalid log file format");
}
if (!messages.length) {
throw new Error("No messages found in log file");
}
if (!messages.every(m => m.id && m.channel_id && m.timestamp)) {
throw new Error("Invalid message format");
}
await addMessagesBulkIDB(messages);
Toasts.show({
id: Toasts.genId(),
message: "Successfully imported logs",
type: Toasts.Type.SUCCESS
});
} catch (e) {
console.error(e);
Toasts.show({
id: Toasts.genId(),
message: "Error importing logs. Check the console for more information",
type: Toasts.Type.FAILURE
});
}
}
export async function exportLogs() {
const filename = "message-logger-logs-idb.json";
const messages = await getAllMessagesIDB();
const data = JSON.stringify({ messages }, null, 2);
export async function downloadLoggedMessages() {
const filename = "message-logger-logs.json";
const exportData = await exportLogs();
const data = new TextEncoder().encode(exportData);
if (IS_WEB || IS_VESKTOP) {
const file = new File([data], filename, { type: "application/json" });
@ -104,5 +42,10 @@ export async function exportLogs() {
} else {
DiscordNative.fileManager.saveWithDialog(data, filename);
}
}
export async function exportLogs() {
const logger_data = await DataStore.get(LOGGED_MESSAGES_KEY, MessageLoggerStore);
return JSON.stringify(logger_data, null, 4);
}

View file

@ -59,8 +59,8 @@ function apostrophe(textInput: string): string {
const words: string[] = corrected.split(", ");
const wordsInputted = textInput.split(" ");
wordsInputted.forEach(element => {
words.forEach(wordelement => {
wordsInputted.forEach((element) => {
words.forEach((wordelement) => {
if (removeApostrophes(wordelement) === element.toLowerCase()) {
wordsInputted[wordsInputted.indexOf(element)] = restoreCap(
wordelement,
@ -109,9 +109,9 @@ function cap(textInput: string): string {
Settings.plugins.PolishWording.blockedWords.split(", ");
return sentences
.map(element => {
.map((element) => {
if (
!blockedWordsArray.some(word =>
!blockedWordsArray.some((word) =>
element.toLowerCase().startsWith(word.toLowerCase()),
)
) {