Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcherry committed Jan 25, 2024
1 parent c55bd6f commit cf4bf05
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 105 deletions.
19 changes: 10 additions & 9 deletions web/src/app/api/casts/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { type NextRequest, NextResponse } from "next/server";
import { getCasts } from "@/lib/services/casts";
import { type NextRequest, NextResponse } from 'next/server';

import { getCasts } from '@/lib/services/casts';

export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams;
const fid = searchParams.get('fid');
if (!fid) {
return NextResponse.json({ error: 'fid is required' }, { status: 400 });
}
const casts = await getCasts(fid);
return NextResponse.json({ casts })
const searchParams = request.nextUrl.searchParams;
const fid = searchParams.get('fid');
if (!fid) {
return NextResponse.json({ error: 'fid is required' }, { status: 400 });
}
const casts = await getCasts(fid);
return NextResponse.json({ casts });
}
9 changes: 5 additions & 4 deletions web/src/app/api/feed/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { type NextRequest, NextResponse } from "next/server";
import { getFeed } from "@/lib/services/feed";
import { type NextRequest, NextResponse } from 'next/server';

import { getFeed } from '@/lib/services/feed';

export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams;
const fid = searchParams.get("fid");
const fid = searchParams.get('fid');
if (!fid) {
return NextResponse.json({ error: "fid is required" }, { status: 400 });
return NextResponse.json({ error: 'fid is required' }, { status: 400 });
}
const feed = await getFeed(fid);
return NextResponse.json({ feed });
Expand Down
19 changes: 10 additions & 9 deletions web/src/app/api/user/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { type NextRequest, NextResponse } from "next/server";
import { getProfile } from "@/lib/services/user";
import { type NextRequest, NextResponse } from 'next/server';

import { getProfile } from '@/lib/services/user';

export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams;
const fid = searchParams.get('fid');
if (!fid) {
return NextResponse.json({ error: 'fid is required' }, { status: 400 });
}
const profile = await getProfile(fid);
return NextResponse.json({ profile })
const searchParams = request.nextUrl.searchParams;
const fid = searchParams.get('fid');
if (!fid) {
return NextResponse.json({ error: 'fid is required' }, { status: 400 });
}
const profile = await getProfile(fid);
return NextResponse.json({ profile });
}
15 changes: 8 additions & 7 deletions web/src/lib/database/db.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Kysely, PostgresDialect } from "kysely";
import { Pool } from "pg";
import { DB } from "./types";
import { Kysely, PostgresDialect } from 'kysely';
import { Pool } from 'pg';

import { DB } from './types';

const dialect = new PostgresDialect({
pool: new Pool({
connectionString: process.env.DATABASE_URL
})
pool: new Pool({
connectionString: process.env.DATABASE_URL,
}),
});

export const db = new Kysely<DB>({
dialect
dialect,
});
61 changes: 31 additions & 30 deletions web/src/lib/services/casts.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import { sql } from "kysely";
import { db } from "../database/db";
import { sql } from 'kysely';

import { db } from '../database/db';

export function formatHash(hash: Buffer) {
return hash.toString("hex");
return hash.toString('hex');
}

