firefish/packages/backend/src/models/repositories/channel.ts

57 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-01-13 04:40:33 +00:00
import { db } from "@/db/postgre.js";
2023-11-26 20:33:46 +00:00
import { Channel } from "@/models/entities/channel.js";
import type { Packed } from "@/misc/schema.js";
import { DriveFiles, ChannelFollowings, NoteUnreads } from "../index.js";
2023-01-13 04:40:33 +00:00
import type { User } from "@/models/entities/user.js";
export const ChannelRepository = db.getRepository(Channel).extend({
async pack(
2023-01-13 04:40:33 +00:00
src: Channel["id"] | Channel,
me?: { id: User["id"] } | null | undefined,
): Promise<Packed<"Channel">> {
const channel =
typeof src === "object" ? src : await this.findOneByOrFail({ id: src });
const meId = me ? me.id : null;
2023-01-13 04:40:33 +00:00
const banner = channel.bannerId
? await DriveFiles.findOneBy({ id: channel.bannerId })
: null;
2023-01-13 04:40:33 +00:00
const hasUnreadNote = meId
? (await NoteUnreads.findOneBy({
noteChannelId: channel.id,
userId: meId,
})) != null
2023-01-13 04:40:33 +00:00
: undefined;
2023-01-13 04:40:33 +00:00
const following = meId
? await ChannelFollowings.findOneBy({
followerId: meId,
followeeId: channel.id,
})
2023-01-13 04:40:33 +00:00
: null;
return {
id: channel.id,
createdAt: channel.createdAt.toISOString(),
2023-01-13 04:40:33 +00:00
lastNotedAt: channel.lastNotedAt
? channel.lastNotedAt.toISOString()
: null,
name: channel.name,
description: channel.description,
userId: channel.userId,
bannerId: channel.bannerId,
bannerUrl: banner ? DriveFiles.getPublicUrl(banner, false) : null,
usersCount: channel.usersCount,
notesCount: channel.notesCount,
2023-01-13 04:40:33 +00:00
...(me
? {
isFollowing: following != null,
hasUnreadNote,
}
2023-01-13 04:40:33 +00:00
: {}),
};
},
});