From cac90524ed39c0755a0d9ae80be7c37df12d1025 Mon Sep 17 00:00:00 2001 From: GoodM4ven Date: Fri, 8 Mar 2024 14:34:56 +0300 Subject: [PATCH 1/2] [Enhancement] Accounting for "www." prefix for duplicates --- lib/api/controllers/links/postLink.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/api/controllers/links/postLink.ts b/lib/api/controllers/links/postLink.ts index ec03d2a..e286454 100644 --- a/lib/api/controllers/links/postLink.ts +++ b/lib/api/controllers/links/postLink.ts @@ -116,9 +116,18 @@ export default async function postLink( }); if (user?.preventDuplicateLinks) { + const trimmedUrl = link.url?.trim(); + const wwwPrefix = 'www.'; + const hasWwwPrefix = trimmedUrl?.includes(`://${wwwPrefix}`); + const urlWithoutWww = hasWwwPrefix ? trimmedUrl?.replace(`://${wwwPrefix}`, '://') : trimmedUrl; + const urlWithWww = hasWwwPrefix ? trimmedUrl : trimmedUrl?.replace('://', `://${wwwPrefix}`); + const existingLink = await prisma.link.findFirst({ where: { - url: link.url?.trim(), + OR: [ + { url: trimmedUrl }, + { url: hasWwwPrefix ? urlWithoutWww : urlWithWww }, // Toggling "www." + ], collection: { ownerId: userId, }, From 4a71af8a67f68289ab95df318e2b71479ac190af Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Mon, 15 Apr 2024 00:37:18 -0400 Subject: [PATCH 2/2] remove trailing slashes + small improvement --- lib/api/controllers/links/postLink.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/api/controllers/links/postLink.ts b/lib/api/controllers/links/postLink.ts index e286454..7746dd6 100644 --- a/lib/api/controllers/links/postLink.ts +++ b/lib/api/controllers/links/postLink.ts @@ -116,24 +116,24 @@ export default async function postLink( }); if (user?.preventDuplicateLinks) { - const trimmedUrl = link.url?.trim(); - const wwwPrefix = 'www.'; - const hasWwwPrefix = trimmedUrl?.includes(`://${wwwPrefix}`); - const urlWithoutWww = hasWwwPrefix ? trimmedUrl?.replace(`://${wwwPrefix}`, '://') : trimmedUrl; - const urlWithWww = hasWwwPrefix ? trimmedUrl : trimmedUrl?.replace('://', `://${wwwPrefix}`); + const url = link.url?.trim().replace(/\/+$/, ""); // trim and remove trailing slashes from the URL + const hasWwwPrefix = url?.includes(`://www.`); + const urlWithoutWww = hasWwwPrefix ? url?.replace(`://www.`, "://") : url; + const urlWithWww = hasWwwPrefix ? url : url?.replace("://", `://www.`); + + console.log(url, urlWithoutWww, urlWithWww); const existingLink = await prisma.link.findFirst({ where: { - OR: [ - { url: trimmedUrl }, - { url: hasWwwPrefix ? urlWithoutWww : urlWithWww }, // Toggling "www." - ], + OR: [{ url: urlWithWww }, { url: urlWithoutWww }], collection: { ownerId: userId, }, }, }); + console.log(url, urlWithoutWww, urlWithWww, "DONE!"); + if (existingLink) return { response: "Link already exists", @@ -180,7 +180,7 @@ export default async function postLink( const newLink = await prisma.link.create({ data: { - url: link.url?.trim(), + url: link.url?.trim().replace(/\/+$/, ""), name: link.name, description, type: linkType,