Skip to content

Commit

Permalink
add loooooooo.ng generator
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Apr 1, 2024
1 parent 4f25f17 commit a4f1673
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 26 deletions.
2 changes: 2 additions & 0 deletions apps/web/app/api/domains/default/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const GET = withAuth(async ({ workspace }) => {
sptifi: true,
gitnew: true,
amznid: true,
loooooooong: true,
},
});

Expand Down Expand Up @@ -52,6 +53,7 @@ export const PUT = withAuth(async ({ req, workspace }) => {
sptifi: defaultDomains.includes("spti.fi"),
gitnew: defaultDomains.includes("git.new"),
amznid: defaultDomains.includes("amzn.id"),
loooooooong: defaultDomains.includes("loooooooo.ng"),
},
});

Expand Down
5 changes: 4 additions & 1 deletion apps/web/app/api/links/random/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export const GET = async (req: NextRequest) => {
}
}

const response = await getRandomKey(domain);
const response = await getRandomKey({
domain,
long: domain === "loooooooo.ng",
});
return NextResponse.json(response);
} catch (error) {
return handleAndReturnErrorResponse(error);
Expand Down
8 changes: 6 additions & 2 deletions apps/web/lib/api/links/process-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export async function processLink({
// coerce type with ! cause we already checked if it exists
const { allowedHostnames } = DUB_DOMAINS.find((d) => d.slug === domain)!;
const urlDomain = getDomainWithoutWWW(url) || "";
if (!allowedHostnames.includes(urlDomain)) {
if (allowedHostnames && !allowedHostnames.includes(urlDomain)) {
return {
link: payload,
error: `Invalid url. You can only use ${domain} short links for URLs starting with ${allowedHostnames
Expand All @@ -119,7 +119,11 @@ export async function processLink({
}

if (!key) {
key = await getRandomKey(domain, payload["prefix"]);
key = await getRandomKey({
domain,
prefix: payload["prefix"],
long: domain === "loooooooo.ng",
});
} else if (!skipKeyChecks) {
const processedKey = processKey(key);
if (processedKey === null) {
Expand Down
17 changes: 11 additions & 6 deletions apps/web/lib/planetscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,24 @@ export async function getDomainOrLink({
}
}

export async function getRandomKey(
domain: string,
prefix?: string,
): Promise<string> {
export async function getRandomKey({
domain,
prefix,
long,
}: {
domain: string;
prefix?: string;
long?: boolean;
}): Promise<string> {
/* recursively get random key till it gets one that's available */
let key = nanoid();
let key = long ? nanoid(69) : nanoid();
if (prefix) {
key = `${prefix.replace(/^\/|\/$/g, "")}/${key}`;
}
const exists = await checkIfKeyExists(domain, key);
if (exists) {
// by the off chance that key already exists
return getRandomKey(domain, prefix);
return getRandomKey({ domain, prefix, long });
} else {
return key;
}
Expand Down
17 changes: 9 additions & 8 deletions apps/web/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,15 @@ model ProjectInvite {
}

model DefaultDomains {
id String @id @default(cuid())
dubsh Boolean @default(true)
chatgpt Boolean @default(true)
sptifi Boolean @default(true)
gitnew Boolean @default(true)
amznid Boolean @default(true)
projectId String @unique
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
id String @id @default(cuid())
dubsh Boolean @default(true)
chatgpt Boolean @default(true)
sptifi Boolean @default(true)
gitnew Boolean @default(true)
amznid Boolean @default(true)
loooooooong Boolean @default(false)
projectId String @unique
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
}


Expand Down
2 changes: 1 addition & 1 deletion apps/web/ui/analytics/clicks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function Clicks() {
>
<p>Link</p>
<strong className="text-gray-800">
{linkConstructor({ domain, key, pretty: true })}
{truncate(linkConstructor({ domain, key, pretty: true }), 24)}
</strong>
<X className="h-4 w-4" />
</Link>
Expand Down
14 changes: 9 additions & 5 deletions apps/web/ui/analytics/link-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getApexDomain,
linkConstructor,
timeAgo,
truncate,
} from "@dub/utils";
import { Archive, EyeOff, Globe } from "lucide-react";
import punycode from "punycode/";
Expand Down Expand Up @@ -56,11 +57,14 @@ export default function LinkPreviewTooltip({
rel="noreferrer"
onClick={(e) => e.stopPropagation()}
>
{linkConstructor({
domain: punycode.toUnicode(data.domain || ""),
key: data.key,
pretty: true,
})}
{truncate(
linkConstructor({
domain: punycode.toUnicode(data.domain || ""),
key: data.key,
pretty: true,
}),
32,
)}
</a>
<CopyButton
value={linkConstructor({ domain: data.domain, key: data.key })}
Expand Down
4 changes: 2 additions & 2 deletions apps/web/ui/analytics/top-links.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TopLinksTabs } from "@/lib/analytics";
import { LoadingSpinner, Modal, Switch, useRouterStuff } from "@dub/ui";
import { fetcher, linkConstructor } from "@dub/utils";
import { fetcher, linkConstructor, truncate } from "@dub/utils";
import { Maximize, X } from "lucide-react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
Expand Down Expand Up @@ -86,7 +86,7 @@ export default function TopLinks() {
}
>
<strong className="text-gray-800">
{linkConstructor({ domain, key, pretty: true })}
{truncate(linkConstructor({ domain, key, pretty: true }), 24)}
</strong>
<X className="h-4 w-4" />
</Link>
Expand Down
2 changes: 1 addition & 1 deletion apps/web/ui/modals/add-edit-link-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function AddEditLinkModal({
if (!props && activeDefaultDomains) {
const urlDomain = getDomainWithoutWWW(url) || "";
const defaultDomain = activeDefaultDomains.find(
({ allowedHostnames }) => allowedHostnames.includes(urlDomain),
({ allowedHostnames }) => allowedHostnames?.includes(urlDomain),
);
if (defaultDomain) {
setData((prev) => ({ ...prev, domain: defaultDomain.slug }));
Expand Down
13 changes: 13 additions & 0 deletions packages/utils/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ export const DUB_DOMAINS = [
],
projectId: DUB_WORKSPACE_ID,
},
{
id: "clugy7euh000a11h6ignmj42o",
slug: "loooooooo.ng",
verified: true,
primary: false,
archived: true,
publicStats: true,
target: "https://dub.co/tools/url-lengthener",
type: "redirect",
placeholder: "https://d.to/long",
clicks: 0,
projectId: DUB_WORKSPACE_ID,
},
]
: []),
];
Expand Down

0 comments on commit a4f1673

Please sign in to comment.