2022-03-26 06:34:00 +00:00
|
|
|
import { db } from '@/db/postgre.js';
|
2022-02-27 02:07:39 +00: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 13:44:21 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
export const ChannelRepository = db.getRepository(Channel).extend({
|
|
|
|
async pack(
|
2020-08-18 13:44:21 +00:00
|
|
|
src: Channel['id'] | Channel,
|
2021-03-24 02:05:37 +00:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2021-09-22 13:35:55 +00:00
|
|
|
): Promise<Packed<'Channel'>> {
|
2022-03-26 06:34:00 +00:00
|
|
|
const channel = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
2021-03-24 02:05:37 +00:00
|
|
|
const meId = me ? me.id : null;
|
2020-08-18 13:44:21 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const banner = channel.bannerId ? await DriveFiles.findOneBy({ id: channel.bannerId }) : null;
|
2020-08-18 13:44:21 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const hasUnreadNote = meId ? (await NoteUnreads.findOneBy({ noteChannelId: channel.id, userId: meId })) != null : undefined;
|
2020-08-18 13:44:21 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const following = meId ? await ChannelFollowings.findOneBy({
|
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,
|
2021-12-09 14:58:30 +00:00
|
|
|
} : {}),
|
2020-08-18 13:44:21 +00:00
|
|
|
};
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
|
|
|
});
|