11/* eslint-disable no-underscore-dangle */
22import { logger } from '../../../logger' ;
33import { showUserCommunitiesMessage } from './messages' ;
4- import { Community , Order } from '../../../models' ;
4+ import { Community , Order , User } from '../../../models' ;
55import { validateParams , validateObjectId } from '../../validations' ;
66import { MainContext } from '../../start' ;
77import { CommunityContext } from './communityContext' ;
88import { Telegraf } from 'telegraf' ;
9+ import { getUserI18nContext } from '../../../util' ;
910
1011async function getOrderCountByCommunity ( ) : Promise < number [ ] > {
1112 const data = await Order . aggregate ( [
@@ -21,6 +22,7 @@ async function findCommunities(currency: string) {
2122 const communities = await Community . find ( {
2223 currencies : currency ,
2324 public : true ,
25+ enabled : { $ne : false } ,
2426 } ) ;
2527 const orderCount = await getOrderCountByCommunity ( ) ;
2628 return communities . map ( comm => {
@@ -49,9 +51,15 @@ export const setComm = async (ctx: MainContext) => {
4951 if ( groupName [ 0 ] == '@' ) {
5052 // Allow find communities case insensitive
5153 const regex = new RegExp ( [ '^' , groupName , '$' ] . join ( '' ) , 'i' ) ;
52- community = await Community . findOne ( { group : regex } ) ;
54+ community = await Community . findOne ( {
55+ group : regex ,
56+ enabled : { $ne : false } ,
57+ } ) ;
5358 } else if ( groupName [ 0 ] == '-' ) {
54- community = await Community . findOne ( { group : groupName } ) ;
59+ community = await Community . findOne ( {
60+ group : groupName ,
61+ enabled : { $ne : false } ,
62+ } ) ;
5563 }
5664 if ( ! community ) {
5765 return await ctx . reply ( ctx . i18n . t ( 'community_not_found' ) ) ;
@@ -70,7 +78,11 @@ export const communityAdmin = async (ctx: CommunityContext) => {
7078 try {
7179 const [ group ] = ( await validateParams ( ctx , 2 , '\\<_community_\\>' ) ) ! ;
7280 const creator_id = ctx . user . id ;
73- const [ community ] = await Community . find ( { group, creator_id } ) ;
81+ const [ community ] = await Community . find ( {
82+ group,
83+ creator_id,
84+ enabled : { $ne : false } ,
85+ } ) ;
7486 if ( ! community ) throw new Error ( 'CommunityNotFound' ) ;
7587 await ctx . scene . enter ( 'COMMUNITY_ADMIN' , { community } ) ;
7688 } catch ( err : any ) {
@@ -89,7 +101,10 @@ export const myComms = async (ctx: MainContext) => {
89101 try {
90102 const { user } = ctx ;
91103
92- const communities = await Community . find ( { creator_id : user . _id } ) ;
104+ const communities = await Community . find ( {
105+ creator_id : user . _id ,
106+ enabled : { $ne : false } ,
107+ } ) ;
93108
94109 if ( ! communities . length )
95110 return await ctx . reply ( ctx . i18n . t ( 'you_dont_have_communities' ) ) ;
@@ -147,6 +162,7 @@ export const updateCommunity = async (
147162 const community = await Community . findOne ( {
148163 _id : id ,
149164 creator_id : user . _id ,
165+ enabled : { $ne : false } ,
150166 } ) ;
151167
152168 if ( ! community ) {
@@ -228,6 +244,7 @@ export const deleteCommunity = async (ctx: CommunityContext) => {
228244 const community = await Community . findOne ( {
229245 _id : id ,
230246 creator_id : ctx . user . _id ,
247+ enabled : { $ne : false } ,
231248 } ) ;
232249
233250 if ( ! community ) {
@@ -241,6 +258,105 @@ export const deleteCommunity = async (ctx: CommunityContext) => {
241258 }
242259} ;
243260
261+ async function findCommunityByInput (
262+ ctx : MainContext ,
263+ input : string ,
264+ ) : Promise < typeof Community . prototype | null > {
265+ if ( input [ 0 ] === '@' ) {
266+ const regex = new RegExp (
267+ `^${ input . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) } $` ,
268+ 'i' ,
269+ ) ;
270+ const matches = await Community . find ( { group : regex } ) . limit ( 2 ) ;
271+ if ( matches . length > 1 ) throw new Error ( 'AmbiguousCommunityInput' ) ;
272+ return matches [ 0 ] ?? null ;
273+ }
274+ if ( ! ( await validateObjectId ( ctx , input ) ) ) return null ;
275+ return Community . findOne ( { _id : input } ) ;
276+ }
277+
278+ export const disableCommunity = async ( ctx : MainContext ) => {
279+ try {
280+ const [ input ] = ( await validateParams (
281+ ctx ,
282+ 2 ,
283+ '\\<_community id \\| @groupUsername_\\>' ,
284+ ) ) ! ;
285+ if ( ! input ) return ;
286+
287+ const community = await findCommunityByInput ( ctx , input ) ;
288+ if ( ! community ) return ctx . reply ( ctx . i18n . t ( 'community_not_found' ) ) ;
289+ if ( community . enabled === false )
290+ return ctx . reply ( ctx . i18n . t ( 'community_already_disabled' ) ) ;
291+
292+ community . enabled = false ;
293+ await community . save ( ) ;
294+
295+ const creator = await User . findById ( community . creator_id ) ;
296+ if ( creator ) {
297+ try {
298+ const creatorI18n = await getUserI18nContext ( creator ) ;
299+ await ctx . telegram . sendMessage (
300+ creator . tg_id ,
301+ creatorI18n . t ( 'community_disabled_by_admin' , {
302+ communityName : community . name ,
303+ } ) ,
304+ ) ;
305+ } catch ( notifyError ) {
306+ logger . error ( notifyError ) ;
307+ }
308+ }
309+
310+ return ctx . reply ( ctx . i18n . t ( 'operation_successful' ) ) ;
311+ } catch ( error : any ) {
312+ if ( error . message === 'AmbiguousCommunityInput' )
313+ return ctx . reply ( ctx . i18n . t ( 'ambiguous_community_input' ) ) ;
314+ logger . error ( error ) ;
315+ await ctx . reply ( ctx . i18n . t ( 'generic_error' ) ) ;
316+ }
317+ } ;
318+
319+ export const enableCommunity = async ( ctx : MainContext ) => {
320+ try {
321+ const [ input ] = ( await validateParams (
322+ ctx ,
323+ 2 ,
324+ '\\<_community id \\| @groupUsername_\\>' ,
325+ ) ) ! ;
326+ if ( ! input ) return ;
327+
328+ const community = await findCommunityByInput ( ctx , input ) ;
329+ if ( ! community ) return ctx . reply ( ctx . i18n . t ( 'community_not_found' ) ) ;
330+ if ( community . enabled !== false )
331+ return ctx . reply ( ctx . i18n . t ( 'community_already_enabled' ) ) ;
332+
333+ community . enabled = true ;
334+ await community . save ( ) ;
335+
336+ const creator = await User . findById ( community . creator_id ) ;
337+ if ( creator ) {
338+ try {
339+ const creatorI18n = await getUserI18nContext ( creator ) ;
340+ await ctx . telegram . sendMessage (
341+ creator . tg_id ,
342+ creatorI18n . t ( 'community_enabled_by_admin' , {
343+ communityName : community . name ,
344+ } ) ,
345+ ) ;
346+ } catch ( notifyError ) {
347+ logger . error ( notifyError ) ;
348+ }
349+ }
350+
351+ return ctx . reply ( ctx . i18n . t ( 'operation_successful' ) ) ;
352+ } catch ( error : any ) {
353+ if ( error . message === 'AmbiguousCommunityInput' )
354+ return ctx . reply ( ctx . i18n . t ( 'ambiguous_community_input' ) ) ;
355+ logger . error ( error ) ;
356+ await ctx . reply ( ctx . i18n . t ( 'generic_error' ) ) ;
357+ }
358+ } ;
359+
244360export const changeVisibility = async ( ctx : CommunityContext ) => {
245361 try {
246362 ctx . deleteMessage ( ) ;
@@ -251,6 +367,7 @@ export const changeVisibility = async (ctx: CommunityContext) => {
251367 const community = await Community . findOne ( {
252368 _id : id ,
253369 creator_id : ctx . user . _id ,
370+ enabled : { $ne : false } ,
254371 } ) ;
255372
256373 if ( ! community ) {
0 commit comments