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 { ChannelFollowingsRepository, ChannelsRepository, DriveFilesRepository, NoteUnreadsRepository } 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 { Channel } from '@/models/entities/Channel.js';
|
|
|
|
import { UserEntityService } from './UserEntityService.js';
|
|
|
|
import { DriveFileEntityService } from './DriveFileEntityService.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ChannelEntityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.channelsRepository)
|
|
|
|
private channelsRepository: ChannelsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.channelFollowingsRepository)
|
|
|
|
private channelFollowingsRepository: ChannelFollowingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.noteUnreadsRepository)
|
|
|
|
private noteUnreadsRepository: NoteUnreadsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private driveFileEntityService: DriveFileEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async pack(
|
|
|
|
src: Channel['id'] | Channel,
|
|
|
|
me?: { id: User['id'] } | null | undefined,
|
|
|
|
): Promise<Packed<'Channel'>> {
|
|
|
|
const channel = typeof src === 'object' ? src : await this.channelsRepository.findOneByOrFail({ id: src });
|
|
|
|
const meId = me ? me.id : null;
|
|
|
|
|
|
|
|
const banner = channel.bannerId ? await this.driveFilesRepository.findOneBy({ id: channel.bannerId }) : null;
|
|
|
|
|
|
|
|
const hasUnreadNote = meId ? (await this.noteUnreadsRepository.findOneBy({ noteChannelId: channel.id, userId: meId })) != null : undefined;
|
|
|
|
|
|
|
|
const following = meId ? await this.channelFollowingsRepository.findOneBy({
|
|
|
|
followerId: meId,
|
|
|
|
followeeId: channel.id,
|
|
|
|
}) : null;
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: channel.id,
|
|
|
|
createdAt: channel.createdAt.toISOString(),
|
|
|
|
lastNotedAt: channel.lastNotedAt ? channel.lastNotedAt.toISOString() : null,
|
|
|
|
name: channel.name,
|
|
|
|
description: channel.description,
|
|
|
|
userId: channel.userId,
|
|
|
|
bannerUrl: banner ? this.driveFileEntityService.getPublicUrl(banner, false) : null,
|
|
|
|
usersCount: channel.usersCount,
|
|
|
|
notesCount: channel.notesCount,
|
|
|
|
|
|
|
|
...(me ? {
|
|
|
|
isFollowing: following != null,
|
|
|
|
hasUnreadNote,
|
|
|
|
} : {}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|