Skip to content

Commit

Permalink
add following/followers api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kualta committed Jul 17, 2024
1 parent 1c6c709 commit 7c74f33
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
29 changes: 29 additions & 0 deletions src/app/api/user/[id]/followers/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { LimitType } from "@lens-protocol/client";
import { NextRequest, NextResponse } from "next/server";
import { getServerAuth } from "~/utils/getServerAuth";

export const dynamic = "force-dynamic";

export async function GET(req: NextRequest, { params }: { params: { id: string } }) {
const id = params.id;
const cursor = req.nextUrl.searchParams.get("cursor") ?? undefined;

try {
const { client } = await getServerAuth();

const following = await client.profile.followers({
limit: LimitType.Fifty,
of: id,
cursor
});

if (!following) {
return NextResponse.json({ error: "Failed to fetch followers" }, { status: 500 });
}

return NextResponse.json({ users: following, nextCursor: following.pageInfo.next }, { status: 200 });
} catch (error) {
console.error("Failed to follow profile: ", error.message);
return NextResponse.json({ error: `${error.message}` }, { status: 500 });
}
}
18 changes: 10 additions & 8 deletions src/app/api/user/[id]/following/route.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { NextResponse } from "next/server";
import { LimitType } from "@lens-protocol/client";
import { NextRequest, NextResponse } from "next/server";
import { getServerAuth } from "~/utils/getServerAuth";

export const dynamic = "force-dynamic";

export async function GET({ params }: { params: { id: string } }) {
export async function GET(req: NextRequest, { params }: { params: { id: string } }) {
const id = params.id;
const cursor = req.nextUrl.searchParams.get("cursor") ?? undefined;

try {
const { client, profileId: authedProfileId } = await getServerAuth();
const { client } = await getServerAuth();

const followers = await client.profile.following({
const following = await client.profile.following({
cursor,
for: id,
limit: LimitType.Fifty,
});

if (!followers) {
if (!following) {
return NextResponse.json({ error: "Failed to fetch followers" }, { status: 500 });
}

const isFollowingMe = followers.items.some((follower) => follower.id === authedProfileId);

return NextResponse.json({ result: isFollowingMe }, { status: 200 });
return NextResponse.json({ users: following }, { status: 200 });
} catch (error) {
console.error("Failed to follow profile: ", error.message);
return NextResponse.json({ error: `${error.message}` }, { status: 500 });
Expand Down

0 comments on commit 7c74f33

Please sign in to comment.