vote federation

This commit is contained in:
rimu 2023-12-17 20:33:27 +13:00
parent c3839e6873
commit 0fffaf188b
2 changed files with 47 additions and 4 deletions

View file

@ -19,9 +19,9 @@ from app.constants import SUBSCRIPTION_NONMEMBER, SUBSCRIPTION_MEMBER, SUBSCRIPT
SUBSCRIPTION_BANNED, SUBSCRIPTION_PENDING 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(): def utcnow():
return datetime.now(timezone.utc) return datetime.utcnow()
class FullTextSearchQuery(BaseQuery, SearchQueryMixin): 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}" 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): 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) return self.created and self.created > utcnow() - timedelta(days=7)
@staticmethod @staticmethod

View file

@ -179,6 +179,29 @@ def post_vote(post_id: int, vote_direction):
effect=effect) effect=effect)
post.author.reputation += effect post.author.reputation += effect
db.session.add(vote) 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() current_user.last_seen = utcnow()
db.session.commit() db.session.commit()
post.flush_cache() 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) vote = PostReplyVote(user_id=current_user.id, post_reply_id=comment_id, author_id=comment.author.id, effect=effect)
comment.author.reputation += effect comment.author.reputation += effect
db.session.add(vote) 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() current_user.last_seen = utcnow()
db.session.commit() db.session.commit()
comment.post.flush_cache() comment.post.flush_cache()