File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ import type { ServerUserData } from '@cord-sdk/types' ;
2
+ import type { Request , Response } from 'express' ;
3
+ import { fetchCordRESTApi } from 'src/server/fetchCordRESTApi' ;
4
+
5
+ export async function handleAddChannel ( req : Request , res : Response ) {
6
+ const { user_id : requesterID } = ( req as any ) . loginTokenData ;
7
+ const { channelName } = req . params ;
8
+ const { isPrivate } = req . body ;
9
+
10
+ // If userIDs is sent, it's a private channel, which means we need to create
11
+ // an org for its participants. If no userIDs are sent, it's a public channel
12
+ // which means it will be associated with the clack_all org.
13
+
14
+ if ( isPrivate ) {
15
+ await fetchCordRESTApi <
16
+ Promise < {
17
+ success : boolean ;
18
+ message : string ;
19
+ } >
20
+ > (
21
+ `organizations/${ channelName } ` ,
22
+ 'PUT' ,
23
+ JSON . stringify ( {
24
+ name : channelName ,
25
+ members : [ requesterID ] ,
26
+ } ) ,
27
+ ) ;
28
+ }
29
+
30
+ const existingChannels = (
31
+ await fetchCordRESTApi < ServerUserData > ( `users/all_channels_holder` , 'GET' )
32
+ ) . metadata ;
33
+
34
+ const response = await fetchCordRESTApi <
35
+ Promise < {
36
+ success : boolean ;
37
+ message : string ;
38
+ } >
39
+ > (
40
+ `users/all_channels_holder` ,
41
+ 'PUT' ,
42
+ JSON . stringify ( {
43
+ metadata : {
44
+ [ channelName ] : isPrivate ? channelName : '' ,
45
+ ...existingChannels ,
46
+ } ,
47
+ } ) ,
48
+ ) ;
49
+
50
+ res . send ( { success : response . success } ) ;
51
+ }
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ import {
24
24
handleAddOrgMember ,
25
25
handleRemoveOrgMember ,
26
26
} from 'src/server/handlers/orgMembers' ;
27
+ import { handleAddChannel } from 'src/server/handlers/updateChannels' ;
27
28
28
29
const REPO_ROOT = path . join ( __dirname , '..' , '..' ) ;
29
30
dotenv . config ( { path : path . join ( REPO_ROOT , '.env' ) } ) ;
@@ -88,6 +89,8 @@ function main() {
88
89
wrapAsyncHandler ( handleRemoveOrgMember ) ,
89
90
) ;
90
91
92
+ app . put ( '/channels/:channelName' , wrapAsyncHandler ( handleAddChannel ) ) ;
93
+
91
94
// Catch errors and log them
92
95
app . use (
93
96
'/' ,
You can’t perform that action at this time.
0 commit comments