Add stricter ESLint rules

This commit is contained in:
Jesse Wierzbinski 2023-09-12 14:29:13 -10:00
parent 887128356e
commit e618996936
No known key found for this signature in database
GPG key ID: F9A1E418934E40B0
18 changed files with 102 additions and 63 deletions

View file

@ -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: {

9
.prettierrc Normal file
View file

@ -0,0 +1,9 @@
{
"tabWidth": 4,
"useTabs": true,
"arrowParens": "avoid",
"bracketSameLine": true,
"bracketSpacing": true,
"jsxSingleQuote": false,
"trailingComma": "es5"
}

BIN
bun.lockb

Binary file not shown.

View file

@ -30,6 +30,6 @@ export class Application extends BaseEntity {
name: "",
website: null,
vapid_key: null,
}
};
}
}

View file

@ -25,6 +25,6 @@ export class Emoji extends BaseEntity {
url: "",
visible_in_picker: false,
category: undefined,
}
};
}
}

View file

@ -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,10 +18,10 @@ 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")

View file

@ -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", {

View file

@ -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

View file

@ -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")

View file

@ -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,19 +60,19 @@ 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<Favourite[]> {
return Favourite.find({
where: {
object: {
id: this.id
id: this.id,
},
},
});
@ -81,10 +81,12 @@ export class Status extends BaseEntity {
async toAPI(): Promise<APIStatus> {
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,

View file

@ -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,
}
};
}
}

View file

@ -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<Response>;
return (await import(matchedRoute.filePath)).default(
req,
matchedRoute
) as Response | Promise<Response>;
} else {
return new Response(undefined, {
status: 404,
@ -28,4 +31,4 @@ Bun.serve({
},
});
console.log("[+] FediProject started!")
console.log("[+] FediProject started!");

View file

@ -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": {

View file

@ -42,7 +42,8 @@ export default async (
},
});
const lastPost = (await RawObject.find({
const lastPost = (
await RawObject.find({
where: {
data: {
attributedTo: `${getHost()}/@${user.username}`,
@ -54,7 +55,8 @@ export default async (
},
},
take: 1,
}))[0];
})
)[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[],
})
);
}

View file

@ -10,10 +10,10 @@ export default async (
matchedRoute: MatchedRoute
): Promise<Response> => {
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);
};

View file

@ -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());
};

View file

@ -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;
}
};

View file

@ -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);
}
return jsonResponse(
{
error: error,
},
status
);
};