2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { FollowingsRepository } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
|
|
|
import type { Packed } from '@/misc/schema.js';
|
|
|
|
import type { } from '@/models/entities/Blocking.js';
|
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { Following } from '@/models/entities/Following.js';
|
|
|
|
import { UserEntityService } from './UserEntityService.js';
|
2019-04-12 16:43:22 +00: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;
|
|
|
|
};
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
@Injectable()
|
|
|
|
export class FollowingEntityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.followingsRepository)
|
|
|
|
private followingsRepository: FollowingsRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public isLocalFollower(following: Following): following is LocalFollowerFollowing {
|
2019-04-12 16:43:22 +00:00
|
|
|
return following.followerHost == null;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2019-04-12 16:43:22 +00:00
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public isRemoteFollower(following: Following): following is RemoteFollowerFollowing {
|
2019-04-12 16:43:22 +00:00
|
|
|
return following.followerHost != null;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2019-04-12 16:43:22 +00:00
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public isLocalFollowee(following: Following): following is LocalFolloweeFollowing {
|
2019-04-12 16:43:22 +00:00
|
|
|
return following.followeeHost == null;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2019-04-12 16:43:22 +00:00
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public isRemoteFollowee(following: Following): following is RemoteFolloweeFollowing {
|
2019-04-12 16:43:22 +00:00
|
|
|
return following.followeeHost != null;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2019-04-12 16:43:22 +00:00
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async pack(
|
2019-04-07 12:50:36 +00:00
|
|
|
src: Following['id'] | Following,
|
2021-03-24 02:05:37 +00:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-07 12:50:36 +00:00
|
|
|
opts?: {
|
|
|
|
populateFollowee?: boolean;
|
|
|
|
populateFollower?: boolean;
|
2022-09-17 18:27:08 +00:00
|
|
|
},
|
2021-09-22 13:35:55 +00:00
|
|
|
): Promise<Packed<'Following'>> {
|
2022-09-17 18:27:08 +00:00
|
|
|
const following = typeof src === 'object' ? src : await this.followingsRepository.findOneByOrFail({ id: src });
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
if (opts == null) opts = {};
|
|
|
|
|
2019-04-23 13:35:26 +00:00
|
|
|
return await awaitAll({
|
2019-04-07 12:50:36 +00:00
|
|
|
id: following.id,
|
2019-04-23 13:35:26 +00:00
|
|
|
createdAt: following.createdAt.toISOString(),
|
2019-04-07 12:50:36 +00:00
|
|
|
followeeId: following.followeeId,
|
|
|
|
followerId: following.followerId,
|
2022-09-17 18:27:08 +00:00
|
|
|
followee: opts.populateFollowee ? this.userEntityService.pack(following.followee ?? following.followeeId, me, {
|
2021-12-09 14:58:30 +00:00
|
|
|
detail: true,
|
2019-04-23 13:35:26 +00:00
|
|
|
}) : undefined,
|
2022-09-17 18:27:08 +00:00
|
|
|
follower: opts.populateFollower ? this.userEntityService.pack(following.follower ?? following.followerId, me, {
|
2021-12-09 14:58:30 +00:00
|
|
|
detail: true,
|
2019-04-23 13:35:26 +00:00
|
|
|
}) : undefined,
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2019-04-25 04:27:07 +00:00
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public packMany(
|
2019-04-25 04:27:07 +00:00
|
|
|
followings: any[],
|
2021-03-24 02:05:37 +00:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-25 04:27:07 +00:00
|
|
|
opts?: {
|
|
|
|
populateFollowee?: boolean;
|
|
|
|
populateFollower?: boolean;
|
2022-09-17 18:27:08 +00:00
|
|
|
},
|
2019-04-25 04:27:07 +00:00
|
|
|
) {
|
|
|
|
return Promise.all(followings.map(x => this.pack(x, me, opts)));
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|