From 6602d0b78e159e3089b8a0c920b12005b58555bc Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 3 Jul 2019 23:59:56 +0100 Subject: [PATCH] Create id of object --- daemon.py | 27 ++++++++++++++++----------- posts.py | 16 ++++++++++++---- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/daemon.py b/daemon.py index d86e1113..75a04b4f 100644 --- a/daemon.py +++ b/daemon.py @@ -107,16 +107,17 @@ class PubServer(BaseHTTPRequestHandler): Client to server message post https://www.w3.org/TR/activitypub/#client-to-server-outbox-delivery """ - if not messageJson.get('object'): - if messageJson.get('type') and messageJson.get('content'): - if messageJson['type']!='Create': - # https://www.w3.org/TR/activitypub/#object-without-create - if self.server.debug: - print('DEBUG POST to outbox: Adding Create wrapper') - messageJson= \ - outboxMessageCreateWrap(self.server.httpPrefix, \ - self.postToNickname, \ - self.server.domain,messageJson) + if not messageJson.get('type'): + return False + if not messageJson.get('object') and messageJson.get('content'): + if messageJson['type']!='Create': + # https://www.w3.org/TR/activitypub/#object-without-create + if self.server.debug: + print('DEBUG POST to outbox: Adding Create wrapper') + messageJson= \ + outboxMessageCreateWrap(self.server.httpPrefix, \ + self.postToNickname, \ + self.server.domain,messageJson) if messageJson['type']=='Create': if not (messageJson.get('id') and \ messageJson.get('type') and \ @@ -134,7 +135,11 @@ class PubServer(BaseHTTPRequestHandler): if self.server.debug: print('DEBUG POST to outbox: '+messageJson['type']+' is not a permitted activity type') return False - savePostToOutbox(self.server.baseDir,messageJson['id'],self.postToNickname,self.server.domain,messageJson) + if messageJson.get('id'): + postId=messageJson['id'] + else: + postId=None + savePostToOutbox(self.server.baseDir,postId,self.postToNickname,self.server.domain,messageJson) return True def do_GET(self): diff --git a/posts.py b/posts.py index a7e2ca3b..11ddb968 100644 --- a/posts.py +++ b/posts.py @@ -249,11 +249,19 @@ def deleteAllPosts(baseDir: str,nickname: str, domain: str) -> None: except Exception as e: print(e) -def savePostToOutbox(baseDir: str,postId: str,nickname: str, domain: str,postJson: {}) -> None: +def savePostToOutbox(baseDir: str,httpPrefix: str,postId: str,nickname: str, domain: str,postJson: {}) -> None: """Saves the give json to the outbox """ if ':' in domain: domain=domain.split(':')[0] + if not postId: + statusNumber,published = getStatusNumber() + postId=httpPrefix+'://'+domain+'/users/'+nickname+'/statuses/'+statusNumber + postJson['id']=postId+'/activity' + if postJson.get('object'): + postJson['object']['id']=postId + postJson['object']['atomUri']=postId + outboxDir = createOutboxDir(nickname,domain,baseDir) filename=outboxDir+'/'+postId.replace('/','#')+'.json' with open(filename, 'w') as fp: @@ -301,7 +309,7 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \ 'to': [toUrl], 'cc': [], 'sensitive': sensitive, - 'atomUri': httpPrefix+'://'+domain+'/users/'+nickname+'/statuses/'+statusNumber, + 'atomUri': newPostId, 'inReplyToAtomUri': inReplyToAtomUri, 'conversation': 'tag:'+domain+','+conversationDate+':objectId='+conversationId+':objectType=Conversation', 'content': content, @@ -333,7 +341,7 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \ 'to': [toUrl], 'cc': [], 'sensitive': sensitive, - 'atomUri': httpPrefix+'://'+domain+'/users/'+nickname+'/statuses/'+statusNumber, + 'atomUri': newPostId, 'inReplyToAtomUri': inReplyToAtomUri, 'conversation': 'tag:'+domain+','+conversationDate+':objectId='+conversationId+':objectType=Conversation', 'content': content, @@ -349,7 +357,7 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \ newPost['cc']=ccUrl newPost['object']['cc']=ccUrl if saveToFile: - savePostToOutbox(baseDir,newPostId,nickname,domain,newPost) + savePostToOutbox(baseDir,httpPrefix,newPostId,nickname,domain,newPost) return newPost def outboxMessageCreateWrap(httpPrefix str,nickname: str,domain: str,messageJson: {}) -> {}: