mirror of
https://github.com/Xevion/contest-assistant.git
synced 2025-12-06 05:14:41 -06:00
overhaul voting to support smart single voting protections and enforcing, new exceptions, vote clearing functions inside models, persistence updating
This commit is contained in:
1
Pipfile
1
Pipfile
@@ -12,6 +12,7 @@ pytest = "*"
|
|||||||
pytest-asyncio = "*"
|
pytest-asyncio = "*"
|
||||||
sqlalchemy = "*"
|
sqlalchemy = "*"
|
||||||
alembic = "*"
|
alembic = "*"
|
||||||
|
sqlalchemy-json = "*"
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.7"
|
python_version = "3.7"
|
||||||
|
|||||||
@@ -1,3 +1,32 @@
|
|||||||
class FinishedPeriod(Exception):
|
class ContestException(Exception):
|
||||||
"""A inactive period, or a period in it's final state cannot be advanced or further modified."""
|
"""A exception directly related to the Contest Assistant bot."""
|
||||||
pass
|
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'
|
||||||
|
|||||||
129
bot/models.py
129
bot/models.py
@@ -1,15 +1,22 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import enum
|
import enum
|
||||||
import functools
|
import functools
|
||||||
|
import itertools
|
||||||
import logging
|
import logging
|
||||||
from typing import List, Optional, Union
|
from typing import Iterable, List, TYPE_CHECKING, Tuple, Union
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
from sqlalchemy import Boolean, Column, DateTime, Enum, ForeignKey, Integer, Text
|
from sqlalchemy import Boolean, Column, DateTime, Enum, ForeignKey, Integer, Text
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
from sqlalchemy_json import MutableJson
|
||||||
|
|
||||||
from bot import constants, exceptions, helpers
|
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 = logging.getLogger(__file__)
|
||||||
logger.setLevel(constants.LOGGING_LEVEL)
|
logger.setLevel(constants.LOGGING_LEVEL)
|
||||||
@@ -61,9 +68,9 @@ def check_not_finished(func):
|
|||||||
|
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def wrapper(self, *args, **kwargs):
|
def wrapper(self, *args, **kwargs):
|
||||||
if self.state is PeriodStates.FINISHED: raise exceptions.FinishedPeriod(f"Period is in it's Finished state.")
|
if self.state is PeriodStates.FINISHED: raise exceptions.FinishedPeriodException(f"Period is in it's Finished state.")
|
||||||
elif not self.active: raise exceptions.FinishedPeriod("Period is no longer active.")
|
elif not self.active: raise exceptions.FinishedPeriodException("Period is no longer active.")
|
||||||
elif self.completed: raise exceptions.FinishedPeriod("Period is already completed.")
|
elif self.completed: raise exceptions.FinishedPeriodException("Period is already completed.")
|
||||||
func(self, *args, **kwargs)
|
func(self, *args, **kwargs)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
@@ -76,42 +83,112 @@ class Submission(Base):
|
|||||||
id = Column(Integer, primary_key=True) # Doubles as the ID this Guild has in Discord
|
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.
|
user = Column(Integer) # The ID of the user who submitted it.
|
||||||
timestamp = Column(DateTime) # When the Submission was posted
|
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_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.
|
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."""
|
"""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."""
|
"""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:
|
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 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]
|
reacting_user: Union[discord.Member, discord.User]
|
||||||
async for reacting_user in reaction.users():
|
async for reacting_user in reaction.users():
|
||||||
# Check if the user who reacted to this emoji is the user we are looking for
|
# Check if this is our bot reacting
|
||||||
if reacting_user.id == user.id:
|
if reacting_user.id == bot.user.id:
|
||||||
saw_user = True
|
saw_self = True
|
||||||
break
|
else:
|
||||||
|
current.add(reacting_user.id)
|
||||||
|
|
||||||
# Tally the number of votes
|
to_add, to_remove, report = current - old, old - current, ''
|
||||||
votes = reaction.count - 1
|
if len(to_add) > 0:
|
||||||
if votes != self.votes:
|
report += f'Added: {", ".join(map(str, to_add))}'
|
||||||
# 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
|
|
||||||
|
|
||||||
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):
|
class Period(Base):
|
||||||
|
|||||||
Reference in New Issue
Block a user