refactor(database): 🎨 Improve database handlers to have more consistent naming and methods

This commit is contained in:
Jesse Wierzbinski 2024-06-12 14:45:07 -10:00
parent a6159b9d55
commit 5565bf00de
No known key found for this signature in database
47 changed files with 365 additions and 333 deletions

View file

@ -15,7 +15,7 @@ export const objectToInboxRequest = async (
author: User,
userToSendTo: User,
): Promise<Request> => {
if (userToSendTo.isLocal() || !userToSendTo.getUser().endpoints?.inbox) {
if (userToSendTo.isLocal() || !userToSendTo.data.endpoints?.inbox) {
throw new Error("UserToSendTo has no inbox or is a local user");
}
@ -25,7 +25,7 @@ export const objectToInboxRequest = async (
const privateKey = await crypto.subtle.importKey(
"pkcs8",
Buffer.from(author.getUser().privateKey ?? "", "base64"),
Buffer.from(author.data.privateKey ?? "", "base64"),
"Ed25519",
false,
["sign"],
@ -33,7 +33,7 @@ export const objectToInboxRequest = async (
const ctor = new SignatureConstructor(privateKey, author.getUri());
const userInbox = new URL(userToSendTo.getUser().endpoints?.inbox ?? "");
const userInbox = new URL(userToSendTo.data.endpoints?.inbox ?? "");
const request = new Request(userInbox, {
method: "POST",
@ -54,7 +54,7 @@ export const objectToInboxRequest = async (
new LogManager(Bun.stdout).log(
LogLevel.DEBUG,
"Inbox.Signature",
`Sender public key: ${author.getUser().publicKey}`,
`Sender public key: ${author.data.publicKey}`,
);
// Log signed string

View file

@ -35,12 +35,12 @@ export const createLike = async (user: User, note: Note) => {
likerId: user.id,
});
if (note.getAuthor().getUser().instanceId === user.getUser().instanceId) {
if (note.author.data.instanceId === user.data.instanceId) {
// Notify the user that their post has been favourited
await db.insert(Notifications).values({
accountId: user.id,
type: "favourite",
notifiedId: note.getAuthor().id,
notifiedId: note.author.id,
noteId: note.id,
});
} else {
@ -65,12 +65,12 @@ export const deleteLike = async (user: User, note: Note) => {
and(
eq(Notifications.accountId, user.id),
eq(Notifications.type, "favourite"),
eq(Notifications.notifiedId, note.getAuthor().id),
eq(Notifications.notifiedId, note.author.id),
eq(Notifications.noteId, note.id),
),
);
if (user.isLocal() && note.getAuthor().isRemote()) {
if (user.isLocal() && note.author.isRemote()) {
// User is local, federate the delete
// TODO: Federate this
}

View file

@ -43,8 +43,7 @@ export const findManyNotifications = async (
output.map(async (notif) => ({
...notif,
account: transformOutputToUserWithRelations(notif.account),
status:
(await Note.fromId(notif.noteId, userId))?.getStatus() ?? null,
status: (await Note.fromId(notif.noteId, userId))?.data ?? null,
})),
);
};
@ -59,7 +58,7 @@ export const notificationToAPI = async (
id: notification.id,
type: notification.type,
status: notification.status
? await Note.fromStatus(notification.status).toAPI(account)
? await new Note(notification.status).toAPI(account)
: undefined,
};
};

View file

@ -323,7 +323,7 @@ export const parseTextMentions = async (text: string): Promise<User[]> => {
export const replaceTextMentions = async (text: string, mentions: User[]) => {
let finalText = text;
for (const mention of mentions) {
const user = mention.getUser();
const user = mention.data;
// Replace @username and @username@domain
if (user.instance) {
finalText = finalText.replace(
@ -439,7 +439,7 @@ export const federateNote = async (note: Note) => {
// TODO: Add queue system
const request = await objectToInboxRequest(
note.toLysand(),
note.getAuthor(),
note.author,
user,
);
@ -455,9 +455,7 @@ export const federateNote = async (note: Note) => {
dualLogger.log(
LogLevel.ERROR,
"Federation.Status",
`Failed to federate status ${
note.getStatus().id
} to ${user.getUri()}`,
`Failed to federate status ${note.data.id} to ${user.getUri()}`,
);
}
}

View file

@ -131,8 +131,8 @@ export const followRequestUser = async (
await db
.update(Relationships)
.set({
following: isRemote ? false : !followee.getUser().isLocked,
requested: isRemote ? true : followee.getUser().isLocked,
following: isRemote ? false : !followee.data.isLocked,
requested: isRemote ? true : followee.data.isLocked,
showingReblogs: reblogs,
notifying: notify,
languages: languages,
@ -143,8 +143,8 @@ export const followRequestUser = async (
await db
.update(Relationships)
.set({
requestedBy: isRemote ? true : followee.getUser().isLocked,
followedBy: isRemote ? false : followee.getUser().isLocked,
requestedBy: isRemote ? true : followee.data.isLocked,
followedBy: isRemote ? false : followee.data.isLocked,
})
.where(
and(
@ -209,7 +209,7 @@ export const followRequestUser = async (
} else {
await db.insert(Notifications).values({
accountId: follower.id,
type: followee.getUser().isLocked ? "follow_request" : "follow",
type: followee.data.isLocked ? "follow_request" : "follow",
notifiedId: followee.id,
});
}
@ -522,7 +522,7 @@ export const followRequestToLysand = (
throw new Error("Followee must be a remote user");
}
if (!followee.getUser().uri) {
if (!followee.data.uri) {
throw new Error("Followee must have a URI in database");
}
@ -550,7 +550,7 @@ export const followAcceptToLysand = (
throw new Error("Followee must be a local user");
}
if (!follower.getUser().uri) {
if (!follower.data.uri) {
throw new Error("Follower must have a URI in database");
}