2020-08-18 13:44:21 +00:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
2021-08-19 09:33:41 +00:00
|
|
|
import { Channel } from '../entities/channel.js';
|
|
|
|
import { SchemaType } from '@/misc/schema.js';
|
|
|
|
import { DriveFiles, ChannelFollowings, NoteUnreads } from '../index.js';
|
|
|
|
import { User } from '../entities/user.js';
|
2020-08-18 13:44:21 +00:00
|
|
|
|
|
|
|
export type PackedChannel = SchemaType<typeof packedChannelSchema>;
|
|
|
|
|
|
|
|
@EntityRepository(Channel)
|
|
|
|
export class ChannelRepository extends Repository<Channel> {
|
|
|
|
public async pack(
|
|
|
|
src: Channel['id'] | Channel,
|
2021-03-24 02:05:37 +00:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2020-08-18 13:44:21 +00:00
|
|
|
): Promise<PackedChannel> {
|
2021-02-13 06:33:38 +00:00
|
|
|
const channel = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
2021-03-24 02:05:37 +00:00
|
|
|
const meId = me ? me.id : null;
|
2020-08-18 13:44:21 +00:00
|
|
|
|
|
|
|
const banner = channel.bannerId ? await DriveFiles.findOne(channel.bannerId) : null;
|
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
const hasUnreadNote = meId ? (await NoteUnreads.findOne({ noteChannelId: channel.id, userId: meId })) != null : undefined;
|
2020-08-18 13:44:21 +00:00
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
const following = meId ? await ChannelFollowings.findOne({
|
2020-08-18 13:44:21 +00:00
|
|
|
followerId: meId,
|
|
|
|
followeeId: channel.id,
|
2021-03-24 02:05:37 +00:00
|
|
|
}) : null;
|
2020-08-18 13:44:21 +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,
|
|
|
|
bannerUrl: banner ? DriveFiles.getPublicUrl(banner, false) : null,
|
|
|
|
usersCount: channel.usersCount,
|
|
|
|
notesCount: channel.notesCount,
|
|
|
|
|
|
|
|
...(me ? {
|
|
|
|
isFollowing: following != null,
|
|
|
|
hasUnreadNote,
|
|
|
|
} : {})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const packedChannelSchema = {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
properties: {
|
|
|
|
id: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
format: 'date-time',
|
|
|
|
},
|
|
|
|
lastNotedAt: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
|
|
|
format: 'date-time',
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
type: 'string' as const,
|
|
|
|
nullable: true as const, optional: false as const,
|
|
|
|
},
|
|
|
|
bannerUrl: {
|
|
|
|
type: 'string' as const,
|
|
|
|
format: 'url',
|
|
|
|
nullable: true as const, optional: false as const,
|
|
|
|
},
|
|
|
|
notesCount: {
|
|
|
|
type: 'number' as const,
|
|
|
|
nullable: false as const, optional: false as const,
|
|
|
|
},
|
|
|
|
usersCount: {
|
|
|
|
type: 'number' as const,
|
|
|
|
nullable: false as const, optional: false as const,
|
|
|
|
},
|
|
|
|
isFollowing: {
|
|
|
|
type: 'boolean' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
|
|
|
},
|
|
|
|
userId: {
|
|
|
|
type: 'string' as const,
|
2021-08-18 13:04:04 +00:00
|
|
|
nullable: true as const, optional: false as const,
|
2020-08-18 13:44:21 +00:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|