2019-04-07 14:50:36 +02:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { Users } from '../index.js';
|
|
|
|
import { Following } from '@/models/entities/following.js';
|
|
|
|
import { awaitAll } from '@/prelude/await-all.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
2019-04-12 18:43:22 +02:00
|
|
|
|
|
|
|
type LocalFollowerFollowing = Following & {
|
|
|
|
followerHost: null;
|
|
|
|
followerInbox: null;
|
|
|
|
followerSharedInbox: null;
|
|
|
|
};
|
|
|
|
|
|
|
|
type RemoteFollowerFollowing = Following & {
|
|
|
|
followerHost: string;
|
|
|
|
followerInbox: string;
|
|
|
|
followerSharedInbox: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type LocalFolloweeFollowing = Following & {
|
|
|
|
followeeHost: null;
|
|
|
|
followeeInbox: null;
|
|
|
|
followeeSharedInbox: null;
|
|
|
|
};
|
|
|
|
|
|
|
|
type RemoteFolloweeFollowing = Following & {
|
|
|
|
followeeHost: string;
|
|
|
|
followeeInbox: string;
|
|
|
|
followeeSharedInbox: string;
|
|
|
|
};
|
2019-04-07 14:50:36 +02:00
|
|
|
|
|
|
|
@EntityRepository(Following)
|
|
|
|
export class FollowingRepository extends Repository<Following> {
|
2019-04-12 18:43:22 +02:00
|
|
|
public isLocalFollower(following: Following): following is LocalFollowerFollowing {
|
|
|
|
return following.followerHost == null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isRemoteFollower(following: Following): following is RemoteFollowerFollowing {
|
|
|
|
return following.followerHost != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isLocalFollowee(following: Following): following is LocalFolloweeFollowing {
|
|
|
|
return following.followeeHost == null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isRemoteFollowee(following: Following): following is RemoteFolloweeFollowing {
|
|
|
|
return following.followeeHost != null;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
public async pack(
|
|
|
|
src: Following['id'] | Following,
|
2021-03-24 03:05:37 +01:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-07 14:50:36 +02:00
|
|
|
opts?: {
|
|
|
|
populateFollowee?: boolean;
|
|
|
|
populateFollower?: boolean;
|
|
|
|
}
|
2021-09-22 15:35:55 +02:00
|
|
|
): Promise<Packed<'Following'>> {
|
2021-02-13 07:33:38 +01:00
|
|
|
const following = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
2019-04-07 14:50:36 +02:00
|
|
|
|
|
|
|
if (opts == null) opts = {};
|
|
|
|
|
2019-04-23 15:35:26 +02:00
|
|
|
return await awaitAll({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: following.id,
|
2019-04-23 15:35:26 +02:00
|
|
|
createdAt: following.createdAt.toISOString(),
|
2019-04-07 14:50:36 +02:00
|
|
|
followeeId: following.followeeId,
|
|
|
|
followerId: following.followerId,
|
|
|
|
followee: opts.populateFollowee ? Users.pack(following.followee || following.followeeId, me, {
|
2021-12-09 15:58:30 +01:00
|
|
|
detail: true,
|
2019-04-23 15:35:26 +02:00
|
|
|
}) : undefined,
|
2019-04-07 14:50:36 +02:00
|
|
|
follower: opts.populateFollower ? Users.pack(following.follower || following.followerId, me, {
|
2021-12-09 15:58:30 +01:00
|
|
|
detail: true,
|
2019-04-23 15:35:26 +02:00
|
|
|
}) : undefined,
|
2019-04-07 14:50:36 +02:00
|
|
|
});
|
|
|
|
}
|
2019-04-25 06:27:07 +02:00
|
|
|
|
|
|
|
public packMany(
|
|
|
|
followings: any[],
|
2021-03-24 03:05:37 +01:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-25 06:27:07 +02:00
|
|
|
opts?: {
|
|
|
|
populateFollowee?: boolean;
|
|
|
|
populateFollower?: boolean;
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
return Promise.all(followings.map(x => this.pack(x, me, opts)));
|
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|