Implementing a bare bones interaction with the task backend

This commit is contained in:
Keith Stevens
2022-12-17 14:20:39 +09:00
parent 668f81c5b2
commit 8617f5b239
6 changed files with 170 additions and 45 deletions
-41
View File
@@ -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);
+59
View File
@@ -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);
};
+48
View File
@@ -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();
};
-1
View File
@@ -19,7 +19,6 @@ export default function Home() {
return (
<div className={styles.App}>
<header className={styles.AppHeader}>
{/* <img src={logo} className="App-logo" alt="logo" /> */}
<h2>Open Chat Gpt</h2>
<p>
Open chat gpt is a project meant to give everyone access to a great
+53
View File
@@ -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 <div>Loading</div>;
}
return (
<div>
<div>{registeredTask.id}</div>
<div>{registeredTask.task.type}</div>
<div>{registeredTask.task.text}</div>
<div>{registeredTask.task.summary}</div>
<div>
{registeredTask.task.scale.min} to {registeredTask.task.scale.max}
</div>
<input type="text" ref={responseEl} />
<button onClick={submitResponse}>Submit Response</button>
</div>
);
}
+10 -3
View File
@@ -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
}