Skip to content

Commit

Permalink
Add Link Aliases feature
Browse files Browse the repository at this point in the history
closes #495
  • Loading branch information
steven-tey committed Jun 25, 2024
1 parent 0e0a62e commit 39954b9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
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 @@ -37,6 +37,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 @@ -268,6 +268,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 @@ -280,6 +284,7 @@ model Link {
@@index(lastClicked)
@@index(userId)
@@fulltext([key, url])
@@index([parentLinkId])
}

model Tag {
Expand Down

0 comments on commit 39954b9

Please sign in to comment.