mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(database): 🎨 Update database and schema names to be clearer
This commit is contained in:
parent
9081036c6d
commit
88b3ec7b43
92 changed files with 6785 additions and 1018 deletions
|
|
@ -1,9 +1,9 @@
|
|||
import type { InferSelectModel } from "drizzle-orm";
|
||||
import { db } from "~drizzle/db";
|
||||
import type { application } from "~drizzle/schema";
|
||||
import type { Applications } from "~drizzle/schema";
|
||||
import type { Application as APIApplication } from "~types/mastodon/application";
|
||||
|
||||
export type Application = InferSelectModel<typeof application>;
|
||||
export type Application = InferSelectModel<typeof Applications>;
|
||||
|
||||
/**
|
||||
* Retrieves the application associated with the given access token.
|
||||
|
|
@ -13,7 +13,7 @@ export type Application = InferSelectModel<typeof application>;
|
|||
export const getFromToken = async (
|
||||
token: string,
|
||||
): Promise<Application | null> => {
|
||||
const result = await db.query.token.findFirst({
|
||||
const result = await db.query.Tokens.findFirst({
|
||||
where: (tokens, { eq }) => eq(tokens.accessToken, token),
|
||||
with: {
|
||||
application: true,
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@ import type { InferSelectModel } from "drizzle-orm";
|
|||
import type * as Lysand from "lysand-types";
|
||||
import { MediaBackendType } from "media-manager";
|
||||
import { db } from "~drizzle/db";
|
||||
import { attachment } from "~drizzle/schema";
|
||||
import { Attachments } from "~drizzle/schema";
|
||||
import type { AsyncAttachment as APIAsyncAttachment } from "~types/mastodon/async_attachment";
|
||||
import type { Attachment as APIAttachment } from "~types/mastodon/attachment";
|
||||
|
||||
export type Attachment = InferSelectModel<typeof attachment>;
|
||||
export type Attachment = InferSelectModel<typeof Attachments>;
|
||||
|
||||
export const attachmentToAPI = (
|
||||
attachment: Attachment,
|
||||
|
|
@ -86,12 +86,12 @@ export const attachmentToLysand = (
|
|||
|
||||
export const attachmentFromLysand = async (
|
||||
attachmentToConvert: Lysand.ContentFormat,
|
||||
): Promise<InferSelectModel<typeof attachment>> => {
|
||||
): Promise<InferSelectModel<typeof Attachments>> => {
|
||||
const key = Object.keys(attachmentToConvert)[0];
|
||||
const value = attachmentToConvert[key];
|
||||
|
||||
const result = await db
|
||||
.insert(attachment)
|
||||
.insert(Attachments)
|
||||
.values({
|
||||
mimeType: key,
|
||||
url: value.content,
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import { type InferSelectModel, and, eq } from "drizzle-orm";
|
||||
import type * as Lysand from "lysand-types";
|
||||
import { db } from "~drizzle/db";
|
||||
import { emoji, instance } from "~drizzle/schema";
|
||||
import { Emojis, Instances } from "~drizzle/schema";
|
||||
import type { Emoji as APIEmoji } from "~types/mastodon/emoji";
|
||||
import { addInstanceIfNotExists } from "./Instance";
|
||||
|
||||
export type EmojiWithInstance = InferSelectModel<typeof emoji> & {
|
||||
instance: InferSelectModel<typeof instance> | null;
|
||||
export type EmojiWithInstance = InferSelectModel<typeof Emojis> & {
|
||||
instance: InferSelectModel<typeof Instances> | null;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -18,7 +18,7 @@ export const parseEmojis = async (text: string) => {
|
|||
const regex = /:[a-zA-Z0-9_]+:/g;
|
||||
const matches = text.match(regex);
|
||||
if (!matches) return [];
|
||||
const emojis = await db.query.emoji.findMany({
|
||||
const emojis = await db.query.Emojis.findMany({
|
||||
where: (emoji, { eq, or }) =>
|
||||
or(
|
||||
...matches
|
||||
|
|
@ -45,27 +45,27 @@ export const fetchEmoji = async (
|
|||
): Promise<EmojiWithInstance> => {
|
||||
const existingEmoji = await db
|
||||
.select()
|
||||
.from(emoji)
|
||||
.innerJoin(instance, eq(emoji.instanceId, instance.id))
|
||||
.from(Emojis)
|
||||
.innerJoin(Instances, eq(Emojis.instanceId, Instances.id))
|
||||
.where(
|
||||
and(
|
||||
eq(emoji.shortcode, emojiToFetch.name),
|
||||
host ? eq(instance.baseUrl, host) : undefined,
|
||||
eq(Emojis.shortcode, emojiToFetch.name),
|
||||
host ? eq(Instances.baseUrl, host) : undefined,
|
||||
),
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
if (existingEmoji[0])
|
||||
return {
|
||||
...existingEmoji[0].Emoji,
|
||||
instance: existingEmoji[0].Instance,
|
||||
...existingEmoji[0].Emojis,
|
||||
instance: existingEmoji[0].Instances,
|
||||
};
|
||||
|
||||
const foundInstance = host ? await addInstanceIfNotExists(host) : null;
|
||||
|
||||
const result = (
|
||||
await db
|
||||
.insert(emoji)
|
||||
.insert(Emojis)
|
||||
.values({
|
||||
shortcode: emojiToFetch.name,
|
||||
url: Object.entries(emojiToFetch.url)[0][1].content,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import type * as Lysand from "lysand-types";
|
||||
import { db } from "~drizzle/db";
|
||||
import { instance } from "~drizzle/schema";
|
||||
import { Instances } from "~drizzle/schema";
|
||||
|
||||
/**
|
||||
* Represents an instance in the database.
|
||||
|
|
@ -38,7 +38,7 @@ export const addInstanceIfNotExists = async (url: string) => {
|
|||
|
||||
return (
|
||||
await db
|
||||
.insert(instance)
|
||||
.insert(Instances)
|
||||
.values({
|
||||
baseUrl: host,
|
||||
name: metadata.name,
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@ import { config } from "config-manager";
|
|||
import { type InferSelectModel, and, eq } from "drizzle-orm";
|
||||
import type * as Lysand from "lysand-types";
|
||||
import { db } from "~drizzle/db";
|
||||
import { like, notification } from "~drizzle/schema";
|
||||
import { Likes, Notifications } from "~drizzle/schema";
|
||||
import type { StatusWithRelations } from "./Status";
|
||||
import type { UserWithRelations } from "./User";
|
||||
|
||||
export type Like = InferSelectModel<typeof like>;
|
||||
export type Like = InferSelectModel<typeof Likes>;
|
||||
|
||||
/**
|
||||
* Represents a Like entity in the database.
|
||||
|
|
@ -33,18 +33,18 @@ export const createLike = async (
|
|||
user: UserWithRelations,
|
||||
status: StatusWithRelations,
|
||||
) => {
|
||||
await db.insert(like).values({
|
||||
await db.insert(Likes).values({
|
||||
likedId: status.id,
|
||||
likerId: user.id,
|
||||
});
|
||||
|
||||
if (status.author.instanceId === user.instanceId) {
|
||||
// Notify the user that their post has been favourited
|
||||
await db.insert(notification).values({
|
||||
await db.insert(Notifications).values({
|
||||
accountId: user.id,
|
||||
type: "favourite",
|
||||
notifiedId: status.authorId,
|
||||
statusId: status.id,
|
||||
noteId: status.id,
|
||||
});
|
||||
} else {
|
||||
// TODO: Add database jobs for federating this
|
||||
|
|
@ -61,18 +61,18 @@ export const deleteLike = async (
|
|||
status: StatusWithRelations,
|
||||
) => {
|
||||
await db
|
||||
.delete(like)
|
||||
.where(and(eq(like.likedId, status.id), eq(like.likerId, user.id)));
|
||||
.delete(Likes)
|
||||
.where(and(eq(Likes.likedId, status.id), eq(Likes.likerId, user.id)));
|
||||
|
||||
// Notify the user that their post has been favourited
|
||||
await db
|
||||
.delete(notification)
|
||||
.delete(Notifications)
|
||||
.where(
|
||||
and(
|
||||
eq(notification.accountId, user.id),
|
||||
eq(notification.type, "favourite"),
|
||||
eq(notification.notifiedId, status.authorId),
|
||||
eq(notification.statusId, status.id),
|
||||
eq(Notifications.accountId, user.id),
|
||||
eq(Notifications.type, "favourite"),
|
||||
eq(Notifications.notifiedId, status.authorId),
|
||||
eq(Notifications.noteId, status.id),
|
||||
),
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import type { InferSelectModel } from "drizzle-orm";
|
||||
import { db } from "~drizzle/db";
|
||||
import type { notification } from "~drizzle/schema";
|
||||
import type { Notifications } from "~drizzle/schema";
|
||||
import { Note } from "~packages/database-interface/note";
|
||||
import type { Notification as APINotification } from "~types/mastodon/notification";
|
||||
import type { StatusWithRelations } from "./Status";
|
||||
|
|
@ -12,7 +12,7 @@ import {
|
|||
userToAPI,
|
||||
} from "./User";
|
||||
|
||||
export type Notification = InferSelectModel<typeof notification>;
|
||||
export type Notification = InferSelectModel<typeof Notifications>;
|
||||
|
||||
export type NotificationWithRelations = Notification & {
|
||||
status: StatusWithRelations | null;
|
||||
|
|
@ -20,9 +20,9 @@ export type NotificationWithRelations = Notification & {
|
|||
};
|
||||
|
||||
export const findManyNotifications = async (
|
||||
query: Parameters<typeof db.query.notification.findMany>[0],
|
||||
query: Parameters<typeof db.query.Notifications.findMany>[0],
|
||||
): Promise<NotificationWithRelations[]> => {
|
||||
const output = await db.query.notification.findMany({
|
||||
const output = await db.query.Notifications.findMany({
|
||||
...query,
|
||||
with: {
|
||||
...query?.with,
|
||||
|
|
@ -30,7 +30,7 @@ export const findManyNotifications = async (
|
|||
with: {
|
||||
...userRelations,
|
||||
},
|
||||
extras: userExtrasTemplate("notification_account"),
|
||||
extras: userExtrasTemplate("Notifications_account"),
|
||||
},
|
||||
},
|
||||
extras: {
|
||||
|
|
@ -42,7 +42,7 @@ export const findManyNotifications = async (
|
|||
output.map(async (notif) => ({
|
||||
...notif,
|
||||
account: transformOutputToUserWithRelations(notif.account),
|
||||
status: (await Note.fromId(notif.statusId))?.getStatus() ?? null,
|
||||
status: (await Note.fromId(notif.noteId))?.getStatus() ?? null,
|
||||
})),
|
||||
);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import type { InferSelectModel } from "drizzle-orm";
|
||||
import type * as Lysand from "lysand-types";
|
||||
import { db } from "~drizzle/db";
|
||||
import { lysandObject } from "~drizzle/schema";
|
||||
import { LysandObjects } from "~drizzle/schema";
|
||||
import { findFirstUser } from "./User";
|
||||
|
||||
export type LysandObject = InferSelectModel<typeof lysandObject>;
|
||||
export type LysandObject = InferSelectModel<typeof LysandObjects>;
|
||||
|
||||
/**
|
||||
* Represents a Lysand object in the database.
|
||||
|
|
@ -29,7 +29,7 @@ export const createFromObject = async (
|
|||
where: (user, { eq }) => eq(user.uri, authorUri),
|
||||
});
|
||||
|
||||
return await db.insert(lysandObject).values({
|
||||
return await db.insert(LysandObjects).values({
|
||||
authorId: author?.id,
|
||||
createdAt: new Date(object.created_at).toISOString(),
|
||||
extensions: object.extensions,
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import type { InferSelectModel } from "drizzle-orm";
|
||||
import { db } from "~drizzle/db";
|
||||
import { relationship } from "~drizzle/schema";
|
||||
import { Relationships } from "~drizzle/schema";
|
||||
import type { Relationship as APIRelationship } from "~types/mastodon/relationship";
|
||||
import type { User } from "./User";
|
||||
|
||||
export type Relationship = InferSelectModel<typeof relationship>;
|
||||
export type Relationship = InferSelectModel<typeof Relationships>;
|
||||
|
||||
/**
|
||||
* Creates a new relationship between two users.
|
||||
|
|
@ -18,7 +18,7 @@ export const createNewRelationship = async (
|
|||
): Promise<Relationship> => {
|
||||
return (
|
||||
await db
|
||||
.insert(relationship)
|
||||
.insert(Relationships)
|
||||
.values({
|
||||
ownerId: owner.id,
|
||||
subjectId: other.id,
|
||||
|
|
@ -46,12 +46,12 @@ export const checkForBidirectionalRelationships = async (
|
|||
user2: User,
|
||||
createIfNotExists = true,
|
||||
): Promise<boolean> => {
|
||||
const relationship1 = await db.query.relationship.findFirst({
|
||||
const relationship1 = await db.query.Relationships.findFirst({
|
||||
where: (rel, { and, eq }) =>
|
||||
and(eq(rel.ownerId, user1.id), eq(rel.subjectId, user2.id)),
|
||||
});
|
||||
|
||||
const relationship2 = await db.query.relationship.findFirst({
|
||||
const relationship2 = await db.query.Relationships.findFirst({
|
||||
where: (rel, { and, eq }) =>
|
||||
and(eq(rel.ownerId, user2.id), eq(rel.subjectId, user1.id)),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -27,13 +27,13 @@ import {
|
|||
import { parse } from "marked";
|
||||
import { db } from "~drizzle/db";
|
||||
import {
|
||||
attachment,
|
||||
emojiToStatus,
|
||||
instance,
|
||||
notification,
|
||||
status,
|
||||
statusToMentions,
|
||||
user,
|
||||
Attachments,
|
||||
EmojiToNote,
|
||||
Instances,
|
||||
NoteToMentions,
|
||||
Notes,
|
||||
Notifications,
|
||||
Users,
|
||||
} from "~drizzle/schema";
|
||||
import { Note } from "~packages/database-interface/note";
|
||||
import { LogLevel } from "~packages/log-manager";
|
||||
|
|
@ -61,17 +61,17 @@ import {
|
|||
userRelations,
|
||||
} from "./User";
|
||||
|
||||
export type Status = InferSelectModel<typeof status>;
|
||||
export type Status = InferSelectModel<typeof Notes>;
|
||||
|
||||
export type StatusWithRelations = Status & {
|
||||
author: UserWithRelations;
|
||||
mentions: UserWithInstance[];
|
||||
attachments: InferSelectModel<typeof attachment>[];
|
||||
attachments: InferSelectModel<typeof Attachments>[];
|
||||
reblog: StatusWithoutRecursiveRelations | null;
|
||||
emojis: EmojiWithInstance[];
|
||||
likes: Like[];
|
||||
inReplyTo: Status | null;
|
||||
quoting: Status | null;
|
||||
reply: Status | null;
|
||||
quote: Status | null;
|
||||
application: Application | null;
|
||||
reblogCount: number;
|
||||
likeCount: number;
|
||||
|
|
@ -80,20 +80,20 @@ export type StatusWithRelations = Status & {
|
|||
|
||||
export type StatusWithoutRecursiveRelations = Omit<
|
||||
StatusWithRelations,
|
||||
"inReplyTo" | "quoting" | "reblog"
|
||||
"reply" | "quote" | "reblog"
|
||||
>;
|
||||
|
||||
export const noteExtras = {
|
||||
reblogCount:
|
||||
sql`(SELECT COUNT(*) FROM "Status" "status" WHERE "status"."reblogId" = "status".id)`.as(
|
||||
sql`(SELECT COUNT(*) FROM "Notes" WHERE "Notes"."reblogId" = "Notes".id)`.as(
|
||||
"reblog_count",
|
||||
),
|
||||
likeCount:
|
||||
sql`(SELECT COUNT(*) FROM "Like" "like" WHERE "like"."likedId" = "status".id)`.as(
|
||||
sql`(SELECT COUNT(*) FROM "Likes" WHERE "Likes"."likedId" = "Notes".id)`.as(
|
||||
"like_count",
|
||||
),
|
||||
replyCount:
|
||||
sql`(SELECT COUNT(*) FROM "Status" "status" WHERE "status"."inReplyToPostId" = "status".id)`.as(
|
||||
sql`(SELECT COUNT(*) FROM "Notes" WHERE "Notes"."replyId" = "Notes".id)`.as(
|
||||
"reply_count",
|
||||
),
|
||||
};
|
||||
|
|
@ -104,15 +104,15 @@ export const noteExtras = {
|
|||
* @returns
|
||||
*/
|
||||
export const findManyNotes = async (
|
||||
query: Parameters<typeof db.query.status.findMany>[0],
|
||||
query: Parameters<typeof db.query.Notes.findMany>[0],
|
||||
): Promise<StatusWithRelations[]> => {
|
||||
const output = await db.query.status.findMany({
|
||||
const output = await db.query.Notes.findMany({
|
||||
...query,
|
||||
with: {
|
||||
...query?.with,
|
||||
attachments: {
|
||||
where: (attachment, { eq }) =>
|
||||
eq(attachment.statusId, sql`"status"."id"`),
|
||||
eq(attachment.noteId, sql`"Notes"."id"`),
|
||||
},
|
||||
emojis: {
|
||||
with: {
|
||||
|
|
@ -127,7 +127,7 @@ export const findManyNotes = async (
|
|||
with: {
|
||||
...userRelations,
|
||||
},
|
||||
extras: userExtrasTemplate("status_author"),
|
||||
extras: userExtrasTemplate("Notes_author"),
|
||||
},
|
||||
mentions: {
|
||||
with: {
|
||||
|
|
@ -157,7 +157,7 @@ export const findManyNotes = async (
|
|||
user: {
|
||||
with: userRelations,
|
||||
extras: userExtrasTemplate(
|
||||
"status_reblog_mentions_user",
|
||||
"Notes_reblog_mentions_user",
|
||||
),
|
||||
},
|
||||
},
|
||||
|
|
@ -166,15 +166,15 @@ export const findManyNotes = async (
|
|||
with: {
|
||||
...userRelations,
|
||||
},
|
||||
extras: userExtrasTemplate("status_reblog_author"),
|
||||
extras: userExtrasTemplate("Notes_reblog_author"),
|
||||
},
|
||||
},
|
||||
extras: {
|
||||
...noteExtras,
|
||||
},
|
||||
},
|
||||
inReplyTo: true,
|
||||
quoting: true,
|
||||
reply: true,
|
||||
quote: true,
|
||||
},
|
||||
extras: {
|
||||
...noteExtras,
|
||||
|
|
@ -187,25 +187,17 @@ export const findManyNotes = async (
|
|||
author: transformOutputToUserWithRelations(post.author),
|
||||
mentions: post.mentions.map((mention) => ({
|
||||
...mention.user,
|
||||
endpoints: mention.user.endpoints as User["endpoints"],
|
||||
endpoints: mention.user.endpoints,
|
||||
})),
|
||||
emojis: (post.emojis ?? []).map(
|
||||
(emoji) =>
|
||||
(emoji as unknown as Record<string, object>)
|
||||
.emoji as EmojiWithInstance,
|
||||
),
|
||||
emojis: (post.emojis ?? []).map((emoji) => emoji.emoji),
|
||||
reblog: post.reblog && {
|
||||
...post.reblog,
|
||||
author: transformOutputToUserWithRelations(post.reblog.author),
|
||||
mentions: post.reblog.mentions.map((mention) => ({
|
||||
...mention.user,
|
||||
endpoints: mention.user.endpoints as User["endpoints"],
|
||||
endpoints: mention.user.endpoints,
|
||||
})),
|
||||
emojis: (post.reblog.emojis ?? []).map(
|
||||
(emoji) =>
|
||||
(emoji as unknown as Record<string, object>)
|
||||
.emoji as EmojiWithInstance,
|
||||
),
|
||||
emojis: (post.reblog.emojis ?? []).map((emoji) => emoji.emoji),
|
||||
reblogCount: Number(post.reblog.reblogCount),
|
||||
likeCount: Number(post.reblog.likeCount),
|
||||
replyCount: Number(post.reblog.replyCount),
|
||||
|
|
@ -217,15 +209,15 @@ export const findManyNotes = async (
|
|||
};
|
||||
|
||||
export const findFirstNote = async (
|
||||
query: Parameters<typeof db.query.status.findFirst>[0],
|
||||
query: Parameters<typeof db.query.Notes.findFirst>[0],
|
||||
): Promise<StatusWithRelations | null> => {
|
||||
const output = await db.query.status.findFirst({
|
||||
const output = await db.query.Notes.findFirst({
|
||||
...query,
|
||||
with: {
|
||||
...query?.with,
|
||||
attachments: {
|
||||
where: (attachment, { eq }) =>
|
||||
eq(attachment.statusId, sql`"status"."id"`),
|
||||
eq(attachment.noteId, sql`"Notes"."id"`),
|
||||
},
|
||||
emojis: {
|
||||
with: {
|
||||
|
|
@ -240,7 +232,7 @@ export const findFirstNote = async (
|
|||
with: {
|
||||
...userRelations,
|
||||
},
|
||||
extras: userExtrasTemplate("status_author"),
|
||||
extras: userExtrasTemplate("Notes_author"),
|
||||
},
|
||||
mentions: {
|
||||
with: {
|
||||
|
|
@ -270,7 +262,7 @@ export const findFirstNote = async (
|
|||
user: {
|
||||
with: userRelations,
|
||||
extras: userExtrasTemplate(
|
||||
"status_reblog_mentions_user",
|
||||
"Notes_reblog_mentions_user",
|
||||
),
|
||||
},
|
||||
},
|
||||
|
|
@ -279,15 +271,15 @@ export const findFirstNote = async (
|
|||
with: {
|
||||
...userRelations,
|
||||
},
|
||||
extras: userExtrasTemplate("status_reblog_author"),
|
||||
extras: userExtrasTemplate("Notes_reblog_author"),
|
||||
},
|
||||
},
|
||||
extras: {
|
||||
...noteExtras,
|
||||
},
|
||||
},
|
||||
inReplyTo: true,
|
||||
quoting: true,
|
||||
reply: true,
|
||||
quote: true,
|
||||
},
|
||||
extras: {
|
||||
...noteExtras,
|
||||
|
|
@ -302,25 +294,17 @@ export const findFirstNote = async (
|
|||
author: transformOutputToUserWithRelations(output.author),
|
||||
mentions: output.mentions.map((mention) => ({
|
||||
...mention.user,
|
||||
endpoints: mention.user.endpoints as User["endpoints"],
|
||||
endpoints: mention.user.endpoints,
|
||||
})),
|
||||
emojis: (output.emojis ?? []).map(
|
||||
(emoji) =>
|
||||
(emoji as unknown as Record<string, object>)
|
||||
.emoji as EmojiWithInstance,
|
||||
),
|
||||
emojis: (output.emojis ?? []).map((emoji) => emoji.emoji),
|
||||
reblog: output.reblog && {
|
||||
...output.reblog,
|
||||
author: transformOutputToUserWithRelations(output.reblog.author),
|
||||
mentions: output.reblog.mentions.map((mention) => ({
|
||||
...mention.user,
|
||||
endpoints: mention.user.endpoints as User["endpoints"],
|
||||
endpoints: mention.user.endpoints,
|
||||
})),
|
||||
emojis: (output.reblog.emojis ?? []).map(
|
||||
(emoji) =>
|
||||
(emoji as unknown as Record<string, object>)
|
||||
.emoji as EmojiWithInstance,
|
||||
),
|
||||
emojis: (output.reblog.emojis ?? []).map((emoji) => emoji.emoji),
|
||||
reblogCount: Number(output.reblog.reblogCount),
|
||||
likeCount: Number(output.reblog.likeCount),
|
||||
replyCount: Number(output.reblog.replyCount),
|
||||
|
|
@ -340,7 +324,7 @@ export const resolveNote = async (
|
|||
}
|
||||
|
||||
const foundStatus = await Note.fromSql(
|
||||
eq(status.uri, uri ?? providedNote?.uri ?? ""),
|
||||
eq(Notes.uri, uri ?? providedNote?.uri ?? ""),
|
||||
);
|
||||
|
||||
if (foundStatus) return foundStatus;
|
||||
|
|
@ -478,20 +462,20 @@ export const parseTextMentions = async (
|
|||
|
||||
const foundUsers = await db
|
||||
.select({
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
baseUrl: instance.baseUrl,
|
||||
id: Users.id,
|
||||
username: Users.username,
|
||||
baseUrl: Instances.baseUrl,
|
||||
})
|
||||
.from(user)
|
||||
.leftJoin(instance, eq(user.instanceId, instance.id))
|
||||
.from(Users)
|
||||
.leftJoin(Instances, eq(Users.instanceId, Instances.id))
|
||||
.where(
|
||||
or(
|
||||
...mentionedPeople.map((person) =>
|
||||
and(
|
||||
eq(user.username, person?.[1] ?? ""),
|
||||
eq(Users.username, person?.[1] ?? ""),
|
||||
isLocal(person?.[2])
|
||||
? isNull(user.instanceId)
|
||||
: eq(instance.baseUrl, person?.[2] ?? ""),
|
||||
? isNull(Users.instanceId)
|
||||
: eq(Instances.baseUrl, person?.[2] ?? ""),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
|
@ -701,10 +685,10 @@ export const editStatus = async (
|
|||
// Connect emojis
|
||||
for (const emoji of data.emojis) {
|
||||
await db
|
||||
.insert(emojiToStatus)
|
||||
.insert(EmojiToNote)
|
||||
.values({
|
||||
emojiId: emoji.id,
|
||||
statusId: updated.id,
|
||||
noteId: updated.id,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
|
@ -712,9 +696,9 @@ export const editStatus = async (
|
|||
// Connect mentions
|
||||
for (const mention of mentions) {
|
||||
await db
|
||||
.insert(statusToMentions)
|
||||
.insert(NoteToMentions)
|
||||
.values({
|
||||
statusId: updated.id,
|
||||
noteId: updated.id,
|
||||
userId: mention.id,
|
||||
})
|
||||
.execute();
|
||||
|
|
@ -723,28 +707,28 @@ export const editStatus = async (
|
|||
// Send notifications for mentioned local users
|
||||
for (const mention of mentions ?? []) {
|
||||
if (mention.instanceId === null) {
|
||||
await db.insert(notification).values({
|
||||
await db.insert(Notifications).values({
|
||||
accountId: statusToEdit.authorId,
|
||||
notifiedId: mention.id,
|
||||
type: "mention",
|
||||
statusId: updated.id,
|
||||
noteId: updated.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Set attachment parents
|
||||
await db
|
||||
.update(attachment)
|
||||
.update(Attachments)
|
||||
.set({
|
||||
statusId: updated.id,
|
||||
noteId: updated.id,
|
||||
})
|
||||
.where(inArray(attachment.id, data.media_attachments ?? []));
|
||||
.where(inArray(Attachments.id, data.media_attachments ?? []));
|
||||
|
||||
return await Note.fromId(updated.id);
|
||||
};
|
||||
|
||||
export const isFavouritedBy = async (status: Status, user: User) => {
|
||||
return !!(await db.query.like.findFirst({
|
||||
return !!(await db.query.Likes.findFirst({
|
||||
where: (like, { and, eq }) =>
|
||||
and(eq(like.likerId, user.id), eq(like.likedId, status.id)),
|
||||
}));
|
||||
|
|
@ -779,8 +763,8 @@ export const statusToLysand = (status: StatusWithRelations): Lysand.Note => {
|
|||
),
|
||||
is_sensitive: status.sensitive,
|
||||
mentions: status.mentions.map((mention) => mention.uri || ""),
|
||||
quotes: getStatusUri(status.quoting) ?? undefined,
|
||||
replies_to: getStatusUri(status.inReplyTo) ?? undefined,
|
||||
quotes: getStatusUri(status.quote) ?? undefined,
|
||||
replies_to: getStatusUri(status.reply) ?? undefined,
|
||||
subject: status.spoilerText,
|
||||
visibility: status.visibility as Lysand.Visibility,
|
||||
extensions: {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import type { InferSelectModel } from "drizzle-orm";
|
||||
import type { token } from "~drizzle/schema";
|
||||
import type { Tokens } from "~drizzle/schema";
|
||||
|
||||
/**
|
||||
* The type of token.
|
||||
|
|
@ -8,4 +8,4 @@ export enum TokenType {
|
|||
BEARER = "Bearer",
|
||||
}
|
||||
|
||||
export type Token = InferSelectModel<typeof token>;
|
||||
export type Token = InferSelectModel<typeof Tokens>;
|
||||
|
|
|
|||
|
|
@ -7,13 +7,13 @@ import { htmlToText } from "html-to-text";
|
|||
import type * as Lysand from "lysand-types";
|
||||
import { db } from "~drizzle/db";
|
||||
import {
|
||||
application,
|
||||
emojiToUser,
|
||||
instance,
|
||||
notification,
|
||||
relationship,
|
||||
token,
|
||||
user,
|
||||
Applications,
|
||||
EmojiToUser,
|
||||
Instances,
|
||||
Notifications,
|
||||
Relationships,
|
||||
Tokens,
|
||||
Users,
|
||||
} from "~drizzle/schema";
|
||||
import { LogLevel } from "~packages/log-manager";
|
||||
import type { Account as APIAccount } from "~types/mastodon/account";
|
||||
|
|
@ -31,14 +31,14 @@ import { addInstanceIfNotExists } from "./Instance";
|
|||
import { createNewRelationship } from "./Relationship";
|
||||
import type { Token } from "./Token";
|
||||
|
||||
export type User = InferSelectModel<typeof user>;
|
||||
export type User = InferSelectModel<typeof Users>;
|
||||
|
||||
export type UserWithInstance = User & {
|
||||
instance: InferSelectModel<typeof instance> | null;
|
||||
instance: InferSelectModel<typeof Instances> | null;
|
||||
};
|
||||
|
||||
export type UserWithRelations = User & {
|
||||
instance: InferSelectModel<typeof instance> | null;
|
||||
instance: InferSelectModel<typeof Instances> | null;
|
||||
emojis: EmojiWithInstance[];
|
||||
followerCount: number;
|
||||
followingCount: number;
|
||||
|
|
@ -46,8 +46,8 @@ export type UserWithRelations = User & {
|
|||
};
|
||||
|
||||
export type UserWithRelationsAndRelationships = UserWithRelations & {
|
||||
relationships: InferSelectModel<typeof relationship>[];
|
||||
relationshipSubjects: InferSelectModel<typeof relationship>[];
|
||||
relationships: InferSelectModel<typeof Relationships>[];
|
||||
relationshipSubjects: InferSelectModel<typeof Relationships>[];
|
||||
};
|
||||
|
||||
export const userRelations: {
|
||||
|
|
@ -76,15 +76,15 @@ export const userRelations: {
|
|||
|
||||
export const userExtras = {
|
||||
followerCount:
|
||||
sql`(SELECT COUNT(*) FROM "Relationship" "relationships" WHERE ("relationships"."ownerId" = "user".id AND "relationships"."following" = true))`.as(
|
||||
sql`(SELECT COUNT(*) FROM "Relationships" "relationships" WHERE ("relationships"."ownerId" = "Users".id AND "relationships"."following" = true))`.as(
|
||||
"follower_count",
|
||||
),
|
||||
followingCount:
|
||||
sql`(SELECT COUNT(*) FROM "Relationship" "relationshipSubjects" WHERE ("relationshipSubjects"."subjectId" = "user".id AND "relationshipSubjects"."following" = true))`.as(
|
||||
sql`(SELECT COUNT(*) FROM "Relationships" "relationshipSubjects" WHERE ("relationshipSubjects"."subjectId" = "Users".id AND "relationshipSubjects"."following" = true))`.as(
|
||||
"following_count",
|
||||
),
|
||||
statusCount:
|
||||
sql`(SELECT COUNT(*) FROM "Status" "statuses" WHERE "statuses"."authorId" = "user".id)`.as(
|
||||
sql`(SELECT COUNT(*) FROM "Notes" WHERE "Notes"."authorId" = "Users".id)`.as(
|
||||
"status_count",
|
||||
),
|
||||
};
|
||||
|
|
@ -92,15 +92,15 @@ export const userExtras = {
|
|||
export const userExtrasTemplate = (name: string) => ({
|
||||
// @ts-ignore
|
||||
followerCount: sql([
|
||||
`(SELECT COUNT(*) FROM "Relationship" "relationships" WHERE ("relationships"."ownerId" = "${name}".id AND "relationships"."following" = true))`,
|
||||
`(SELECT COUNT(*) FROM "Relationships" "relationships" WHERE ("relationships"."ownerId" = "${name}".id AND "relationships"."following" = true))`,
|
||||
]).as("follower_count"),
|
||||
// @ts-ignore
|
||||
followingCount: sql([
|
||||
`(SELECT COUNT(*) FROM "Relationship" "relationshipSubjects" WHERE ("relationshipSubjects"."subjectId" = "${name}".id AND "relationshipSubjects"."following" = true))`,
|
||||
`(SELECT COUNT(*) FROM "Relationships" "relationshipSubjects" WHERE ("relationshipSubjects"."subjectId" = "${name}".id AND "relationshipSubjects"."following" = true))`,
|
||||
]).as("following_count"),
|
||||
// @ts-ignore
|
||||
statusCount: sql([
|
||||
`(SELECT COUNT(*) FROM "Status" "statuses" WHERE "statuses"."authorId" = "${name}".id)`,
|
||||
`(SELECT COUNT(*) FROM "Notes" WHERE "Notes"."authorId" = "${name}".id)`,
|
||||
]).as("status_count"),
|
||||
});
|
||||
|
||||
|
|
@ -151,12 +151,12 @@ export const followRequestUser = async (
|
|||
reblogs = false,
|
||||
notify = false,
|
||||
languages: string[] = [],
|
||||
): Promise<InferSelectModel<typeof relationship>> => {
|
||||
): Promise<InferSelectModel<typeof Relationships>> => {
|
||||
const isRemote = followee.instanceId !== null;
|
||||
|
||||
const updatedRelationship = (
|
||||
await db
|
||||
.update(relationship)
|
||||
.update(Relationships)
|
||||
.set({
|
||||
following: isRemote ? false : !followee.isLocked,
|
||||
requested: isRemote ? true : followee.isLocked,
|
||||
|
|
@ -164,7 +164,7 @@ export const followRequestUser = async (
|
|||
notifying: notify,
|
||||
languages: languages,
|
||||
})
|
||||
.where(eq(relationship.id, relationshipId))
|
||||
.where(eq(Relationships.id, relationshipId))
|
||||
.returning()
|
||||
)[0];
|
||||
|
||||
|
|
@ -195,17 +195,17 @@ export const followRequestUser = async (
|
|||
|
||||
return (
|
||||
await db
|
||||
.update(relationship)
|
||||
.update(Relationships)
|
||||
.set({
|
||||
following: false,
|
||||
requested: false,
|
||||
})
|
||||
.where(eq(relationship.id, relationshipId))
|
||||
.where(eq(Relationships.id, relationshipId))
|
||||
.returning()
|
||||
)[0];
|
||||
}
|
||||
} else {
|
||||
await db.insert(notification).values({
|
||||
await db.insert(Notifications).values({
|
||||
accountId: follower.id,
|
||||
type: followee.isLocked ? "follow_request" : "follow",
|
||||
notifiedId: followee.id,
|
||||
|
|
@ -277,7 +277,7 @@ export const transformOutputToUserWithRelations = (
|
|||
emojiId: string;
|
||||
emoji?: EmojiWithInstance;
|
||||
}[];
|
||||
instance: InferSelectModel<typeof instance> | null;
|
||||
instance: InferSelectModel<typeof Instances> | null;
|
||||
endpoints: unknown;
|
||||
},
|
||||
): UserWithRelations => {
|
||||
|
|
@ -306,9 +306,9 @@ export const transformOutputToUserWithRelations = (
|
|||
};
|
||||
|
||||
export const findManyUsers = async (
|
||||
query: Parameters<typeof db.query.user.findMany>[0],
|
||||
query: Parameters<typeof db.query.Users.findMany>[0],
|
||||
): Promise<UserWithRelations[]> => {
|
||||
const output = await db.query.user.findMany({
|
||||
const output = await db.query.Users.findMany({
|
||||
...query,
|
||||
with: {
|
||||
...userRelations,
|
||||
|
|
@ -324,9 +324,9 @@ export const findManyUsers = async (
|
|||
};
|
||||
|
||||
export const findFirstUser = async (
|
||||
query: Parameters<typeof db.query.user.findFirst>[0],
|
||||
query: Parameters<typeof db.query.Users.findFirst>[0],
|
||||
): Promise<UserWithRelations | null> => {
|
||||
const output = await db.query.user.findFirst({
|
||||
const output = await db.query.Users.findFirst({
|
||||
...query,
|
||||
with: {
|
||||
...userRelations,
|
||||
|
|
@ -418,7 +418,7 @@ export const resolveUser = async (
|
|||
|
||||
const newUser = (
|
||||
await db
|
||||
.insert(user)
|
||||
.insert(Users)
|
||||
.values({
|
||||
username: data.username,
|
||||
uri: data.uri,
|
||||
|
|
@ -456,7 +456,7 @@ export const resolveUser = async (
|
|||
|
||||
// Add emojis to user
|
||||
if (emojis.length > 0) {
|
||||
await db.insert(emojiToUser).values(
|
||||
await db.insert(EmojiToUser).values(
|
||||
emojis.map((emoji) => ({
|
||||
emojiId: emoji.id,
|
||||
userId: newUser.id,
|
||||
|
|
@ -494,15 +494,15 @@ export const resolveWebFinger = async (
|
|||
// Check if user not already in database
|
||||
const foundUser = await db
|
||||
.select()
|
||||
.from(user)
|
||||
.innerJoin(instance, eq(user.instanceId, instance.id))
|
||||
.where(and(eq(user.username, identifier), eq(instance.baseUrl, host)))
|
||||
.from(Users)
|
||||
.innerJoin(Instances, eq(Users.instanceId, Instances.id))
|
||||
.where(and(eq(Users.username, identifier), eq(Instances.baseUrl, host)))
|
||||
.limit(1);
|
||||
|
||||
if (foundUser[0])
|
||||
return (
|
||||
(await findFirstUser({
|
||||
where: (user, { eq }) => eq(user.id, foundUser[0].User.id),
|
||||
where: (user, { eq }) => eq(user.id, foundUser[0].Users.id),
|
||||
})) || null
|
||||
);
|
||||
|
||||
|
|
@ -580,7 +580,7 @@ export const createNewLocalUser = async (data: {
|
|||
|
||||
const newUser = (
|
||||
await db
|
||||
.insert(user)
|
||||
.insert(Users)
|
||||
.values({
|
||||
username: data.username,
|
||||
displayName: data.display_name ?? data.username,
|
||||
|
|
@ -662,12 +662,12 @@ export const retrieveUserAndApplicationFromToken = async (
|
|||
const output = (
|
||||
await db
|
||||
.select({
|
||||
token: token,
|
||||
application: application,
|
||||
token: Tokens,
|
||||
application: Applications,
|
||||
})
|
||||
.from(token)
|
||||
.leftJoin(application, eq(token.applicationId, application.id))
|
||||
.where(eq(token.accessToken, access_token))
|
||||
.from(Tokens)
|
||||
.leftJoin(Applications, eq(Tokens.applicationId, Applications.id))
|
||||
.where(eq(Tokens.accessToken, access_token))
|
||||
.limit(1)
|
||||
)[0];
|
||||
|
||||
|
|
@ -686,7 +686,7 @@ export const retrieveToken = async (
|
|||
if (!access_token) return null;
|
||||
|
||||
return (
|
||||
(await db.query.token.findFirst({
|
||||
(await db.query.Tokens.findFirst({
|
||||
where: (tokens, { eq }) => eq(tokens.accessToken, access_token),
|
||||
})) ?? null
|
||||
);
|
||||
|
|
@ -700,8 +700,8 @@ export const retrieveToken = async (
|
|||
export const getRelationshipToOtherUser = async (
|
||||
user: UserWithRelations,
|
||||
other: User,
|
||||
): Promise<InferSelectModel<typeof relationship>> => {
|
||||
const foundRelationship = await db.query.relationship.findFirst({
|
||||
): Promise<InferSelectModel<typeof Relationships>> => {
|
||||
const foundRelationship = await db.query.Relationships.findFirst({
|
||||
where: (relationship, { and, eq }) =>
|
||||
and(
|
||||
eq(relationship.ownerId, user.id),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue