-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added block, unblock and blocklist commands
- Loading branch information
Showing
18 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const { User, Block, Order } = require('../../../models'); | ||
const messages = require('./messages'); | ||
const globalMessages = require('../../messages'); | ||
|
||
const block = async (ctx, username) => { | ||
const userToBlock = await User.findOne({ username }); | ||
const user = ctx.user; | ||
|
||
if (!userToBlock) { | ||
await globalMessages.notFoundUserMessage(ctx); | ||
return; | ||
} | ||
|
||
const areExistingOrders = await Order.exists({ | ||
$or: [ | ||
{ seller_id: user.id, buyer_id: userToBlock.id }, | ||
{ seller_id: userToBlock.id, buyer_id: user.id } | ||
], | ||
status: { $nin: ["PENDING", "CLOSED", "CANCELED_BY_ADMIN", "EXPIRED", "COMPLETED_BY_ADMIN"] } | ||
}); | ||
|
||
if(areExistingOrders) { | ||
await messages.ordersInProcess(ctx); | ||
return; | ||
} | ||
|
||
const isAlreadyBlocked = await Block.exists({blocker_tg_id: user.tg_id, blocked_tg_id: userToBlock.tg_id}) | ||
if (isAlreadyBlocked) { | ||
await messages.userAlreadyBlocked(ctx); | ||
return; | ||
} | ||
|
||
const block = new Block({ | ||
blocker_tg_id: user.tg_id, | ||
blocked_tg_id: userToBlock.tg_id, | ||
}); | ||
await block.save(); | ||
await messages.userBlocked(ctx); | ||
}; | ||
|
||
const unblock = async (ctx, username) => { | ||
const userToUnblock = await User.findOne({ username }); | ||
const user = ctx.user; | ||
|
||
const result = await Block.deleteOne({blocker_tg_id: user.tg_id, blocked_tg_id: userToUnblock.tg_id}) | ||
|
||
if (result.deletedCount === 1) { | ||
await messages.userUnblocked(ctx); | ||
} else { | ||
await globalMessages.notFoundUserMessage(ctx); | ||
} | ||
}; | ||
|
||
const blocklist = async ctx => { | ||
const blocks = await Block.find({ blocker_tg_id: ctx.user.tg_id }); | ||
const tgIdBlocks = blocks.map(blocked => blocked.blocked_tg_id) | ||
|
||
if (!tgIdBlocks.length) { | ||
await messages.blocklistEmptyMessage(ctx) | ||
return; | ||
} | ||
|
||
const usersBlocked = await User.find({tg_id: { $in: tgIdBlocks}}); | ||
await messages.blocklistMessage(ctx, usersBlocked) | ||
}; | ||
|
||
module.exports = { block, unblock, blocklist }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const commands = require('./commands'); | ||
const { userMiddleware } = require('../../middleware/user'); | ||
|
||
exports.configure = bot => { | ||
bot.command('block', userMiddleware, async (ctx, next) => { | ||
const args = ctx.message.text.split(' '); | ||
if (args.length !== 2) return next(); | ||
commands.block(ctx, args[1]); | ||
}); | ||
|
||
bot.command('unblock', userMiddleware, async (ctx, next) => { | ||
const args = ctx.message.text.split(' '); | ||
if (args.length !== 2) return next(); | ||
commands.unblock(ctx, args[1]); | ||
}); | ||
bot.command('blocklist', userMiddleware, commands.blocklist); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const { logger } = require('../../../logger'); | ||
|
||
|
||
const ordersInProcess = async ctx => { | ||
try { | ||
ctx.reply(ctx.i18n.t('orders_in_process')); | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
}; | ||
|
||
const userAlreadyBlocked = async ctx => { | ||
try { | ||
ctx.reply(ctx.i18n.t('user_already_blocked')); | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
}; | ||
|
||
const userBlocked = async ctx => { | ||
try { | ||
ctx.reply(ctx.i18n.t('user_blocked')); | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
}; | ||
|
||
const userUnblocked = async ctx => { | ||
try { | ||
ctx.reply(ctx.i18n.t('user_unblocked')); | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
}; | ||
|
||
const blocklistMessage = async (ctx, usersBlocked) => { | ||
try { | ||
const userList = usersBlocked.map(block => block.username); | ||
ctx.reply(userList.join('\n')); | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
}; | ||
|
||
const blocklistEmptyMessage = async (ctx) => { | ||
try { | ||
ctx.reply(ctx.i18n.t('blocklist_empty')); | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
}; | ||
|
||
module.exports = { userAlreadyBlocked, userBlocked, userUnblocked, blocklistMessage, blocklistEmptyMessage, ordersInProcess } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import mongoose, { Document, Schema } from 'mongoose'; | ||
|
||
export interface IBlock extends Document { | ||
blocker_tg_id: string; | ||
blocked_tg_id: string; | ||
created_at: Date; | ||
} | ||
|
||
const blockSchema = new Schema<IBlock>({ | ||
blocker_tg_id: { type: String }, | ||
blocked_tg_id: { type: String }, | ||
created_at: { type: Date, default: Date.now }, | ||
}); | ||
|
||
export default mongoose.model<IBlock>('Block', blockSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters