From 2b038256382dd124d4376282f874f2d4e3f191d3 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 3 Sep 2021 13:00:23 +0100 Subject: [PATCH] Move likes update to like module --- daemon.py | 2 +- inbox.py | 2 +- like.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- utils.py | 61 ------------------------------------------------- 4 files changed, 69 insertions(+), 64 deletions(-) diff --git a/daemon.py b/daemon.py index 98d0c32bd..aec07c50a 100644 --- a/daemon.py +++ b/daemon.py @@ -228,6 +228,7 @@ from categories import setHashtagCategory from categories import updateHashtagCategories from languages import getActorLanguages from languages import setActorLanguages +from like import updateLikesCollection from utils import replaceUsersWithAt from utils import localActorUrl from utils import isfloat @@ -265,7 +266,6 @@ from utils import isSystemAccount from utils import setConfigParam from utils import getConfigParam from utils import removeIdEnding -from utils import updateLikesCollection from utils import undoLikesCollectionEntry from utils import deletePost from utils import isBlogPost diff --git a/inbox.py b/inbox.py index df51ab579..cc1dc105e 100644 --- a/inbox.py +++ b/inbox.py @@ -14,6 +14,7 @@ import time import random from linked_data_sig import verifyJsonSignature from languages import understoodPostLanguage +from like import updateLikesCollection from utils import getUserPaths from utils import getBaseContentFromPost from utils import acctDir @@ -43,7 +44,6 @@ from utils import deletePost from utils import removeModerationPostFromIndex from utils import loadJson from utils import saveJson -from utils import updateLikesCollection from utils import undoLikesCollectionEntry from utils import hasGroupType from utils import localActorUrl diff --git a/like.py b/like.py index b0617c942..e928e6bcb 100644 --- a/like.py +++ b/like.py @@ -7,6 +7,8 @@ __email__ = "bob@freedombone.net" __status__ = "Production" __module_group__ = "ActivityPub" +import os +from pprint import pprint from utils import removeDomainPort from utils import hasObjectDict from utils import hasUsersPath @@ -16,10 +18,13 @@ from utils import urlPermitted from utils import getNicknameFromActor from utils import getDomainFromActor from utils import locatePost -from utils import updateLikesCollection from utils import undoLikesCollectionEntry from utils import hasGroupType from utils import localActorUrl +from utils import loadJson +from utils import saveJson +from utils import removePostFromCache +from utils import getCachedPostFilename from posts import sendSignedJson from session import postJson from webfinger import webfingerHandle @@ -407,3 +412,64 @@ def outboxUndoLike(recentPostsCache: {}, domain, debug) if debug: print('DEBUG: post undo liked via c2s - ' + postFilename) + + +def updateLikesCollection(recentPostsCache: {}, + baseDir: str, postFilename: str, + objectUrl: str, actor: str, + nickname: str, domain: str, debug: bool) -> None: + """Updates the likes collection within a post + """ + postJsonObject = loadJson(postFilename) + if not postJsonObject: + return + # remove any cached version of this post so that the + # like icon is changed + removePostFromCache(postJsonObject, recentPostsCache) + cachedPostFilename = getCachedPostFilename(baseDir, nickname, + domain, postJsonObject) + if cachedPostFilename: + if os.path.isfile(cachedPostFilename): + os.remove(cachedPostFilename) + + if not hasObjectDict(postJsonObject): + if debug: + pprint(postJsonObject) + print('DEBUG: post ' + objectUrl + ' has no object') + return + if not objectUrl.endswith('/likes'): + objectUrl = objectUrl + '/likes' + if not postJsonObject['object'].get('likes'): + if debug: + print('DEBUG: Adding initial like to ' + objectUrl) + likesJson = { + "@context": "https://www.w3.org/ns/activitystreams", + 'id': objectUrl, + 'type': 'Collection', + "totalItems": 1, + 'items': [{ + 'type': 'Like', + 'actor': actor + }] + } + postJsonObject['object']['likes'] = likesJson + else: + if not postJsonObject['object']['likes'].get('items'): + postJsonObject['object']['likes']['items'] = [] + for likeItem in postJsonObject['object']['likes']['items']: + if likeItem.get('actor'): + if likeItem['actor'] == actor: + # already liked + return + newLike = { + 'type': 'Like', + 'actor': actor + } + postJsonObject['object']['likes']['items'].append(newLike) + itlen = len(postJsonObject['object']['likes']['items']) + postJsonObject['object']['likes']['totalItems'] = itlen + + if debug: + print('DEBUG: saving post with likes added') + pprint(postJsonObject) + saveJson(postJsonObject, postFilename) diff --git a/utils.py b/utils.py index 6b0276533..749c89c98 100644 --- a/utils.py +++ b/utils.py @@ -2065,67 +2065,6 @@ def undoLikesCollectionEntry(recentPostsCache: {}, saveJson(postJsonObject, postFilename) -def updateLikesCollection(recentPostsCache: {}, - baseDir: str, postFilename: str, - objectUrl: str, actor: str, - nickname: str, domain: str, debug: bool) -> None: - """Updates the likes collection within a post - """ - postJsonObject = loadJson(postFilename) - if not postJsonObject: - return - # remove any cached version of this post so that the - # like icon is changed - removePostFromCache(postJsonObject, recentPostsCache) - cachedPostFilename = getCachedPostFilename(baseDir, nickname, - domain, postJsonObject) - if cachedPostFilename: - if os.path.isfile(cachedPostFilename): - os.remove(cachedPostFilename) - - if not hasObjectDict(postJsonObject): - if debug: - pprint(postJsonObject) - print('DEBUG: post ' + objectUrl + ' has no object') - return - if not objectUrl.endswith('/likes'): - objectUrl = objectUrl + '/likes' - if not postJsonObject['object'].get('likes'): - if debug: - print('DEBUG: Adding initial like to ' + objectUrl) - likesJson = { - "@context": "https://www.w3.org/ns/activitystreams", - 'id': objectUrl, - 'type': 'Collection', - "totalItems": 1, - 'items': [{ - 'type': 'Like', - 'actor': actor - }] - } - postJsonObject['object']['likes'] = likesJson - else: - if not postJsonObject['object']['likes'].get('items'): - postJsonObject['object']['likes']['items'] = [] - for likeItem in postJsonObject['object']['likes']['items']: - if likeItem.get('actor'): - if likeItem['actor'] == actor: - # already liked - return - newLike = { - 'type': 'Like', - 'actor': actor - } - postJsonObject['object']['likes']['items'].append(newLike) - itlen = len(postJsonObject['object']['likes']['items']) - postJsonObject['object']['likes']['totalItems'] = itlen - - if debug: - print('DEBUG: saving post with likes added') - pprint(postJsonObject) - saveJson(postJsonObject, postFilename) - - def undoAnnounceCollectionEntry(recentPostsCache: {}, baseDir: str, postFilename: str, actor: str, domain: str, debug: bool) -> None: