Look up avatars for profile screen

master
Bob Mottram 2019-07-22 15:09:21 +01:00
parent ed6a98af07
commit e4503b7143
11 changed files with 118 additions and 43 deletions

View File

@ -280,7 +280,7 @@ def sendAnnounceViaServer(session,fromNickname: str,password: str,
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:

View File

@ -107,7 +107,7 @@ def sendAvailabilityViaServer(session,nickname: str,password: str,
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:

View File

@ -95,7 +95,7 @@ def sendBlockViaServer(session,fromNickname: str,password: str,
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:
@ -167,7 +167,7 @@ def sendUndoBlockViaServer(session,fromNickname: str,password: str,
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:

View File

@ -651,12 +651,26 @@ class PubServer(BaseHTTPRequestHandler):
getPerson = personLookup(self.server.domain,self.path.replace('/following',''), \ getPerson = personLookup(self.server.domain,self.path.replace('/following',''), \
self.server.baseDir) self.server.baseDir)
if getPerson: 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._set_headers('text/html')
self.wfile.write(htmlProfile(self.server.baseDir, \ self.wfile.write(htmlProfile(self.server.baseDir, \
self.server.httpPrefix, \ self.server.httpPrefix, \
authorized, \ authorized, \
self.server.ocapAlways, \ self.server.ocapAlways, \
getPerson,'following', \ getPerson,'following', \
self.server.session, \
self.server.cachedWebfingers, \
self.server.personCache, \
following).encode('utf-8')) following).encode('utf-8'))
self.server.GETbusy=False self.server.GETbusy=False
return return
@ -680,12 +694,20 @@ class PubServer(BaseHTTPRequestHandler):
getPerson = personLookup(self.server.domain,self.path.replace('/followers',''), \ getPerson = personLookup(self.server.domain,self.path.replace('/followers',''), \
self.server.baseDir) self.server.baseDir)
if getPerson: 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._set_headers('text/html')
self.wfile.write(htmlProfile(self.server.baseDir, \ self.wfile.write(htmlProfile(self.server.baseDir, \
self.server.httpPrefix, \ self.server.httpPrefix, \
authorized, \ authorized, \
self.server.ocapAlways, \ self.server.ocapAlways, \
getPerson,'followers', \ getPerson,'followers', \
self.server.session, \
self.server.cachedWebfingers, \
self.server.personCache, \
followers).encode('utf-8')) followers).encode('utf-8'))
self.server.GETbusy=False self.server.GETbusy=False
return return
@ -699,12 +721,20 @@ class PubServer(BaseHTTPRequestHandler):
self.server.baseDir) self.server.baseDir)
if getPerson: if getPerson:
if 'text/html' in self.headers['Accept']: 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._set_headers('text/html')
self.wfile.write(htmlProfile(self.server.baseDir, \ self.wfile.write(htmlProfile(self.server.baseDir, \
self.server.httpPrefix, \ self.server.httpPrefix, \
authorized, \ authorized, \
self.server.ocapAlways, \ self.server.ocapAlways, \
getPerson,'posts').encode('utf-8')) getPerson,'posts',
self.server.session, \
self.server.cachedWebfingers, \
self.server.personCache).encode('utf-8'))
else: else:
self._set_headers('application/json') self._set_headers('application/json')
self.wfile.write(json.dumps(getPerson).encode('utf-8')) self.wfile.write(json.dumps(getPerson).encode('utf-8'))

View File

@ -116,7 +116,7 @@ def sendDeleteViaServer(session,fromNickname: str,password: str,
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:

View File

@ -513,7 +513,7 @@ def sendFollowRequestViaServer(session,fromNickname: str,password: str,
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:
@ -588,7 +588,7 @@ def sendUnfollowRequestViaServer(session,fromNickname: str,password: str,
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:

View File

@ -312,7 +312,7 @@ def sendLikeViaServer(session,fromNickname: str,password: str,
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:
@ -383,7 +383,7 @@ def sendUndoLikeViaServer(session,fromNickname: str,password: str,
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:

View File

@ -114,16 +114,16 @@ def parseUserFeed(session,feedUrl: str,asHeader: {}) -> None:
yield item yield item
def getPersonBox(session,wfRequest: {},personCache: {}, \ 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"'} asHeader = {'Accept': 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'}
personUrl = getUserUrl(wfRequest) personUrl = getUserUrl(wfRequest)
if not personUrl: if not personUrl:
return None,None,None,None,None return None,None,None,None,None,None
personJson = getPersonFromCache(personUrl,personCache) personJson = getPersonFromCache(personUrl,personCache)
if not personJson: if not personJson:
personJson = getJson(session,personUrl,asHeader,None) personJson = getJson(session,personUrl,asHeader,None)
if not personJson: if not personJson:
return None,None,None,None,None return None,None,None,None,None,None
boxJson=None boxJson=None
if not personJson.get(boxName): if not personJson.get(boxName):
if personJson.get('endpoints'): if personJson.get('endpoints'):
@ -133,7 +133,7 @@ def getPersonBox(session,wfRequest: {},personCache: {}, \
boxJson=personJson[boxName] boxJson=personJson[boxName]
if not boxJson: if not boxJson:
return None,None,None,None,None return None,None,None,None,None,None
personId=None personId=None
if personJson.get('id'): if personJson.get('id'):
@ -155,10 +155,14 @@ def getPersonBox(session,wfRequest: {},personCache: {}, \
capabilityAcquisition=None capabilityAcquisition=None
if personJson.get('capabilityAcquisitionEndpoint'): if personJson.get('capabilityAcquisitionEndpoint'):
capabilityAcquisition=personJson['capabilityAcquisitionEndpoint'] capabilityAcquisition=personJson['capabilityAcquisitionEndpoint']
avaratUrl=None
if personJson.get('icon'):
if personJson['icon'].get('url'):
avaratUrl=personJson['icon']['url']
storePersonInCache(personUrl,personJson,personCache) 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, \ def getPosts(session,outboxUrl: str,maxPosts: int, \
maxMentions: int, \ maxMentions: int, \
@ -656,7 +660,7 @@ def sendPost(session,baseDir: str,nickname: str, domain: str, port: int, \
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
# If there are more than one followers on the target domain # If there are more than one followers on the target domain
@ -743,7 +747,7 @@ def sendPostViaServer(session,fromNickname: str,password: str, \
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:
@ -869,7 +873,7 @@ def sendSignedJson(postJsonObject: {},session,baseDir: str, \
postToBox='outbox' postToBox='outbox'
# get the actor inbox/outbox/capabilities for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if nickname=='capabilities': if nickname=='capabilities':
@ -1319,7 +1323,7 @@ def getPublicPostsOfPerson(nickname: str,domain: str, \
if not wfRequest: if not wfRequest:
sys.exit() sys.exit()
personUrl,pubKeyId,pubKey,personId,shaedInbox,capabilityAcquisition= \ personUrl,pubKeyId,pubKey,personId,shaedInbox,capabilityAcquisition,avatarUrl= \
getPersonBox(session,wfRequest,personCache,'outbox') getPersonBox(session,wfRequest,personCache,'outbox')
wfResult = json.dumps(wfRequest, indent=4, sort_keys=True) wfResult = json.dumps(wfRequest, indent=4, sort_keys=True)

View File

@ -187,7 +187,7 @@ def sendRoleViaServer(session,delegatorNickname: str,password: str,
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:

View File

@ -118,7 +118,7 @@ def sendSkillViaServer(session,nickname: str,password: str,
postToBox='outbox' postToBox='outbox'
# get the actor inbox for the To handle # 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) getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl: if not inboxUrl:

View File

@ -11,6 +11,7 @@ from pprint import pprint
from person import personBoxJson from person import personBoxJson
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from posts import getPersonBox
def htmlHeader(css=None,lang='en') -> str: def htmlHeader(css=None,lang='en') -> str:
if not css: if not css:
@ -37,30 +38,43 @@ def htmlFooter() -> str:
'</html>\n' '</html>\n'
return htmlStr 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 """Shows posts on the profile screen
""" """
profileStr='' profileStr=''
outboxFeed=personBoxJson(baseDir,domain, \ outboxFeed= \
port,'/users/'+nickname+'/outbox?page=1', \ personBoxJson(baseDir,domain, \
httpPrefix, \ port,'/users/'+nickname+'/outbox?page=1', \
4, 'outbox', \ httpPrefix, \
authorized, \ 4, 'outbox', \
ocapAlways) authorized, \
ocapAlways)
for item in outboxFeed['orderedItems']: for item in outboxFeed['orderedItems']:
if item['type']=='Create': if item['type']=='Create':
profileStr+=individualPostAsHtml(item) profileStr+= \
individualPostAsHtml(session,wfRequest,personCache, \
domain,item)
return profileStr 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 """Shows following on the profile screen
""" """
profileStr='' profileStr=''
for item in followingJson['orderedItems']: for item in followingJson['orderedItems']:
profileStr+=individualFollowAsHtml(item) profileStr+=individualFollowAsHtml(session,wfRequest,personCache,domain,item)
return profileStr 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 """Show the profile page as html
""" """
nickname=profileJson['name'] nickname=profileJson['name']
@ -303,24 +317,40 @@ def htmlProfile(baseDir: str,httpPrefix: str,authorized: bool,ocapAlways: bool,p
'}' '}'
if selected=='posts': 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': 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() profileStr=htmlHeader(profileStyle)+profileStr+htmlFooter()
return profileStr return profileStr
def individualFollowAsHtml(followUrl: str) -> str: def individualFollowAsHtml(session,wfRequest: {}, \
personCache: {},domain: str, \
followUrl: str) -> str:
nickname=getNicknameFromActor(followUrl) nickname=getNicknameFromActor(followUrl)
domain,port=getDomainFromActor(followUrl) domain,port=getDomainFromActor(followUrl)
titleStr='@'+nickname+'@'+domain 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 \ return \
'<div class="container">\n' \ '<div class="container">\n' \
'<a href="'+followUrl+'">' \ '<a href="'+followUrl+'">' \
'<img src="'+followUrl+'/avatar.png" alt="Avatar">\n'+ \ '<img src="'+avatarUrl+'" alt="Avatar">\n'+ \
'<p>'+titleStr+'</p></a>'+ \ '<p>'+titleStr+'</p></a>'+ \
'</div>\n' '</div>\n'
def individualPostAsHtml(postJsonObject: {}) -> str: def individualPostAsHtml(session,wfRequest: {},personCache: {}, \
domain: str,postJsonObject: {}) -> str:
avatarPosition='' avatarPosition=''
containerClass='container' containerClass='container'
timeClass='time-right' timeClass='time-right'
@ -361,18 +391,26 @@ def individualPostAsHtml(postJsonObject: {}) -> str:
'<a href="'+attach['url']+'">' \ '<a href="'+attach['url']+'">' \
'<img src="'+attach['url']+'" alt="'+imageDescription+'" title="'+imageDescription+'" class="attachment"></a>\n' '<img src="'+attach['url']+'" alt="'+imageDescription+'" title="'+imageDescription+'" class="attachment"></a>\n'
attachmentCtr+=1 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 \ return \
'<div class="'+containerClass+'">\n' \ '<div class="'+containerClass+'">\n' \
'<a href="'+postJsonObject['actor']+'">' \ '<a href="'+postJsonObject['actor']+'">' \
'<img src="'+postJsonObject['actor']+'/avatar.png" alt="Avatar"'+avatarPosition+'></a>\n'+ \ '<img src="'+avatarUrl+'" alt="Avatar"'+avatarPosition+'></a>\n'+ \
'<p class="post-title">'+titleStr+'</p>'+ \ '<p class="post-title">'+titleStr+'</p>'+ \
postJsonObject['object']['content']+'\n'+ \ postJsonObject['object']['content']+'\n'+ \
attachmentStr+ \ attachmentStr+ \
'<span class="'+timeClass+'">'+postJsonObject['object']['published']+'</span>\n'+ \ '<span class="'+timeClass+'">'+postJsonObject['object']['published']+'</span>\n'+ \
'</div>\n' '</div>\n'
def htmlTimeline(timelineJson: {}) -> str: def htmlTimeline(session,wfRequest: {},personCache: {}, \
domain: str,timelineJson: {}) -> str:
"""Show the timeline as html """Show the timeline as html
""" """
if not timelineJson.get('orderedItems'): if not timelineJson.get('orderedItems'):
@ -380,7 +418,8 @@ def htmlTimeline(timelineJson: {}) -> str:
tlStr=htmlHeader() tlStr=htmlHeader()
for item in timelineJson['orderedItems']: for item in timelineJson['orderedItems']:
if item['type']=='Create': if item['type']=='Create':
tlStr+=individualPostAsHtml(item) tlStr+=individualPostAsHtml(session,wfRequest,personCache, \
domain,item)
tlStr+=htmlFooter() tlStr+=htmlFooter()
return tlStr return tlStr
@ -394,11 +433,13 @@ def htmlOutbox(outboxJson: {}) -> str:
""" """
return htmlTimeline(outboxJson) return htmlTimeline(outboxJson)
def htmlIndividualPost(postJsonObject: {}) -> str: def htmlIndividualPost(session,wfRequest: {},personCache: {}, \
domain: str,postJsonObject: {}) -> str:
"""Show an individual post as html """Show an individual post as html
""" """
return htmlHeader()+ \ return htmlHeader()+ \
individualPostAsHtml(postJsonObject)+ \ individualPostAsHtml(session,wfRequest,personCache, \
domain,postJsonObject)+ \
htmlFooter() htmlFooter()
def htmlPostReplies(postJsonObject: {}) -> str: def htmlPostReplies(postJsonObject: {}) -> str: