mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-09-12 12:03:06 +08:00
Implementing a bare bones interaction with the task backend
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
@@ -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();
|
||||
};
|
||||
Reference in New Issue
Block a user