2022-03-26 15:34:00 +09:00
|
|
|
import { db } from '@/db/postgre.js';
|
2022-02-27 11:07:39 +09: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-13 01:43:22 +09: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 21:50:36 +09:00
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
export const FollowingRepository = db.getRepository(Following).extend({
|
|
|
|
isLocalFollower(following: Following): following is LocalFollowerFollowing {
|
2019-04-13 01:43:22 +09:00
|
|
|
return following.followerHost == null;
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
2019-04-13 01:43:22 +09:00
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
isRemoteFollower(following: Following): following is RemoteFollowerFollowing {
|
2019-04-13 01:43:22 +09:00
|
|
|
return following.followerHost != null;
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
2019-04-13 01:43:22 +09:00
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
isLocalFollowee(following: Following): following is LocalFolloweeFollowing {
|
2019-04-13 01:43:22 +09:00
|
|
|
return following.followeeHost == null;
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
2019-04-13 01:43:22 +09:00
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
isRemoteFollowee(following: Following): following is RemoteFolloweeFollowing {
|
2019-04-13 01:43:22 +09:00
|
|
|
return following.followeeHost != null;
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
2019-04-13 01:43:22 +09:00
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
async pack(
|
2019-04-07 21:50:36 +09:00
|
|
|
src: Following['id'] | Following,
|
2021-03-24 11:05:37 +09:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-07 21:50:36 +09:00
|
|
|
opts?: {
|
|
|
|
populateFollowee?: boolean;
|
|
|
|
populateFollower?: boolean;
|
|
|
|
}
|
2021-09-22 22:35:55 +09:00
|
|
|
): Promise<Packed<'Following'>> {
|
2022-03-26 15:34:00 +09:00
|
|
|
const following = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
if (opts == null) opts = {};
|
|
|
|
|
2019-04-23 22:35:26 +09:00
|
|
|
return await awaitAll({
|
2019-04-07 21:50:36 +09:00
|
|
|
id: following.id,
|
2019-04-23 22:35:26 +09:00
|
|
|
createdAt: following.createdAt.toISOString(),
|
2019-04-07 21:50:36 +09:00
|
|
|
followeeId: following.followeeId,
|
|
|
|
followerId: following.followerId,
|
|
|
|
followee: opts.populateFollowee ? Users.pack(following.followee || following.followeeId, me, {
|
2021-12-09 23:58:30 +09:00
|
|
|
detail: true,
|
2019-04-23 22:35:26 +09:00
|
|
|
}) : undefined,
|
2019-04-07 21:50:36 +09:00
|
|
|
follower: opts.populateFollower ? Users.pack(following.follower || following.followerId, me, {
|
2021-12-09 23:58:30 +09:00
|
|
|
detail: true,
|
2019-04-23 22:35:26 +09:00
|
|
|
}) : undefined,
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
2019-04-25 13:27:07 +09:00
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
packMany(
|
2019-04-25 13:27:07 +09:00
|
|
|
followings: any[],
|
2021-03-24 11:05:37 +09:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-25 13:27:07 +09:00
|
|
|
opts?: {
|
|
|
|
populateFollowee?: boolean;
|
|
|
|
populateFollower?: boolean;
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
return Promise.all(followings.map(x => this.pack(x, me, opts)));
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
|
|
|
});
|