Skip to content
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

Exclude duplicated votes #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions helpers/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
const db = require('./db')
const _ = require('lodash')
const admins = require('./admins')
const { Lock } = require('semaphore-async-await')
const { isRuChat } = require('./isRuChat')
const { isOver10000 } = require('./goldenBorodutchSubCount')

Expand Down Expand Up @@ -106,8 +105,6 @@ async function startRequest(bot, msg) {
* @param {Teleram:Message} msg Message that triggered inline
*/
async function voteQuery(bot, msg) {
const lock = new Lock(1)
await lock.acquire()
try {
const options = msg.data.split('~')
const requestId = options[1]
Expand Down Expand Up @@ -142,7 +139,8 @@ async function voteQuery(bot, msg) {
request.voters_ban = request.voters_ban.filter(
(arrayVoter) => !arrayVoter._id.equals(voter._id)
)
request.voters_noban.push(voter)
request.update({ 'voters_noban._id': { $ne: voter._id } },
{ $addToSet: { voters_noban: voter } })
} else {
const alreadyThere = _.find(request.voters_ban, (arrayVoter) =>
arrayVoter._id.equals(voter._id)
Expand All @@ -159,16 +157,14 @@ async function voteQuery(bot, msg) {
request.voters_noban = request.voters_noban.filter(
(arrayVoter) => !arrayVoter._id.equals(voter._id)
)
request.voters_ban.push(voter)
request.update({ 'voters_ban._id': { $ne: voter._id } },
{ $addToSet: { voters_ban: voter } })
}
request = await request.save()
await updateMessage(bot, request)
} catch (err) {
console.error(err)
// Do nothing
} finally {
lock.release()
}
}

/**
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"lodash": "^4.17.21",
"mongoose": "^5.7.0",
"node-telegram-bot-api": "^0.30.0",
"nodemon": "^1.19.2",
"semaphore-async-await": "^1.5.1"
"nodemon": "^1.19.2"
},
"devDependencies": {
"axios": "^0.21.2",
Expand Down
Loading