diff --git a/.eslintrc.cjs b/.eslintrc.cjs index ad661a7a..8282912f 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -3,6 +3,7 @@ module.exports = { "eslint:recommended", "plugin:@typescript-eslint/strict-type-checked", "plugin:@typescript-eslint/stylistic", + "plugin:prettier/recommended", ], parser: "@typescript-eslint/parser", parserOptions: { diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..af7d3b67 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,9 @@ +{ + "tabWidth": 4, + "useTabs": true, + "arrowParens": "avoid", + "bracketSameLine": true, + "bracketSpacing": true, + "jsxSingleQuote": false, + "trailingComma": "es5" +} diff --git a/bun.lockb b/bun.lockb index bd57f75f..ffc1c522 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/database/entities/Application.ts b/database/entities/Application.ts index 10eb7fef..b70a7562 100644 --- a/database/entities/Application.ts +++ b/database/entities/Application.ts @@ -30,6 +30,6 @@ export class Application extends BaseEntity { name: "", website: null, vapid_key: null, - } + }; } -} \ No newline at end of file +} diff --git a/database/entities/Emoji.ts b/database/entities/Emoji.ts index 53b59691..920c0518 100644 --- a/database/entities/Emoji.ts +++ b/database/entities/Emoji.ts @@ -25,6 +25,6 @@ export class Emoji extends BaseEntity { url: "", visible_in_picker: false, category: undefined, - } + }; } -} \ No newline at end of file +} diff --git a/database/entities/Favourite.ts b/database/entities/Favourite.ts index 2c61fe9e..c71297f0 100644 --- a/database/entities/Favourite.ts +++ b/database/entities/Favourite.ts @@ -1,4 +1,10 @@ -import { BaseEntity, Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; +import { + BaseEntity, + Column, + Entity, + ManyToOne, + PrimaryGeneratedColumn, +} from "typeorm"; import { User } from "./User"; import { Status } from "./Status"; @@ -12,12 +18,12 @@ export class Favourite extends BaseEntity { @PrimaryGeneratedColumn("uuid") id!: string; - @ManyToOne(() => User, (user) => user.id) + @ManyToOne(() => User, user => user.id) actor!: User; - @ManyToOne(() => Status, (status) => status.id) + @ManyToOne(() => Status, status => status.id) object!: Status; @Column("datetime") published!: Date; -} \ No newline at end of file +} diff --git a/database/entities/Instance.ts b/database/entities/Instance.ts index c54aff58..6d0ac79d 100644 --- a/database/entities/Instance.ts +++ b/database/entities/Instance.ts @@ -1,4 +1,10 @@ -import { BaseEntity, Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; +import { + BaseEntity, + Column, + Entity, + ManyToOne, + PrimaryGeneratedColumn, +} from "typeorm"; import { APIInstance } from "~types/entities/instance"; import { User } from "./User"; @@ -9,7 +15,7 @@ export class Instance extends BaseEntity { @PrimaryGeneratedColumn("uuid") id!: string; - @ManyToOne(() => User, (user) => user.id) + @ManyToOne(() => User, user => user.id) contact_account!: User; @Column("jsonb", { @@ -63,4 +69,4 @@ export class Instance extends BaseEntity { max_toot_chars: 0, }; } -} \ No newline at end of file +} diff --git a/database/entities/RawObject.ts b/database/entities/RawObject.ts index 983b100f..61477fc0 100644 --- a/database/entities/RawObject.ts +++ b/database/entities/RawObject.ts @@ -1,7 +1,5 @@ import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm"; -import { - APObject, -} from "activitypub-types"; +import { APObject } from "activitypub-types"; /** * Stores an ActivityPub object as raw JSON-LD data @@ -15,4 +13,4 @@ export class RawObject extends BaseEntity { @Column("json") data!: APObject; -} \ No newline at end of file +} diff --git a/database/entities/Renote.ts b/database/entities/Renote.ts index 45f789b0..b4c9d782 100644 --- a/database/entities/Renote.ts +++ b/database/entities/Renote.ts @@ -18,10 +18,10 @@ export class Renote extends BaseEntity { @PrimaryGeneratedColumn("uuid") id!: string; - @ManyToOne(() => User, (user) => user.id) + @ManyToOne(() => User, user => user.id) actor!: User; - @ManyToOne(() => Status, (status) => status.id) + @ManyToOne(() => Status, status => status.id) object!: Status; @Column("datetime") diff --git a/database/entities/Status.ts b/database/entities/Status.ts index ee6a2a68..42c2318f 100644 --- a/database/entities/Status.ts +++ b/database/entities/Status.ts @@ -27,7 +27,7 @@ export class Status extends BaseEntity { @PrimaryGeneratedColumn("uuid") id!: string; - @ManyToOne(() => User, (user) => user.id) + @ManyToOne(() => User, user => user.id) account!: User; @CreateDateColumn() @@ -36,7 +36,7 @@ export class Status extends BaseEntity { @UpdateDateColumn() updated_at!: Date; - @ManyToOne(() => Status, (status) => status.id, { + @ManyToOne(() => Status, status => status.id, { nullable: true, }) reblog?: Status; @@ -60,31 +60,33 @@ export class Status extends BaseEntity { }) spoiler_text!: string; - @ManyToOne(() => Application, (app) => app.id, { - nullable: true + @ManyToOne(() => Application, app => app.id, { + nullable: true, }) application!: Application | null; - @ManyToMany(() => Emoji, (emoji) => emoji.id) + @ManyToMany(() => Emoji, emoji => emoji.id) emojis!: Emoji[]; async getFavourites(): Promise { return Favourite.find({ where: { object: { - id: this.id + id: this.id, }, }, }); } - + async toAPI(): Promise { return { account: await this.account.toAPI(), - application: await this.application?.toAPI() ?? null, + application: (await this.application?.toAPI()) ?? null, bookmarked: false, created_at: this.created_at.toISOString(), - emojis: await Promise.all(this.emojis.map(async (emoji) => await emoji.toAPI())), + emojis: await Promise.all( + this.emojis.map(async emoji => await emoji.toAPI()) + ), favourited: false, favourites_count: (await this.getFavourites()).length, id: this.id, @@ -96,7 +98,7 @@ export class Status extends BaseEntity { muted: false, pinned: false, poll: null, - reblog: this.isReblog ? await this.reblog?.toAPI() ?? null : null, + reblog: this.isReblog ? (await this.reblog?.toAPI()) ?? null : null, reblogged: false, reblogs_count: 0, replies_count: 0, diff --git a/database/entities/User.ts b/database/entities/User.ts index 6a8c3e4b..2e0e6cf9 100644 --- a/database/entities/User.ts +++ b/database/entities/User.ts @@ -1,5 +1,12 @@ import { getConfig, getHost } from "@config"; -import { BaseEntity, Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm"; +import { + BaseEntity, + Column, + CreateDateColumn, + Entity, + PrimaryGeneratedColumn, + UpdateDateColumn, +} from "typeorm"; import { APIAccount } from "~types/entities/account"; const config = getConfig(); @@ -73,6 +80,6 @@ export class User extends BaseEntity { discoverable: undefined, role: undefined, mute_expires_at: undefined, - } + }; } -} \ No newline at end of file +} diff --git a/index.ts b/index.ts index f44d8fde..3845ce40 100644 --- a/index.ts +++ b/index.ts @@ -4,7 +4,7 @@ import "reflect-metadata"; const router = new Bun.FileSystemRouter({ style: "nextjs", dir: process.cwd() + "/server/api", -}) +}); console.log("[+] Starting FediProject..."); @@ -18,7 +18,10 @@ Bun.serve({ if (matchedRoute) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - return (await import(matchedRoute.filePath)).default(req, matchedRoute) as Response | Promise; + return (await import(matchedRoute.filePath)).default( + req, + matchedRoute + ) as Response | Promise; } else { return new Response(undefined, { status: 404, @@ -28,4 +31,4 @@ Bun.serve({ }, }); -console.log("[+] FediProject started!") +console.log("[+] FediProject started!"); diff --git a/package.json b/package.json index 5056ff83..a5702f7c 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,11 @@ "activitypub-types": "^1.0.3", "bun-types": "latest", "eslint": "^8.49.0", + "eslint-config-prettier": "^9.0.0", + "eslint-formatter-pretty": "^5.0.0", + "eslint-formatter-summary": "^1.1.0", + "eslint-plugin-prettier": "^5.0.0", + "prettier": "^3.0.3", "typescript": "^5.2.2" }, "peerDependencies": { diff --git a/server/api/@[username]/outbox.json/index.ts b/server/api/@[username]/outbox.json/index.ts index 9e45b6e1..fde4224c 100644 --- a/server/api/@[username]/outbox.json/index.ts +++ b/server/api/@[username]/outbox.json/index.ts @@ -42,19 +42,21 @@ export default async ( }, }); - const lastPost = (await RawObject.find({ - where: { - data: { - attributedTo: `${getHost()}/@${user.username}`, + const lastPost = ( + await RawObject.find({ + where: { + data: { + attributedTo: `${getHost()}/@${user.username}`, + }, }, - }, - order: { - data: { - published: "ASC", + order: { + data: { + published: "ASC", + }, }, - }, - take: 1, - }))[0]; + take: 1, + }) + )[0]; if (!page) return jsonLdResponse( @@ -73,7 +75,7 @@ export default async ( }) ); else { - let posts: RawObject[] = [] + let posts: RawObject[] = []; if (min_id) { posts = await RawObject.find({ @@ -107,8 +109,6 @@ export default async ( }); } - - return jsonLdResponse( await compact({ "@context": [ @@ -145,7 +145,7 @@ export default async ( }&page=true`, orderedItems: posts .slice(0, 10) - .map((post) => post.data) as NodeObject[], + .map(post => post.data) as NodeObject[], }) ); } diff --git a/server/api/object/[uuid]/index.ts b/server/api/object/[uuid]/index.ts index db1a4dec..bcf6e206 100644 --- a/server/api/object/[uuid]/index.ts +++ b/server/api/object/[uuid]/index.ts @@ -10,10 +10,10 @@ export default async ( matchedRoute: MatchedRoute ): Promise => { const object = await RawObject.findOneBy({ - id: matchedRoute.params.id + id: matchedRoute.params.id, }); - if (!object) return errorResponse("Object not found", 404) + if (!object) return errorResponse("Object not found", 404); return jsonLdResponse(object); }; diff --git a/server/api/v1/accounts/[id]/index.ts b/server/api/v1/accounts/[id]/index.ts index a4102bb8..d4430b15 100644 --- a/server/api/v1/accounts/[id]/index.ts +++ b/server/api/v1/accounts/[id]/index.ts @@ -15,8 +15,7 @@ export default async ( id, }); - if (!user) - return errorResponse("User not found", 404) + if (!user) return errorResponse("User not found", 404); return jsonResponse(user.toAPI()); }; diff --git a/utils/config.ts b/utils/config.ts index fa6699f4..67a33205 100644 --- a/utils/config.ts +++ b/utils/config.ts @@ -7,20 +7,20 @@ export interface ConfigType { username: string; password: string; database: string; - } + }; http: { port: number; base_url: string; - } - [ key: string ]: unknown; + }; + [key: string]: unknown; } export const getConfig = () => { return data as ConfigType; -} +}; export const getHost = () => { const url = new URL(getConfig().http.base_url); return url.host; -} \ No newline at end of file +}; diff --git a/utils/response.ts b/utils/response.ts index 6ec44486..17e29fcf 100644 --- a/utils/response.ts +++ b/utils/response.ts @@ -4,11 +4,11 @@ import { NodeObject } from "jsonld"; export const jsonResponse = (data: object, status = 200) => { return new Response(JSON.stringify(data), { headers: { - "Content-Type": "application/json" + "Content-Type": "application/json", }, status, }); -} +}; export const jsonLdResponse = (data: NodeObject | APObject, status = 200) => { return new Response(JSON.stringify(data), { @@ -20,7 +20,10 @@ export const jsonLdResponse = (data: NodeObject | APObject, status = 200) => { }; export const errorResponse = (error: string, status = 500) => { - return jsonResponse({ - error: error - }, status); -} \ No newline at end of file + return jsonResponse( + { + error: error, + }, + status + ); +};