2020-08-18 15:44:21 +02:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { Channel } from '@/models/entities/channel.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
|
|
|
import { DriveFiles, ChannelFollowings, NoteUnreads } from '../index.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
2020-08-18 15:44:21 +02:00
|
|
|
|
|
|
|
@EntityRepository(Channel)
|
|
|
|
export class ChannelRepository extends Repository<Channel> {
|
|
|
|
public async pack(
|
|
|
|
src: Channel['id'] | Channel,
|
2021-03-24 03:05:37 +01:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2021-09-22 15:35:55 +02:00
|
|
|
): Promise<Packed<'Channel'>> {
|
2021-02-13 07:33:38 +01:00
|
|
|
const channel = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
2021-03-24 03:05:37 +01:00
|
|
|
const meId = me ? me.id : null;
|
2020-08-18 15:44:21 +02:00
|
|
|
|
|
|
|
const banner = channel.bannerId ? await DriveFiles.findOne(channel.bannerId) : null;
|
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
const hasUnreadNote = meId ? (await NoteUnreads.findOne({ noteChannelId: channel.id, userId: meId })) != null : undefined;
|
2020-08-18 15:44:21 +02:00
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
const following = meId ? await ChannelFollowings.findOne({
|
2020-08-18 15:44:21 +02:00
|
|
|
followerId: meId,
|
|
|
|
followeeId: channel.id,
|
2021-03-24 03:05:37 +01:00
|
|
|
}) : null;
|
2020-08-18 15:44:21 +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,
|
|
|
|
bannerUrl: banner ? DriveFiles.getPublicUrl(banner, false) : null,
|
|
|
|
usersCount: channel.usersCount,
|
|
|
|
notesCount: channel.notesCount,
|
|
|
|
|
|
|
|
...(me ? {
|
|
|
|
isFollowing: following != null,
|
|
|
|
hasUnreadNote,
|
2021-12-09 15:58:30 +01:00
|
|
|
} : {}),
|
2020-08-18 15:44:21 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|