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

Add Link Aliases feature #1016

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions apps/web/lib/middleware/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export default async function LinkMiddleware(
geo,
expiredUrl,
doIndex,
parentLinkId,
} = link;

// by default, we only index default dub domain links (e.g. dub.sh)
Expand Down Expand Up @@ -190,6 +191,7 @@ export default async function LinkMiddleware(
recordClick({
req,
linkId,
parentLinkId,
clickId,
...(url && { url: getFinalUrl(url, { req }) }),
}),
Expand Down
11 changes: 9 additions & 2 deletions apps/web/lib/tinybird/record-click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import { ratelimit } from "../upstash";
export async function recordClick({
req,
linkId,
parentLinkId,
clickId,
url,
}: {
req: NextRequest;
linkId: string;
parentLinkId?: string;
clickId?: string;
url?: string;
}) {
Expand Down Expand Up @@ -56,8 +58,13 @@ export async function recordClick({
timestamp: new Date(Date.now()).toISOString(),
identity_hash,
click_id: clickId || nanoid(16),
link_id: linkId,
alias_link_id: "",
/*
if the link is a child alias of another link,
log the click under the parent link ID
else log the click under the link ID
*/
link_id: parentLinkId || linkId,
alias_link_id: parentLinkId ? linkId : "",
url: url || "",
ip:
// only record IP if it's a valid IP and not from EU
Expand Down
1 change: 1 addition & 0 deletions apps/web/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface RedisLinkProps {
geo?: object;
doIndex?: boolean;
projectId?: string;
parentLinkId: string;
}

export interface EdgeLinkProps {
Expand Down
5 changes: 5 additions & 0 deletions apps/web/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ model Link {
// Comments on the particular shortlink
comments String? @db.LongText

parentLink Link? @relation("LinkAliases", fields: [parentLinkId], references: [id], onDelete: NoAction, onUpdate: NoAction)
parentLinkId String?
childLinks Link[] @relation("LinkAliases")

@@unique([domain, key])
@@unique([projectId, externalId])
@@index(projectId)
Expand All @@ -304,6 +308,7 @@ model Link {
@@index(lastClicked)
@@index(userId)
@@fulltext([key, url])
@@index([parentLinkId])
}

model Tag {
Expand Down