export async function getCasts(fid: string, limit = 100) {
const profile = db
.selectFrom("user_data")
.selectFrom('user_data')
.select([
"fid",
sql`MAX(CASE WHEN type = 1 THEN value ELSE NULL END)`.as("pfp_url"),
sql`MAX(CASE WHEN type = 2 THEN value ELSE NULL END)`.as("display_name"),
sql`MAX(CASE WHEN type = 3 THEN value ELSE NULL END)`.as("bio"),
sql`MAX(CASE WHEN type = 6 THEN value ELSE NULL END)`.as("username"),
'fid',
sql`MAX(CASE WHEN type = 1 THEN value ELSE NULL END)`.as('pfp_url'),
sql`MAX(CASE WHEN type = 2 THEN value ELSE NULL END)`.as('display_name'),
sql`MAX(CASE WHEN type = 3 THEN value ELSE NULL END)`.as('bio'),
sql`MAX(CASE WHEN type = 6 THEN value ELSE NULL END)`.as('username'),
])
.where("deleted_at", "is", null)
.where("fid", "=", fid)
.groupBy("fid")
.as("profile");
.where('deleted_at', 'is', null)
.where('fid', '=', fid)
.groupBy('fid')
.as('profile');

const casts = await db
.selectFrom("casts")
.leftJoin(profile, "profile.fid", "casts.fid")
.selectFrom('casts')
.leftJoin(profile, 'profile.fid', 'casts.fid')
.select([
"casts.hash",
"casts.timestamp",
"casts.text",
"casts.embeds",
"casts.mentions",
"casts.mentions_positions",
"casts.fid",
"profile.pfp_url",
"profile.display_name",
"profile.bio",
"profile.username",
'casts.hash',
'casts.timestamp',
'casts.text',
'casts.embeds',
'casts.mentions',
'casts.mentions_positions',
'casts.fid',
'profile.pfp_url',
'profile.display_name',
'profile.bio',
'profile.username',
])
.where("casts.fid", "=", fid)
.where("casts.deleted_at", "is", null)
.where("casts.parent_hash", "is", null)
.orderBy("casts.timestamp", "desc")
.where('casts.fid', '=', fid)
.where('casts.deleted_at', 'is', null)
.where('casts.parent_hash', 'is', null)
.orderBy('casts.timestamp', 'desc')
.limit(limit)
.execute();

Expand Down
67 changes: 34 additions & 33 deletions web/src/lib/services/feed.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
import { sql } from "kysely";
import { db } from "../database/db";
import { formatHash } from "./casts";
import { sql } from 'kysely';

import { db } from '../database/db';
import { formatHash } from './casts';

export async function getFeed(fid: string, limit = 100) {
const follows = db
.selectFrom("links")
.select("target_fid")
.where("fid", "=", fid);
.selectFrom('links')
.select('target_fid')
.where('fid', '=', fid);

const profiles = db
.selectFrom("user_data")
.selectFrom('user_data')
.select([
"fid",
sql`MAX(CASE WHEN type = 1 THEN value ELSE NULL END)`.as("pfp_url"),
sql`MAX(CASE WHEN type = 2 THEN value ELSE NULL END)`.as("display_name"),
sql`MAX(CASE WHEN type = 3 THEN value ELSE NULL END)`.as("bio"),
sql`MAX(CASE WHEN type = 6 THEN value ELSE NULL END)`.as("username"),
'fid',
sql`MAX(CASE WHEN type = 1 THEN value ELSE NULL END)`.as('pfp_url'),
sql`MAX(CASE WHEN type = 2 THEN value ELSE NULL END)`.as('display_name'),
sql`MAX(CASE WHEN type = 3 THEN value ELSE NULL END)`.as('bio'),
sql`MAX(CASE WHEN type = 6 THEN value ELSE NULL END)`.as('username'),
])
.where("deleted_at", "is", null)
.where("fid", "in", follows)
.groupBy("fid")
.as("profiles");
.where('deleted_at', 'is', null)
.where('fid', 'in', follows)
.groupBy('fid')
.as('profiles');

const casts = await db
.selectFrom("casts")
.leftJoin(profiles, "profiles.fid", "casts.fid")
.selectFrom('casts')
.leftJoin(profiles, 'profiles.fid', 'casts.fid')
.select([
"casts.hash",
"casts.timestamp",
"casts.text",
"casts.embeds",
"casts.mentions",
"casts.mentions_positions",
"casts.fid",
"profiles.pfp_url",
"profiles.display_name",
"profiles.bio",
"profiles.username",
'casts.hash',
'casts.timestamp',
'casts.text',
'casts.embeds',
'casts.mentions',
'casts.mentions_positions',
'casts.fid',
'profiles.pfp_url',
'profiles.display_name',
'profiles.bio',
'profiles.username',
])
.where("casts.fid", "in", follows)
.where("casts.deleted_at", "is", null)
.where("casts.parent_hash", "is", null)
.orderBy("casts.timestamp", "desc")
.where('casts.fid', 'in', follows)
.where('casts.deleted_at', 'is', null)
.where('casts.parent_hash', 'is', null)
.orderBy('casts.timestamp', 'desc')
.limit(limit)
.execute();

Expand Down
29 changes: 16 additions & 13 deletions web/src/lib/services/user.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { sql } from "kysely";
import { db } from "../database/db";
import { sql } from 'kysely';

import { db } from '../database/db';

export async function getProfile(fid: string) {
const profile = await db.selectFrom('user_data')
.select([
'fid',
sql`MAX(CASE WHEN type = 1 THEN value ELSE NULL END)`.as('pfp_url'),
sql`MAX(CASE WHEN type = 2 THEN value ELSE NULL END)`.as('display_name'),
sql`MAX(CASE WHEN type = 3 THEN value ELSE NULL END)`.as('bio'),
sql`MAX(CASE WHEN type = 6 THEN value ELSE NULL END)`.as('username')
])
.where('deleted_at', 'is', null)
.where('fid', '=', fid)
.groupBy('fid').execute();
const profile = await db
.selectFrom('user_data')
.select([
'fid',
sql`MAX(CASE WHEN type = 1 THEN value ELSE NULL END)`.as('pfp_url'),
sql`MAX(CASE WHEN type = 2 THEN value ELSE NULL END)`.as('display_name'),
sql`MAX(CASE WHEN type = 3 THEN value ELSE NULL END)`.as('bio'),
sql`MAX(CASE WHEN type = 6 THEN value ELSE NULL END)`.as('username'),
])
.where('deleted_at', 'is', null)
.where('fid', '=', fid)
.groupBy('fid')
.execute();
return profile[0];
}

0 comments on commit cf4bf05

Please sign in to comment.