From ec07f83e5a6aba44cd842afcd0417e85f7d70aa3 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 6 Jun 2020 19:16:16 +0100 Subject: [PATCH] Fix circular dependency --- daemon.py | 14 +++--- like.py | 128 +----------------------------------------------------- utils.py | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 133 deletions(-) diff --git a/daemon.py b/daemon.py index 34c9873c..7b2eb96c 100644 --- a/daemon.py +++ b/daemon.py @@ -143,6 +143,8 @@ from shares import getSharesFeedForPerson from shares import addShare from shares import removeShare from shares import expireShares +from utils import updateLikesCollection +from utils import undoLikesCollectionEntry from utils import deletePost from utils import isBlogPost from utils import removeAvatarFromCache @@ -176,8 +178,6 @@ from outbox import postMessageToOutbox from happening import removeCalendarEvent from bookmarks import bookmark from bookmarks import undoBookmark -from like import updateLikesCollectionEntry -from like import undoLikesCollectionEntry import os @@ -2525,11 +2525,11 @@ class PubServer(BaseHTTPRequestHandler): self.server.domain, actorLiked) if likedPostFilename: - updateLikesCollectionEntry(self.server.recentPostsCache, - self.server.baseDir, - likedPostFilename, likeUrl, - likeActor, self.server.domain, - self.server.debug) + updateLikesCollection(self.server.recentPostsCache, + self.server.baseDir, + likedPostFilename, likeUrl, + likeActor, self.server.domain, + self.server.debug) # send out the like to followers self._postToOutbox(likeJson, self.server.projectVersion) self.server.GETbusy = False diff --git a/like.py b/like.py index ce8fadc2..248d4de8 100644 --- a/like.py +++ b/like.py @@ -6,16 +6,12 @@ __maintainer__ = "Bob Mottram" __email__ = "bob@freedombone.net" __status__ = "Production" -import os -from pprint import pprint -from utils import removePostFromCache from utils import urlPermitted from utils import getNicknameFromActor from utils import getDomainFromActor from utils import locatePost -from utils import getCachedPostFilename -from utils import loadJson -from utils import saveJson +from utils import updateLikesCollection +from utils import undoLikesCollectionEntry from posts import sendSignedJson from session import postJson from webfinger import webfingerHandle @@ -23,64 +19,6 @@ from auth import createBasicAuthHeader from posts import getPersonBox -def undoLikesCollectionEntry(recentPostsCache: {}, - baseDir: str, postFilename: str, objectUrl: str, - actor: str, domain: str, debug: bool) -> None: - """Undoes a like for a particular actor - """ - postJsonObject = loadJson(postFilename) - if postJsonObject: - # remove any cached version of this post so that the - # like icon is changed - nickname = getNicknameFromActor(actor) - cachedPostFilename = getCachedPostFilename(baseDir, nickname, - domain, postJsonObject) - if cachedPostFilename: - if os.path.isfile(cachedPostFilename): - os.remove(cachedPostFilename) - removePostFromCache(postJsonObject, recentPostsCache) - - if not postJsonObject.get('type'): - return - if postJsonObject['type'] != 'Create': - return - if not postJsonObject.get('object'): - if debug: - pprint(postJsonObject) - print('DEBUG: post '+objectUrl+' has no object') - return - if not isinstance(postJsonObject['object'], dict): - return - if not postJsonObject['object'].get('likes'): - return - if not isinstance(postJsonObject['object']['likes'], dict): - return - if not postJsonObject['object']['likes'].get('items'): - return - totalItems = 0 - if postJsonObject['object']['likes'].get('totalItems'): - totalItems = postJsonObject['object']['likes']['totalItems'] - itemFound = False - for likeItem in postJsonObject['object']['likes']['items']: - if likeItem.get('actor'): - if likeItem['actor'] == actor: - if debug: - print('DEBUG: like was removed for ' + actor) - postJsonObject['object']['likes']['items'].remove(likeItem) - itemFound = True - break - if itemFound: - if totalItems == 1: - if debug: - print('DEBUG: likes was removed from post') - del postJsonObject['object']['likes'] - else: - itlen = len(postJsonObject['object']['likes']['items']) - postJsonObject['object']['likes']['totalItems'] = itlen - - saveJson(postJsonObject, postFilename) - - def likedByPerson(postJsonObject: {}, nickname: str, domain: str) -> bool: """Returns True if the given post is liked by the given person """ @@ -110,68 +48,6 @@ def noOfLikes(postJsonObject: {}) -> int: return len(postJsonObject['object']['likes']['items']) -def updateLikesCollection(recentPostsCache: {}, - baseDir: str, postFilename: str, - objectUrl: str, - actor: str, domain: str, debug: bool) -> None: - """Updates the likes collection within a post - """ - postJsonObject = loadJson(postFilename) - if postJsonObject: - # remove any cached version of this post so that the - # like icon is changed - nickname = getNicknameFromActor(actor) - cachedPostFilename = getCachedPostFilename(baseDir, nickname, - domain, postJsonObject) - if cachedPostFilename: - if os.path.isfile(cachedPostFilename): - os.remove(cachedPostFilename) - removePostFromCache(postJsonObject, recentPostsCache) - - if not postJsonObject.get('object'): - if debug: - pprint(postJsonObject) - print('DEBUG: post ' + objectUrl + ' has no object') - return - if not isinstance(postJsonObject['object'], dict): - return - if not objectUrl.endswith('/likes'): - objectUrl = objectUrl + '/likes' - if not postJsonObject['object'].get('likes'): - if debug: - print('DEBUG: Adding initial likes 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: - 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 like(recentPostsCache: {}, session, baseDir: str, federationList: [], nickname: str, domain: str, port: int, diff --git a/utils.py b/utils.py index f0b5d243..67d179d3 100644 --- a/utils.py +++ b/utils.py @@ -11,6 +11,7 @@ import time import shutil import datetime import json +from pprint import pprint from calendar import monthrange @@ -802,3 +803,123 @@ def getFileCaseInsensitive(path: str) -> str: if os.path.isfile(newpath) and f.lower() == filename: return newpath return path + + +def undoLikesCollectionEntry(recentPostsCache: {}, + baseDir: str, postFilename: str, objectUrl: str, + actor: str, domain: str, debug: bool) -> None: + """Undoes a like for a particular actor + """ + postJsonObject = loadJson(postFilename) + if postJsonObject: + # remove any cached version of this post so that the + # like icon is changed + nickname = getNicknameFromActor(actor) + cachedPostFilename = getCachedPostFilename(baseDir, nickname, + domain, postJsonObject) + if cachedPostFilename: + if os.path.isfile(cachedPostFilename): + os.remove(cachedPostFilename) + removePostFromCache(postJsonObject, recentPostsCache) + + if not postJsonObject.get('type'): + return + if postJsonObject['type'] != 'Create': + return + if not postJsonObject.get('object'): + if debug: + pprint(postJsonObject) + print('DEBUG: post '+objectUrl+' has no object') + return + if not isinstance(postJsonObject['object'], dict): + return + if not postJsonObject['object'].get('likes'): + return + if not isinstance(postJsonObject['object']['likes'], dict): + return + if not postJsonObject['object']['likes'].get('items'): + return + totalItems = 0 + if postJsonObject['object']['likes'].get('totalItems'): + totalItems = postJsonObject['object']['likes']['totalItems'] + itemFound = False + for likeItem in postJsonObject['object']['likes']['items']: + if likeItem.get('actor'): + if likeItem['actor'] == actor: + if debug: + print('DEBUG: like was removed for ' + actor) + postJsonObject['object']['likes']['items'].remove(likeItem) + itemFound = True + break + if itemFound: + if totalItems == 1: + if debug: + print('DEBUG: likes was removed from post') + del postJsonObject['object']['likes'] + else: + itlen = len(postJsonObject['object']['likes']['items']) + postJsonObject['object']['likes']['totalItems'] = itlen + + saveJson(postJsonObject, postFilename) + + +def updateLikesCollection(recentPostsCache: {}, + baseDir: str, postFilename: str, + objectUrl: str, + actor: str, domain: str, debug: bool) -> None: + """Updates the likes collection within a post + """ + postJsonObject = loadJson(postFilename) + if postJsonObject: + # remove any cached version of this post so that the + # like icon is changed + nickname = getNicknameFromActor(actor) + cachedPostFilename = getCachedPostFilename(baseDir, nickname, + domain, postJsonObject) + if cachedPostFilename: + if os.path.isfile(cachedPostFilename): + os.remove(cachedPostFilename) + removePostFromCache(postJsonObject, recentPostsCache) + + if not postJsonObject.get('object'): + if debug: + pprint(postJsonObject) + print('DEBUG: post ' + objectUrl + ' has no object') + return + if not isinstance(postJsonObject['object'], dict): + return + if not objectUrl.endswith('/likes'): + objectUrl = objectUrl + '/likes' + if not postJsonObject['object'].get('likes'): + if debug: + print('DEBUG: Adding initial likes 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: + 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)