fix(api): 🐛 Fix incorrect relationships being returned (small rewrite)

This commit is contained in:
Jesse Wierzbinski 2024-06-11 13:42:36 -10:00
parent 20d1a5f39e
commit c4da7e1484
No known key found for this signature in database
11 changed files with 2325 additions and 29 deletions

View file

@ -61,8 +61,11 @@ export const checkForBidirectionalRelationships = async (
and(eq(rel.ownerId, user2.id), eq(rel.subjectId, user1.id)),
});
if (!relationship1 && !relationship2 && createIfNotExists) {
if (!relationship1 && createIfNotExists) {
await createNewRelationship(user1, user2);
}
if (!relationship2 && createIfNotExists) {
await createNewRelationship(user2, user1);
}

View file

@ -17,7 +17,11 @@ import { LogLevel } from "~/packages/log-manager";
import type { Application } from "./Application";
import type { EmojiWithInstance } from "./Emoji";
import { objectToInboxRequest } from "./Federation";
import { type Relationship, createNewRelationship } from "./Relationship";
import {
type Relationship,
checkForBidirectionalRelationships,
createNewRelationship,
} from "./Relationship";
import type { Token } from "./Token";
export type UserType = InferSelectModel<typeof Users>;
@ -135,14 +139,22 @@ export const followRequestUser = async (
})
.where(eq(Relationships.id, relationshipId));
// Set requested_by on other side
await db
.update(Relationships)
.set({
requestedBy: isRemote ? true : followee.getUser().isLocked,
followedBy: isRemote ? false : followee.getUser().isLocked,
})
.where(
and(
eq(Relationships.ownerId, followee.id),
eq(Relationships.subjectId, follower.id),
),
);
const updatedRelationship = await db.query.Relationships.findFirst({
where: (rel, { eq }) => eq(rel.id, relationshipId),
extras: {
requestedBy:
sql<boolean>`(SELECT "requested" FROM "Relationships" WHERE "Relationships"."ownerId" = ${followee.id} AND "Relationships"."subjectId" = ${follower.id})`.as(
"requested_by",
),
},
});
if (!updatedRelationship) {
@ -186,12 +198,6 @@ export const followRequestUser = async (
const result = await db.query.Relationships.findFirst({
where: (rel, { eq }) => eq(rel.id, relationshipId),
extras: {
requestedBy:
sql<boolean>`(SELECT "requested" FROM "Relationships" WHERE "Relationships"."ownerId" = ${followee.id} AND "Relationships"."subjectId" = ${follower.id})`.as(
"requested_by",
),
},
});
if (!result) {
@ -483,18 +489,14 @@ export const getRelationshipToOtherUser = async (
user: User,
other: User,
): Promise<Relationship> => {
await checkForBidirectionalRelationships(user, other);
const foundRelationship = await db.query.Relationships.findFirst({
where: (relationship, { and, eq }) =>
and(
eq(relationship.ownerId, user.id),
eq(relationship.subjectId, other.id),
),
extras: {
requestedBy:
sql<boolean>`(SELECT "requested" FROM "Relationships" WHERE "Relationships"."ownerId" = ${other.id} AND "Relationships"."subjectId" = ${user.id})`.as(
"requested_by",
),
},
});
if (!foundRelationship) {