2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { FollowingsRepository } from '@/models/_.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { } from '@/models/Blocking.js';
|
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiFollowing } from '@/models/Following.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { UserEntityService } from './UserEntityService.js';
|
2019-04-12 18:43:22 +02:00
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
type LocalFollowerFollowing = MiFollowing & {
|
2019-04-12 18:43:22 +02:00
|
|
|
followerHost: null;
|
|
|
|
followerInbox: null;
|
|
|
|
followerSharedInbox: null;
|
|
|
|
};
|
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
type RemoteFollowerFollowing = MiFollowing & {
|
2019-04-12 18:43:22 +02:00
|
|
|
followerHost: string;
|
|
|
|
followerInbox: string;
|
|
|
|
followerSharedInbox: string;
|
|
|
|
};
|
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
type LocalFolloweeFollowing = MiFollowing & {
|
2019-04-12 18:43:22 +02:00
|
|
|
followeeHost: null;
|
|
|
|
followeeInbox: null;
|
|
|
|
followeeSharedInbox: null;
|
|
|
|
};
|
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
type RemoteFolloweeFollowing = MiFollowing & {
|
2019-04-12 18:43:22 +02:00
|
|
|
followeeHost: string;
|
|
|
|
followeeInbox: string;
|
|
|
|
followeeSharedInbox: string;
|
|
|
|
};
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export class FollowingEntityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.followingsRepository)
|
|
|
|
private followingsRepository: FollowingsRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public isLocalFollower(following: MiFollowing): following is LocalFollowerFollowing {
|
2019-04-12 18:43:22 +02:00
|
|
|
return following.followerHost == null;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-12 18:43:22 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public isRemoteFollower(following: MiFollowing): following is RemoteFollowerFollowing {
|
2019-04-12 18:43:22 +02:00
|
|
|
return following.followerHost != null;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-12 18:43:22 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public isLocalFollowee(following: MiFollowing): following is LocalFolloweeFollowing {
|
2019-04-12 18:43:22 +02:00
|
|
|
return following.followeeHost == null;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-12 18:43:22 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public isRemoteFollowee(following: MiFollowing): following is RemoteFolloweeFollowing {
|
2019-04-12 18:43:22 +02:00
|
|
|
return following.followeeHost != null;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-12 18:43:22 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async pack(
|
2023-08-16 10:51:28 +02:00
|
|
|
src: MiFollowing['id'] | MiFollowing,
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2019-04-07 14:50:36 +02:00
|
|
|
opts?: {
|
|
|
|
populateFollowee?: boolean;
|
|
|
|
populateFollower?: boolean;
|
2022-09-17 20:27:08 +02:00
|
|
|
},
|
2021-09-22 15:35:55 +02:00
|
|
|
): Promise<Packed<'Following'>> {
|
2022-09-17 20:27:08 +02:00
|
|
|
const following = typeof src === 'object' ? src : await this.followingsRepository.findOneByOrFail({ id: 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,
|
2022-09-17 20:27:08 +02:00
|
|
|
followee: opts.populateFollowee ? this.userEntityService.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,
|
2022-09-17 20:27:08 +02:00
|
|
|
follower: opts.populateFollower ? this.userEntityService.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
|
|
|
});
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-25 06:27:07 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public packMany(
|
2019-04-25 06:27:07 +02:00
|
|
|
followings: any[],
|
2023-08-16 10:51:28 +02:00
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2019-04-25 06:27:07 +02:00
|
|
|
opts?: {
|
|
|
|
populateFollowee?: boolean;
|
|
|
|
populateFollower?: boolean;
|
2022-09-17 20:27:08 +02:00
|
|
|
},
|
2019-04-25 06:27:07 +02:00
|
|
|
) {
|
|
|
|
return Promise.all(followings.map(x => this.pack(x, me, opts)));
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|