epicyon/webinterface.py

408 lines
14 KiB
Python
Raw Normal View History

2019-07-20 21:13:36 +00:00
__filename__ = "webinterface.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "0.0.1"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import json
from pprint import pprint
2019-07-21 22:38:44 +00:00
from person import personBoxJson
2019-07-21 12:41:31 +00:00
from utils import getNicknameFromActor
from utils import getDomainFromActor
2019-07-20 21:13:36 +00:00
2019-07-21 18:18:58 +00:00
def htmlHeader(css=None,lang='en') -> str:
if not css:
htmlStr= \
'<!DOCTYPE html>\n' \
'<html lang="'+lang+'">\n' \
' <meta charset="utf-8">\n' \
' <style>\n' \
' @import url("epicyon.css");\n'+ \
' </style>\n' \
' <body>\n'
else:
htmlStr= \
'<!DOCTYPE html>\n' \
'<html lang="'+lang+'">\n' \
' <meta charset="utf-8">\n' \
' <style>\n'+css+'</style>\n' \
' <body>\n'
2019-07-20 21:13:36 +00:00
return htmlStr
def htmlFooter() -> str:
htmlStr= \
' </body>\n' \
'</html>\n'
return htmlStr
2019-07-22 09:38:02 +00:00
def htmlProfilePosts(baseDir: str,httpPrefix: str,authorized: bool,ocapAlways: bool,nickname: str,domain: str,port: int) -> 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(item)
return profileStr
def htmlProfileFollowing(baseDir: str,httpPrefix: str,authorized: bool,ocapAlways: bool,nickname: str,domain: str,port: int,followingJson: {}) -> str:
"""Shows following on the profile screen
"""
profileStr=''
for item in followingJson['orderedItems']:
profileStr+=individualFollowAsHtml(item)
return profileStr
2019-07-22 09:38:02 +00:00
def htmlProfile(baseDir: str,httpPrefix: str,authorized: bool,ocapAlways: bool,profileJson: {},selected: str,extraJson=None) -> str:
2019-07-20 21:13:36 +00:00
"""Show the profile page as html
"""
2019-07-21 18:18:58 +00:00
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)
2019-07-21 20:36:58 +00:00
profileDescription=profileJson['publicKey']['summary']
profileDescription='A test description'
2019-07-22 09:38:02 +00:00
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'
2019-07-22 10:01:10 +00:00
actor=profileJson['id']
2019-07-21 18:18:58 +00:00
profileStr= \
' <div class="hero-image">' \
' <div class="hero-text">' \
2019-07-21 20:36:58 +00:00
' <img src="'+profileJson['icon']['url']+'" alt="'+nickname+'@'+domainFull+'">' \
2019-07-21 18:18:58 +00:00
' <h1>'+preferredName+'</h1>' \
' <p><b>@'+nickname+'@'+domainFull+'</b></p>' \
2019-07-21 20:36:58 +00:00
' <p>'+profileDescription+'</p>' \
2019-07-21 18:18:58 +00:00
' </div>' \
2019-07-21 19:37:48 +00:00
'</div>' \
'<div class="container">\n' \
' <center>' \
2019-07-22 10:01:10 +00:00
' <a href="'+actor+'"><button class="'+postsButton+'"><span>Posts </span></button></a>' \
' <a href="'+actor+'/following"><button class="'+followingButton+'"><span>Following </span></button></a>' \
' <a href="'+actor+'/followers"><button class="'+followersButton+'"><span>Followers </span></button></a>' \
' <a href="'+actor+'/roles"><button class="'+rolesButton+'"><span>Roles </span></button></a>' \
' <a href="'+actor+'/skills"><button class="'+skillsButton+'"><span>Skills </span></button></a>' \
' <a href="'+actor+'/shares"><button class="'+sharesButton+'"><span>Shares </span></button></a>' \
2019-07-21 19:37:48 +00:00
' </center>' \
2019-07-21 18:18:58 +00:00
'</div>'
profileStyle= \
'body, html {' \
' height: 100%;' \
' margin: 0;' \
' font-family: Arial, Helvetica, sans-serif;' \
'}' \
'' \
'.hero-image {' \
2019-07-22 10:01:10 +00:00
' background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("'+actor+'/image.png");' \
2019-07-21 18:18:58 +00:00
' height: 50%;' \
' background-position: center;' \
' background-repeat: no-repeat;' \
' background-size: cover;' \
' position: relative;' \
'}' \
'' \
'.hero-text {' \
' text-align: center;' \
' position: absolute;' \
' top: 50%;' \
' left: 50%;' \
' transform: translate(-50%, -50%);' \
' color: white;' \
'}' \
'' \
2019-07-21 20:22:58 +00:00
'.hero-text img {' \
' border-radius: 10%;' \
2019-07-21 20:36:58 +00:00
' width: 50%;' \
2019-07-21 20:22:58 +00:00
'}' \
'' \
2019-07-21 18:18:58 +00:00
'.hero-text button {' \
' border: none;' \
' outline: 0;' \
' display: inline-block;' \
' padding: 10px 25px;' \
' color: black;' \
' background-color: #ddd;' \
' text-align: center;' \
' cursor: pointer;' \
'}' \
'' \
'.hero-text button:hover {' \
' background-color: #555;' \
' color: white;' \
2019-07-21 19:37:48 +00:00
'}' \
'' \
'.button {' \
' border-radius: 4px;' \
' background-color: #999;' \
' border: none;' \
' color: #FFFFFF;' \
' text-align: center;' \
' font-size: 18px;' \
' padding: 10px;' \
2019-07-21 20:40:47 +00:00
' width: 20%;' \
' max-width: 200px;' \
' min-width: 100px;' \
2019-07-21 19:37:48 +00:00
' transition: all 0.5s;' \
' cursor: pointer;' \
' margin: 5px;' \
'}' \
'' \
'.button span {' \
' cursor: pointer;' \
' display: inline-block;' \
' position: relative;' \
' transition: 0.5s;' \
'}' \
'' \
'.button span:after {' \
" content: '\\00bb';" \
' position: absolute;' \
' opacity: 0;' \
' top: 0;' \
' right: -20px;' \
' transition: 0.5s;' \
'}' \
'' \
'.button:hover span {' \
' padding-right: 25px;' \
'}' \
'' \
'.button:hover span:after {' \
' opacity: 1;' \
' right: 0;' \
'}' \
2019-07-22 09:38:02 +00:00
'.buttonselected {' \
' border-radius: 4px;' \
' background-color: #666;' \
' border: none;' \
' color: #FFFFFF;' \
' text-align: center;' \
' font-size: 18px;' \
' padding: 10px;' \
' width: 20%;' \
' max-width: 200px;' \
' min-width: 100px;' \
' transition: all 0.5s;' \
' cursor: pointer;' \
' margin: 5px;' \
'}' \
'' \
'.buttonselected span {' \
' cursor: pointer;' \
' display: inline-block;' \
' position: relative;' \
' transition: 0.5s;' \
'}' \
'' \
'.buttonselected span:after {' \
" content: '\\00bb';" \
' position: absolute;' \
' opacity: 0;' \
' top: 0;' \
' right: -20px;' \
' transition: 0.5s;' \
'}' \
'' \
'.buttonselected:hover span {' \
' padding-right: 25px;' \
'}' \
'' \
'.buttonselected:hover span:after {' \
' opacity: 1;' \
' right: 0;' \
'}' \
2019-07-21 19:37:48 +00:00
'.container {' \
' border: 2px solid #dedede;' \
' background-color: #f1f1f1;' \
' border-radius: 5px;' \
' padding: 10px;' \
' margin: 10px 0;' \
2019-07-21 22:38:44 +00:00
'}' \
'' \
'.container {' \
' border: 2px solid #dedede;' \
' background-color: #f1f1f1;' \
' border-radius: 5px;' \
' padding: 10px;' \
' margin: 10px 0;' \
'}' \
'' \
'.darker {' \
' border-color: #ccc;' \
' background-color: #ddd;' \
'}' \
'' \
'.container::after {' \
' content: "";' \
' clear: both;' \
' display: table;' \
'}' \
'' \
'.container img {' \
' float: left;' \
' max-width: 60px;' \
' width: 100%;' \
' margin-right: 20px;' \
' border-radius: 10%;' \
'}' \
'' \
'.container img.attachment {' \
' max-width: 100%;' \
' margin-left: 25%;' \
' width: 50%;' \
' border-radius: 10%;' \
'}' \
'.container img.right {' \
' float: right;' \
' margin-left: 20px;' \
' margin-right:0;' \
'}' \
'' \
'.time-right {' \
' float: right;' \
' color: #aaa;' \
'}' \
'' \
'.time-left {' \
' float: left;' \
' color: #999;' \
'}' \
'' \
'.post-title {' \
' margin-top: 0px;' \
' color: #999;' \
2019-07-21 19:37:48 +00:00
'}'
2019-07-21 22:38:44 +00:00
2019-07-22 09:38:02 +00:00
if selected=='posts':
profileStr+=htmlProfilePosts(baseDir,httpPrefix,authorized,ocapAlways,nickname,domain,port)
if selected=='following' or selected=='followers':
profileStr+=htmlProfileFollowing(baseDir,httpPrefix,authorized,ocapAlways,nickname,domain,port,extraJson)
2019-07-21 18:18:58 +00:00
profileStr=htmlHeader(profileStyle)+profileStr+htmlFooter()
return profileStr
2019-07-20 21:13:36 +00:00
def individualFollowAsHtml(followUrl: str) -> str:
nickname=getNicknameFromActor(followUrl)
domain,port=getDomainFromActor(followUrl)
titleStr='@'+nickname+'@'+domain
return \
'<div class="container">\n' \
'<a href="'+followUrl+'">' \
'<img src="'+followUrl+'/avatar.png" alt="Avatar">\n'+ \
'<p>'+titleStr+'</p></a>'+ \
'</div>\n'
2019-07-21 09:09:28 +00:00
def individualPostAsHtml(postJsonObject: {}) -> str:
2019-07-21 11:20:49 +00:00
avatarPosition=''
containerClass='container'
timeClass='time-right'
2019-07-21 13:03:57 +00:00
nickname=getNicknameFromActor(postJsonObject['actor'])
domain,port=getDomainFromActor(postJsonObject['actor'])
titleStr='@'+nickname+'@'+domain
2019-07-21 11:20:49 +00:00
if postJsonObject['object']['inReplyTo']:
containerClass='container darker'
avatarPosition=' class="right"'
timeClass='time-left'
2019-07-21 13:03:57 +00:00
if '/statuses/' in postJsonObject['object']['inReplyTo']:
replyNickname=getNicknameFromActor(postJsonObject['object']['inReplyTo'])
replyDomain,replyPort=getDomainFromActor(postJsonObject['object']['inReplyTo'])
2019-07-21 13:05:07 +00:00
if replyNickname and replyDomain:
titleStr+=' <i>replying to</i> <a href="'+postJsonObject['object']['inReplyTo']+'">@'+replyNickname+'@'+replyDomain+'</a>'
2019-07-21 13:03:57 +00:00
else:
titleStr+=' <i>replying to</i> '+postJsonObject['object']['inReplyTo']
2019-07-21 11:20:49 +00:00
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+='<br>'
attachmentStr+= \
2019-07-21 12:24:38 +00:00
'<a href="'+attach['url']+'">' \
'<img src="'+attach['url']+'" alt="'+imageDescription+'" title="'+imageDescription+'" class="attachment"></a>\n'
2019-07-21 11:20:49 +00:00
attachmentCtr+=1
2019-07-21 13:03:57 +00:00
2019-07-21 09:09:28 +00:00
return \
2019-07-21 11:20:49 +00:00
'<div class="'+containerClass+'">\n' \
2019-07-21 12:11:28 +00:00
'<a href="'+postJsonObject['actor']+'">' \
'<img src="'+postJsonObject['actor']+'/avatar.png" alt="Avatar"'+avatarPosition+'></a>\n'+ \
2019-07-21 13:03:57 +00:00
'<p class="post-title">'+titleStr+'</p>'+ \
2019-07-21 09:09:28 +00:00
postJsonObject['object']['content']+'\n'+ \
2019-07-21 11:20:49 +00:00
attachmentStr+ \
'<span class="'+timeClass+'">'+postJsonObject['object']['published']+'</span>\n'+ \
2019-07-21 11:52:13 +00:00
'</div>\n'
2019-07-21 09:09:28 +00:00
def htmlTimeline(timelineJson: {}) -> str:
"""Show the timeline as html
"""
if not timelineJson.get('orderedItems'):
return ""
tlStr=htmlHeader()
for item in timelineJson['orderedItems']:
if item['type']=='Create':
tlStr+=individualPostAsHtml(item)
tlStr+=htmlFooter()
return tlStr
2019-07-20 21:13:36 +00:00
def htmlInbox(inboxJson: {}) -> str:
"""Show the inbox as html
"""
2019-07-21 09:09:28 +00:00
return htmlTimeline(inboxJson)
2019-07-20 21:13:36 +00:00
def htmlOutbox(outboxJson: {}) -> str:
"""Show the Outbox as html
"""
2019-07-21 09:09:28 +00:00
return htmlTimeline(outboxJson)
2019-07-20 21:13:36 +00:00
def htmlIndividualPost(postJsonObject: {}) -> str:
"""Show an individual post as html
"""
2019-07-21 09:09:28 +00:00
return htmlHeader()+ \
individualPostAsHtml(postJsonObject)+ \
htmlFooter()
2019-07-20 21:13:36 +00:00
def htmlPostReplies(postJsonObject: {}) -> str:
"""Show the replies to an individual post as html
"""
return htmlHeader()+"<h1>Replies</h1>"+htmlFooter()