Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: strip hash from urls before comparing #23

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
},
"dependencies": {
"@googleapis/drive": "8.4.0",
"compare-urls": "4.1.0",
"consola": "3.2.3",
"discord.js": "14.14.1"
"discord.js": "14.14.1",
"normalize-url": "8.0.0"
},
"devDependencies": {
"@biomejs/biome": "1.4.1",
Expand Down
23 changes: 20 additions & 3 deletions src/embeds.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { drive_v3 } from "@googleapis/drive";
import { deepMatch, sleep } from "bun";
import compareUrls from "compare-urls";
import {
EmbedBuilder,
Message,
Expand All @@ -12,6 +11,9 @@ import {
isJSONEncodable,
} from "discord.js";
import { GaxiosError } from "gaxios";
import normalizeUrl, {
type Options as NormalizeUrlOptions,
} from "normalize-url";
import { driveClient, fileTypes } from "./gdrive";
import { appendInvisible, decodeAppendedInvisible } from "./util";

Expand Down Expand Up @@ -146,13 +148,28 @@ const createEmbedsMessage = async (
* @param fileUrls URLs of files to suppress embeds of
*/
const suppressEmbeds = async (message: Message, fileUrls: string[]) => {
const normalizeOptions: NormalizeUrlOptions = {
defaultProtocol: "https",
normalizeProtocol: true,
forceHttps: true,
removeExplicitPort: true,
stripHash: true,
removeQueryParameters: true,
};
const embedsUrls = message.embeds.map(({ url }) => url);
const shouldSuppress =
!!embedsUrls.length &&
embedsUrls.every(
// return false if null, which means the embed is not a link
(embedUrl) =>
embedUrl && fileUrls.some((fileUrl) => compareUrls(fileUrl, embedUrl)),
(embedUrl) => {
if (!embedUrl) {
return false;
}
const _normalizedEmbedUrl = normalizeUrl(embedUrl, normalizeOptions);
return fileUrls.some((fileUrl) =>
normalizeUrl(fileUrl, normalizeOptions),
);
},
);

// do not send a request if no change is needed
Expand Down