From 179f36b529f083e4b42f9e6cd9e2f99dbe08da11 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 11 Oct 2021 10:42:43 +0100 Subject: [PATCH] Tidying --- context.py | 17 +++++++++++++++++ daemon.py | 24 +++++------------------- posts.py | 28 ++++------------------------ 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/context.py b/context.py index c7e51d6da..089e019f4 100644 --- a/context.py +++ b/context.py @@ -20,6 +20,23 @@ validContexts = ( ) +def getIndividualPostContext() -> []: + """Returns the context for an individual post + """ + return [ + 'https://www.w3.org/ns/activitystreams', + { + "ostatus": "http://ostatus.org#", + "atomUri": "ostatus:atomUri", + "inReplyToAtomUri": "ostatus:inReplyToAtomUri", + "conversation": "ostatus:conversation", + "sensitive": "as:sensitive", + "toot": "http://joinmastodon.org/ns#", + "votersCount": "toot:votersCount" + } + ] + + def hasValidContext(postJsonObject: {}) -> bool: """Are the links within the @context of a post recognised? """ diff --git a/daemon.py b/daemon.py index 57c0cd9c6..e6b205d79 100644 --- a/daemon.py +++ b/daemon.py @@ -341,6 +341,7 @@ from filters import isFiltered from filters import addGlobalFilter from filters import removeGlobalFilter from context import hasValidContext +from context import getIndividualPostContext from speaker import getSSMLbox from city import getSpoofedCity import os @@ -8757,18 +8758,8 @@ class PubServer(BaseHTTPRequestHandler): postJsonObject['type'] == 'Create' and \ hasObjectDict(postJsonObject): unwrappedJson = postJsonObject['object'] - unwrappedJson['@context'] = [ - 'https://www.w3.org/ns/activitystreams', - { - 'atomUri': 'ostatus:atomUri', - 'conversation': 'ostatus:conversation', - 'inReplyToAtomUri': 'ostatus:inReplyToAtomUri', - 'ostatus': 'http://ostatus.org#', - 'sensitive': 'as:sensitive', - 'toot': 'http://joinmastodon.org/ns#', - 'votersCount': 'toot:votersCount' - } - ] + unwrappedJson['@context'] = \ + getIndividualPostContext() msg = json.dumps(unwrappedJson, ensure_ascii=False) else: @@ -10791,14 +10782,9 @@ class PubServer(BaseHTTPRequestHandler): actor/collections/featuredTags TODO add ability to set a featured tags """ + postContext = getIndividualPostContext() featuredTagsCollection = { - '@context': ['https://www.w3.org/ns/activitystreams', - {'atomUri': 'ostatus:atomUri', - 'conversation': 'ostatus:conversation', - 'inReplyToAtomUri': 'ostatus:inReplyToAtomUri', - 'sensitive': 'as:sensitive', - 'toot': 'http://joinmastodon.org/ns#', - 'votersCount': 'toot:votersCount'}], + '@context': postContext, 'id': httpPrefix + '://' + domainFull + path, 'orderedItems': [], 'totalItems': 0, diff --git a/posts.py b/posts.py index 11d794690..a95ad40e9 100644 --- a/posts.py +++ b/posts.py @@ -83,6 +83,7 @@ from git import convertPostToPatch from linked_data_sig import generateJsonSignature from petnames import resolvePetnames from video import convertVideoToNote +from context import getIndividualPostContext def isModerator(baseDir: str, nickname: str) -> bool: @@ -1382,18 +1383,7 @@ def _createPostBase(baseDir: str, nickname: str, domain: str, port: int, summary, content, schedulePost, eventUUID, location, tags) - postContext = [ - 'https://www.w3.org/ns/activitystreams', - { - "ostatus": "http://ostatus.org#", - "atomUri": "ostatus:atomUri", - "inReplyToAtomUri": "ostatus:inReplyToAtomUri", - "conversation": "ostatus:conversation", - "sensitive": "as:sensitive", - "toot": "http://joinmastodon.org/ns#", - "votersCount": "toot:votersCount" - } - ] + postContext = getIndividualPostContext() # make sure that CC doesn't also contain a To address # eg. To: [ "https://mydomain/users/foo/followers" ] @@ -1615,19 +1605,9 @@ def jsonPinPost(baseDir: str, httpPrefix: str, itemsList = [pinnedPostJson] actor = localActorUrl(httpPrefix, nickname, domainFull) + postContext = getIndividualPostContext() return { - '@context': [ - 'https://www.w3.org/ns/activitystreams', - { - 'atomUri': 'ostatus:atomUri', - 'conversation': 'ostatus:conversation', - 'inReplyToAtomUri': 'ostatus:inReplyToAtomUri', - 'ostatus': 'http://ostatus.org#', - 'sensitive': 'as:sensitive', - 'toot': 'http://joinmastodon.org/ns#', - 'votersCount': 'toot:votersCount' - } - ], + '@context': postContext, 'id': actor + '/collections/featured', 'orderedItems': itemsList, 'totalItems': len(itemsList),