1
1
import ChannelNotFoundException from '#apps/channels/exceptions/channel_not_found_exception'
2
2
import Channel from '#apps/channels/models/channel'
3
- import { ChannelType } from '#apps/channels/models/channel_type'
3
+ import { ChannelType , channelTypeToString } from '#apps/channels/models/channel_type'
4
4
import { CachedUser , OccupiedChannel } from '#apps/channels/models/occupied_channels'
5
5
import {
6
6
CreateChannelSchema ,
@@ -20,6 +20,7 @@ import logger from '@adonisjs/core/services/logger'
20
20
import redis from '@adonisjs/redis/services/main'
21
21
import transmit from '@adonisjs/transmit/services/main'
22
22
import jwt from 'jsonwebtoken'
23
+ import ChannelWithIncoherentHierarchyException from '../exceptions/channel_cant_be_children_exception.js'
23
24
24
25
export interface PayloadJWTSFUConnection {
25
26
channelSn ?: string
@@ -54,6 +55,14 @@ export default class ChannelService {
54
55
return server . channels
55
56
}
56
57
58
+ async findAllChannelsByServerWithChildren ( serverId : string ) : Promise < Channel [ ] > {
59
+ const channels = await Channel . query ( )
60
+ . whereNull ( 'parentId' )
61
+ . where ( 'serverId' , serverId )
62
+ . preload ( 'childrens' )
63
+ return channels
64
+ }
65
+
57
66
async findPrivateOrderedForUserOrFail ( userId : string ) : Promise < Channel [ ] > {
58
67
await User . findOrFail ( userId ) . catch ( ( ) => {
59
68
throw new UserNotFoundException ( 'User not found' , { status : 404 , code : 'E_ROW_NOT_FOUND' } )
@@ -160,20 +169,63 @@ export default class ChannelService {
160
169
serverId : string ,
161
170
userId : string
162
171
) : Promise < Channel > {
163
- const sn = generateSnowflake ( )
164
172
const type = newChannel . type as ChannelType
173
+ if ( newChannel . parentId ) {
174
+ if ( type === ChannelType . PRIVATE_CHAT || type === ChannelType . FOLDER_SERVER ) {
175
+ throw new ChannelWithIncoherentHierarchyException (
176
+ `Channel with type ${ channelTypeToString ( type ) } can't have a parent channel` ,
177
+ {
178
+ status : 422 ,
179
+ code : 'E_WRONG_HIERARCHY' ,
180
+ }
181
+ )
182
+ }
183
+
184
+ let parent : Channel
185
+ try {
186
+ parent = await Channel . findOrFail ( newChannel . parentId )
187
+ } catch ( e ) {
188
+ logger . error ( e )
189
+ throw new ChannelNotFoundException ( 'Parent channel not found' , {
190
+ status : 404 ,
191
+ code : 'E_ROWNOTFOUND' ,
192
+ } )
193
+ }
194
+
195
+ if ( ( parent . type as ChannelType ) !== ChannelType . FOLDER_SERVER ) {
196
+ throw new ChannelWithIncoherentHierarchyException (
197
+ `Parent channel is not of type FOLDER_SERVER` ,
198
+ {
199
+ status : 422 ,
200
+ code : 'E_WRONG_HIERARCHY' ,
201
+ }
202
+ )
203
+ }
204
+ }
205
+
206
+ const sn = generateSnowflake ( )
165
207
const firstChannel = await Channel . query ( )
166
- . where ( 'serverId ' , serverId )
208
+ . where ( 'server_id ' , serverId )
167
209
. orderBy ( 'position' )
168
210
. first ( )
169
211
const position = firstChannel != null ? firstChannel . position - 1 : 0
170
- const channel = await Channel . create ( {
171
- name : newChannel . name ,
172
- type : type ,
173
- serverId : serverId ,
174
- serialNumber : sn ,
175
- position,
176
- } )
212
+
213
+ const channel = newChannel . parentId
214
+ ? await Channel . create ( {
215
+ name : newChannel . name ,
216
+ type : type ,
217
+ serverId : serverId ,
218
+ serialNumber : sn ,
219
+ position,
220
+ parentId : newChannel . parentId ,
221
+ } )
222
+ : await Channel . create ( {
223
+ name : newChannel . name ,
224
+ type : type ,
225
+ serverId : serverId ,
226
+ serialNumber : sn ,
227
+ position,
228
+ } )
177
229
logger . info ( 'Created new channel : ' , channel )
178
230
await channel . related ( 'users' ) . attach ( [ userId ] )
179
231
return channel
@@ -186,7 +238,9 @@ export default class ChannelService {
186
238
code : 'E_ROWNOTFOUND' ,
187
239
} )
188
240
} )
241
+ logger . info ( 'Updating channel : ' , payload )
189
242
channel . merge ( payload )
243
+ logger . info ( channel . toJSON ( ) )
190
244
await redis . del ( `channel:${ id } ` )
191
245
return channel . save ( )
192
246
}
0 commit comments