Fix existing bugs in tests, refactor users

This commit is contained in:
Jesse Wierzbinski 2023-10-08 10:20:42 -10:00
parent b7587f8d3f
commit 65ff53e90c
19 changed files with 385 additions and 117 deletions

View file

@ -1,6 +1,5 @@
import { errorResponse, jsonResponse } from "@response";
import { MatchedRoute } from "bun";
import { RawActor } from "~database/entities/RawActor";
import { User } from "~database/entities/User";
/**
@ -20,9 +19,9 @@ export default async (
const user = await User.retrieveFromToken(token);
let foundUser: RawActor | null;
let foundUser: User | null;
try {
foundUser = await RawActor.findOneBy({
foundUser = await User.findOneBy({
id,
});
} catch (e) {
@ -31,7 +30,5 @@ export default async (
if (!foundUser) return errorResponse("User not found", 404);
return jsonResponse(
await foundUser.toAPIAccount(user?.id === foundUser.id)
);
return jsonResponse(await foundUser.toAPI(user?.id === foundUser.id));
};

View file

@ -147,7 +147,7 @@ export default async (req: Request): Promise<Response> => {
// TODO: Check if locale is valid
await User.createNew({
await User.createNewLocal({
username: body.username ?? "",
password: body.password ?? "",
email: body.email ?? "",

View file

@ -0,0 +1,17 @@
import { jsonResponse } from "@response";
import { IsNull } from "typeorm";
import { Emoji } from "~database/entities/Emoji";
/**
* Creates a new user
*/
// eslint-disable-next-line @typescript-eslint/require-await
export default async (): Promise<Response> => {
const emojis = await Emoji.findBy({
instance: IsNull(),
});
return jsonResponse(
await Promise.all(emojis.map(async emoji => await emoji.toAPI()))
);
};

View file

@ -37,7 +37,7 @@ export default async (
if (req.method === "GET") {
return jsonResponse(await foundStatus.toAPI());
} else if (req.method === "DELETE") {
if ((await foundStatus.toAPI()).account.id !== user?.actor.id) {
if ((await foundStatus.toAPI()).account.id !== user?.id) {
return errorResponse("Unauthorized", 401);
}

View file

@ -6,7 +6,6 @@ import { parseRequest } from "@request";
import { errorResponse, jsonResponse } from "@response";
import { APActor } from "activitypub-types";
import { Application } from "~database/entities/Application";
import { RawActor } from "~database/entities/RawActor";
import { RawObject } from "~database/entities/RawObject";
import { Status } from "~database/entities/Status";
import { User } from "~database/entities/User";
@ -108,7 +107,7 @@ export default async (req: Request): Promise<Response> => {
// Get reply account and status if exists
let replyObject: RawObject | null = null;
let replyActor: RawActor | null = null;
let replyUser: User | null = null;
if (in_reply_to_id) {
replyObject = await RawObject.findOne({
@ -117,7 +116,7 @@ export default async (req: Request): Promise<Response> => {
},
});
replyActor = await RawActor.getByActorId(
replyUser = await User.getByActorId(
(replyObject?.data.attributedTo as APActor).id ?? ""
);
}
@ -138,9 +137,9 @@ export default async (req: Request): Promise<Response> => {
spoiler_text: spoiler_text || "",
emojis: [],
reply:
replyObject && replyActor
replyObject && replyUser
? {
actor: replyActor,
user: replyUser,
object: replyObject,
}
: undefined,

View file

@ -0,0 +1,68 @@
import { parseRequest } from "@request";
import { errorResponse, jsonResponse } from "@response";
import { RawObject } from "~database/entities/RawObject";
/**
* Fetch home timeline statuses
*/
export default async (req: Request): Promise<Response> => {
const {
limit = 20,
max_id,
min_id,
since_id,
} = await parseRequest<{
max_id?: string;
since_id?: string;
min_id?: string;
limit?: number;
}>(req);
if (limit < 1 || limit > 40) {
return errorResponse("Limit must be between 1 and 40", 400);
}
let query = RawObject.createQueryBuilder("object")
.where("object.data->>'type' = 'Note'")
// From a user followed by the current user
.andWhere("CAST(object.data->>'to' AS jsonb) @> CAST(:to AS jsonb)", {
to: JSON.stringify([
"https://www.w3.org/ns/activitystreams#Public",
]),
})
.orderBy("object.data->>'published'", "DESC")
.take(limit);
if (max_id) {
const maxPost = await RawObject.findOneBy({ id: max_id });
if (maxPost) {
query = query.andWhere("object.data->>'published' < :max_date", {
max_date: maxPost.data.published,
});
}
}
if (min_id) {
const minPost = await RawObject.findOneBy({ id: min_id });
if (minPost) {
query = query.andWhere("object.data->>'published' > :min_date", {
min_date: minPost.data.published,
});
}
}
if (since_id) {
const sincePost = await RawObject.findOneBy({ id: since_id });
if (sincePost) {
query = query.andWhere("object.data->>'published' >= :since_date", {
since_date: sincePost.data.published,
});
}
}
const objects = await query.getMany();
return jsonResponse(
await Promise.all(objects.map(async object => await object.toAPI()))
);
};

View file

@ -35,7 +35,7 @@ export default async (
email,
});
if (!user || !(await Bun.password.verify(password, user.password)))
if (!user || !(await Bun.password.verify(password, user.password || "")))
return errorResponse("Invalid username or password", 401);
// Get application