mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 22:09:16 +01:00
refactor: 🚨 Always explicitely state member accessibility
This commit is contained in:
parent
7a73b8db91
commit
54cea29ce9
36 changed files with 227 additions and 196 deletions
|
|
@ -15,7 +15,7 @@ import { BaseInterface } from "./base.ts";
|
|||
export type ApplicationType = InferSelectModel<typeof Applications>;
|
||||
|
||||
export class Application extends BaseInterface<typeof Applications> {
|
||||
static schema: z.ZodType<APIApplication> = z.object({
|
||||
public static schema: z.ZodType<APIApplication> = z.object({
|
||||
name: z.string(),
|
||||
website: z.string().url().optional().nullable(),
|
||||
vapid_key: z.string().optional().nullable(),
|
||||
|
|
@ -23,7 +23,7 @@ export class Application extends BaseInterface<typeof Applications> {
|
|||
scopes: z.string().optional(),
|
||||
});
|
||||
|
||||
async reload(): Promise<void> {
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Application.fromId(this.data.id);
|
||||
|
||||
if (!reloaded) {
|
||||
|
|
@ -95,7 +95,7 @@ export class Application extends BaseInterface<typeof Applications> {
|
|||
return Application.fromSql(eq(Applications.clientId, clientId));
|
||||
}
|
||||
|
||||
async update(
|
||||
public async update(
|
||||
newApplication: Partial<ApplicationType>,
|
||||
): Promise<ApplicationType> {
|
||||
await db
|
||||
|
|
@ -113,11 +113,11 @@ export class Application extends BaseInterface<typeof Applications> {
|
|||
return updated.data;
|
||||
}
|
||||
|
||||
save(): Promise<ApplicationType> {
|
||||
public save(): Promise<ApplicationType> {
|
||||
return this.update(this.data);
|
||||
}
|
||||
|
||||
async delete(ids?: string[]): Promise<void> {
|
||||
public async delete(ids?: string[]): Promise<void> {
|
||||
if (Array.isArray(ids)) {
|
||||
await db.delete(Applications).where(inArray(Applications.id, ids));
|
||||
} else {
|
||||
|
|
@ -141,7 +141,7 @@ export class Application extends BaseInterface<typeof Applications> {
|
|||
return application;
|
||||
}
|
||||
|
||||
get id() {
|
||||
public get id() {
|
||||
return this.data.id;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ import { BaseInterface } from "./base.ts";
|
|||
export type AttachmentType = InferSelectModel<typeof Attachments>;
|
||||
|
||||
export class Attachment extends BaseInterface<typeof Attachments> {
|
||||
static schema: z.ZodType<ApiAttachment> = z.object({
|
||||
public static schema: z.ZodType<ApiAttachment> = z.object({
|
||||
id: z.string().uuid(),
|
||||
type: z.enum(["unknown", "image", "gifv", "video", "audio"]),
|
||||
url: z.string().url(),
|
||||
|
|
@ -47,7 +47,7 @@ export class Attachment extends BaseInterface<typeof Attachments> {
|
|||
blurhash: z.string().nullable(),
|
||||
});
|
||||
|
||||
async reload(): Promise<void> {
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Attachment.fromId(this.data.id);
|
||||
|
||||
if (!reloaded) {
|
||||
|
|
@ -102,7 +102,7 @@ export class Attachment extends BaseInterface<typeof Attachments> {
|
|||
return found.map((s) => new Attachment(s));
|
||||
}
|
||||
|
||||
async update(
|
||||
public async update(
|
||||
newAttachment: Partial<AttachmentType>,
|
||||
): Promise<AttachmentType> {
|
||||
await db
|
||||
|
|
@ -120,11 +120,11 @@ export class Attachment extends BaseInterface<typeof Attachments> {
|
|||
return updated.data;
|
||||
}
|
||||
|
||||
save(): Promise<AttachmentType> {
|
||||
public save(): Promise<AttachmentType> {
|
||||
return this.update(this.data);
|
||||
}
|
||||
|
||||
async delete(ids?: string[]): Promise<void> {
|
||||
public async delete(ids?: string[]): Promise<void> {
|
||||
if (Array.isArray(ids)) {
|
||||
await db.delete(Attachments).where(inArray(Attachments.id, ids));
|
||||
} else {
|
||||
|
|
@ -148,7 +148,7 @@ export class Attachment extends BaseInterface<typeof Attachments> {
|
|||
return attachment;
|
||||
}
|
||||
|
||||
get id() {
|
||||
public get id() {
|
||||
return this.data.id;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ export abstract class BaseInterface<
|
|||
*
|
||||
* @param data - The data for the model.
|
||||
*/
|
||||
constructor(public data: Columns) {}
|
||||
public constructor(public data: Columns) {}
|
||||
|
||||
/**
|
||||
* Saves the current state of the model to the database.
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ export type EmojiWithInstance = InferSelectModel<typeof Emojis> & {
|
|||
};
|
||||
|
||||
export class Emoji extends BaseInterface<typeof Emojis, EmojiWithInstance> {
|
||||
static schema = z.object({
|
||||
public static schema = z.object({
|
||||
shortcode: z.string(),
|
||||
url: z.string(),
|
||||
visible_in_picker: z.boolean(),
|
||||
|
|
@ -30,7 +30,7 @@ export class Emoji extends BaseInterface<typeof Emojis, EmojiWithInstance> {
|
|||
static_url: z.string(),
|
||||
});
|
||||
|
||||
async reload(): Promise<void> {
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Emoji.fromId(this.data.id);
|
||||
|
||||
if (!reloaded) {
|
||||
|
|
@ -88,7 +88,7 @@ export class Emoji extends BaseInterface<typeof Emojis, EmojiWithInstance> {
|
|||
return found.map((s) => new Emoji(s));
|
||||
}
|
||||
|
||||
async update(
|
||||
public async update(
|
||||
newEmoji: Partial<EmojiWithInstance>,
|
||||
): Promise<EmojiWithInstance> {
|
||||
await db.update(Emojis).set(newEmoji).where(eq(Emojis.id, this.id));
|
||||
|
|
@ -103,11 +103,11 @@ export class Emoji extends BaseInterface<typeof Emojis, EmojiWithInstance> {
|
|||
return updated.data;
|
||||
}
|
||||
|
||||
save(): Promise<EmojiWithInstance> {
|
||||
public save(): Promise<EmojiWithInstance> {
|
||||
return this.update(this.data);
|
||||
}
|
||||
|
||||
async delete(ids?: string[]): Promise<void> {
|
||||
public async delete(ids?: string[]): Promise<void> {
|
||||
if (Array.isArray(ids)) {
|
||||
await db.delete(Emojis).where(inArray(Emojis.id, ids));
|
||||
} else {
|
||||
|
|
@ -160,7 +160,7 @@ export class Emoji extends BaseInterface<typeof Emojis, EmojiWithInstance> {
|
|||
return await Emoji.fromVersia(emojiToFetch, foundInstance?.id ?? null);
|
||||
}
|
||||
|
||||
get id() {
|
||||
public get id() {
|
||||
return this.data.id;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ import { User } from "./user.ts";
|
|||
export type InstanceType = InferSelectModel<typeof Instances>;
|
||||
|
||||
export class Instance extends BaseInterface<typeof Instances> {
|
||||
async reload(): Promise<void> {
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Instance.fromId(this.data.id);
|
||||
|
||||
if (!reloaded) {
|
||||
|
|
@ -78,7 +78,9 @@ export class Instance extends BaseInterface<typeof Instances> {
|
|||
return found.map((s) => new Instance(s));
|
||||
}
|
||||
|
||||
async update(newInstance: Partial<InstanceType>): Promise<InstanceType> {
|
||||
public async update(
|
||||
newInstance: Partial<InstanceType>,
|
||||
): Promise<InstanceType> {
|
||||
await db
|
||||
.update(Instances)
|
||||
.set(newInstance)
|
||||
|
|
@ -94,11 +96,11 @@ export class Instance extends BaseInterface<typeof Instances> {
|
|||
return updated.data;
|
||||
}
|
||||
|
||||
save(): Promise<InstanceType> {
|
||||
public save(): Promise<InstanceType> {
|
||||
return this.update(this.data);
|
||||
}
|
||||
|
||||
async delete(ids?: string[]): Promise<void> {
|
||||
public async delete(ids?: string[]): Promise<void> {
|
||||
if (Array.isArray(ids)) {
|
||||
await db.delete(Instances).where(inArray(Instances.id, ids));
|
||||
} else {
|
||||
|
|
@ -130,11 +132,11 @@ export class Instance extends BaseInterface<typeof Instances> {
|
|||
return instance;
|
||||
}
|
||||
|
||||
get id() {
|
||||
public get id() {
|
||||
return this.data.id;
|
||||
}
|
||||
|
||||
static async fetchMetadata(url: string): Promise<{
|
||||
public static async fetchMetadata(url: string): Promise<{
|
||||
metadata: InstanceMetadata;
|
||||
protocol: "versia" | "activitypub";
|
||||
} | null> {
|
||||
|
|
@ -315,7 +317,7 @@ export class Instance extends BaseInterface<typeof Instances> {
|
|||
}
|
||||
}
|
||||
|
||||
static async resolve(url: string): Promise<Instance> {
|
||||
public static async resolve(url: string): Promise<Instance> {
|
||||
const logger = getLogger("federation");
|
||||
const host = new URL(url).host;
|
||||
|
||||
|
|
@ -345,7 +347,7 @@ export class Instance extends BaseInterface<typeof Instances> {
|
|||
});
|
||||
}
|
||||
|
||||
static getCount(): Promise<number> {
|
||||
public static getCount(): Promise<number> {
|
||||
return db.$count(Instances);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ export type LikeType = InferSelectModel<typeof Likes> & {
|
|||
};
|
||||
|
||||
export class Like extends BaseInterface<typeof Likes, LikeType> {
|
||||
static schema = z.object({
|
||||
public static schema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
permissions: z.array(z.nativeEnum(RolePermission)),
|
||||
|
|
@ -34,7 +34,7 @@ export class Like extends BaseInterface<typeof Likes, LikeType> {
|
|||
icon: z.string().nullable(),
|
||||
});
|
||||
|
||||
async reload(): Promise<void> {
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Like.fromId(this.data.id);
|
||||
|
||||
if (!reloaded) {
|
||||
|
|
@ -97,7 +97,7 @@ export class Like extends BaseInterface<typeof Likes, LikeType> {
|
|||
return found.map((s) => new Like(s));
|
||||
}
|
||||
|
||||
async update(newRole: Partial<LikeType>): Promise<LikeType> {
|
||||
public async update(newRole: Partial<LikeType>): Promise<LikeType> {
|
||||
await db.update(Likes).set(newRole).where(eq(Likes.id, this.id));
|
||||
|
||||
const updated = await Like.fromId(this.data.id);
|
||||
|
|
@ -109,11 +109,11 @@ export class Like extends BaseInterface<typeof Likes, LikeType> {
|
|||
return updated.data;
|
||||
}
|
||||
|
||||
save(): Promise<LikeType> {
|
||||
public save(): Promise<LikeType> {
|
||||
return this.update(this.data);
|
||||
}
|
||||
|
||||
async delete(ids?: string[]): Promise<void> {
|
||||
public async delete(ids?: string[]): Promise<void> {
|
||||
if (Array.isArray(ids)) {
|
||||
await db.delete(Likes).where(inArray(Likes.id, ids));
|
||||
} else {
|
||||
|
|
@ -135,7 +135,7 @@ export class Like extends BaseInterface<typeof Likes, LikeType> {
|
|||
return role;
|
||||
}
|
||||
|
||||
get id() {
|
||||
public get id() {
|
||||
return this.data.id;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ import { User } from "./user.ts";
|
|||
* Gives helpers to fetch notes from database in a nice format
|
||||
*/
|
||||
export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
||||
static schema: z.ZodType<ApiStatus> = z.object({
|
||||
public static schema: z.ZodType<ApiStatus> = z.object({
|
||||
id: z.string().uuid(),
|
||||
uri: z.string().url(),
|
||||
url: z.string().url(),
|
||||
|
|
@ -143,14 +143,14 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
bookmarked: z.boolean(),
|
||||
});
|
||||
|
||||
save(): Promise<StatusWithRelations> {
|
||||
public save(): Promise<StatusWithRelations> {
|
||||
return this.update(this.data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param userRequestingNoteId Used to calculate visibility of the note
|
||||
*/
|
||||
async reload(userRequestingNoteId?: string): Promise<void> {
|
||||
public async reload(userRequestingNoteId?: string): Promise<void> {
|
||||
const reloaded = await Note.fromId(this.data.id, userRequestingNoteId);
|
||||
|
||||
if (!reloaded) {
|
||||
|
|
@ -187,7 +187,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param userRequestingNoteId - The ID of the user requesting the note (used to check visibility of the note)
|
||||
* @returns The fetched note
|
||||
*/
|
||||
static async fromId(
|
||||
public static async fromId(
|
||||
id: string | null,
|
||||
userRequestingNoteId?: string,
|
||||
): Promise<Note | null> {
|
||||
|
|
@ -207,7 +207,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param userRequestingNoteId - The ID of the user requesting the note (used to check visibility of the note)
|
||||
* @returns The fetched notes
|
||||
*/
|
||||
static async fromIds(
|
||||
public static async fromIds(
|
||||
ids: string[],
|
||||
userRequestingNoteId?: string,
|
||||
): Promise<Note[]> {
|
||||
|
|
@ -227,7 +227,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param userId - The ID of the user requesting the note (used to check visibility of the note)
|
||||
* @returns The fetched note
|
||||
*/
|
||||
static async fromSql(
|
||||
public static async fromSql(
|
||||
sql: SQL<unknown> | undefined,
|
||||
orderBy: SQL<unknown> | undefined = desc(Notes.id),
|
||||
userId?: string,
|
||||
|
|
@ -256,7 +256,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param userId - The ID of the user requesting the note (used to check visibility of the note)
|
||||
* @returns - The fetched notes
|
||||
*/
|
||||
static async manyFromSql(
|
||||
public static async manyFromSql(
|
||||
sql: SQL<unknown> | undefined,
|
||||
orderBy: SQL<unknown> | undefined = desc(Notes.id),
|
||||
limit?: number,
|
||||
|
|
@ -276,11 +276,11 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
return found.map((s) => new Note(s));
|
||||
}
|
||||
|
||||
get id() {
|
||||
public get id() {
|
||||
return this.data.id;
|
||||
}
|
||||
|
||||
async federateToUsers(): Promise<void> {
|
||||
public async federateToUsers(): Promise<void> {
|
||||
const users = await this.getUsersToFederateTo();
|
||||
|
||||
for (const user of users) {
|
||||
|
|
@ -296,7 +296,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* - Users that can see the note
|
||||
* @returns The users that should be federated to
|
||||
*/
|
||||
async getUsersToFederateTo(): Promise<User[]> {
|
||||
public async getUsersToFederateTo(): Promise<User[]> {
|
||||
// Mentioned users
|
||||
const mentionedUsers =
|
||||
this.data.mentions.length > 0
|
||||
|
|
@ -339,7 +339,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
return deduplicatedUsersById;
|
||||
}
|
||||
|
||||
get author() {
|
||||
public get author() {
|
||||
return new User(this.data.author);
|
||||
}
|
||||
|
||||
|
|
@ -347,7 +347,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* Get the number of notes in the database (excluding remote notes)
|
||||
* @returns The number of notes in the database
|
||||
*/
|
||||
static async getCount(): Promise<number> {
|
||||
public static async getCount(): Promise<number> {
|
||||
return await db.$count(
|
||||
Notes,
|
||||
sql`EXISTS (SELECT 1 FROM "Users" WHERE "Users"."id" = ${Notes.authorId} AND "Users"."instanceId" IS NULL)`,
|
||||
|
|
@ -369,7 +369,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
);
|
||||
}
|
||||
|
||||
isRemote() {
|
||||
public isRemote() {
|
||||
return this.author.isRemote();
|
||||
}
|
||||
|
||||
|
|
@ -377,7 +377,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* Update a note from remote federated servers
|
||||
* @returns The updated note
|
||||
*/
|
||||
async updateFromRemote(): Promise<Note> {
|
||||
public async updateFromRemote(): Promise<Note> {
|
||||
if (!this.isRemote()) {
|
||||
throw new Error("Cannot refetch a local note (it is not remote)");
|
||||
}
|
||||
|
|
@ -398,7 +398,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param data - The data to create the note from
|
||||
* @returns The created note
|
||||
*/
|
||||
static async fromData(data: {
|
||||
public static async fromData(data: {
|
||||
author: User;
|
||||
content: ContentFormat;
|
||||
visibility: ApiStatus["visibility"];
|
||||
|
|
@ -488,7 +488,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param data - The data to update the note from
|
||||
* @returns The updated note
|
||||
*/
|
||||
async updateFromData(data: {
|
||||
public async updateFromData(data: {
|
||||
author: User;
|
||||
content?: ContentFormat;
|
||||
visibility?: ApiStatus["visibility"];
|
||||
|
|
@ -649,7 +649,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param uri - The URI of the note to resolve
|
||||
* @returns The resolved note
|
||||
*/
|
||||
static async resolve(uri: string): Promise<Note | null> {
|
||||
public static async resolve(uri: string): Promise<Note | null> {
|
||||
// Check if note not already in database
|
||||
const foundNote = await Note.fromSql(eq(Notes.uri, uri));
|
||||
|
||||
|
|
@ -678,7 +678,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param uri - The URI of the note to save
|
||||
* @returns The saved note, or null if the note could not be fetched
|
||||
*/
|
||||
static async saveFromRemote(uri: string): Promise<Note | null> {
|
||||
public static async saveFromRemote(uri: string): Promise<Note | null> {
|
||||
let note: VersiaNote | null = null;
|
||||
|
||||
if (uri) {
|
||||
|
|
@ -715,7 +715,10 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param author Author of the note
|
||||
* @returns The saved note
|
||||
*/
|
||||
static async fromVersia(note: VersiaNote, author: User): Promise<Note> {
|
||||
public static async fromVersia(
|
||||
note: VersiaNote,
|
||||
author: User,
|
||||
): Promise<Note> {
|
||||
const emojis: Emoji[] = [];
|
||||
const logger = getLogger("federation");
|
||||
|
||||
|
|
@ -803,7 +806,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
return await Note.fromData(newData);
|
||||
}
|
||||
|
||||
async delete(ids?: string[]): Promise<void> {
|
||||
public async delete(ids?: string[]): Promise<void> {
|
||||
if (Array.isArray(ids)) {
|
||||
await db.delete(Notes).where(inArray(Notes.id, ids));
|
||||
} else {
|
||||
|
|
@ -811,7 +814,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
}
|
||||
}
|
||||
|
||||
async update(
|
||||
public async update(
|
||||
newStatus: Partial<StatusWithRelations>,
|
||||
): Promise<StatusWithRelations> {
|
||||
await db.update(Notes).set(newStatus).where(eq(Notes.id, this.data.id));
|
||||
|
|
@ -830,7 +833,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param user The user to check.
|
||||
* @returns Whether this status is viewable by the user.
|
||||
*/
|
||||
async isViewableByUser(user: User | null): Promise<boolean> {
|
||||
public async isViewableByUser(user: User | null): Promise<boolean> {
|
||||
if (this.author.id === user?.id) {
|
||||
return true;
|
||||
}
|
||||
|
|
@ -863,7 +866,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param userFetching - The user fetching the note (used to check if the note is favourite and such)
|
||||
* @returns The note in the Mastodon API format
|
||||
*/
|
||||
async toApi(userFetching?: User | null): Promise<ApiStatus> {
|
||||
public async toApi(userFetching?: User | null): Promise<ApiStatus> {
|
||||
const data = this.data;
|
||||
|
||||
// Convert mentions of local users from @username@host to @username
|
||||
|
|
@ -957,11 +960,14 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
};
|
||||
}
|
||||
|
||||
getUri(): string {
|
||||
public getUri(): string {
|
||||
return this.data.uri || localObjectUri(this.id);
|
||||
}
|
||||
|
||||
static getUri(id: string | null, uri?: string | null): string | null {
|
||||
public static getUri(
|
||||
id: string | null,
|
||||
uri?: string | null,
|
||||
): string | null {
|
||||
if (!id) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -972,14 +978,14 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* Get the frontend URI of this note
|
||||
* @returns The frontend URI of this note
|
||||
*/
|
||||
getMastoUri(): string {
|
||||
public getMastoUri(): string {
|
||||
return new URL(
|
||||
`/@${this.author.data.username}/${this.id}`,
|
||||
config.http.base_url,
|
||||
).toString();
|
||||
}
|
||||
|
||||
deleteToVersia(): VersiaDelete {
|
||||
public deleteToVersia(): VersiaDelete {
|
||||
const id = crypto.randomUUID();
|
||||
|
||||
return {
|
||||
|
|
@ -996,7 +1002,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* Convert a note to the Versia format
|
||||
* @returns The note in the Versia format
|
||||
*/
|
||||
toVersia(): VersiaNote {
|
||||
public toVersia(): VersiaNote {
|
||||
const status = this.data;
|
||||
return {
|
||||
type: "Note",
|
||||
|
|
@ -1045,7 +1051,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param fetcher - The user fetching the ancestors
|
||||
* @returns The ancestors of this post
|
||||
*/
|
||||
async getAncestors(fetcher: User | null): Promise<Note[]> {
|
||||
public async getAncestors(fetcher: User | null): Promise<Note[]> {
|
||||
const ancestors: Note[] = [];
|
||||
|
||||
let currentStatus: Note = this;
|
||||
|
|
@ -1080,7 +1086,10 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
* @param depth - The depth of the recursion (internal)
|
||||
* @returns The descendants of this post
|
||||
*/
|
||||
async getDescendants(fetcher: User | null, depth = 0): Promise<Note[]> {
|
||||
public async getDescendants(
|
||||
fetcher: User | null,
|
||||
depth = 0,
|
||||
): Promise<Note[]> {
|
||||
const descendants: Note[] = [];
|
||||
for (const child of await this.getReplyChildren(fetcher?.id)) {
|
||||
descendants.push(child);
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ export class Relationship extends BaseInterface<
|
|||
typeof Relationships,
|
||||
RelationshipWithOpposite
|
||||
> {
|
||||
static schema = z.object({
|
||||
public static schema = z.object({
|
||||
id: z.string(),
|
||||
blocked_by: z.boolean(),
|
||||
blocking: z.boolean(),
|
||||
|
|
@ -43,7 +43,7 @@ export class Relationship extends BaseInterface<
|
|||
showing_reblogs: z.boolean(),
|
||||
});
|
||||
|
||||
async reload(): Promise<void> {
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Relationship.fromId(this.data.id);
|
||||
|
||||
if (!reloaded) {
|
||||
|
|
@ -232,7 +232,7 @@ export class Relationship extends BaseInterface<
|
|||
return output;
|
||||
}
|
||||
|
||||
async update(
|
||||
public async update(
|
||||
newRelationship: Partial<RelationshipType>,
|
||||
): Promise<RelationshipWithOpposite> {
|
||||
await db
|
||||
|
|
@ -250,11 +250,11 @@ export class Relationship extends BaseInterface<
|
|||
return updated.data;
|
||||
}
|
||||
|
||||
save(): Promise<RelationshipWithOpposite> {
|
||||
public save(): Promise<RelationshipWithOpposite> {
|
||||
return this.update(this.data);
|
||||
}
|
||||
|
||||
async delete(ids?: string[]): Promise<void> {
|
||||
public async delete(ids?: string[]): Promise<void> {
|
||||
if (Array.isArray(ids)) {
|
||||
await db
|
||||
.delete(Relationships)
|
||||
|
|
@ -286,7 +286,7 @@ export class Relationship extends BaseInterface<
|
|||
return relationship;
|
||||
}
|
||||
|
||||
get id() {
|
||||
public get id() {
|
||||
return this.data.id;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import { BaseInterface } from "./base.ts";
|
|||
export type RoleType = InferSelectModel<typeof Roles>;
|
||||
|
||||
export class Role extends BaseInterface<typeof Roles> {
|
||||
static schema = z.object({
|
||||
public static schema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
permissions: z.array(z.nativeEnum(RolePermission)),
|
||||
|
|
@ -28,7 +28,7 @@ export class Role extends BaseInterface<typeof Roles> {
|
|||
icon: z.string().nullable(),
|
||||
});
|
||||
|
||||
async reload(): Promise<void> {
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Role.fromId(this.data.id);
|
||||
|
||||
if (!reloaded) {
|
||||
|
|
@ -127,7 +127,7 @@ export class Role extends BaseInterface<typeof Roles> {
|
|||
return found.map((s) => new Role(s));
|
||||
}
|
||||
|
||||
async update(newRole: Partial<RoleType>): Promise<RoleType> {
|
||||
public async update(newRole: Partial<RoleType>): Promise<RoleType> {
|
||||
await db.update(Roles).set(newRole).where(eq(Roles.id, this.id));
|
||||
|
||||
const updated = await Role.fromId(this.data.id);
|
||||
|
|
@ -139,11 +139,11 @@ export class Role extends BaseInterface<typeof Roles> {
|
|||
return updated.data;
|
||||
}
|
||||
|
||||
save(): Promise<RoleType> {
|
||||
public save(): Promise<RoleType> {
|
||||
return this.update(this.data);
|
||||
}
|
||||
|
||||
async delete(ids?: string[]): Promise<void> {
|
||||
public async delete(ids?: string[]): Promise<void> {
|
||||
if (Array.isArray(ids)) {
|
||||
await db.delete(Roles).where(inArray(Roles.id, ids));
|
||||
} else {
|
||||
|
|
@ -183,7 +183,7 @@ export class Role extends BaseInterface<typeof Roles> {
|
|||
);
|
||||
}
|
||||
|
||||
get id() {
|
||||
public get id() {
|
||||
return this.data.id;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,9 +10,9 @@ enum TimelineType {
|
|||
}
|
||||
|
||||
export class Timeline<Type extends Note | User> {
|
||||
constructor(private type: TimelineType) {}
|
||||
public constructor(private type: TimelineType) {}
|
||||
|
||||
static getNoteTimeline(
|
||||
public static getNoteTimeline(
|
||||
sql: SQL<unknown> | undefined,
|
||||
limit: number,
|
||||
url: string,
|
||||
|
|
@ -26,7 +26,7 @@ export class Timeline<Type extends Note | User> {
|
|||
);
|
||||
}
|
||||
|
||||
static getUserTimeline(
|
||||
public static getUserTimeline(
|
||||
sql: SQL<unknown> | undefined,
|
||||
limit: number,
|
||||
url: string,
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ import { Role } from "./role.ts";
|
|||
* Gives helpers to fetch users from database in a nice format
|
||||
*/
|
||||
export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
||||
static schema: z.ZodType<ApiAccount> = z.object({
|
||||
public static schema: z.ZodType<ApiAccount> = z.object({
|
||||
id: z.string(),
|
||||
username: z.string(),
|
||||
acct: z.string(),
|
||||
|
|
@ -125,7 +125,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
mute_expires_at: z.string().optional(),
|
||||
});
|
||||
|
||||
async reload(): Promise<void> {
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await User.fromId(this.data.id);
|
||||
|
||||
if (!reloaded) {
|
||||
|
|
@ -135,7 +135,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
this.data = reloaded.data;
|
||||
}
|
||||
|
||||
static async fromId(id: string | null): Promise<User | null> {
|
||||
public static async fromId(id: string | null): Promise<User | null> {
|
||||
if (!id) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -143,11 +143,11 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
return await User.fromSql(eq(Users.id, id));
|
||||
}
|
||||
|
||||
static async fromIds(ids: string[]): Promise<User[]> {
|
||||
public static async fromIds(ids: string[]): Promise<User[]> {
|
||||
return await User.manyFromSql(inArray(Users.id, ids));
|
||||
}
|
||||
|
||||
static async fromSql(
|
||||
public static async fromSql(
|
||||
sql: SQL<unknown> | undefined,
|
||||
orderBy: SQL<unknown> | undefined = desc(Users.id),
|
||||
) {
|
||||
|
|
@ -162,7 +162,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
return new User(found[0]);
|
||||
}
|
||||
|
||||
static async manyFromSql(
|
||||
public static async manyFromSql(
|
||||
sql: SQL<unknown> | undefined,
|
||||
orderBy: SQL<unknown> | undefined = desc(Users.id),
|
||||
limit?: number,
|
||||
|
|
@ -180,26 +180,26 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
return found.map((s) => new User(s));
|
||||
}
|
||||
|
||||
get id() {
|
||||
public get id() {
|
||||
return this.data.id;
|
||||
}
|
||||
|
||||
isLocal() {
|
||||
public isLocal() {
|
||||
return this.data.instanceId === null;
|
||||
}
|
||||
|
||||
isRemote() {
|
||||
public isRemote() {
|
||||
return !this.isLocal();
|
||||
}
|
||||
|
||||
getUri() {
|
||||
public getUri() {
|
||||
return (
|
||||
this.data.uri ||
|
||||
new URL(`/users/${this.data.id}`, config.http.base_url).toString()
|
||||
);
|
||||
}
|
||||
|
||||
static getUri(id: string, uri: string | null, baseUrl: string) {
|
||||
public static getUri(id: string, uri: string | null, baseUrl: string) {
|
||||
return uri || new URL(`/users/${id}`, baseUrl).toString();
|
||||
}
|
||||
|
||||
|
|
@ -270,7 +270,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
return foundRelationship;
|
||||
}
|
||||
|
||||
async unfollow(followee: User, relationship: Relationship) {
|
||||
public async unfollow(followee: User, relationship: Relationship) {
|
||||
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(
|
||||
|
|
@ -329,7 +329,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
);
|
||||
}
|
||||
|
||||
static async webFinger(
|
||||
public static async webFinger(
|
||||
manager: FederationRequester,
|
||||
username: string,
|
||||
hostname: string,
|
||||
|
|
@ -344,11 +344,11 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
);
|
||||
}
|
||||
|
||||
static getCount(): Promise<number> {
|
||||
public static getCount(): Promise<number> {
|
||||
return db.$count(Users, isNull(Users.instanceId));
|
||||
}
|
||||
|
||||
static async getActiveInPeriod(milliseconds: number) {
|
||||
public static async getActiveInPeriod(milliseconds: number) {
|
||||
return (
|
||||
await db
|
||||
.select({
|
||||
|
|
@ -368,7 +368,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
)[0].count;
|
||||
}
|
||||
|
||||
async delete(ids?: string[]): Promise<void> {
|
||||
public async delete(ids?: string[]): Promise<void> {
|
||||
if (Array.isArray(ids)) {
|
||||
await db.delete(Users).where(inArray(Users.id, ids));
|
||||
} else {
|
||||
|
|
@ -376,7 +376,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
}
|
||||
}
|
||||
|
||||
async resetPassword() {
|
||||
public async resetPassword() {
|
||||
const resetToken = randomString(32, "hex");
|
||||
|
||||
await this.update({
|
||||
|
|
@ -386,7 +386,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
return resetToken;
|
||||
}
|
||||
|
||||
async pin(note: Note) {
|
||||
public async pin(note: Note) {
|
||||
return (
|
||||
await db
|
||||
.insert(UserToPinnedNotes)
|
||||
|
|
@ -398,7 +398,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
)[0];
|
||||
}
|
||||
|
||||
async unpin(note: Note) {
|
||||
public async unpin(note: Note) {
|
||||
return (
|
||||
await db
|
||||
.delete(UserToPinnedNotes)
|
||||
|
|
@ -412,7 +412,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
)[0];
|
||||
}
|
||||
|
||||
save(): Promise<UserWithRelations> {
|
||||
public save(): Promise<UserWithRelations> {
|
||||
return this.update(this.data);
|
||||
}
|
||||
|
||||
|
|
@ -534,7 +534,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
}
|
||||
}
|
||||
|
||||
async updateFromRemote(): Promise<User> {
|
||||
public async updateFromRemote(): Promise<User> {
|
||||
if (!this.isRemote()) {
|
||||
throw new Error(
|
||||
"Cannot refetch a local user (they are not remote)",
|
||||
|
|
@ -548,7 +548,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
return this;
|
||||
}
|
||||
|
||||
static async saveFromRemote(uri: string): Promise<User> {
|
||||
public static async saveFromRemote(uri: string): Promise<User> {
|
||||
if (!URL.canParse(uri)) {
|
||||
throw new Error(`Invalid URI: ${uri}`);
|
||||
}
|
||||
|
|
@ -618,7 +618,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
return finalUser;
|
||||
}
|
||||
|
||||
static async fromVersia(
|
||||
public static async fromVersia(
|
||||
user: VersiaUser,
|
||||
instance: Instance,
|
||||
): Promise<User> {
|
||||
|
|
@ -686,7 +686,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
return user;
|
||||
}
|
||||
|
||||
static async resolve(uri: string): Promise<User | null> {
|
||||
public static async resolve(uri: string): Promise<User | null> {
|
||||
// Check if user not already in database
|
||||
const foundUser = await User.fromSql(eq(Users.uri, uri));
|
||||
|
||||
|
|
@ -715,7 +715,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
* @param config The config to use
|
||||
* @returns The raw URL for the user's avatar
|
||||
*/
|
||||
getAvatarUrl(config: Config) {
|
||||
public getAvatarUrl(config: Config) {
|
||||
if (!this.data.avatar) {
|
||||
return (
|
||||
config.defaults.avatar ||
|
||||
|
|
@ -725,7 +725,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
return this.data.avatar;
|
||||
}
|
||||
|
||||
static async generateKeys() {
|
||||
public static async generateKeys() {
|
||||
const keys = await crypto.subtle.generateKey("Ed25519", true, [
|
||||
"sign",
|
||||
"verify",
|
||||
|
|
@ -747,7 +747,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
};
|
||||
}
|
||||
|
||||
static async fromDataLocal(data: {
|
||||
public static async fromDataLocal(data: {
|
||||
username: string;
|
||||
display_name?: string;
|
||||
password: string | undefined;
|
||||
|
|
@ -807,24 +807,28 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
* @param config The config to use
|
||||
* @returns The raw URL for the user's header
|
||||
*/
|
||||
getHeaderUrl(config: Config) {
|
||||
public getHeaderUrl(config: Config) {
|
||||
if (!this.data.header) {
|
||||
return config.defaults.header || "";
|
||||
}
|
||||
return this.data.header;
|
||||
}
|
||||
|
||||
getAcct() {
|
||||
public getAcct() {
|
||||
return this.isLocal()
|
||||
? this.data.username
|
||||
: `${this.data.username}@${this.data.instance?.baseUrl}`;
|
||||
}
|
||||
|
||||
static getAcct(isLocal: boolean, username: string, baseUrl?: string) {
|
||||
public static getAcct(
|
||||
isLocal: boolean,
|
||||
username: string,
|
||||
baseUrl?: string,
|
||||
) {
|
||||
return isLocal ? username : `${username}@${baseUrl}`;
|
||||
}
|
||||
|
||||
async update(
|
||||
public async update(
|
||||
newUser: Partial<UserWithRelations>,
|
||||
): Promise<UserWithRelations> {
|
||||
await db.update(Users).set(newUser).where(eq(Users.id, this.id));
|
||||
|
|
@ -865,7 +869,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
* @param signatureMethod HTTP method to embed in signature (default: POST)
|
||||
* @returns The signed string and headers to send with the request
|
||||
*/
|
||||
async sign(
|
||||
public async sign(
|
||||
entity: KnownEntity | Collection,
|
||||
signatureUrl: string | URL,
|
||||
signatureMethod: HttpVerb = "POST",
|
||||
|
|
@ -902,7 +906,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
*
|
||||
* @returns The requester
|
||||
*/
|
||||
static async getFederationRequester(): Promise<FederationRequester> {
|
||||
public static async getFederationRequester(): Promise<FederationRequester> {
|
||||
const signatureConstructor = await SignatureConstructor.fromStringKey(
|
||||
config.instance.keys.private,
|
||||
config.http.base_url,
|
||||
|
|
@ -916,7 +920,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
*
|
||||
* @returns The requester
|
||||
*/
|
||||
async getFederationRequester(): Promise<FederationRequester> {
|
||||
public async getFederationRequester(): Promise<FederationRequester> {
|
||||
const signatureConstructor = await SignatureConstructor.fromStringKey(
|
||||
this.data.privateKey ?? "",
|
||||
this.getUri(),
|
||||
|
|
@ -930,7 +934,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
*
|
||||
* @param entity Entity to federate
|
||||
*/
|
||||
async federateToFollowers(entity: KnownEntity): Promise<void> {
|
||||
public async federateToFollowers(entity: KnownEntity): Promise<void> {
|
||||
// Get followers
|
||||
const followers = await User.manyFromSql(
|
||||
and(
|
||||
|
|
@ -951,7 +955,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
* @param user User to federate to
|
||||
* @returns Whether the federation was successful
|
||||
*/
|
||||
async federateToUser(
|
||||
public async federateToUser(
|
||||
entity: KnownEntity,
|
||||
user: User,
|
||||
): Promise<{ ok: boolean }> {
|
||||
|
|
@ -985,7 +989,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
return { ok: true };
|
||||
}
|
||||
|
||||
toApi(isOwnAccount = false): ApiAccount {
|
||||
public toApi(isOwnAccount = false): ApiAccount {
|
||||
const user = this.data;
|
||||
return {
|
||||
id: user.id,
|
||||
|
|
@ -1055,7 +1059,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
};
|
||||
}
|
||||
|
||||
toVersia(): VersiaUser {
|
||||
public toVersia(): VersiaUser {
|
||||
if (this.isRemote()) {
|
||||
throw new Error("Cannot convert remote user to Versia format");
|
||||
}
|
||||
|
|
@ -1132,7 +1136,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
};
|
||||
}
|
||||
|
||||
toMention(): ApiMention {
|
||||
public toMention(): ApiMention {
|
||||
return {
|
||||
url: this.getUri(),
|
||||
username: this.data.username,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue