From e4503b7143a0f0af56b80ddb08bdd42d1b8ec6a3 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 22 Jul 2019 15:09:21 +0100 Subject: [PATCH] Look up avatars for profile screen --- announce.py | 2 +- availability.py | 2 +- blocking.py | 4 +-- daemon.py | 32 ++++++++++++++++++- delete.py | 2 +- follow.py | 4 +-- like.py | 4 +-- posts.py | 22 +++++++------ roles.py | 2 +- skills.py | 2 +- webinterface.py | 85 ++++++++++++++++++++++++++++++++++++------------- 11 files changed, 118 insertions(+), 43 deletions(-) diff --git a/announce.py b/announce.py index e5539d71..3214a5cd 100644 --- a/announce.py +++ b/announce.py @@ -280,7 +280,7 @@ def sendAnnounceViaServer(session,fromNickname: str,password: str, postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: diff --git a/availability.py b/availability.py index 8b1ee195..6bee702a 100644 --- a/availability.py +++ b/availability.py @@ -107,7 +107,7 @@ def sendAvailabilityViaServer(session,nickname: str,password: str, postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: diff --git a/blocking.py b/blocking.py index fb5cbf6b..bd7e8f99 100644 --- a/blocking.py +++ b/blocking.py @@ -95,7 +95,7 @@ def sendBlockViaServer(session,fromNickname: str,password: str, postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: @@ -167,7 +167,7 @@ def sendUndoBlockViaServer(session,fromNickname: str,password: str, postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: diff --git a/daemon.py b/daemon.py index 9d06f388..03908d62 100644 --- a/daemon.py +++ b/daemon.py @@ -651,12 +651,26 @@ class PubServer(BaseHTTPRequestHandler): getPerson = personLookup(self.server.domain,self.path.replace('/following',''), \ self.server.baseDir) if getPerson: + if not self.server.session: + if self.server.debug: + print('DEBUG: creating new session for c2s') + self.server.session= \ + createSession(self.server.domain,self.server.port,self.server.useTor) + + if not self.server.session: + if self.server.debug: + print('DEBUG: creating new session') + self.server.session= \ + createSession(self.server.domain,self.server.port,self.server.useTor) self._set_headers('text/html') self.wfile.write(htmlProfile(self.server.baseDir, \ self.server.httpPrefix, \ authorized, \ self.server.ocapAlways, \ getPerson,'following', \ + self.server.session, \ + self.server.cachedWebfingers, \ + self.server.personCache, \ following).encode('utf-8')) self.server.GETbusy=False return @@ -680,12 +694,20 @@ class PubServer(BaseHTTPRequestHandler): getPerson = personLookup(self.server.domain,self.path.replace('/followers',''), \ self.server.baseDir) if getPerson: + if not self.server.session: + if self.server.debug: + print('DEBUG: creating new session') + self.server.session= \ + createSession(self.server.domain,self.server.port,self.server.useTor) self._set_headers('text/html') self.wfile.write(htmlProfile(self.server.baseDir, \ self.server.httpPrefix, \ authorized, \ self.server.ocapAlways, \ getPerson,'followers', \ + self.server.session, \ + self.server.cachedWebfingers, \ + self.server.personCache, \ followers).encode('utf-8')) self.server.GETbusy=False return @@ -699,12 +721,20 @@ class PubServer(BaseHTTPRequestHandler): self.server.baseDir) if getPerson: if 'text/html' in self.headers['Accept']: + if not self.server.session: + if self.server.debug: + print('DEBUG: creating new session') + self.server.session= \ + createSession(self.server.domain,self.server.port,self.server.useTor) self._set_headers('text/html') self.wfile.write(htmlProfile(self.server.baseDir, \ self.server.httpPrefix, \ authorized, \ self.server.ocapAlways, \ - getPerson,'posts').encode('utf-8')) + getPerson,'posts', + self.server.session, \ + self.server.cachedWebfingers, \ + self.server.personCache).encode('utf-8')) else: self._set_headers('application/json') self.wfile.write(json.dumps(getPerson).encode('utf-8')) diff --git a/delete.py b/delete.py index e10ff06d..10208129 100644 --- a/delete.py +++ b/delete.py @@ -116,7 +116,7 @@ def sendDeleteViaServer(session,fromNickname: str,password: str, postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: diff --git a/follow.py b/follow.py index f018cabd..e7dca64b 100644 --- a/follow.py +++ b/follow.py @@ -513,7 +513,7 @@ def sendFollowRequestViaServer(session,fromNickname: str,password: str, postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: @@ -588,7 +588,7 @@ def sendUnfollowRequestViaServer(session,fromNickname: str,password: str, postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: diff --git a/like.py b/like.py index f9cab144..5298b001 100644 --- a/like.py +++ b/like.py @@ -312,7 +312,7 @@ def sendLikeViaServer(session,fromNickname: str,password: str, postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: @@ -383,7 +383,7 @@ def sendUndoLikeViaServer(session,fromNickname: str,password: str, postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: diff --git a/posts.py b/posts.py index 50366e95..58ba83b1 100644 --- a/posts.py +++ b/posts.py @@ -114,16 +114,16 @@ def parseUserFeed(session,feedUrl: str,asHeader: {}) -> None: yield item def getPersonBox(session,wfRequest: {},personCache: {}, \ - boxName='inbox') -> (str,str,str,str,str): + boxName='inbox') -> (str,str,str,str,str,str): asHeader = {'Accept': 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'} personUrl = getUserUrl(wfRequest) if not personUrl: - return None,None,None,None,None + return None,None,None,None,None,None personJson = getPersonFromCache(personUrl,personCache) if not personJson: personJson = getJson(session,personUrl,asHeader,None) if not personJson: - return None,None,None,None,None + return None,None,None,None,None,None boxJson=None if not personJson.get(boxName): if personJson.get('endpoints'): @@ -133,7 +133,7 @@ def getPersonBox(session,wfRequest: {},personCache: {}, \ boxJson=personJson[boxName] if not boxJson: - return None,None,None,None,None + return None,None,None,None,None,None personId=None if personJson.get('id'): @@ -155,10 +155,14 @@ def getPersonBox(session,wfRequest: {},personCache: {}, \ capabilityAcquisition=None if personJson.get('capabilityAcquisitionEndpoint'): capabilityAcquisition=personJson['capabilityAcquisitionEndpoint'] + avaratUrl=None + if personJson.get('icon'): + if personJson['icon'].get('url'): + avaratUrl=personJson['icon']['url'] storePersonInCache(personUrl,personJson,personCache) - return boxJson,pubKeyId,pubKey,personId,sharedInbox,capabilityAcquisition + return boxJson,pubKeyId,pubKey,personId,sharedInbox,capabilityAcquisition,avatarUrl def getPosts(session,outboxUrl: str,maxPosts: int, \ maxMentions: int, \ @@ -656,7 +660,7 @@ def sendPost(session,baseDir: str,nickname: str, domain: str, port: int, \ postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,toPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,toPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) # If there are more than one followers on the target domain @@ -743,7 +747,7 @@ def sendPostViaServer(session,fromNickname: str,password: str, \ postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: @@ -869,7 +873,7 @@ def sendSignedJson(postJsonObject: {},session,baseDir: str, \ postToBox='outbox' # get the actor inbox/outbox/capabilities for the To handle - inboxUrl,pubKeyId,pubKey,toPersonId,sharedInboxUrl,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,toPersonId,sharedInboxUrl,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if nickname=='capabilities': @@ -1319,7 +1323,7 @@ def getPublicPostsOfPerson(nickname: str,domain: str, \ if not wfRequest: sys.exit() - personUrl,pubKeyId,pubKey,personId,shaedInbox,capabilityAcquisition= \ + personUrl,pubKeyId,pubKey,personId,shaedInbox,capabilityAcquisition,avatarUrl= \ getPersonBox(session,wfRequest,personCache,'outbox') wfResult = json.dumps(wfRequest, indent=4, sort_keys=True) diff --git a/roles.py b/roles.py index db55deab..30000731 100644 --- a/roles.py +++ b/roles.py @@ -187,7 +187,7 @@ def sendRoleViaServer(session,delegatorNickname: str,password: str, postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: diff --git a/skills.py b/skills.py index 4c3bb9a8..9d8be959 100644 --- a/skills.py +++ b/skills.py @@ -118,7 +118,7 @@ def sendSkillViaServer(session,nickname: str,password: str, postToBox='outbox' # get the actor inbox for the To handle - inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl = \ getPersonBox(session,wfRequest,personCache,postToBox) if not inboxUrl: diff --git a/webinterface.py b/webinterface.py index 3fba05ce..17ebe70f 100644 --- a/webinterface.py +++ b/webinterface.py @@ -11,6 +11,7 @@ from pprint import pprint from person import personBoxJson from utils import getNicknameFromActor from utils import getDomainFromActor +from posts import getPersonBox def htmlHeader(css=None,lang='en') -> str: if not css: @@ -37,30 +38,43 @@ def htmlFooter() -> str: '\n' return htmlStr -def htmlProfilePosts(baseDir: str,httpPrefix: str,authorized: bool,ocapAlways: bool,nickname: str,domain: str,port: int) -> str: +def htmlProfilePosts(baseDir: str,httpPrefix: str, \ + authorized: bool,ocapAlways: bool, \ + nickname: str,domain: str,port: int, \ + session,wfRequest: {},personCache: {}) -> str: """Shows posts on the profile screen """ profileStr='' - outboxFeed=personBoxJson(baseDir,domain, \ - port,'/users/'+nickname+'/outbox?page=1', \ - httpPrefix, \ - 4, 'outbox', \ - authorized, \ - ocapAlways) + outboxFeed= \ + personBoxJson(baseDir,domain, \ + port,'/users/'+nickname+'/outbox?page=1', \ + httpPrefix, \ + 4, 'outbox', \ + authorized, \ + ocapAlways) for item in outboxFeed['orderedItems']: if item['type']=='Create': - profileStr+=individualPostAsHtml(item) + profileStr+= \ + individualPostAsHtml(session,wfRequest,personCache, \ + domain,item) return profileStr -def htmlProfileFollowing(baseDir: str,httpPrefix: str,authorized: bool,ocapAlways: bool,nickname: str,domain: str,port: int,followingJson: {}) -> str: +def htmlProfileFollowing(baseDir: str,httpPrefix: str, \ + authorized: bool,ocapAlways: bool, \ + nickname: str,domain: str,port: int, \ + session,wfRequest: {},personCache: {}, \ + followingJson: {}) -> str: """Shows following on the profile screen """ profileStr='' for item in followingJson['orderedItems']: - profileStr+=individualFollowAsHtml(item) + profileStr+=individualFollowAsHtml(session,wfRequest,personCache,domain,item) return profileStr -def htmlProfile(baseDir: str,httpPrefix: str,authorized: bool,ocapAlways: bool,profileJson: {},selected: str,extraJson=None) -> str: +def htmlProfile(baseDir: str,httpPrefix: str,authorized: bool, \ + ocapAlways: bool,profileJson: {},selected: str, \ + session,wfRequest: {},personCache: {}, \ + extraJson=None) -> str: """Show the profile page as html """ nickname=profileJson['name'] @@ -303,24 +317,40 @@ def htmlProfile(baseDir: str,httpPrefix: str,authorized: bool,ocapAlways: bool,p '}' if selected=='posts': - profileStr+=htmlProfilePosts(baseDir,httpPrefix,authorized,ocapAlways,nickname,domain,port) + profileStr+= \ + htmlProfilePosts(baseDir,httpPrefix,authorized, \ + ocapAlways,nickname,domain,port, \ + session,wfRequest,personCache) if selected=='following' or selected=='followers': - profileStr+=htmlProfileFollowing(baseDir,httpPrefix,authorized,ocapAlways,nickname,domain,port,extraJson) + profileStr+= \ + htmlProfileFollowing(baseDir,httpPrefix, \ + authorized,ocapAlways,nickname, \ + domain,port,session, \ + wfRequest,personCache,extraJson) profileStr=htmlHeader(profileStyle)+profileStr+htmlFooter() return profileStr -def individualFollowAsHtml(followUrl: str) -> str: +def individualFollowAsHtml(session,wfRequest: {}, \ + personCache: {},domain: str, \ + followUrl: str) -> str: nickname=getNicknameFromActor(followUrl) domain,port=getDomainFromActor(followUrl) titleStr='@'+nickname+'@'+domain + avatarUrl=followUrl+'/avatar.png' + if domain not in followUrl: + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl2 = \ + getPersonBox(session,wfRequest,personCache,'outbox') + if avatarUrl2: + avatarUrl=avatarUrl2 return \ '
\n' \ '' \ - 'Avatar\n'+ \ + 'Avatar\n'+ \ '

'+titleStr+'

'+ \ '
\n' -def individualPostAsHtml(postJsonObject: {}) -> str: +def individualPostAsHtml(session,wfRequest: {},personCache: {}, \ + domain: str,postJsonObject: {}) -> str: avatarPosition='' containerClass='container' timeClass='time-right' @@ -361,18 +391,26 @@ def individualPostAsHtml(postJsonObject: {}) -> str: '' \ ''+imageDescription+'\n' attachmentCtr+=1 - + + avatarUrl=postJsonObject['actor']+'/avatar.png' + if domain not in postJsonObject['actor']: + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl2 = \ + getPersonBox(session,wfRequest,personCache,'outbox') + if avatarUrl2: + avatarUrl=avatarUrl2 + return \ '
\n' \ '' \ - 'Avatar\n'+ \ + 'Avatar\n'+ \ '

'+titleStr+'

'+ \ postJsonObject['object']['content']+'\n'+ \ attachmentStr+ \ ''+postJsonObject['object']['published']+'\n'+ \ '
\n' -def htmlTimeline(timelineJson: {}) -> str: +def htmlTimeline(session,wfRequest: {},personCache: {}, \ + domain: str,timelineJson: {}) -> str: """Show the timeline as html """ if not timelineJson.get('orderedItems'): @@ -380,7 +418,8 @@ def htmlTimeline(timelineJson: {}) -> str: tlStr=htmlHeader() for item in timelineJson['orderedItems']: if item['type']=='Create': - tlStr+=individualPostAsHtml(item) + tlStr+=individualPostAsHtml(session,wfRequest,personCache, \ + domain,item) tlStr+=htmlFooter() return tlStr @@ -394,11 +433,13 @@ def htmlOutbox(outboxJson: {}) -> str: """ return htmlTimeline(outboxJson) -def htmlIndividualPost(postJsonObject: {}) -> str: +def htmlIndividualPost(session,wfRequest: {},personCache: {}, \ + domain: str,postJsonObject: {}) -> str: """Show an individual post as html """ return htmlHeader()+ \ - individualPostAsHtml(postJsonObject)+ \ + individualPostAsHtml(session,wfRequest,personCache, \ + domain,postJsonObject)+ \ htmlFooter() def htmlPostReplies(postJsonObject: {}) -> str: