2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 05:28:29 +00:00
|
|
|
import type { ChannelFavoritesRepository, ChannelFollowingsRepository, ChannelsRepository, DriveFilesRepository, NoteUnreadsRepository, NotesRepository } from '@/models/_.js';
|
2023-03-10 05:22:37 +00:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2023-09-20 02:33:36 +00:00
|
|
|
import type { } from '@/models/Blocking.js';
|
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiChannel } from '@/models/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(
|
2023-08-16 08:51:28 +00:00
|
|
|
src: MiChannel['id'] | MiChannel,
|
|
|
|
me?: { id: MiUser['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;
|
|
|
|
|
2023-07-11 05:58:58 +00:00
|
|
|
const hasUnreadNote = meId ? await this.noteUnreadsRepository.exist({
|
|
|
|
where: {
|
|
|
|
noteChannelId: channel.id,
|
2023-07-19 02:27:50 +00:00
|
|
|
userId: meId,
|
2023-07-11 05:58:58 +00:00
|
|
|
},
|
|
|
|
}) : undefined;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2023-07-11 05:58:58 +00:00
|
|
|
const isFollowing = meId ? await this.channelFollowingsRepository.exist({
|
|
|
|
where: {
|
|
|
|
followerId: meId,
|
|
|
|
followeeId: channel.id,
|
|
|
|
},
|
|
|
|
}) : false;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2023-07-11 05:58:58 +00:00
|
|
|
const isFavorited = meId ? await this.channelFavoritesRepository.exist({
|
|
|
|
where: {
|
|
|
|
userId: meId,
|
|
|
|
channelId: channel.id,
|
|
|
|
},
|
|
|
|
}) : false;
|
2023-03-31 02:30:27 +00:00
|
|
|
|
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,
|
2023-05-05 23:15:17 +00:00
|
|
|
isArchived: channel.isArchived,
|
2022-09-17 18:27:08 +00:00
|
|
|
usersCount: channel.usersCount,
|
|
|
|
notesCount: channel.notesCount,
|
2023-08-05 04:58:31 +00:00
|
|
|
isSensitive: channel.isSensitive,
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
...(me ? {
|
2023-07-11 05:58:58 +00:00
|
|
|
isFollowing,
|
|
|
|
isFavorited,
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|