From 0fffaf188b85faf51642230b2048d1f1614ecf0d Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sun, 17 Dec 2023 20:33:27 +1300 Subject: [PATCH] vote federation --- app/models.py | 6 ++---- app/post/routes.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/app/models.py b/app/models.py index 469087a8..949a406a 100644 --- a/app/models.py +++ b/app/models.py @@ -19,9 +19,9 @@ from app.constants import SUBSCRIPTION_NONMEMBER, SUBSCRIPTION_MEMBER, SUBSCRIPT SUBSCRIPTION_BANNED, SUBSCRIPTION_PENDING -# same as datetime.utcnow() except with the UTC timezone explicitly added. datetime.utcnow() is depreciated in python 3.12+ +# datetime.utcnow() is depreciated in Python 3.12 so it will need to be swapped out eventually def utcnow(): - return datetime.now(timezone.utc) + return datetime.utcnow() class FullTextSearchQuery(BaseQuery, SearchQueryMixin): @@ -362,8 +362,6 @@ class User(UserMixin, db.Model): return self.ap_profile_id if self.ap_profile_id else f"https://{current_app.config['SERVER_NAME']}/u/{self.user_name}" def created_recently(self): - if self.created.tzinfo is None: - self.created = self.created.replace(tzinfo=timezone.utc) return self.created and self.created > utcnow() - timedelta(days=7) @staticmethod diff --git a/app/post/routes.py b/app/post/routes.py index d317c2f6..12dfec85 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -179,6 +179,29 @@ def post_vote(post_id: int, vote_direction): effect=effect) post.author.reputation += effect db.session.add(vote) + + if post.community.is_local(): + ... + else: + action_type = 'Like' if vote_direction == 'upvote' else 'Dislike' + action_json = { + 'actor': current_user.profile_id(), + 'object': post.profile_id(), + 'type': action_type, + 'id': f"https://{current_app.config['SERVER_NAME']}/activities/{action_type.lower()}/{gibberish(15)}", + 'audience': post.community.profile_id() + } + try: + message = HttpSignature.signed_request(post.community.ap_inbox_url, action_json, current_user.private_key, + current_user.ap_profile_id + '#main-key') + if message.status_code != 200: + flash('Response status code was not 200', 'warning') + current_app.logger.error('Response code for reply attempt was ' + + str(message.status_code) + ' ' + message.text) + except Exception as ex: + flash('Failed to send reply: ' + str(ex), 'error') + current_app.logger.error("Exception while trying to send reply" + str(ex)) + current_user.last_seen = utcnow() db.session.commit() post.flush_cache() @@ -231,6 +254,28 @@ def comment_vote(comment_id, vote_direction): vote = PostReplyVote(user_id=current_user.id, post_reply_id=comment_id, author_id=comment.author.id, effect=effect) comment.author.reputation += effect db.session.add(vote) + + if comment.community.is_local(): + ... + else: + action_type = 'Like' if vote_direction == 'upvote' else 'Dislike' + action_json = { + 'actor': current_user.profile_id(), + 'object': comment.profile_id(), + 'type': action_type, + 'id': f"https://{current_app.config['SERVER_NAME']}/activities/{action_type.lower()}/{gibberish(15)}", + 'audience': comment.community.profile_id() + } + try: + message = HttpSignature.signed_request(comment.community.ap_inbox_url, action_json, current_user.private_key, + current_user.ap_profile_id + '#main-key') + if message.status_code != 200: + flash('Response status code was not 200', 'warning') + current_app.logger.error('Response code for reply attempt was ' + + str(message.status_code) + ' ' + message.text) + except Exception as ex: + flash('Failed to send reply: ' + str(ex), 'error') + current_app.logger.error("Exception while trying to send reply" + str(ex)) current_user.last_seen = utcnow() db.session.commit() comment.post.flush_cache()