mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
Add new API endpoints
This commit is contained in:
parent
95b46ba2e4
commit
1fb4600445
|
|
@ -79,6 +79,7 @@ Working endpoints are:
|
||||||
- `/api/v1/accounts/update_credentials`
|
- `/api/v1/accounts/update_credentials`
|
||||||
- `/api/v1/accounts/verify_credentials`
|
- `/api/v1/accounts/verify_credentials`
|
||||||
- `/api/v1/accounts/familiar_followers`
|
- `/api/v1/accounts/familiar_followers`
|
||||||
|
- `/api/v1/statuses/:id` (`GET`, `DELETE`)
|
||||||
- `/api/v1/statuses`
|
- `/api/v1/statuses`
|
||||||
- `/api/v1/apps`
|
- `/api/v1/apps`
|
||||||
- `/api/v1/apps/verify_credentials`
|
- `/api/v1/apps/verify_credentials`
|
||||||
|
|
|
||||||
|
|
@ -222,6 +222,7 @@ export class RawActor extends BaseEntity {
|
||||||
* @returns Whether an actor with the specified ID exists in the database.
|
* @returns Whether an actor with the specified ID exists in the database.
|
||||||
*/
|
*/
|
||||||
static async exists(id: string) {
|
static async exists(id: string) {
|
||||||
|
console.log(!!(await RawActor.getByActorId(id)));
|
||||||
return !!(await RawActor.getByActorId(id));
|
return !!(await RawActor.getByActorId(id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||||
import { APActor, APImage, APObject, DateTime } from "activitypub-types";
|
import { APImage, APObject, DateTime } from "activitypub-types";
|
||||||
import { getConfig } from "@config";
|
import { getConfig } from "@config";
|
||||||
import { appendFile } from "fs/promises";
|
import { appendFile } from "fs/promises";
|
||||||
import { APIStatus } from "~types/entities/status";
|
import { APIStatus } from "~types/entities/status";
|
||||||
|
|
@ -64,7 +64,7 @@ export class RawObject extends BaseEntity {
|
||||||
account:
|
account:
|
||||||
(await (
|
(await (
|
||||||
await RawActor.getByActorId(
|
await RawActor.getByActorId(
|
||||||
(this.data.attributedTo as APActor).id ?? ""
|
this.data.attributedTo as string
|
||||||
)
|
)
|
||||||
)?.toAPIAccount()) ?? (null as unknown as APIAccount),
|
)?.toAPIAccount()) ?? (null as unknown as APIAccount),
|
||||||
created_at: new Date(this.data.published as DateTime).toISOString(),
|
created_at: new Date(this.data.published as DateTime).toISOString(),
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,8 @@ export class Status extends BaseEntity {
|
||||||
|
|
||||||
async remove(options?: RemoveOptions | undefined) {
|
async remove(options?: RemoveOptions | undefined) {
|
||||||
// Delete object
|
// Delete object
|
||||||
await this.object.remove(options);
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||||
|
if (this.object) await this.object.remove(options);
|
||||||
|
|
||||||
return await super.remove(options);
|
return await super.remove(options);
|
||||||
}
|
}
|
||||||
|
|
@ -146,6 +147,8 @@ export class Status extends BaseEntity {
|
||||||
inReplyTo: data.reply?.object
|
inReplyTo: data.reply?.object
|
||||||
? data.reply.object.data.id
|
? data.reply.object.data.id
|
||||||
: undefined,
|
: undefined,
|
||||||
|
published: new Date().toISOString(),
|
||||||
|
tag: [],
|
||||||
attributedTo: `${config.http.base_url}/@${data.account.username}`,
|
attributedTo: `${config.http.base_url}/@${data.account.username}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -231,7 +234,8 @@ export class Status extends BaseEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
async toAPI(): Promise<APIStatus> {
|
async toAPI(): Promise<APIStatus> {
|
||||||
return {
|
return await this.object.toAPI();
|
||||||
|
/* return {
|
||||||
account: await this.account.toAPI(),
|
account: await this.account.toAPI(),
|
||||||
application: (await this.application?.toAPI()) ?? null,
|
application: (await this.application?.toAPI()) ?? null,
|
||||||
bookmarked: false,
|
bookmarked: false,
|
||||||
|
|
@ -241,7 +245,7 @@ export class Status extends BaseEntity {
|
||||||
),
|
),
|
||||||
favourited: false,
|
favourited: false,
|
||||||
favourites_count: this.likes.length,
|
favourites_count: this.likes.length,
|
||||||
id: this.id,
|
id: this.object.id,
|
||||||
in_reply_to_account_id: null,
|
in_reply_to_account_id: null,
|
||||||
in_reply_to_id: null,
|
in_reply_to_id: null,
|
||||||
language: null,
|
language: null,
|
||||||
|
|
@ -263,6 +267,6 @@ export class Status extends BaseEntity {
|
||||||
url: `${config.http.base_url}/@${this.account.username}/${this.id}`,
|
url: `${config.http.base_url}/@${this.account.username}/${this.id}`,
|
||||||
visibility: "public",
|
visibility: "public",
|
||||||
quote: null,
|
quote: null,
|
||||||
};
|
}; */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -150,6 +150,7 @@ export class User extends BaseEntity {
|
||||||
relations: {
|
relations: {
|
||||||
user: {
|
user: {
|
||||||
relationships: true,
|
relationships: true,
|
||||||
|
actor: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
@ -224,8 +225,21 @@ export class User extends BaseEntity {
|
||||||
|
|
||||||
async updateActor() {
|
async updateActor() {
|
||||||
// Check if actor exists
|
// Check if actor exists
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||||
const actor = this.actor ? this.actor : new RawActor();
|
|
||||||
|
// Check is actor already exists
|
||||||
|
const actorExists = await RawActor.getByActorId(
|
||||||
|
`${config.http.base_url}/@${this.username}`
|
||||||
|
);
|
||||||
|
|
||||||
|
let actor: RawActor;
|
||||||
|
|
||||||
|
if (actorExists) {
|
||||||
|
actor = actorExists;
|
||||||
|
} else {
|
||||||
|
actor = new RawActor();
|
||||||
|
}
|
||||||
|
|
||||||
actor.data = {
|
actor.data = {
|
||||||
"@context": [
|
"@context": [
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ export default async (
|
||||||
},
|
},
|
||||||
isReblog: exclude_reblogs ? true : undefined,
|
isReblog: exclude_reblogs ? true : undefined,
|
||||||
},
|
},
|
||||||
relations: ["account", "emojis", "announces", "likes"],
|
relations: ["account", "emojis", "announces", "likes", "object"],
|
||||||
order: {
|
order: {
|
||||||
created_at: "DESC",
|
created_at: "DESC",
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -37,10 +37,12 @@ export default async (
|
||||||
if (req.method === "GET") {
|
if (req.method === "GET") {
|
||||||
return jsonResponse(await foundStatus.toAPI());
|
return jsonResponse(await foundStatus.toAPI());
|
||||||
} else if (req.method === "DELETE") {
|
} else if (req.method === "DELETE") {
|
||||||
if ((await foundStatus.toAPI()).account.id !== user?.id) {
|
if ((await foundStatus.toAPI()).account.id !== user?.actor.id) {
|
||||||
return errorResponse("Unauthorized", 401);
|
return errorResponse("Unauthorized", 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Implement delete and redraft functionality
|
||||||
|
|
||||||
// Get associated Status object
|
// Get associated Status object
|
||||||
const status = await Status.createQueryBuilder("status")
|
const status = await Status.createQueryBuilder("status")
|
||||||
.leftJoinAndSelect("status.object", "object")
|
.leftJoinAndSelect("status.object", "object")
|
||||||
|
|
@ -54,7 +56,7 @@ export default async (
|
||||||
// Delete status and all associated objects
|
// Delete status and all associated objects
|
||||||
await status.object.remove();
|
await status.object.remove();
|
||||||
|
|
||||||
return jsonResponse({});
|
return jsonResponse({}, 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
return jsonResponse({});
|
return jsonResponse({});
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ const config = getConfig();
|
||||||
let token: Token;
|
let token: Token;
|
||||||
let user: User;
|
let user: User;
|
||||||
let user2: User;
|
let user2: User;
|
||||||
|
let status: APIStatus | null = null;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
if (!AppDataSource.isInitialized) await AppDataSource.initialize();
|
if (!AppDataSource.isInitialized) await AppDataSource.initialize();
|
||||||
|
|
@ -99,11 +100,11 @@ describe("POST /api/v1/statuses", () => {
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
expect(response.headers.get("content-type")).toBe("application/json");
|
expect(response.headers.get("content-type")).toBe("application/json");
|
||||||
|
|
||||||
const status: APIStatus = await response.json();
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
||||||
|
status = (await response.json()) as APIStatus;
|
||||||
expect(status.content).toBe("Hello, world!");
|
expect(status.content).toBe("Hello, world!");
|
||||||
expect(status.visibility).toBe("public");
|
expect(status.visibility).toBe("public");
|
||||||
expect(status.account.id).toBe(user.id);
|
expect(status.account.id).toBe(user.actor.id);
|
||||||
expect(status.replies_count).toBe(0);
|
expect(status.replies_count).toBe(0);
|
||||||
expect(status.favourites_count).toBe(0);
|
expect(status.favourites_count).toBe(0);
|
||||||
expect(status.reblogged).toBe(false);
|
expect(status.reblogged).toBe(false);
|
||||||
|
|
@ -215,7 +216,7 @@ describe("GET /api/v1/accounts/:id/statuses", () => {
|
||||||
// Basic validation
|
// Basic validation
|
||||||
expect(status1.content).toBe("Hello, world!");
|
expect(status1.content).toBe("Hello, world!");
|
||||||
expect(status1.visibility).toBe("public");
|
expect(status1.visibility).toBe("public");
|
||||||
expect(status1.account.id).toBe(user.id);
|
expect(status1.account.id).toBe(user.actor.id);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -562,6 +563,65 @@ describe("GET /api/v1/accounts/familiar_followers", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("GET /api/v1/statuses/:id", () => {
|
||||||
|
test("should return the specified status object", async () => {
|
||||||
|
const response = await fetch(
|
||||||
|
`${config.http.base_url}/api/v1/statuses/${status?.id}`,
|
||||||
|
{
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token.access_token}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(response.status).toBe(200);
|
||||||
|
expect(response.headers.get("content-type")).toBe("application/json");
|
||||||
|
|
||||||
|
const statusJson = await response.json();
|
||||||
|
|
||||||
|
expect(statusJson.id).toBe(status?.id);
|
||||||
|
expect(statusJson.content).toBeDefined();
|
||||||
|
expect(statusJson.created_at).toBeDefined();
|
||||||
|
expect(statusJson.account).toBeDefined();
|
||||||
|
expect(statusJson.reblog).toBeDefined();
|
||||||
|
expect(statusJson.application).toBeDefined();
|
||||||
|
expect(statusJson.emojis).toBeDefined();
|
||||||
|
expect(statusJson.media_attachments).toBeDefined();
|
||||||
|
expect(statusJson.poll).toBeDefined();
|
||||||
|
expect(statusJson.card).toBeDefined();
|
||||||
|
expect(statusJson.visibility).toBeDefined();
|
||||||
|
expect(statusJson.sensitive).toBeDefined();
|
||||||
|
expect(statusJson.spoiler_text).toBeDefined();
|
||||||
|
expect(statusJson.uri).toBeDefined();
|
||||||
|
expect(statusJson.url).toBeDefined();
|
||||||
|
expect(statusJson.replies_count).toBeDefined();
|
||||||
|
expect(statusJson.reblogs_count).toBeDefined();
|
||||||
|
expect(statusJson.favourites_count).toBeDefined();
|
||||||
|
expect(statusJson.favourited).toBeDefined();
|
||||||
|
expect(statusJson.reblogged).toBeDefined();
|
||||||
|
expect(statusJson.muted).toBeDefined();
|
||||||
|
expect(statusJson.bookmarked).toBeDefined();
|
||||||
|
expect(statusJson.pinned).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("DELETE /api/v1/statuses/:id", () => {
|
||||||
|
test("should delete the specified status object", async () => {
|
||||||
|
const response = await fetch(
|
||||||
|
`${config.http.base_url}/api/v1/statuses/${status?.id}`,
|
||||||
|
{
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token.access_token}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(response.status).toBe(200);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
const activities = await RawActivity.createQueryBuilder("activity")
|
const activities = await RawActivity.createQueryBuilder("activity")
|
||||||
.where("activity.data->>'actor' = :actor", {
|
.where("activity.data->>'actor' = :actor", {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue