From 8617f5b2396bb51cd4ad217420d12cde5568ee89 Mon Sep 17 00:00:00 2001 From: Keith Stevens Date: Sat, 17 Dec 2022 14:20:39 +0900 Subject: [PATCH] Implementing a bare bones interaction with the task backend --- website/pages/api/auth/[...nextauth].js | 41 ----------------- website/pages/api/new_task.js | 59 +++++++++++++++++++++++++ website/pages/api/update_task.js | 48 ++++++++++++++++++++ website/pages/index.js | 1 - website/pages/new_task.js | 53 ++++++++++++++++++++++ website/prisma/schema.prisma | 13 ++++-- 6 files changed, 170 insertions(+), 45 deletions(-) create mode 100644 website/pages/api/new_task.js create mode 100644 website/pages/api/update_task.js create mode 100644 website/pages/new_task.js diff --git a/website/pages/api/auth/[...nextauth].js b/website/pages/api/auth/[...nextauth].js index 06044ac1..c4ff116c 100644 --- a/website/pages/api/auth/[...nextauth].js +++ b/website/pages/api/auth/[...nextauth].js @@ -36,47 +36,6 @@ export const authOptions = { return session; }, }, - events: { - /** - * When a new user signs in, we register them with the Labeler backend. - */ - async signIn({ user, account, profile, isNewUser }) { - if (!isNewUser) { - return; - } - try { - // Register the new user with the Labeler Backend. - const res = await fetch(`${process.env.FASTAPI_URL}/api/v1/labelers`, { - method: "POST", - headers: { - "X-API-Key": process.env.FASTAPI_KEY, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - discord_username: user.id, - display_name: user.name || user.email, - is_enabled: true, - notes: account.provider, - }), - }); - if (res.status !== 200) { - console.error(res.statusText); - return; - } - // Update the User entry with the Labeler Backend's ID so we can - // reference it later. - const { id: labelerId } = await res.json(); - await prisma.user.update({ - where: { id: user.id }, - data: { - labelerId, - }, - }); - } catch (error) { - console.error(error); - } - }, - }, }; export default NextAuth(authOptions); diff --git a/website/pages/api/new_task.js b/website/pages/api/new_task.js new file mode 100644 index 00000000..d9bd5012 --- /dev/null +++ b/website/pages/api/new_task.js @@ -0,0 +1,59 @@ +import { unstable_getServerSession } from "next-auth/next"; +import { authOptions } from "./auth/[...nextauth]"; + +/** + * Returns a list of prompts from the Labeler Backend. + */ +export default async (req, res) => { + const session = await unstable_getServerSession(req, res, authOptions); + + if (!session) { + res.status(401).end(); + return; + } + + const taskRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/tasks/`, { + method: "POST", + headers: { + "X-API-Key": process.env.FASTAPI_KEY, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + type: "rate_summary", + user: { + id: session.user.id, + display_name: session.user.name, + auth_method: "local", + }, + }), + }); + const task = await taskRes.json(); + + const registeredTask = await prisma.registeredTask.create({ + data: { + task, + user: { + connect: { + id: session.user.id, + }, + }, + }, + }); + + const ackRes = await fetch( + `${process.env.FASTAPI_URL}/api/v1/tasks/${task.id}/ack`, + { + method: "POST", + headers: { + "X-API-Key": process.env.FASTAPI_KEY, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + post_id: registeredTask.id, + }), + } + ); + const ack = await ackRes.json(); + + res.status(200).json(registeredTask); +}; diff --git a/website/pages/api/update_task.js b/website/pages/api/update_task.js new file mode 100644 index 00000000..54044781 --- /dev/null +++ b/website/pages/api/update_task.js @@ -0,0 +1,48 @@ +import { unstable_getServerSession } from "next-auth/next"; +import { authOptions } from "./auth/[...nextauth]"; + +/** + * Returns a list of prompts from the Labeler Backend. + */ +export default async (req, res) => { + const session = await unstable_getServerSession(req, res, authOptions); + + if (!session) { + res.status(401).end(); + return; + } + + const { id, rating } = await JSON.parse(req.body); + + const registeredTask = await prisma.registeredTask.findUnique({ + where: { id }, + select: { task: true }, + }); + + const interactionRes = await fetch( + `${process.env.FASTAPI_URL}/api/v1/tasks/interaction`, + { + method: "POST", + headers: { + "X-API-Key": process.env.FASTAPI_KEY, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + type: "text_reply_to_post", + user: { + id: session.user.id, + display_name: session.user.name, + auth_method: "local", + }, + post_id: id, + user_post_id: "1234", + text: rating, + }), + } + ); + console.log(interactionRes.status); + const interaction = await interactionRes.json(); + console.log(interaction); + + res.status(200).end(); +}; diff --git a/website/pages/index.js b/website/pages/index.js index d150d6c7..a5e186cb 100644 --- a/website/pages/index.js +++ b/website/pages/index.js @@ -19,7 +19,6 @@ export default function Home() { return (
- {/* logo */}

Open Chat Gpt

Open chat gpt is a project meant to give everyone access to a great diff --git a/website/pages/new_task.js b/website/pages/new_task.js new file mode 100644 index 00000000..a971f42c --- /dev/null +++ b/website/pages/new_task.js @@ -0,0 +1,53 @@ +import axios from "axios"; +import Head from "next/head"; +import Image from "next/image"; +import { useSession, signIn, signOut } from "next-auth/react"; +import { useEffect, useRef, useState } from "react"; +import useSWRImmutable from "swr/immutable"; +import useSWRMutation from "swr/mutation"; + +const fetcher = (url) => axios.get(url).then((res) => res.data); + +async function sendRequest(url, { arg }) { + return fetch(url, { + method: "POST", + body: JSON.stringify(arg), + }); +} + +export default function NewPage() { + const responseEl = useRef(null); + const { + data: registeredTask, + errors, + isLoading, + } = useSWRImmutable("/api/new_task", fetcher); + const { trigger, isMutating } = useSWRMutation( + "/api/update_task", + sendRequest + ); + + const submitResponse = () => { + trigger({ + id: registeredTask.id, + rating: responseEl.current.value, + }); + }; + if (isLoading) { + return

Loading
; + } + + return ( +
+
{registeredTask.id}
+
{registeredTask.task.type}
+
{registeredTask.task.text}
+
{registeredTask.task.summary}
+
+ {registeredTask.task.scale.min} to {registeredTask.task.scale.max} +
+ + +
+ ); +} diff --git a/website/prisma/schema.prisma b/website/prisma/schema.prisma index 5ef3e0f0..d8a4772f 100644 --- a/website/prisma/schema.prisma +++ b/website/prisma/schema.prisma @@ -41,11 +41,10 @@ model User { emailVerified DateTime? image String? - // Records the unique user id stored in the Labeler Backend. - labelerId Int? - accounts Account[] sessions Session[] + + tasks RegisteredTask[] } model VerificationToken { @@ -55,3 +54,11 @@ model VerificationToken { @@unique([identifier, token]) } + +model RegisteredTask { + id String @id @default(uuid()) + task Json + + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + userId String +}