__filename__ = "webinterface.py" __author__ = "Bob Mottram" __license__ = "AGPL3+" __version__ = "0.0.1" __maintainer__ = "Bob Mottram" __email__ = "bob@freedombone.net" __status__ = "Production" import json import time import os from shutil import copyfile from pprint import pprint from person import personBoxJson from utils import getNicknameFromActor from utils import getDomainFromActor from posts import getPersonBox def htmlGetLoginCredentials(loginParams: str,lastLoginTime: int) -> (str,str): """Receives login credentials via HTTPServer POST """ if not loginParams.startswith('username='): return None,None # minimum time between login attempts currTime=int(time.time()) if currTime str: if not os.path.isfile(baseDir+'/accounts/login.png'): copyfile(baseDir+'/img/login.png',baseDir+'/accounts/login.png') if os.path.isfile(baseDir+'/img/login-background.png'): if not os.path.isfile(baseDir+'/accounts/login-background.png'): copyfile(baseDir+'/img/login-background.png',baseDir+'/accounts/login-background.png') loginText='

Welcome. Please enter your login details below.

' if os.path.isfile(baseDir+'/accounts/login.txt'): with open(baseDir+'/accounts/login.txt', 'r') as file: loginText = '

'+file.read()+'

' with open(baseDir+'/epicyon-login.css', 'r') as cssFile: loginCSS = cssFile.read() loginForm=htmlHeader(loginCSS) loginForm+= \ '
' \ '
' \ ' login image'+ \ loginText+ \ '
' \ '' \ '
' \ ' ' \ ' ' \ '' \ ' ' \ ' ' \ '' \ ' ' \ '
' \ '
' loginForm+=htmlFooter() return loginForm def htmlNewPost(baseDir: str,path: str) -> str: newPostText='

Enter your post text below.

' if os.path.isfile(baseDir+'/accounts/newpost.txt'): with open(baseDir+'/accounts/newpost.txt', 'r') as file: newPostText = '

'+file.read()+'

' with open(baseDir+'/epicyon-profile.css', 'r') as cssFile: newPostCSS = cssFile.read() pathBase=path.replace('/newpost','').replace('/newshare','').replace('/newunlisted','').replace('/newfollowers','').replace('/newdm','') scopeIcon='scope_public.png' scopeDescription='Public' placeholderSubject='Subject or Content Warning (optional)...' placeholderMessage='Write something...' extraFields='' endpoint='newpost' if path.endswith('/newunlisted'): scopeIcon='scope_unlisted.png' scopeDescription='Unlisted' endpoint='newunlisted' if path.endswith('/newfollowers'): scopeIcon='scope_followers.png' scopeDescription='Followers Only' endpoint='newfollowers' if path.endswith('/newdm'): scopeIcon='scope_dm.png' scopeDescription='Direct Message' endpoint='newdm' if path.endswith('/newshare'): scopeIcon='scope_share.png' scopeDescription='Shared Item' placeholderSubject='Name of the shared item...' placeholderMessage='Description of the item being shared...' endpoint='newshare' extraFields= \ '
' \ ' ' \ ' ' \ '
' \ '' newPostForm=htmlHeader(newPostCSS) newPostForm+= \ '
' \ '
' \ ' ' \ ' ' \ '' \ ' ' \ ''+extraFields+ \ '
' \ ' ' \ ' ' \ '
' \ ' ' \ '
' \ '
' newPostForm+=htmlFooter() return newPostForm def htmlHeader(css=None,lang='en') -> str: if not css: htmlStr= \ '\n' \ '\n' \ ' \n' \ ' \n' \ ' \n' else: htmlStr= \ '\n' \ '\n' \ ' \n' \ ' \n' \ ' \n' return htmlStr def htmlFooter() -> str: htmlStr= \ ' \n' \ '\n' return htmlStr 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) for item in outboxFeed['orderedItems']: if item['type']=='Create': 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, \ session,wfRequest: {},personCache: {}, \ followingJson: {}) -> str: """Shows following on the profile screen """ profileStr='' for item in followingJson['orderedItems']: profileStr+=individualFollowAsHtml(session,wfRequest,personCache,domain,item) return profileStr def htmlProfileRoles(nickname: str,domain: str,rolesJson: {}) -> str: """Shows roles on the profile screen """ profileStr='' for project,rolesList in rolesJson.items(): profileStr+='

