From 9ae245e9ecf5aa2d4b8b7f29f10461c6970645c6 Mon Sep 17 00:00:00 2001 From: Robert Wasinger Date: Sun, 6 Oct 2024 23:42:31 -0700 Subject: [PATCH] Strings rule enums drool --- dojo_plugin/api/v1/discord.py | 60 +++++++++++++++++++--------------- dojo_plugin/models/__init__.py | 16 ++++----- dojo_plugin/utils/discord.py | 1 - 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/dojo_plugin/api/v1/discord.py b/dojo_plugin/api/v1/discord.py index 2b4eeeec..40a99307 100644 --- a/dojo_plugin/api/v1/discord.py +++ b/dojo_plugin/api/v1/discord.py @@ -71,14 +71,12 @@ def get(self, discord_id): def get_user_activity_prop(discord_id, activity, start=None, end=None): user = DiscordUsers.query.filter_by(discord_id=discord_id).first() - if activity is DiscordUserActivity.ActivityType.thanks: - prop_name = "thanks" + if activity == "thanks": count = user.thanks_count(start, end) if user else 0 - elif activity is DiscordUserActivity.ActivityType.memes: - prop_name = "memes" + elif activity == "memes": count = user.meme_count(start, end) if user else 0 - return {"success": True, prop_name: count} + return {"success": True, activity: count} def get_user_activity(self, discord_id, activity, request): authorization = request.headers.get("Authorization") @@ -118,39 +116,49 @@ def post_user_activity(discord_id, activity, request): except json.JSONDecodeError: return {"success": False, "error": f"Invalid JSON data {request.data}"}, 400 - if "from_user_id" not in data: - return {"success": False, "error": f"Invalid JSON data"}, 400 - - # These are discord user_ids - thanker = data.get("from_user_id", "") - kwargs = {'guild_id': data.get("guild_id"), - 'channel_id': data.get("channel_id"), - 'message_id': data.get("message_id"), - 'timestamp': data.get("timestamp"), - 'message_timestamp': datetime.datetime.fromisoformat(data.get("message_timestamp")), - 'activity_type': activity - } - entry = DiscordUserActivity(discord_id, thanker, **kwargs) + + expected_vals = ['source_user_id', + 'guild_id', + 'channel_id', + 'message_id', + 'message_timestamp', + ] + + for ev in expected_vals: + if ev not in data: + return {"success": False, "error": f"Invalid JSON data - {ev} not found!"}, 400 + + kwargs = { + 'user_id' : discord_id, + 'source_user_id': data.get("source_user_id", ""), + 'guild_id': data.get("guild_id"), + 'channel_id': data.get("channel_id"), + 'message_id': data.get("message_id"), + 'timestamp': data.get("timestamp"), + 'message_timestamp': datetime.datetime.fromisoformat(data.get("message_timestamp")), + 'type': activity + } + entry = DiscordUserActivity(**kwargs) db.session.add(entry) db.session.commit() - return get_user_activity_prop(discord_id, activity) + return get_user_activity_prop(discord_id, activity), 200 @discord_namespace.route("/memes/user/", methods=["GET", "POST"]) class DiscordMemes(Resource): def get(self, discord_id): - get_user_activity(discord_id, DiscordUserActivity.ActivityType.memes, request) + return get_user_activity(discord_id, "memes", request) def post(self, discord_id): - post_user_activity(discord_id, DiscordUserActivity.ActivityType.memes, request) + return post_user_activity(discord_id, "memes", request) @discord_namespace.route("/thanks/user/", methods=["GET", "POST"]) class DiscordThanks(Resource): def get(self, discord_id): - get_user_activity(discord_id, DiscordUserActivity.ActivityType.thanks, request) + return get_user_activity(discord_id, "thanks", request) def post(self, discord_id): - post_user_activity(discord_id, DiscordUserActivity.ActivityType.thanks, request) + return post_user_activity(discord_id, "thanks", request) @discord_namespace.route("/thanks/leaderboard", methods=["GET"]) @@ -170,9 +178,9 @@ def year_stamp(): except: return {"success": False, "error": "invalid start format"}, 400 - thanks_scores = DiscordUserActivity.query.with_entities(DiscordUserActivity.user_id, db.func.count(DiscordUserActivity.user_id) - ).filter(and_(DiscordUserActivity.timestamp >= start), - DiscordUserActivity.activity_type == DiscordUserActivity.ActivityType.thanks + thanks_scores = DiscordUserActivity.query.with_entities(DiscordUserActivity.user_id, db.func.count(db.func.distinct(DiscordUserActivity.message_id)) + ).filter(and_(DiscordUserActivity.message_timestamp >= start), + DiscordUserActivity.type == "thanks" ).group_by(DiscordUserActivity.user_id ).order_by(db.func.count(DiscordUserActivity.user_id).desc())[:100] diff --git a/dojo_plugin/models/__init__.py b/dojo_plugin/models/__init__.py index a0922b2e..f9dc83d4 100644 --- a/dojo_plugin/models/__init__.py +++ b/dojo_plugin/models/__init__.py @@ -754,16 +754,12 @@ class SSHKeys(db.Model): class DiscordUserActivity(db.Model): - class ActivityType(enum.Enum): - thanks = 1 - memes = 2 - __tablename__ = "discord_user_activity" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.BigInteger) source_user_id = db.Column(db.BigInteger) timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow) - activity_type = db.Column(db.Enum(ActivityType)) + type = db.Column(db.String(80), index=True) guild_id = db.Column(db.BigInteger) channel_id = db.Column(db.BigInteger) message_id = db.Column(db.BigInteger) @@ -782,7 +778,8 @@ def thanks_count(self, start=None, end=None): count = DiscordUserActivity.query.filter(and_(DiscordUserActivity.user_id == self.discord_id), DiscordUserActivity.message_timestamp >= start if start else True, DiscordUserActivity.message_timestamp <= end if end else True, - DiscordUserActivity.activity_type == DiscordUserActivity.ActivityType.thanks).count() + DiscordUserActivity.type == "thanks" + ).with_entities(db.func.distinct(DiscordUserActivity.message_id)).count() return count def meme_count(self, start=None, end=None, weekly=True): @@ -790,8 +787,8 @@ def meme_count(self, start=None, end=None, weekly=True): return DiscordUserActivity.query.filter(and_(DiscordUserActivity.user_id == self.discord_id), DiscordUserActivity.message_timestamp >= start if start else True, DiscordUserActivity.message_timestamp <= end if end else True, - DiscordUserActivity.activity_type == DiscordUserActivity.ActivityType.memes - ).count() + DiscordUserActivity.type == "memes" + ).with_entities(db.func.distinct(DiscordUserActivity.message_id)).count() meme_weeks = self.meme_dates(start, end) return len(meme_weeks) @@ -800,7 +797,7 @@ def meme_dates(self, start=None, end=None): memes = DiscordUserActivity.query.filter(and_(DiscordUserActivity.user_id == self.discord_id), DiscordUserActivity.timestamp >= start if start else True, DiscordUserActivity.timestamp <= end if end else True, - DiscordUserActivity.activity_type == DiscordUserActivity.ActivityType.memes + DiscordUserActivity.type == "memes" ).order_by(DiscordUserActivity.timestamp).all() start = memes[0].timestamp if not start else start @@ -814,7 +811,6 @@ def valid_week(week, memes): return [w for w in class_weeks if valid_week(w, memes)] - __repr__ = columns_repr(["user", "discord_id"]) diff --git a/dojo_plugin/utils/discord.py b/dojo_plugin/utils/discord.py index c9ea3900..d52c0a5b 100644 --- a/dojo_plugin/utils/discord.py +++ b/dojo_plugin/utils/discord.py @@ -77,7 +77,6 @@ def get_discord_member(user_id): discord_user = DiscordUsers.query.filter_by(user_id=user_id).first() if not discord_user: return None - return get_discord_member_name(discord_user.discord_id) @cache.memoize(timeout=3600)