Skip to content

Commit

Permalink
trpc bb
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-jamming-reilly committed Sep 1, 2023
1 parent b188c86 commit 69c82dd
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 102 deletions.
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 2,
"useTabs": false
}
15 changes: 15 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ model SocialProfile {
user User? @relation(fields: [userId], references: [id])
}

model Clip {
id String @id @default(cuid())
//
user_id String?
url String
query String?
title String
body String?
start_ms Int
end_ms Int
//
user User? @relation(fields: [user_id], references: [id])
}

model SearchQuery {
id String @id @default(uuid())
query String
Expand Down Expand Up @@ -69,6 +83,7 @@ model User {
sessions Session[]
profile SocialProfile?
queries SearchQuery[]
clips Clip[]
}

model VerificationToken {
Expand Down
71 changes: 17 additions & 54 deletions src/app/p/[name]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { env } from "~/env.mjs";
import SearchPage from "./SearchPage";
import UploadList from "./UploadList";
import ProfileHeader from "./ProfileHeader";
import { SearchQuery } from "@prisma/client";
import { trpcServer } from "~/lib/trpc-server";

function msDate(date: string): number {
const ms = new Date(date);
Expand All @@ -14,81 +14,44 @@ function msDate(date: string): number {

type SearchParamType = string | string[] | undefined;

interface PageProps {
params: { name: string };
searchParams: { [key: string]: SearchParamType };
}

function parseSearchQuery(param: SearchParamType): string | undefined {
if (Array.isArray(param)) return param[0];
else return param;
}

async function getLatestSearches(query: string | undefined, author: string) {
if (query) return [];

const searches = await prisma.searchQuery.findMany({
where: {
author: author,
},
orderBy: {
create_date: "desc",
},
take: 100,
});

const queries = new Set(searches.map((search) => search.query.toLowerCase()));
let uniqueSearches: SearchQuery[] = [];
async function getSearchResults(query: string | undefined, author: string) {
if (!query) return [];

searches.forEach((search) => {
if (queries.has(search.query.toLowerCase())) {
uniqueSearches.push(search);
queries.delete(search.query.toLowerCase());
}
return await trpcServer.search.fromAuthor({
query,
author,
});

if (uniqueSearches.length > 30) {
return uniqueSearches.splice(0, 30);
}

return uniqueSearches;
}

async function getSearchResults(
client: SearchClient,
query: string | undefined,
author: string
) {
if (!query) return [];
return client.search.documentSegmentsByQuery(query, author, 0, 20);
}
async function getLatestSearches(query: string | undefined, author: string) {
if (query) return [];

interface PageProps {
params: { name: string };
searchParams: { [key: string]: SearchParamType };
return await trpcServer.search.previousSearches({ author });
}

export default async function Page({ params, searchParams }: PageProps) {
const client = new SearchClient({
BASE: env.PARASOCIAL_API_BASE_URL,
});
const author = decodeURI(params.name);
const query = parseSearchQuery(searchParams.q);

const [profile, documents, searchResults, latestSearches] = await Promise.all(
[
client.profile.getYoutube(author),
client.search.allDocuments(author),
getSearchResults(client, query, author),
trpcServer.profile.getYoutubeProfile({ channel: author }),
trpcServer.profile.getDocuments({ channel: author }),
getSearchResults(query, author),
getLatestSearches(query, author),
]
);

if (query) {
// log the search query
await prisma.searchQuery.create({
data: {
query: query,
author: author,
},
});
}

return (
<main className="my-4 flex flex-col items-center sm:container">
<div className="flex w-full flex-col justify-between sm:w-fit sm:flex-row">
Expand Down
18 changes: 6 additions & 12 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { SearchClient } from "~/lib/search";
import { getServerAuthSession } from "~/server/auth";
import { env } from "~/env.mjs";

import Profile from "./Profile";
import Link from "next/link";
import AuthBtn from "../components/SignInBtn";
import LogoutBtn from "../components/LogoutBtn";

import AuthBtn from "~/components/SignInBtn";
import LogoutBtn from "~/components/LogoutBtn";
import { getServerAuthSession } from "~/server/auth";
import { trpcServer } from "~/lib/trpc-server";

export default async function Home() {
const client = new SearchClient({
BASE: env.PARASOCIAL_API_BASE_URL,
});
import Profile from "./Profile";

const profiles = await client.profile.getAllProfiles();
export default async function Home() {
const session = await getServerAuthSession();
const profiles = await trpcServer.profile.getAll({});

return (
<main className="container mt-4 flex min-h-screen flex-col items-center">
Expand Down
15 changes: 8 additions & 7 deletions src/lib/trpc-server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { httpBatchLink } from "@trpc/client";

import { appRouter } from "~/server/api/root";
import { getBaseUrl } from "~/lib/utils"
import { getBaseUrl } from "~/lib/utils";
// import { getServerAuthSession } from "~/server/auth";

export const trpcServer = appRouter.createCaller({
links: [
httpBatchLink({
url: `${getBaseUrl}/api/trpc`,
}),
],
});
links: [
httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
}),
],
});
2 changes: 2 additions & 0 deletions src/server/api/root.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { searchRouter } from "~/server/api/routers/search";
import { profileRouter } from "./routers/profile";
import { createTRPCRouter } from "~/server/api/trpc";

/**
Expand All @@ -8,6 +9,7 @@ import { createTRPCRouter } from "~/server/api/trpc";
*/
export const appRouter = createTRPCRouter({
search: searchRouter,
profile: profileRouter,
});

// export type definition of API
Expand Down
47 changes: 47 additions & 0 deletions src/server/api/routers/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { z } from "zod";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { SearchClient } from "~/lib/search";
import { env } from "~/env.mjs";
import { prisma } from "~/server/db";
import { SearchQuery } from "@prisma/client";

const social = new SearchClient({
BASE: env.PARASOCIAL_API_BASE_URL,
});

export const profileRouter = createTRPCRouter({
getAll: publicProcedure
.input(
z.object({
skip: z.number().default(0),
limit: z.number().min(10).max(50).default(20),
})
)
.query(async ({ input }) => {
return await social.profile.getAllProfiles();
}),
getYoutubeProfile: publicProcedure
.input(
z.object({
channel: z.string(),
})
)
.query(async ({ input }) => {
return await social.profile.getYoutube(input.channel);
}),
getDocuments: publicProcedure
.input(
z.object({
channel: z.string(),
skip: z.number().default(0),
limit: z.number().min(10).max(50).default(20),
})
)
.query(async ({ input }) => {
return await social.search.allDocuments(
input.channel,
input.skip,
input.limit
);
}),
});
72 changes: 60 additions & 12 deletions src/server/api/routers/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ import {
publicProcedure,
// protectedProcedure,
} from "~/server/api/trpc";
import { SearchClient } from "~/lib/search"
import { env } from "~/env.mjs"
import { SearchClient } from "~/lib/search";
import { env } from "~/env.mjs";
import { prisma } from "~/server/db";
import { SearchQuery } from "@prisma/client";
import { getServerAuthSession } from "~/server/auth";

const social = new SearchClient({
BASE: env.PARASOCIAL_API_BASE_URL,
});

export const searchRouter = createTRPCRouter({
previousSearches: publicProcedure
.input(z.object({ author: z.string() }))
.input(
z.object({
author: z.string(),
limit: z.number().min(10).max(100).default(35),
})
)
.query(async ({ input }) => {
//

const searches = await prisma.searchQuery.findMany({
where: {
author: input.author,
Expand All @@ -29,21 +33,65 @@ export const searchRouter = createTRPCRouter({
take: 100,
});

const queries = new Set(searches.map((search) => search.query.toLowerCase()));
const queries = new Set(
searches.map((search) => search.query.toLowerCase())
);
let uniqueSearches: SearchQuery[] = [];

for (const search of searches) {
if (queries.has(search.query.toLowerCase())) {
const query = search.query.toLowerCase();

if (queries.has(query)) {
uniqueSearches.push(search);
queries.delete(search.query.toLowerCase());
queries.delete(query);

if (uniqueSearches.length >= input.limit) break;
}
}

return
return uniqueSearches;
}),
fromAuthor: publicProcedure
.input(
z.object({
query: z.string(),
author: z.string(),
skip: z.number().default(0),
limit: z.number().min(10).max(50).default(20),
})
)
.query(async ({ input }) => {
const session = await getServerAuthSession();

const [results] = await Promise.all([
social.search.documentSegmentsByQuery(
input.query,
input.author,
input.skip,
input.limit
),
prisma.searchQuery.create({
data: {
user_id: session?.user.id ?? undefined,
query: input.query,
author: input.author,
},
}),
]);

return results;
}),
searchByAuthor: publicProcedure
.input(z.object({ query: z.string(), author: z.string() }))
fromDocument: publicProcedure
.input(
z.object({
query: z.string(),
url: z.string().url(),
skip: z.number().optional(),
})
)
.query(async ({ input }) => {
return await social.search.documentSegmentsByQuery(input.query, input.author)
// const limit = 20;
// return await social.search.documentSegmentsByQuery(input.query, input.author, input.skip, limit)
return [];
}),
});
3 changes: 3 additions & 0 deletions src/server/api/routers/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";

export const userRouter = createTRPCRouter({});
Loading

0 comments on commit 69c82dd

Please sign in to comment.