Sharkey/packages/backend/src/core/entities/ChannelEntityService.ts

103 lines
3.2 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* 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';
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';
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';
import { IdService } from '@/core/IdService.js';
2022-09-17 20:27:08 +02:00
import { DriveFileEntityService } from './DriveFileEntityService.js';
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,
@Inject(DI.channelFavoritesRepository)
private channelFavoritesRepository: ChannelFavoritesRepository,
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
2022-09-17 20:27:08 +02:00
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
private noteEntityService: NoteEntityService,
2022-09-17 20:27:08 +02:00
private driveFileEntityService: DriveFileEntityService,
private idService: IdService,
2022-09-17 20:27:08 +02:00
) {
}
@bindThis
2022-09-17 20:27:08 +02:00
public async pack(
src: MiChannel['id'] | MiChannel,
me?: { id: MiUser['id'] } | null | undefined,
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;
const isFollowing = meId ? await this.channelFollowingsRepository.exists({
where: {
followerId: meId,
followeeId: channel.id,
},
}) : false;
2022-09-17 20:27:08 +02:00
const isFavorited = meId ? await this.channelFavoritesRepository.exists({
where: {
userId: meId,
channelId: channel.id,
},
}) : false;
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: 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,
bannerUrl: banner ? this.driveFileEntityService.getPublicUrl(banner) : null,
pinnedNoteIds: channel.pinnedNoteIds,
color: channel.color,
isArchived: channel.isArchived,
2022-09-17 20:27:08 +02:00
usersCount: channel.usersCount,
notesCount: channel.notesCount,
isSensitive: channel.isSensitive,
allowRenoteToExternal: channel.allowRenoteToExternal,
2022-09-17 20:27:08 +02:00
...(me ? {
isFollowing,
isFavorited,
hasUnreadNote: false, // 後方互換性のため
2022-09-17 20:27:08 +02:00
} : {}),
...(detailed ? {
pinnedNotes: (await this.noteEntityService.packMany(pinnedNotes, me)).sort((a, b) => channel.pinnedNoteIds.indexOf(a.id) - channel.pinnedNoteIds.indexOf(b.id)),
} : {}),
2022-09-17 20:27:08 +02:00
};
}
}