-
Notifications
You must be signed in to change notification settings - Fork 139
Feat: Add /disablecommunity and /enablecommunity commands for superadmins #807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
703c27b
8976fec
696abba
70669f0
7801963
49a1e26
e115cc1
a8647c0
19c286a
c21fa02
d51e3d0
e6b6ce1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| /* eslint-disable no-underscore-dangle */ | ||
| import { logger } from '../../../logger'; | ||
| import { showUserCommunitiesMessage } from './messages'; | ||
| import { Community, Order } from '../../../models'; | ||
| import { Community, Order, User } from '../../../models'; | ||
| import { validateParams, validateObjectId } from '../../validations'; | ||
| import { MainContext } from '../../start'; | ||
| import { CommunityContext } from './communityContext'; | ||
| import { Telegraf } from 'telegraf'; | ||
| import { getUserI18nContext } from '../../../util'; | ||
|
|
||
| async function getOrderCountByCommunity(): Promise<number[]> { | ||
| const data = await Order.aggregate([ | ||
|
|
@@ -21,6 +22,7 @@ async function findCommunities(currency: string) { | |
| const communities = await Community.find({ | ||
| currencies: currency, | ||
| public: true, | ||
| enabled: { $ne: false }, | ||
| }); | ||
| const orderCount = await getOrderCountByCommunity(); | ||
| return communities.map(comm => { | ||
|
|
@@ -49,9 +51,15 @@ export const setComm = async (ctx: MainContext) => { | |
| if (groupName[0] == '@') { | ||
| // Allow find communities case insensitive | ||
| const regex = new RegExp(['^', groupName, '$'].join(''), 'i'); | ||
| community = await Community.findOne({ group: regex }); | ||
| community = await Community.findOne({ | ||
| group: regex, | ||
| enabled: { $ne: false }, | ||
| }); | ||
| } else if (groupName[0] == '-') { | ||
| community = await Community.findOne({ group: groupName }); | ||
| community = await Community.findOne({ | ||
| group: groupName, | ||
| enabled: { $ne: false }, | ||
| }); | ||
| } | ||
| if (!community) { | ||
| return await ctx.reply(ctx.i18n.t('community_not_found')); | ||
|
|
@@ -70,7 +78,11 @@ export const communityAdmin = async (ctx: CommunityContext) => { | |
| try { | ||
| const [group] = (await validateParams(ctx, 2, '\\<_community_\\>'))!; | ||
| const creator_id = ctx.user.id; | ||
| const [community] = await Community.find({ group, creator_id }); | ||
| const [community] = await Community.find({ | ||
| group, | ||
| creator_id, | ||
| enabled: { $ne: false }, | ||
| }); | ||
| if (!community) throw new Error('CommunityNotFound'); | ||
| await ctx.scene.enter('COMMUNITY_ADMIN', { community }); | ||
| } catch (err: any) { | ||
|
|
@@ -89,7 +101,10 @@ export const myComms = async (ctx: MainContext) => { | |
| try { | ||
| const { user } = ctx; | ||
|
|
||
| const communities = await Community.find({ creator_id: user._id }); | ||
| const communities = await Community.find({ | ||
| creator_id: user._id, | ||
| enabled: { $ne: false }, | ||
| }); | ||
|
|
||
| if (!communities.length) | ||
| return await ctx.reply(ctx.i18n.t('you_dont_have_communities')); | ||
|
|
@@ -147,6 +162,7 @@ export const updateCommunity = async ( | |
| const community = await Community.findOne({ | ||
| _id: id, | ||
| creator_id: user._id, | ||
| enabled: { $ne: false }, | ||
| }); | ||
|
|
||
| if (!community) { | ||
|
|
@@ -228,6 +244,7 @@ export const deleteCommunity = async (ctx: CommunityContext) => { | |
| const community = await Community.findOne({ | ||
| _id: id, | ||
| creator_id: ctx.user._id, | ||
| enabled: { $ne: false }, | ||
| }); | ||
|
|
||
| if (!community) { | ||
|
|
@@ -241,6 +258,144 @@ export const deleteCommunity = async (ctx: CommunityContext) => { | |
| } | ||
| }; | ||
|
|
||
| async function findCommunityByInput( | ||
| ctx: MainContext, | ||
| input: string, | ||
| ): Promise<typeof Community.prototype | null> { | ||
| if (input[0] === '@') { | ||
| const escapedInput = input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| const regex = new RegExp(`^${escapedInput}$`, 'i'); | ||
| return Community.findOne({ group: regex }); | ||
| } | ||
| if (!(await validateObjectId(ctx, input))) return null; | ||
| return Community.findOne({ _id: input }); | ||
| } | ||
|
|
||
| function buildCommunityInfoText( | ||
| ctx: MainContext, | ||
| community: typeof Community.prototype, | ||
| creatorUsername: string, | ||
| localeKey: string, | ||
| ): string { | ||
| const solversText = | ||
| community.solvers.length > 0 | ||
| ? community.solvers | ||
| .map((s: { username: string }) => `@${s.username}`) | ||
| .join(', ') | ||
| : '-'; | ||
| const groupText = community.group || '-'; | ||
| return ctx.i18n.t(localeKey, { | ||
| communityName: community.name, | ||
| group: groupText, | ||
| solvers: solversText, | ||
| creatorUsername, | ||
| }); | ||
| } | ||
|
|
||
| export const disableCommunity = async (ctx: MainContext) => { | ||
| try { | ||
| const [input] = (await validateParams( | ||
| ctx, | ||
| 2, | ||
| '\\<_community id \\| @groupUsername_\\>', | ||
| ))!; | ||
| if (!input) return; | ||
|
|
||
| const community = await findCommunityByInput(ctx, input); | ||
| if (community === null) { | ||
| return ctx.reply(ctx.i18n.t('community_not_found')); | ||
| } | ||
|
|
||
| if (community.enabled === false) { | ||
| return ctx.reply(ctx.i18n.t('community_already_disabled')); | ||
| } | ||
|
|
||
| community.enabled = false; | ||
| await community.save(); | ||
|
|
||
| const creator = await User.findById(community.creator_id); | ||
| const creatorUsername = creator?.username || 'unknown'; | ||
|
|
||
| if (creator) { | ||
| try { | ||
| const creatorI18n = await getUserI18nContext(creator); | ||
| await ctx.telegram.sendMessage( | ||
| creator.tg_id, | ||
| creatorI18n.t('community_disabled_by_admin', { | ||
| communityName: community.name, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This notification is sent with the superadmin locale (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed — disableCommunity and enableCommunity now call getUserI18nContext(creator) to get the creator's own locale before sending the DM, instead of using ctx.i18n which carries the superadmin's locale. |
||
| }), | ||
| ); | ||
| } catch (notifyError) { | ||
| logger.error(notifyError); | ||
| } | ||
| } | ||
|
|
||
| return ctx.reply( | ||
| buildCommunityInfoText( | ||
| ctx, | ||
| community, | ||
| creatorUsername, | ||
| 'community_disabled_info', | ||
| ), | ||
| ); | ||
| } catch (error) { | ||
| logger.error(error); | ||
| await ctx.reply(ctx.i18n.t('generic_error')); | ||
| } | ||
| }; | ||
|
|
||
| export const enableCommunity = async (ctx: MainContext) => { | ||
| try { | ||
| const [input] = (await validateParams( | ||
| ctx, | ||
| 2, | ||
| '\\<_community id \\| @groupUsername_\\>', | ||
| ))!; | ||
| if (!input) return; | ||
|
|
||
| const community = await findCommunityByInput(ctx, input); | ||
| if (community === null) { | ||
| return ctx.reply(ctx.i18n.t('community_not_found')); | ||
| } | ||
|
|
||
| if (community.enabled !== false) { | ||
| return ctx.reply(ctx.i18n.t('community_already_enabled')); | ||
| } | ||
|
|
||
| community.enabled = true; | ||
| await community.save(); | ||
|
|
||
| const creator = await User.findById(community.creator_id); | ||
| const creatorUsername = creator?.username || 'unknown'; | ||
|
|
||
| if (creator) { | ||
| try { | ||
| const creatorI18n = await getUserI18nContext(creator); | ||
| await ctx.telegram.sendMessage( | ||
| creator.tg_id, | ||
| creatorI18n.t('community_enabled_by_admin', { | ||
| communityName: community.name, | ||
| }), | ||
| ); | ||
| } catch (notifyError) { | ||
| logger.error(notifyError); | ||
| } | ||
| } | ||
|
|
||
| return ctx.reply( | ||
| buildCommunityInfoText( | ||
| ctx, | ||
| community, | ||
| creatorUsername, | ||
| 'community_enabled_info', | ||
| ), | ||
| ); | ||
| } catch (error) { | ||
| logger.error(error); | ||
| await ctx.reply(ctx.i18n.t('generic_error')); | ||
| } | ||
| }; | ||
|
|
||
| export const changeVisibility = async (ctx: CommunityContext) => { | ||
| try { | ||
| ctx.deleteMessage(); | ||
|
|
@@ -251,6 +406,7 @@ export const changeVisibility = async (ctx: CommunityContext) => { | |
| const community = await Community.findOne({ | ||
| _id: id, | ||
| creator_id: ctx.user._id, | ||
| enabled: { $ne: false }, | ||
| }); | ||
|
|
||
| if (!community) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.