'+project+'

' for role in rolesList: profileStr+='

'+role+'

' profileStr+='
' if len(profileStr)==0: profileStr+='

@'+nickname+'@'+domain+' has no roles assigned

' else: profileStr='
'+profileStr+'
' return profileStr def htmlProfileSkills(nickname: str,domain: str,skillsJson: {}) -> str: """Shows skills on the profile screen """ profileStr='' for skill,level in skillsJson.items(): profileStr+='
'+skill+'

' if len(profileStr)==0: profileStr+='

@'+nickname+'@'+domain+' has no skills assigned

' else: profileStr='
'+profileStr+'
' return profileStr def htmlProfileShares(nickname: str,domain: str,sharesJson: {}) -> str: """Shows shares on the profile screen """ profileStr='' for item in sharesJson['orderedItems']: profileStr+='
' profileStr+='' profileStr+='' profileStr+='Item image' profileStr+='

'+item['summary']+'

' profileStr+='

Type: '+item['itemType']+' ' profileStr+='Category: '+item['category']+' ' profileStr+='Location: '+item['location']+'

' profileStr+='
' if len(profileStr)==0: profileStr+='

@'+nickname+'@'+domain+' is not sharing any items

' else: profileStr='
'+profileStr+'
' return profileStr 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'] if not nickname: return "" preferredName=profileJson['preferredUsername'] domain,port=getDomainFromActor(profileJson['id']) if not domain: return "" domainFull=domain if port: domainFull=domain+':'+str(port) profileDescription=profileJson['publicKey']['summary'] profileDescription='A test description' postsButton='button' followingButton='button' followersButton='button' rolesButton='button' skillsButton='button' sharesButton='button' if selected=='posts': postsButton='buttonselected' elif selected=='following': followingButton='buttonselected' elif selected=='followers': followersButton='buttonselected' elif selected=='roles': rolesButton='buttonselected' elif selected=='skills': skillsButton='buttonselected' elif selected=='shares': sharesButton='buttonselected' actor=profileJson['id'] profileStr= \ '
' \ '
' \ ' '+nickname+'@'+domainFull+'' \ '

'+preferredName+'

' \ '

@'+nickname+'@'+domainFull+'

' \ '

'+profileDescription+'

' \ '
' \ '
' \ '
\n' \ '
' \ ' ' \ ' ' \ ' ' \ ' ' \ ' ' \ ' ' \ '
' \ '
' with open(baseDir+'/epicyon-profile.css', 'r') as cssFile: profileStyle = cssFile.read().replace('image.png',actor+'/image.png') if selected=='posts': 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,session, \ wfRequest,personCache,extraJson) if selected=='roles': profileStr+= \ htmlProfileRoles(nickname,domainFull,extraJson) if selected=='skills': profileStr+= \ htmlProfileSkills(nickname,domainFull,extraJson) if selected=='shares': profileStr+= \ htmlProfileShares(nickname,domainFull,extraJson) profileStr=htmlHeader(profileStyle)+profileStr+htmlFooter() return profileStr 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,preferredName = \ getPersonBox(session,wfRequest,personCache,'outbox') if avatarUrl2: avatarUrl=avatarUrl2 if preferredName: titleStr=preferredName+' '+titleStr return \ '
\n' \ '' \ 'Avatar\n'+ \ '

'+titleStr+'

