From 33ac0d7792fa6a0f94d62db2a3d05070d5fe89ae Mon Sep 17 00:00:00 2001 From: Xevion Date: Tue, 16 Feb 2021 07:27:02 -0600 Subject: [PATCH] overhaul voting to support smart single voting protections and enforcing, new exceptions, vote clearing functions inside models, persistence updating --- Pipfile | 1 + bot/exceptions.py | 33 +++++++++++- bot/models.py | 133 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 137 insertions(+), 30 deletions(-) diff --git a/Pipfile b/Pipfile index 1a13147..123fbd5 100644 --- a/Pipfile +++ b/Pipfile @@ -12,6 +12,7 @@ pytest = "*" pytest-asyncio = "*" sqlalchemy = "*" alembic = "*" +sqlalchemy-json = "*" [requires] python_version = "3.7" diff --git a/bot/exceptions.py b/bot/exceptions.py index 5ed9c1b..1ad9fae 100644 --- a/bot/exceptions.py +++ b/bot/exceptions.py @@ -1,3 +1,32 @@ -class FinishedPeriod(Exception): - """A inactive period, or a period in it's final state cannot be advanced or further modified.""" +class ContestException(Exception): + """A exception directly related to the Contest Assistant bot.""" pass + + +class FinishedPeriodException(ContestException): + """A inactive period, or a period in it's final state cannot be advanced or further modified.""" + + def __repr__(self) -> str: + return 'Period is inactive.' + + +class DatabaseDoubleVoteException(ContestException): + """ + The database was asked to increment a vote for a submission with a user ID that was already added. + + Companion to `DatabaseNoVoteException` + """ + + def __repr__(self) -> str: + return 'You can\'t vote for a submission twice.' + + +class DatabaseNoVoteException(ContestException): + """ + The database was asked to decrement a vote for a submission with a user ID that did not or no longer exists for the given submission. + + Companion to `DatabaseDoubleVoteException` + """ + + def __repr__(self) -> str: + return 'You can\'t remove a vote that never or no longer exists' diff --git a/bot/models.py b/bot/models.py index ca6ca5c..4c81a3d 100644 --- a/bot/models.py +++ b/bot/models.py @@ -1,15 +1,22 @@ import datetime import enum import functools +import itertools import logging -from typing import List, Optional, Union +from typing import Iterable, List, TYPE_CHECKING, Tuple, Union import discord from sqlalchemy import Boolean, Column, DateTime, Enum, ForeignKey, Integer, Text from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship +from sqlalchemy_json import MutableJson from bot import constants, exceptions, helpers +from bot.constants import ReactionMarker + +if TYPE_CHECKING: + from sqlalchemy.orm import Session + from bot.bot import ContestBot logger = logging.getLogger(__file__) logger.setLevel(constants.LOGGING_LEVEL) @@ -61,9 +68,9 @@ def check_not_finished(func): @functools.wraps(func) def wrapper(self, *args, **kwargs): - if self.state is PeriodStates.FINISHED: raise exceptions.FinishedPeriod(f"Period is in it's Finished state.") - elif not self.active: raise exceptions.FinishedPeriod("Period is no longer active.") - elif self.completed: raise exceptions.FinishedPeriod("Period is already completed.") + if self.state is PeriodStates.FINISHED: raise exceptions.FinishedPeriodException(f"Period is in it's Finished state.") + elif not self.active: raise exceptions.FinishedPeriodException("Period is no longer active.") + elif self.completed: raise exceptions.FinishedPeriodException("Period is already completed.") func(self, *args, **kwargs) return wrapper @@ -76,42 +83,112 @@ class Submission(Base): id = Column(Integer, primary_key=True) # Doubles as the ID this Guild has in Discord user = Column(Integer) # The ID of the user who submitted it. timestamp = Column(DateTime) # When the Submission was posted - votes = Column(Integer, default=0) + votes: List[int] = Column(MutableJson, default=[]) # A list of IDs correlating to users who voted on this submission. period_id = Column(Integer, ForeignKey("period.id")) # The id of the period this Submission relates to. period = relationship("Period", back_populates="submissions") # The period this submission was made in. - def increment(self) -> None: + @property + def count(self) -> int: + """The number of votes cast for this submission.""" + return len(self.votes) + + def increment(self, user: int) -> None: """Increase the number of votes by one.""" - self.votes += 1 + if user in self.votes: + raise exceptions.DatabaseDoubleVoteException() + self.votes.append(user) - def decrement(self) -> None: + def decrement(self, user: int) -> None: """Decrease the number of votes by one.""" - self.votes -= 1 + if user not in self.votes: + raise exceptions.DatabaseNoVoteException() + self.votes.remove(user) + + def clear_other_votes(self, ignore: Union[int, Iterable[int]], users: Union[int, Iterable[int]], session: Session) -> ReactionMarker: + """ + Removes votes from all submissions in the database for a specific user. + Returns a list of combination Message and User IDs + + :param ignore: The Submission ID(s) to ignore. + :param users: The User ID(s) to clear. + :param session: A SQLAlchemy session to use for querying. + :return: A list of tuples containing a Message ID then User ID who voted for submissions other than the ones being ignored. + """ + if isinstance(ignore, int): ignore = [ignore] + if isinstance(users, int): users = [users] + ignore, users = set(ignore), set(users) + if len(ignore) == 0: logger.warning(f'Clearing ALL votes for user(s): {users}') + if len(users) == 0: return [] + + found: List[Tuple[int, int]] = [] + submissions = session.query(Submission).filter(Submission.id != self.id).all() + for submission in submissions: + # Ignore submissions in the ignore list + if submission.id in ignore: + continue + + # Find what users voted for this submission that we are clearing + votes = set(submission.votes) + same = votes.intersection(users) + if len(same) < 0: + continue + + # Remove votes from the submission by said users + submission.votes = list(votes - same) + + # For each user we found that voted, return a tuple Message ID & User ID + for shared_user in same: + found.append(ReactionMarker(message=submission.id, user=shared_user)) + + return found + + async def update(self, bot: 'ContestBot', message: discord.Message = None) -> None: + """Updates the number of votes in the database by thoroughly evaluating the message.""" + saw_self, current, old = False, set(), set(self.votes) # Votes currently on the message and votes only on the submission - async def verify(self, message: discord.Message, user: Optional[Union[discord.ClientUser, discord.User]] = None) -> bool: - """Sets the number of votes for this Submission.""" - saw_user = False for reaction in message.reactions: - # Check that it's a custom Emoji and that the Emoji is the expected Upvote emoji if helpers.is_upvote(reaction.emoji): - # If a user was specified, look for him in the reactions - if user is not None: - reacting_user: Union[discord.Member, discord.User] - async for reacting_user in reaction.users(): - # Check if the user who reacted to this emoji is the user we are looking for - if reacting_user.id == user.id: - saw_user = True - break + reacting_user: Union[discord.Member, discord.User] + async for reacting_user in reaction.users(): + # Check if this is our bot reacting + if reacting_user.id == bot.user.id: + saw_self = True + else: + current.add(reacting_user.id) - # Tally the number of votes - votes = reaction.count - 1 - if votes != self.votes: - # Make a racket if we counted wrong or somehow missed reactions - logger.warning(f'True vote count was off for Submission {self.id} by {votes - self.votes}.') - self.votes = votes + to_add, to_remove, report = current - old, old - current, '' + if len(to_add) > 0: + report += f'Added: {", ".join(map(str, to_add))}' - return saw_user + with bot.get_session() as session: + channel: discord.TextChannel = message.channel + + # Iterate through each user who has added a reaction since the last check + for user_id in to_add: + # Remove their votes in other submissions + reactions_to_clear = self.clear_other_votes(ignore=self.id, users=user_id, session=session) + + # Then remove all upvote reactions from that user from other submission + for message_id, reaction_tuples in itertools.groupby(reactions_to_clear, lambda marker: marker.message): + message_to_clear: discord.Message = await channel.fetch_message(message_id) + reaction_marker: ReactionMarker + + # Should only iterate once, but we'll ready it for multiple users + for reaction_marker in reaction_tuples: + await message_to_clear.remove_reaction( + bot.get_emoji(constants.Emoji.UPVOTE), + message.guild.get_member(reaction_marker.user) + ) + + if len(to_remove) > 0: + if report: report += ' ' + report += f'Removed: {", ".join(map(str, to_remove))}' + if report: logger.debug(report) + + # If we never saw ourselves in the reaction, add the Upvote emoji + if not saw_self: + await message.add_reaction(constants.Emoji.UPVOTE) class Period(Base):