2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* 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-11-01 05:55:19 +01:00
|
|
|
import type { ChannelFavoritesRepository, ChannelFollowingsRepository, ChannelsRepository, DriveFilesRepository, NotesRepository } from '@/models/_.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { } from '@/models/Blocking.js';
|
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiChannel } from '@/models/Channel.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-10-16 03:45:22 +02:00
|
|
|
import { IdService } from '@/core/IdService.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';
|
2023-11-01 07:32:35 +01:00
|
|
|
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.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,
|
2023-10-16 03:45:22 +02:00
|
|
|
private idService: IdService,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async pack(
|
2023-08-16 10:51:28 +02:00
|
|
|
src: MiChannel['id'] | MiChannel,
|
|
|
|
me?: { id: MiUser['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;
|
|
|
|
|
2024-02-08 08:04:41 +01:00
|
|
|
const isFollowing = meId ? await this.channelFollowingsRepository.exists({
|
2023-07-11 07:58:58 +02:00
|
|
|
where: {
|
|
|
|
followerId: meId,
|
|
|
|
followeeId: channel.id,
|
|
|
|
},
|
|
|
|
}) : false;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2024-02-08 08:04:41 +01:00
|
|
|
const isFavorited = meId ? await this.channelFavoritesRepository.exists({
|
2023-07-11 07:58:58 +02:00
|
|
|
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,
|
2023-10-16 03:45:22 +02:00
|
|
|
createdAt: this.idService.parse(channel.id).date.toISOString(),
|
2022-09-17 20:27:08 +02:00
|
|
|
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,
|
2023-11-03 09:34:23 +01:00
|
|
|
allowRenoteToExternal: channel.allowRenoteToExternal,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
...(me ? {
|
2023-07-11 07:58:58 +02:00
|
|
|
isFollowing,
|
|
|
|
isFavorited,
|
2023-11-01 05:55:19 +01:00
|
|
|
hasUnreadNote: false, // 後方互換性のため
|
2022-09-17 20:27:08 +02:00
|
|
|
} : {}),
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|