Skip to content

Commit

Permalink
Strings rule enums drool
Browse files Browse the repository at this point in the history
  • Loading branch information
robwaz committed Oct 7, 2024
1 parent b50b895 commit 9ae245e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
60 changes: 34 additions & 26 deletions dojo_plugin/api/v1/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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/<discord_id>", 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/<discord_id>", 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"])
Expand All @@ -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]

Expand Down
16 changes: 6 additions & 10 deletions dojo_plugin/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -782,16 +778,17 @@ 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):
if not weekly:
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)
Expand All @@ -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
Expand All @@ -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"])


Expand Down
1 change: 0 additions & 1 deletion dojo_plugin/utils/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9ae245e

Please sign in to comment.