'+ \ '
\n' def individualPostAsHtml(session,wfRequest: {},personCache: {}, \ domain: str,postJsonObject: {}) -> str: avatarPosition='' containerClass='container' timeClass='time-right' nickname=getNicknameFromActor(postJsonObject['actor']) domain,port=getDomainFromActor(postJsonObject['actor']) titleStr='@'+nickname+'@'+domain if postJsonObject['object']['inReplyTo']: containerClass='container darker' avatarPosition=' class="right"' timeClass='time-left' if '/statuses/' in postJsonObject['object']['inReplyTo']: replyNickname=getNicknameFromActor(postJsonObject['object']['inReplyTo']) replyDomain,replyPort=getDomainFromActor(postJsonObject['object']['inReplyTo']) if replyNickname and replyDomain: titleStr+=' replying to @'+replyNickname+'@'+replyDomain+'' else: titleStr+=' replying to '+postJsonObject['object']['inReplyTo'] attachmentStr='' if postJsonObject['object']['attachment']: if isinstance(postJsonObject['object']['attachment'], list): attachmentCtr=0 for attach in postJsonObject['object']['attachment']: if attach.get('mediaType') and attach.get('url'): mediaType=attach['mediaType'] imageDescription='' if attach.get('name'): imageDescription=attach['name'] if mediaType=='image/png' or \ mediaType=='image/jpeg' or \ mediaType=='image/gif': if attach['url'].endswith('.png') or \ attach['url'].endswith('.jpg') or \ attach['url'].endswith('.jpeg') or \ attach['url'].endswith('.gif'): if attachmentCtr>0: attachmentStr+='
' attachmentStr+= \ '' \ ''+imageDescription+'\n' attachmentCtr+=1 avatarUrl=postJsonObject['actor']+'/avatar.png' if domain not in postJsonObject['actor']: inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl2,preferredName = \ getPersonBox(session,wfRequest,personCache,'outbox') if avatarUrl2: avatarUrl=avatarUrl2 if preferredName: titleStr=preferredName+' '+titleStr return \ '
\n' \ '' \ 'Avatar\n'+ \ '

'+titleStr+'

'+ \ postJsonObject['object']['content']+'\n'+ \ attachmentStr+ \ ''+postJsonObject['object']['published']+'\n'+ \ '
\n' def htmlTimeline(session,baseDir: str,wfRequest: {},personCache: {}, \ nickname: str,domain: str,timelineJson: {},boxName: str) -> str: """Show the timeline as html """ with open(baseDir+'/epicyon-profile.css', 'r') as cssFile: profileStyle = \ cssFile.read().replace('banner.png', \ '/users/'+nickname+'/banner.png') localButton='button' personalButton='button' federatedButton='button' newPostButton='button' if boxName=='inbox': localButton='buttonselected' elif boxName=='outbox': personalButton='buttonselected' elif boxName=='federated': federatedButton='buttonselected' actor='/users/'+nickname tlStr=htmlHeader(profileStyle) newPostStr='' if boxName=='inbox': newPostStr=' ' tlStr+= \ '
' \ '
' \ '
\n' \ '
'+newPostStr+ \ ' ' \ ' ' \ ' ' \ '
' \ '
' for item in timelineJson['orderedItems']: if item['type']=='Create': tlStr+=individualPostAsHtml(session,wfRequest,personCache, \ domain,item) tlStr+=htmlFooter() return tlStr def htmlInbox(session,baseDir: str,wfRequest: {},personCache: {}, \ nickname: str,domain: str,inboxJson: {}) -> str: """Show the inbox as html """ return htmlTimeline(session,baseDir,wfRequest,personCache, \ nickname,domain,inboxJson,'inbox') def htmlOutbox(session,baseDir: str,wfRequest: {},personCache: {}, \ nickname: str,domain: str,outboxJson: {}) -> str: """Show the Outbox as html """ return htmlTimeline(session,baseDir,wfRequest,personCache, \ nickname,domain,outboxJson,'outbox') def htmlIndividualPost(session,wfRequest: {},personCache: {}, \ domain: str,postJsonObject: {}) -> str: """Show an individual post as html """ return htmlHeader()+ \ individualPostAsHtml(session,wfRequest,personCache, \ domain,postJsonObject)+ \ htmlFooter() def htmlPostReplies(postJsonObject: {}) -> str: """Show the replies to an individual post as html """ return htmlHeader()+"

Replies

"+htmlFooter()