2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-03-31 06:01:56 +00:00
|
|
|
import type { ChannelFavoritesRepository, ChannelFollowingsRepository, ChannelsRepository, DriveFilesRepository, NoteUnreadsRepository, NotesRepository } from '@/models/index.js';
|
2023-03-10 05:22:37 +00:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { } from '@/models/entities/Blocking.js';
|
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { Channel } from '@/models/entities/Channel.js';
|
2023-03-10 05:22:37 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DriveFileEntityService } from './DriveFileEntityService.js';
|
2023-03-31 06:01:56 +00:00
|
|
|
import { NoteEntityService } from './NoteEntityService.js';
|
|
|
|
import { In } from 'typeorm';
|
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,
|
|
|
|
|
2023-03-31 02:30:27 +00:00
|
|
|
@Inject(DI.channelFavoritesRepository)
|
|
|
|
private channelFavoritesRepository: ChannelFavoritesRepository,
|
|
|
|
|
2023-03-31 06:01:56 +00:00
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
@Inject(DI.noteUnreadsRepository)
|
|
|
|
private noteUnreadsRepository: NoteUnreadsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
2023-03-31 06:01:56 +00:00
|
|
|
private noteEntityService: NoteEntityService,
|
2022-09-17 18:27:08 +00:00
|
|
|
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,
|
2023-03-31 06:01:56 +00:00
|
|
|
detailed?: boolean,
|
2022-09-17 18:27:08 +00:00
|
|
|
): 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;
|
|
|
|
|
2023-03-31 02:30:27 +00:00
|
|
|
const favorite = meId ? await this.channelFavoritesRepository.findOneBy({
|
|
|
|
userId: meId,
|
|
|
|
channelId: channel.id,
|
|
|
|
}) : null;
|
|
|
|
|
2023-03-31 06:01:56 +00:00
|
|
|
const pinnedNotes = channel.pinnedNoteIds.length > 0 ? await this.notesRepository.find({
|
|
|
|
where: {
|
|
|
|
id: In(channel.pinnedNoteIds),
|
|
|
|
},
|
|
|
|
}) : [];
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return {
|
|
|
|
id: channel.id,
|
|
|
|
createdAt: channel.createdAt.toISOString(),
|
|
|
|
lastNotedAt: channel.lastNotedAt ? channel.lastNotedAt.toISOString() : null,
|
|
|
|
name: channel.name,
|
|
|
|
description: channel.description,
|
|
|
|
userId: channel.userId,
|
2023-02-04 04:38:51 +00:00
|
|
|
bannerUrl: banner ? this.driveFileEntityService.getPublicUrl(banner) : null,
|
2023-03-31 06:01:56 +00:00
|
|
|
pinnedNoteIds: channel.pinnedNoteIds,
|
2023-05-02 00:36:40 +00:00
|
|
|
color: channel.color,
|
2022-09-17 18:27:08 +00:00
|
|
|
usersCount: channel.usersCount,
|
|
|
|
notesCount: channel.notesCount,
|
|
|
|
|
|
|
|
...(me ? {
|
|
|
|
isFollowing: following != null,
|
2023-03-31 02:30:27 +00:00
|
|
|
isFavorited: favorite != null,
|
2022-09-17 18:27:08 +00:00
|
|
|
hasUnreadNote,
|
|
|
|
} : {}),
|
2023-03-31 06:01:56 +00:00
|
|
|
|
|
|
|
...(detailed ? {
|
2023-04-12 23:52:30 +00:00
|
|
|
pinnedNotes: (await this.noteEntityService.packMany(pinnedNotes, me)).sort((a, b) => channel.pinnedNoteIds.indexOf(a.id) - channel.pinnedNoteIds.indexOf(b.id)),
|
2023-03-31 06:01:56 +00:00
|
|
|
} : {}),
|
2022-09-17 18:27:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|