@@ -3,7 +3,11 @@ import { PubKey } from '../../session/types';
3
3
import { ToastUtils } from '../../session/utils' ;
4
4
import { Flex } from '../basic/Flex' ;
5
5
import { useDispatch , useSelector } from 'react-redux' ;
6
- import { BanType , updateBanOrUnbanUserModal } from '../../state/ducks/modalDialog' ;
6
+ import {
7
+ BanType ,
8
+ updateBanOrUnbanUserModal ,
9
+ updateServerBanOrUnbanUserModal
10
+ } from '../../state/ducks/modalDialog' ;
7
11
import { SpacerSM } from '../basic/Text' ;
8
12
import { getConversationController } from '../../session/conversations/ConversationController' ;
9
13
import { SessionWrapperModal } from '../SessionWrapperModal' ;
@@ -14,7 +18,9 @@ import { useFocusMount } from '../../hooks/useFocusMount';
14
18
import { useConversationPropsById } from '../../hooks/useParamSelector' ;
15
19
import {
16
20
sogsV3BanUser ,
17
- sogsV3UnbanUser ,
21
+ sogsV3ServerBanUser ,
22
+ sogsV3ServerUnbanUser ,
23
+ sogsV3UnbanUser
18
24
} from '../../session/apis/open_group_api/sogsv3/sogsV3BanUnban' ;
19
25
import { SessionHeaderSearchInput } from '../SessionHeaderSearchInput' ;
20
26
import { isDarkTheme } from '../../state/selectors/theme' ;
@@ -25,7 +31,8 @@ async function banOrUnBanUserCall(
25
31
convo : ConversationModel ,
26
32
textValue : string ,
27
33
banType : BanType ,
28
- deleteAll : boolean
34
+ deleteAll : boolean ,
35
+ isGlobal : boolean
29
36
) {
30
37
// if we don't have valid data entered by the user
31
38
const pubkey = PubKey . from ( textValue ) ;
@@ -39,8 +46,12 @@ async function banOrUnBanUserCall(
39
46
const roomInfos = convo . toOpenGroupV2 ( ) ;
40
47
const isChangeApplied =
41
48
banType === 'ban'
42
- ? await sogsV3BanUser ( pubkey , roomInfos , deleteAll )
43
- : await sogsV3UnbanUser ( pubkey , roomInfos ) ;
49
+ ? isGlobal
50
+ ? await sogsV3ServerBanUser ( pubkey , roomInfos , deleteAll )
51
+ : await sogsV3BanUser ( pubkey , roomInfos , deleteAll )
52
+ : isGlobal
53
+ ? await sogsV3ServerUnbanUser ( pubkey , roomInfos )
54
+ : await sogsV3UnbanUser ( pubkey , roomInfos ) ;
44
55
45
56
if ( ! isChangeApplied ) {
46
57
window ?. log ?. warn ( `failed to ${ banType } user: ${ isChangeApplied } ` ) ;
@@ -92,7 +103,7 @@ export const BanOrUnBanUserDialog = (props: {
92
103
93
104
window ?. log ?. info ( `asked to ${ banType } user: ${ castedPubkey } , banAndDeleteAll:${ deleteAll } ` ) ;
94
105
setInProgress ( true ) ;
95
- const isBanned = await banOrUnBanUserCall ( convo , castedPubkey , banType , deleteAll ) ;
106
+ const isBanned = await banOrUnBanUserCall ( convo , castedPubkey , banType , deleteAll , false ) ;
96
107
if ( isBanned ) {
97
108
// clear input box
98
109
setInputBoxValue ( '' ) ;
@@ -163,4 +174,112 @@ export const BanOrUnBanUserDialog = (props: {
163
174
</ Flex >
164
175
</ SessionWrapperModal >
165
176
) ;
166
- } ;
177
+ }
178
+
179
+ // FIXME: Refactor with BanOrUnBanUserDialog().
180
+ export const ServerBanOrUnBanUserDialog = ( props : {
181
+ conversationId : string ;
182
+ banType : BanType ;
183
+ pubkey ?: string ;
184
+ } ) => {
185
+ const { conversationId, banType, pubkey } = props ;
186
+ const { i18n } = window ;
187
+ const isBan = banType === 'ban' ;
188
+ const dispatch = useDispatch ( ) ;
189
+ const darkMode = useSelector ( isDarkTheme ) ;
190
+ const convo = getConversationController ( ) . get ( conversationId ) ;
191
+ const inputRef = useRef ( null ) ;
192
+
193
+ useFocusMount ( inputRef , true ) ;
194
+ const wasGivenAPubkey = Boolean ( pubkey ?. length ) ;
195
+ const [ inputBoxValue , setInputBoxValue ] = useState ( '' ) ;
196
+ const [ inProgress , setInProgress ] = useState ( false ) ;
197
+
198
+ const sourceConvoProps = useConversationPropsById ( pubkey ) ;
199
+
200
+ const inputTextToDisplay =
201
+ wasGivenAPubkey && sourceConvoProps
202
+ ? `${ sourceConvoProps . displayNameInProfile } ${ PubKey . shorten ( sourceConvoProps . id ) } `
203
+ : undefined ;
204
+
205
+ /**
206
+ * Ban or Unban a user from an open group
207
+ * @param deleteAll Delete all messages for that user in the group (only works with ban)
208
+ */
209
+ const banOrUnBanUser = async ( deleteAll : boolean = false ) => {
210
+ const castedPubkey = pubkey ?. length ? pubkey : inputBoxValue ;
211
+
212
+ window ?. log ?. info ( `asked to ${ banType } user server-wide: ${ castedPubkey } , banAndDeleteAll:${ deleteAll } ` ) ;
213
+ setInProgress ( true ) ;
214
+ const isBanned = await banOrUnBanUserCall ( convo , castedPubkey , banType , deleteAll , true ) ;
215
+ if ( isBanned ) {
216
+ // clear input box
217
+ setInputBoxValue ( '' ) ;
218
+ if ( wasGivenAPubkey ) {
219
+ dispatch ( updateServerBanOrUnbanUserModal ( null ) ) ;
220
+ }
221
+ }
222
+
223
+ setInProgress ( false ) ;
224
+ } ;
225
+
226
+ const serverHost = new window . URL ( convo . toOpenGroupV2 ( ) . serverUrl ) . host ;
227
+ const title = `${ isBan ? window . i18n ( 'banUser' ) : window . i18n ( 'unbanUser' ) } @ ${ serverHost } ` ;
228
+
229
+ const onPubkeyBoxChanges = ( e : React . ChangeEvent < HTMLInputElement > ) => {
230
+ setInputBoxValue ( e . target . value ?. trim ( ) || '' ) ;
231
+ } ;
232
+
233
+ /**
234
+ * Starts procedure for banning/unbanning user and all their messages using dialog
235
+ */
236
+ const startBanAndDeleteAllSequence = async ( ) => {
237
+ await banOrUnBanUser ( true ) ;
238
+ } ;
239
+
240
+ const buttonText = isBan ? i18n ( 'banUser' ) : i18n ( 'unbanUser' ) ;
241
+
242
+ return (
243
+ < SessionWrapperModal
244
+ showExitIcon = { true }
245
+ title = { title }
246
+ onClose = { ( ) => {
247
+ dispatch ( updateServerBanOrUnbanUserModal ( null ) ) ;
248
+ } }
249
+ >
250
+ < Flex container = { true } flexDirection = "column" alignItems = "center" >
251
+ < SessionHeaderSearchInput
252
+ ref = { inputRef }
253
+ type = "text"
254
+ darkMode = { darkMode }
255
+ placeholder = { i18n ( 'enterSessionID' ) }
256
+ dir = "auto"
257
+ onChange = { onPubkeyBoxChanges }
258
+ disabled = { inProgress || wasGivenAPubkey }
259
+ value = { wasGivenAPubkey ? inputTextToDisplay : inputBoxValue }
260
+ />
261
+ < Flex container = { true } >
262
+ < SessionButton
263
+ buttonType = { SessionButtonType . Simple }
264
+ onClick = { banOrUnBanUser }
265
+ text = { buttonText }
266
+ disabled = { inProgress }
267
+ />
268
+ { isBan && (
269
+ < >
270
+ < SpacerSM />
271
+ < SessionButton
272
+ buttonType = { SessionButtonType . Simple }
273
+ buttonColor = { SessionButtonColor . Danger }
274
+ onClick = { startBanAndDeleteAllSequence }
275
+ text = { i18n ( 'serverBanUserAndDeleteAll' ) }
276
+ disabled = { inProgress }
277
+ />
278
+ </ >
279
+ ) }
280
+ </ Flex >
281
+ < SessionSpinner loading = { inProgress } />
282
+ </ Flex >
283
+ </ SessionWrapperModal >
284
+ ) ;
285
+ }
0 commit comments