diff --git a/daemon.py b/daemon.py index f50ca9d85..a288e8c87 100644 --- a/daemon.py +++ b/daemon.py @@ -5242,6 +5242,28 @@ class PubServer(BaseHTTPRequestHandler): print('favicon not sent: ' + callingDomain) self._404() + def _getSpeaker(self, callingDomain: str, path: str, + baseDir: str, domain: str, debug: bool) -> None: + """Returns the speaker file used for TTS and + accessed via c2s + """ + nickname = path.split('/users/')[1] + if '/' in nickname: + nickname = nickname.split('/')[0] + speakerFilename = \ + baseDir + '/accounts/' + nickname + '@' + domain + '/speaker.json' + if not os.path.isfile(speakerFilename): + self._404() + return + + speakerJson = loadJson(speakerFilename) + msg = json.dumps(speakerJson, + ensure_ascii=False).encode('utf-8') + msglen = len(msg) + self._set_headers('application/json', msglen, + None, callingDomain) + self._write(msg) + def _getFonts(self, callingDomain: str, path: str, baseDir: str, debug: bool, GETstartTime, GETtimings: {}) -> None: @@ -10454,6 +10476,16 @@ class PubServer(BaseHTTPRequestHandler): if '/users/' in self.path: usersInPath = True + # authorized endpoint used for TTS of posts + # arriving in your inbox + if authorized and usersInPath and \ + self.path.endswith('/speaker'): + self._getSpeaker(callingDomain, self.path, + self.server.baseDir, + self.server.domain, + self.server.debug) + return + # redirect to the welcome screen if htmlGET and authorized and usersInPath and \ '/welcome' not in self.path: diff --git a/inbox.py b/inbox.py index dd408122e..d44e6de75 100644 --- a/inbox.py +++ b/inbox.py @@ -11,6 +11,8 @@ import os import datetime import time from linked_data_sig import verifyJsonSignature +from utils import getDisplayName +from utils import removeHtml from utils import getConfigParam from utils import hasUsersPath from utils import validPostDate @@ -2134,6 +2136,36 @@ def _bounceDM(senderPostId: str, session, httpPrefix: str, return True +def _updateSpeaker(baseDir: str, nickname: str, domain: str, + postJsonObject: {}, personCache: {}) -> None: + """ Generates a json file which can be used for TTS announcement + of incoming inbox posts + """ + if not postJsonObject.get('object'): + return + if not isinstance(postJsonObject['object'], dict): + return + if not postJsonObject['object'].get('content'): + return + if not isinstance(postJsonObject['object']['content'], str): + return + speakerFilename = \ + baseDir + '/accounts/' + nickname + '@' + domain + '/speaker.json' + content = removeHtml(postJsonObject['object']['content']) + summary = '' + if postJsonObject['object'].get('summary'): + if isinstance(postJsonObject['object']['summary'], str): + summary = postJsonObject['object']['summary'] + speakerName = \ + getDisplayName(baseDir, postJsonObject['actor'], personCache) + speakerJson = { + "name": speakerName, + "summary": summary, + "say": content + } + saveJson(speakerJson, speakerFilename) + + def _inboxAfterInitial(recentPostsCache: {}, maxRecentPosts: int, session, keyId: str, handle: str, messageJson: {}, baseDir: str, httpPrefix: str, sendThreads: [], @@ -2468,6 +2500,9 @@ def _inboxAfterInitial(recentPostsCache: {}, maxRecentPosts: int, destinationFilename, debug): print('ERROR: unable to update ' + boxname + ' index') else: + if boxname == 'inbox': + _updateSpeaker(baseDir, nickname, domain, + postJsonObject, personCache) if not unitTest: if debug: print('Saving inbox post as html to cache')