2024-06-29 08:36:15 +02:00
|
|
|
import type { Relationship as ApiRelationship } from "@lysand-org/client/types";
|
2024-04-11 13:39:07 +02:00
|
|
|
import type { InferSelectModel } from "drizzle-orm";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { db } from "~/drizzle/db";
|
|
|
|
|
import { Relationships } from "~/drizzle/schema";
|
|
|
|
|
import type { User } from "~/packages/database-interface/user";
|
2023-09-22 05:18:05 +02:00
|
|
|
|
2024-06-12 00:32:38 +02:00
|
|
|
export type Relationship = InferSelectModel<typeof Relationships> & {
|
|
|
|
|
requestedBy: boolean;
|
|
|
|
|
};
|
2023-09-22 05:18:05 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Creates a new relationship between two users.
|
|
|
|
|
* @param owner The user who owns the relationship.
|
|
|
|
|
* @param other The user who is the subject of the relationship.
|
|
|
|
|
* @returns The newly created relationship.
|
|
|
|
|
*/
|
2023-11-12 02:37:14 +01:00
|
|
|
export const createNewRelationship = async (
|
2024-04-07 07:30:49 +02:00
|
|
|
owner: User,
|
|
|
|
|
other: User,
|
2023-11-11 03:36:06 +01:00
|
|
|
): Promise<Relationship> => {
|
2024-06-12 00:32:38 +02:00
|
|
|
return {
|
|
|
|
|
...(
|
|
|
|
|
await db
|
|
|
|
|
.insert(Relationships)
|
|
|
|
|
.values({
|
|
|
|
|
ownerId: owner.id,
|
|
|
|
|
subjectId: other.id,
|
|
|
|
|
languages: [],
|
|
|
|
|
following: false,
|
|
|
|
|
showingReblogs: false,
|
|
|
|
|
notifying: false,
|
|
|
|
|
followedBy: false,
|
|
|
|
|
blocking: false,
|
|
|
|
|
blockedBy: false,
|
|
|
|
|
muting: false,
|
|
|
|
|
mutingNotifications: false,
|
|
|
|
|
requested: false,
|
|
|
|
|
domainBlocking: false,
|
|
|
|
|
endorsed: false,
|
|
|
|
|
note: "",
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
})
|
|
|
|
|
.returning()
|
|
|
|
|
)[0],
|
|
|
|
|
requestedBy: false,
|
|
|
|
|
};
|
2023-11-11 03:36:06 +01:00
|
|
|
};
|
2023-09-22 05:18:05 +02:00
|
|
|
|
2023-11-28 23:57:48 +01:00
|
|
|
export const checkForBidirectionalRelationships = async (
|
2024-04-07 07:30:49 +02:00
|
|
|
user1: User,
|
|
|
|
|
user2: User,
|
|
|
|
|
createIfNotExists = true,
|
2023-11-28 23:57:48 +01:00
|
|
|
): Promise<boolean> => {
|
2024-04-17 08:36:01 +02:00
|
|
|
const relationship1 = await db.query.Relationships.findFirst({
|
2024-04-11 13:39:07 +02:00
|
|
|
where: (rel, { and, eq }) =>
|
|
|
|
|
and(eq(rel.ownerId, user1.id), eq(rel.subjectId, user2.id)),
|
2024-04-07 07:30:49 +02:00
|
|
|
});
|
2023-11-28 23:57:48 +01:00
|
|
|
|
2024-04-17 08:36:01 +02:00
|
|
|
const relationship2 = await db.query.Relationships.findFirst({
|
2024-04-11 13:39:07 +02:00
|
|
|
where: (rel, { and, eq }) =>
|
|
|
|
|
and(eq(rel.ownerId, user2.id), eq(rel.subjectId, user1.id)),
|
2024-04-07 07:30:49 +02:00
|
|
|
});
|
2023-11-28 23:57:48 +01:00
|
|
|
|
2024-06-12 01:42:36 +02:00
|
|
|
if (!relationship1 && createIfNotExists) {
|
2024-04-07 07:30:49 +02:00
|
|
|
await createNewRelationship(user1, user2);
|
2024-06-12 01:42:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!relationship2 && createIfNotExists) {
|
2024-04-07 07:30:49 +02:00
|
|
|
await createNewRelationship(user2, user1);
|
|
|
|
|
}
|
2023-11-28 23:57:48 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
return !!relationship1 && !!relationship2;
|
2023-11-28 23:57:48 +01:00
|
|
|
};
|
|
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Converts the relationship to an API-friendly format.
|
|
|
|
|
* @returns The API-friendly relationship.
|
|
|
|
|
*/
|
2024-06-29 08:36:15 +02:00
|
|
|
export const relationshipToApi = (rel: Relationship): ApiRelationship => {
|
2024-04-07 07:30:49 +02:00
|
|
|
return {
|
|
|
|
|
blocked_by: rel.blockedBy,
|
|
|
|
|
blocking: rel.blocking,
|
|
|
|
|
domain_blocking: rel.domainBlocking,
|
|
|
|
|
endorsed: rel.endorsed,
|
|
|
|
|
followed_by: rel.followedBy,
|
|
|
|
|
following: rel.following,
|
|
|
|
|
id: rel.subjectId,
|
|
|
|
|
muting: rel.muting,
|
|
|
|
|
muting_notifications: rel.mutingNotifications,
|
|
|
|
|
notifying: rel.notifying,
|
|
|
|
|
requested: rel.requested,
|
2024-06-12 00:32:38 +02:00
|
|
|
requested_by: rel.requestedBy,
|
2024-04-07 07:30:49 +02:00
|
|
|
showing_reblogs: rel.showingReblogs,
|
|
|
|
|
note: rel.note,
|
|
|
|
|
};
|
2023-11-11 03:36:06 +01:00
|
|
|
};
|