1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Inject, Injectable } from '@nestjs/common';
import { In } from 'typeorm';
import { DI } from '@/di-symbols.js';
import type {
ChannelFavoritesRepository,
ChannelFollowingsRepository, ChannelMutingRepository,
ChannelsRepository,
DriveFilesRepository,
MiDriveFile,
MiNote,
NotesRepository,
} from '@/models/_.js';
import type { Packed } from '@/misc/json-schema.js';
import type { MiUser } from '@/models/User.js';
import type { MiChannel } from '@/models/Channel.js';
import { bindThis } from '@/decorators.js';
import { IdService } from '@/core/IdService.js';
import { DriveFileEntityService } from './DriveFileEntityService.js';
import { NoteEntityService } from './NoteEntityService.js';
@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.channelMutingRepository)
private channelMutingRepository: ChannelMutingRepository,
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
private noteEntityService: NoteEntityService,
private driveFileEntityService: DriveFileEntityService,
private idService: IdService,
) {
}
@bindThis
public async pack(
src: MiChannel['id'] | MiChannel,
me?: { id: MiUser['id'] } | null | undefined,
detailed?: boolean,
opts?: {
bannerFiles?: Map<MiDriveFile['id'], MiDriveFile>;
followings?: Set<MiChannel['id']>;
favorites?: Set<MiChannel['id']>;
muting?: Set<MiChannel['id']>;
pinnedNotes?: Map<MiNote['id'], MiNote>;
},
): Promise<Packed<'Channel'>> {
const channel = typeof src === 'object' ? src : await this.channelsRepository.findOneByOrFail({ id: src });
let bannerFile: MiDriveFile | null = null;
if (channel.bannerId) {
bannerFile = opts?.bannerFiles?.get(channel.bannerId)
?? await this.driveFilesRepository.findOneByOrFail({ id: channel.bannerId });
}
let isFollowing = false;
let isFavorited = false;
let isMuting = false;
if (me) {
isFollowing = opts?.followings?.has(channel.id) ?? await this.channelFollowingsRepository.exists({
where: {
followerId: me.id,
followeeId: channel.id,
},
});
isFavorited = opts?.favorites?.has(channel.id) ?? await this.channelFavoritesRepository.exists({
where: {
userId: me.id,
channelId: channel.id,
},
});
isMuting = opts?.muting?.has(channel.id) ?? await this.channelMutingRepository.exists({
where: {
userId: me.id,
channelId: channel.id,
},
});
}
const pinnedNotes = Array.of<MiNote>();
if (channel.pinnedNoteIds.length > 0) {
pinnedNotes.push(
...(
opts?.pinnedNotes
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
? channel.pinnedNoteIds.map(it => opts.pinnedNotes!.get(it)).filter(it => it != null)
: await this.notesRepository.findBy({ id: In(channel.pinnedNoteIds) })
),
);
}
return {
id: channel.id,
createdAt: this.idService.parse(channel.id).date.toISOString(),
lastNotedAt: channel.lastNotedAt ? channel.lastNotedAt.toISOString() : null,
name: channel.name,
description: channel.description,
userId: channel.userId,
bannerUrl: bannerFile ? this.driveFileEntityService.getPublicUrl(bannerFile) : null,
bannerId: channel.bannerId,
pinnedNoteIds: channel.pinnedNoteIds,
color: channel.color,
isArchived: channel.isArchived,
usersCount: channel.usersCount,
notesCount: channel.notesCount,
isSensitive: channel.isSensitive,
allowRenoteToExternal: channel.allowRenoteToExternal,
...(me ? {
isFollowing,
isFavorited,
isMuting,
hasUnreadNote: false, // 後方互換性のため
} : {}),
...(detailed ? {
pinnedNotes: (await this.noteEntityService.packMany(pinnedNotes, me)).sort((a, b) => channel.pinnedNoteIds.indexOf(a.id) - channel.pinnedNoteIds.indexOf(b.id)),
} : {}),
};
}
@bindThis
public async packMany(
src: MiChannel['id'][] | MiChannel[],
me?: { id: MiUser['id'] } | null | undefined,
detailed?: boolean,
): Promise<Packed<'Channel'>[]> {
// IDのみの要素がある場合、DBからオブジェクトを取得して補う
const channels = src.filter(it => typeof it === 'object') as MiChannel[];
channels.push(
...(await this.channelsRepository.find({
where: {
id: In(src.filter(it => typeof it !== 'object') as MiChannel['id'][]),
},
})),
);
channels.sort((a, b) => a.id.localeCompare(b.id));
const bannerFiles = await this.driveFilesRepository
.findBy({
id: In(channels.map(it => it.bannerId).filter(it => it != null)),
})
.then(it => new Map(it.map(it => [it.id, it])));
const followings = me
? await this.channelFollowingsRepository
.findBy({
followerId: me.id,
followeeId: In(channels.map(it => it.id)),
})
.then(it => new Set(it.map(it => it.followeeId)))
: new Set<MiChannel['id']>();
const favorites = me
? await this.channelFavoritesRepository
.findBy({
userId: me.id,
channelId: In(channels.map(it => it.id)),
})
.then(it => new Set(it.map(it => it.channelId)))
: new Set<MiChannel['id']>();
const muting = me
? await this.channelMutingRepository
.findBy({
userId: me.id,
channelId: In(channels.map(it => it.id)),
})
.then(it => new Set(it.map(it => it.channelId)))
: new Set<MiChannel['id']>();
const pinnedNotes = await this.notesRepository
.find({
where: {
id: In(channels.flatMap(it => it.pinnedNoteIds)),
},
})
.then(it => new Map(it.map(it => [it.id, it])));
return Promise.all(channels.map(it => this.pack(it, me, detailed, {
bannerFiles,
followings,
favorites,
muting,
pinnedNotes,
})));
}
}
|