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-03-31 08:01:56 +02:00
|
|
|
import type { ChannelFavoritesRepository, ChannelFollowingsRepository, ChannelsRepository, DriveFilesRepository, NoteUnreadsRepository, NotesRepository } from '@/models/index.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-17 20:27:08 +02: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 06:22:37 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DriveFileEntityService } from './DriveFileEntityService.js';
|
2023-03-31 08:01:56 +02:00
|
|
|
import { NoteEntityService } from './NoteEntityService.js';
|
|
|
|
import { In } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ChannelEntityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.channelsRepository)
|
|
|
|
private channelsRepository: ChannelsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.channelFollowingsRepository)
|
|
|
|
private channelFollowingsRepository: ChannelFollowingsRepository,
|
|
|
|
|
2023-03-31 04:30:27 +02:00
|
|
|
@Inject(DI.channelFavoritesRepository)
|
|
|
|
private channelFavoritesRepository: ChannelFavoritesRepository,
|
|
|
|
|
2023-03-31 08:01:56 +02:00
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.noteUnreadsRepository)
|
|
|
|
private noteUnreadsRepository: NoteUnreadsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
2023-03-31 08:01:56 +02:00
|
|
|
private noteEntityService: NoteEntityService,
|
2022-09-17 20:27:08 +02:00
|
|
|
private driveFileEntityService: DriveFileEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async pack(
|
|
|
|
src: Channel['id'] | Channel,
|
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2023-03-31 08:01:56 +02:00
|
|
|
detailed?: boolean,
|
2022-09-17 20:27:08 +02: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;
|
|
|
|
|
2023-07-11 07:58:58 +02:00
|
|
|
const hasUnreadNote = meId ? await this.noteUnreadsRepository.exist({
|
|
|
|
where: {
|
|
|
|
noteChannelId: channel.id,
|
2023-07-19 04:27:50 +02:00
|
|
|
userId: meId,
|
2023-07-11 07:58:58 +02:00
|
|
|
},
|
|
|
|
}) : undefined;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-07-11 07:58:58 +02:00
|
|
|
const isFollowing = meId ? await this.channelFollowingsRepository.exist({
|
|
|
|
where: {
|
|
|
|
followerId: meId,
|
|
|
|
followeeId: channel.id,
|
|
|
|
},
|
|
|
|
}) : false;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-07-11 07:58:58 +02:00
|
|
|
const isFavorited = meId ? await this.channelFavoritesRepository.exist({
|
|
|
|
where: {
|
|
|
|
userId: meId,
|
|
|
|
channelId: channel.id,
|
|
|
|
},
|
|
|
|
}) : false;
|
2023-03-31 04:30:27 +02:00
|
|
|
|
2023-03-31 08:01:56 +02:00
|
|
|
const pinnedNotes = channel.pinnedNoteIds.length > 0 ? await this.notesRepository.find({
|
|
|
|
where: {
|
|
|
|
id: In(channel.pinnedNoteIds),
|
|
|
|
},
|
|
|
|
}) : [];
|
|
|
|
|
2022-09-17 20:27:08 +02: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 05:38:51 +01:00
|
|
|
bannerUrl: banner ? this.driveFileEntityService.getPublicUrl(banner) : null,
|
2023-03-31 08:01:56 +02:00
|
|
|
pinnedNoteIds: channel.pinnedNoteIds,
|
2023-05-02 02:36:40 +02:00
|
|
|
color: channel.color,
|
2023-05-06 01:15:17 +02:00
|
|
|
isArchived: channel.isArchived,
|
2022-09-17 20:27:08 +02:00
|
|
|
usersCount: channel.usersCount,
|
|
|
|
notesCount: channel.notesCount,
|
2023-08-05 06:58:31 +02:00
|
|
|
isSensitive: channel.isSensitive,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
...(me ? {
|
2023-07-11 07:58:58 +02:00
|
|
|
isFollowing,
|
|
|
|
isFavorited,
|
2022-09-17 20:27:08 +02:00
|
|
|
hasUnreadNote,
|
|
|
|
} : {}),
|
2023-03-31 08:01:56 +02:00
|
|
|
|
|
|
|
...(detailed ? {
|
2023-04-13 01:52:30 +02:00
|
|
|
pinnedNotes: (await this.noteEntityService.packMany(pinnedNotes, me)).sort((a, b) => channel.pinnedNoteIds.indexOf(a.id) - channel.pinnedNoteIds.indexOf(b.id)),
|
2023-03-31 08:01:56 +02:00
|
|
|
} : {}),
|
2022-09-17 20:27:08 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|