refactor: ♻️ Always use explicit types in every function

This commit is contained in:
Jesse Wierzbinski 2024-11-02 00:43:33 +01:00
parent 54cea29ce9
commit c1dcdc78ae
No known key found for this signature in database
62 changed files with 359 additions and 226 deletions

View file

@ -82,7 +82,8 @@ export class Application extends BaseInterface<typeof Applications> {
token: string,
): Promise<Application | null> {
const result = await db.query.Tokens.findFirst({
where: (tokens, { eq }) => eq(tokens.accessToken, token),
where: (tokens, { eq }): SQL | undefined =>
eq(tokens.accessToken, token),
with: {
application: true,
},
@ -141,7 +142,7 @@ export class Application extends BaseInterface<typeof Applications> {
return application;
}
public get id() {
public get id(): string {
return this.data.id;
}

View file

@ -148,11 +148,11 @@ export class Attachment extends BaseInterface<typeof Attachments> {
return attachment;
}
public get id() {
public get id(): string {
return this.data.id;
}
public static getUrl(name: string) {
public static getUrl(name: string): string {
if (config.media.backend === MediaBackendType.Local) {
return new URL(`/media/${name}`, config.http.base_url).toString();
}

View file

@ -160,7 +160,7 @@ export class Emoji extends BaseInterface<typeof Emojis, EmojiWithInstance> {
return await Emoji.fromVersia(emojiToFetch, foundInstance?.id ?? null);
}
public get id() {
public get id(): string {
return this.data.id;
}

View file

@ -132,7 +132,7 @@ export class Instance extends BaseInterface<typeof Instances> {
return instance;
}
public get id() {
public get id(): string {
return this.data.id;
}

View file

@ -59,7 +59,7 @@ export class Like extends BaseInterface<typeof Likes, LikeType> {
public static async fromSql(
sql: SQL<unknown> | undefined,
orderBy: SQL<unknown> | undefined = desc(Likes.id),
) {
): Promise<Like | null> {
const found = await db.query.Likes.findFirst({
where: sql,
orderBy,
@ -81,7 +81,7 @@ export class Like extends BaseInterface<typeof Likes, LikeType> {
limit?: number,
offset?: number,
extra?: Parameters<typeof db.query.Likes.findMany>[0],
) {
): Promise<Like[]> {
const found = await db.query.Likes.findMany({
where: sql,
orderBy,
@ -135,7 +135,7 @@ export class Like extends BaseInterface<typeof Likes, LikeType> {
return role;
}
public get id() {
public get id(): string {
return this.data.id;
}

View file

@ -276,7 +276,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
return found.map((s) => new Note(s));
}
public get id() {
public get id(): string {
return this.data.id;
}
@ -319,7 +319,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
{
with: {
relationships: {
where: (relationship, { eq, and }) =>
where: (relationship, { eq, and }): SQL | undefined =>
and(
eq(relationship.subjectId, this.data.authorId),
eq(relationship.following, true),
@ -339,7 +339,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
return deduplicatedUsersById;
}
public get author() {
public get author(): User {
return new User(this.data.author);
}
@ -369,7 +369,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
);
}
public isRemote() {
public isRemote(): boolean {
return this.author.isRemote();
}
@ -846,7 +846,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
if (this.data.visibility === "private") {
return user
? !!(await db.query.Relationships.findFirst({
where: (relationship, { and, eq }) =>
where: (relationship, { and, eq }): SQL | undefined =>
and(
eq(relationship.ownerId, user?.id),
eq(relationship.subjectId, Notes.authorId),
@ -877,7 +877,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
// Rewrite all src tags to go through proxy
let replacedContent = new HTMLRewriter()
.on("[src]", {
element(element) {
element(element): void {
element.setAttribute(
"src",
proxyUrl(element.getAttribute("src") ?? "") ?? "",

View file

@ -198,7 +198,7 @@ export class Relationship extends BaseInterface<
ownerId: string;
}): Promise<RelationshipType> {
let output = await db.query.Relationships.findFirst({
where: (rel, { and, eq }) =>
where: (rel, { and, eq }): SQL | undefined =>
and(
eq(rel.ownerId, oppositeTo.subjectId),
eq(rel.subjectId, oppositeTo.ownerId),
@ -286,7 +286,7 @@ export class Relationship extends BaseInterface<
return relationship;
}
public get id() {
public get id(): string {
return this.data.id;
}

View file

@ -1,5 +1,8 @@
import { proxyUrl } from "@/response";
import { RolePermission } from "@versia/client/types";
import {
type VersiaRole as APIRole,
RolePermission,
} from "@versia/client/types";
import { db } from "@versia/kit/db";
import { RoleToUsers, Roles } from "@versia/kit/tables";
import {
@ -53,7 +56,7 @@ export class Role extends BaseInterface<typeof Roles> {
public static async fromSql(
sql: SQL<unknown> | undefined,
orderBy: SQL<unknown> | undefined = desc(Roles.id),
) {
): Promise<Role | null> {
const found = await db.query.Roles.findFirst({
where: sql,
orderBy,
@ -65,10 +68,14 @@ export class Role extends BaseInterface<typeof Roles> {
return new Role(found);
}
public static async getUserRoles(userId: string, isAdmin: boolean) {
public static async getUserRoles(
userId: string,
isAdmin: boolean,
): Promise<Role[]> {
return (
await db.query.RoleToUsers.findMany({
where: (role, { eq }) => eq(role.userId, userId),
where: (role, { eq }): SQL | undefined =>
eq(role.userId, userId),
with: {
role: true,
user: {
@ -115,7 +122,7 @@ export class Role extends BaseInterface<typeof Roles> {
limit?: number,
offset?: number,
extra?: Parameters<typeof db.query.Roles.findMany>[0],
) {
): Promise<Role[]> {
const found = await db.query.Roles.findMany({
where: sql,
orderBy,
@ -165,14 +172,14 @@ export class Role extends BaseInterface<typeof Roles> {
return role;
}
public async linkUser(userId: string) {
public async linkUser(userId: string): Promise<void> {
await db.insert(RoleToUsers).values({
userId,
roleId: this.id,
});
}
public async unlinkUser(userId: string) {
public async unlinkUser(userId: string): Promise<void> {
await db
.delete(RoleToUsers)
.where(
@ -183,11 +190,11 @@ export class Role extends BaseInterface<typeof Roles> {
);
}
public get id() {
public get id(): string {
return this.data.id;
}
public toApi() {
public toApi(): APIRole {
return {
id: this.id,
name: this.data.name,

View file

@ -17,7 +17,7 @@ export class Timeline<Type extends Note | User> {
limit: number,
url: string,
userId?: string,
) {
): Promise<{ link: string; objects: Note[] }> {
return new Timeline<Note>(TimelineType.Note).fetchTimeline(
sql,
limit,
@ -30,7 +30,7 @@ export class Timeline<Type extends Note | User> {
sql: SQL<unknown> | undefined,
limit: number,
url: string,
) {
): Promise<{ link: string; objects: User[] }> {
return new Timeline<User>(TimelineType.User).fetchTimeline(
sql,
limit,

View file

@ -150,7 +150,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
public static async fromSql(
sql: SQL<unknown> | undefined,
orderBy: SQL<unknown> | undefined = desc(Users.id),
) {
): Promise<User | null> {
const found = await findManyUsers({
where: sql,
orderBy,
@ -168,7 +168,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
limit?: number,
offset?: number,
extra?: Parameters<typeof db.query.Users.findMany>[0],
) {
): Promise<User[]> {
const found = await findManyUsers({
where: sql,
orderBy,
@ -180,34 +180,38 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
return found.map((s) => new User(s));
}
public get id() {
public get id(): string {
return this.data.id;
}
public isLocal() {
public isLocal(): boolean {
return this.data.instanceId === null;
}
public isRemote() {
public isRemote(): boolean {
return !this.isLocal();
}
public getUri() {
public getUri(): string {
return (
this.data.uri ||
new URL(`/users/${this.data.id}`, config.http.base_url).toString()
);
}
public static getUri(id: string, uri: string | null, baseUrl: string) {
public static getUri(
id: string,
uri: string | null,
baseUrl: string,
): string {
return uri || new URL(`/users/${id}`, baseUrl).toString();
}
public hasPermission(permission: RolePermissions) {
public hasPermission(permission: RolePermissions): boolean {
return this.getAllPermissions().includes(permission);
}
public getAllPermissions() {
public getAllPermissions(): RolePermissions[] {
return (
this.data.roles
.flatMap((role) => role.permissions)
@ -270,7 +274,10 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
return foundRelationship;
}
public async unfollow(followee: User, relationship: Relationship) {
public async unfollow(
followee: User,
relationship: Relationship,
): Promise<boolean> {
if (followee.isRemote()) {
// TODO: This should reschedule for a later time and maybe notify the server admin if it fails too often
const { ok } = await this.federateToUser(
@ -348,7 +355,9 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
return db.$count(Users, isNull(Users.instanceId));
}
public static async getActiveInPeriod(milliseconds: number) {
public static async getActiveInPeriod(
milliseconds: number,
): Promise<number> {
return (
await db
.select({
@ -376,7 +385,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
}
}
public async resetPassword() {
public async resetPassword(): Promise<string> {
const resetToken = randomString(32, "hex");
await this.update({
@ -386,30 +395,22 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
return resetToken;
}
public async pin(note: Note) {
return (
await db
.insert(UserToPinnedNotes)
.values({
noteId: note.id,
userId: this.id,
})
.returning()
)[0];
public async pin(note: Note): Promise<void> {
await db.insert(UserToPinnedNotes).values({
noteId: note.id,
userId: this.id,
});
}
public async unpin(note: Note) {
return (
await db
.delete(UserToPinnedNotes)
.where(
and(
eq(NoteToMentions.noteId, note.id),
eq(NoteToMentions.userId, this.id),
),
)
.returning()
)[0];
public async unpin(note: Note): Promise<void> {
await db
.delete(UserToPinnedNotes)
.where(
and(
eq(NoteToMentions.noteId, note.id),
eq(NoteToMentions.userId, this.id),
),
);
}
public save(): Promise<UserWithRelations> {
@ -434,7 +435,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
> {
// Get all linked accounts
const accounts = await db.query.OpenIdAccounts.findMany({
where: (User, { eq }) => eq(User.userId, this.id),
where: (User, { eq }): SQL | undefined => eq(User.userId, this.id),
});
return accounts
@ -715,7 +716,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
* @param config The config to use
* @returns The raw URL for the user's avatar
*/
public getAvatarUrl(config: Config) {
public getAvatarUrl(config: Config): string {
if (!this.data.avatar) {
return (
config.defaults.avatar ||
@ -725,7 +726,10 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
return this.data.avatar;
}
public static async generateKeys() {
public static async generateKeys(): Promise<{
private_key: string;
public_key: string;
}> {
const keys = await crypto.subtle.generateKey("Ed25519", true, [
"sign",
"verify",
@ -807,14 +811,14 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
* @param config The config to use
* @returns The raw URL for the user's header
*/
public getHeaderUrl(config: Config) {
public getHeaderUrl(config: Config): string {
if (!this.data.header) {
return config.defaults.header || "";
}
return this.data.header;
}
public getAcct() {
public getAcct(): string {
return this.isLocal()
? this.data.username
: `${this.data.username}@${this.data.instance?.baseUrl}`;
@ -824,7 +828,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
isLocal: boolean,
username: string,
baseUrl?: string,
) {
): string {
return isLocal ? username : `${username}@${baseUrl}`;
}