epicyon/webapp.py

344 lines
13 KiB
Python
Raw Normal View History

2020-11-09 15:22:59 +00:00
__filename__ = "webapp.py"
2020-04-05 09:17:19 +00:00
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-07-24 22:38:42 +00:00
import os
from shutil import copyfile
2019-07-21 12:41:31 +00:00
from utils import getNicknameFromActor
from utils import getDomainFromActor
from utils import locatePost
2019-10-22 11:55:06 +00:00
from utils import loadJson
2019-11-03 09:48:01 +00:00
from shares import getValidSharedItemID
2020-11-09 15:22:59 +00:00
from webapp_utils import getAltPath
from webapp_utils import getIconsDir
2020-11-12 17:05:38 +00:00
from webapp_utils import htmlHeaderWithExternalStyle
2020-11-09 19:41:01 +00:00
from webapp_utils import htmlFooter
from webapp_post import individualPostAsHtml
2019-08-18 13:30:40 +00:00
2020-04-05 09:17:19 +00:00
2020-10-29 12:48:58 +00:00
def htmlFollowingList(cssCache: {}, baseDir: str,
followingFilename: str) -> str:
2020-06-28 21:54:49 +00:00
"""Returns a list of handles being followed
"""
with open(followingFilename, 'r') as followingFile:
msg = followingFile.read()
followingList = msg.split('\n')
followingList.sort()
if followingList:
cssFilename = baseDir + '/epicyon-profile.css'
if os.path.isfile(baseDir + '/epicyon.css'):
cssFilename = baseDir + '/epicyon.css'
2020-10-29 12:48:58 +00:00
2020-11-12 17:05:38 +00:00
followingListHtml = htmlHeaderWithExternalStyle(cssFilename)
for followingAddress in followingList:
if followingAddress:
followingListHtml += \
'<h3>@' + followingAddress + '</h3>'
followingListHtml += htmlFooter()
msg = followingListHtml
2020-06-28 21:54:49 +00:00
return msg
return ''
2020-10-29 12:48:58 +00:00
def htmlHashtagBlocked(cssCache: {}, baseDir: str, translate: {}) -> str:
2019-08-14 10:32:15 +00:00
"""Show the screen for a blocked hashtag
"""
2020-04-05 09:17:19 +00:00
blockedHashtagForm = ''
cssFilename = baseDir + '/epicyon-suspended.css'
if os.path.isfile(baseDir + '/suspended.css'):
cssFilename = baseDir + '/suspended.css'
2020-10-29 12:48:58 +00:00
2020-11-12 17:05:38 +00:00
blockedHashtagForm = htmlHeaderWithExternalStyle(cssFilename)
blockedHashtagForm += '<div><center>\n'
blockedHashtagForm += \
' <p class="screentitle">' + \
translate['Hashtag Blocked'] + '</p>\n'
blockedHashtagForm += \
' <p>See <a href="/terms">' + \
translate['Terms of Service'] + '</a></p>\n'
blockedHashtagForm += '</center></div>\n'
blockedHashtagForm += htmlFooter()
2019-08-14 10:32:15 +00:00
return blockedHashtagForm
2020-03-22 21:16:02 +00:00
2020-04-05 09:17:19 +00:00
2020-10-29 12:48:58 +00:00
def htmlRemoveSharedItem(cssCache: {}, translate: {}, baseDir: str,
2020-07-11 20:31:25 +00:00
actor: str, shareName: str,
callingDomain: str) -> str:
"""Shows a screen asking to confirm the removal of a shared item
"""
2020-04-05 09:17:19 +00:00
itemID = getValidSharedItemID(shareName)
nickname = getNicknameFromActor(actor)
domain, port = getDomainFromActor(actor)
2020-07-11 20:31:25 +00:00
domainFull = domain
if port:
if port != 80 and port != 443:
domainFull = domain + ':' + str(port)
2020-04-05 09:17:19 +00:00
sharesFile = baseDir + '/accounts/' + \
nickname + '@' + domain + '/shares.json'
if not os.path.isfile(sharesFile):
2020-04-05 09:17:19 +00:00
print('ERROR: no shares file ' + sharesFile)
return None
2020-04-05 09:17:19 +00:00
sharesJson = loadJson(sharesFile)
if not sharesJson:
2019-11-03 09:36:04 +00:00
print('ERROR: unable to load shares.json')
return None
2019-11-03 09:48:01 +00:00
if not sharesJson.get(itemID):
2020-04-05 09:17:19 +00:00
print('ERROR: share named "' + itemID + '" is not in ' + sharesFile)
return None
2020-04-05 09:17:19 +00:00
sharedItemDisplayName = sharesJson[itemID]['displayName']
sharedItemImageUrl = None
2019-11-03 09:48:01 +00:00
if sharesJson[itemID].get('imageUrl'):
2020-04-05 09:17:19 +00:00
sharedItemImageUrl = sharesJson[itemID]['imageUrl']
2020-04-05 09:17:19 +00:00
if os.path.isfile(baseDir + '/img/shares-background.png'):
if not os.path.isfile(baseDir + '/accounts/shares-background.png'):
copyfile(baseDir + '/img/shares-background.png',
baseDir + '/accounts/shares-background.png')
2020-04-05 09:17:19 +00:00
cssFilename = baseDir + '/epicyon-follow.css'
if os.path.isfile(baseDir + '/follow.css'):
cssFilename = baseDir + '/follow.css'
2020-10-29 12:48:58 +00:00
2020-11-12 17:05:38 +00:00
sharesStr = htmlHeaderWithExternalStyle(cssFilename)
2020-07-28 10:31:36 +00:00
sharesStr += '<div class="follow">\n'
sharesStr += ' <div class="followAvatar">\n'
sharesStr += ' <center>\n'
if sharedItemImageUrl:
2020-04-05 09:17:19 +00:00
sharesStr += ' <img loading="lazy" src="' + \
2020-07-28 10:31:36 +00:00
sharedItemImageUrl + '"/>\n'
2020-04-05 09:17:19 +00:00
sharesStr += \
' <p class="followText">' + translate['Remove'] + \
2020-07-28 10:31:36 +00:00
' ' + sharedItemDisplayName + ' ?</p>\n'
2020-07-11 20:44:20 +00:00
postActor = getAltPath(actor, domainFull, callingDomain)
2020-07-28 10:31:36 +00:00
sharesStr += ' <form method="POST" action="' + postActor + '/rmshare">\n'
sharesStr += \
' <input type="hidden" name="actor" value="' + actor + '">\n'
2020-04-05 09:17:19 +00:00
sharesStr += ' <input type="hidden" name="shareName" value="' + \
2020-07-28 10:31:36 +00:00
shareName + '">\n'
2020-04-05 09:17:19 +00:00
sharesStr += \
' <button type="submit" class="button" name="submitYes">' + \
2020-07-28 10:31:36 +00:00
translate['Yes'] + '</button>\n'
2020-04-05 09:17:19 +00:00
sharesStr += \
' <a href="' + actor + '/inbox' + '"><button class="button">' + \
2020-07-28 10:31:36 +00:00
translate['No'] + '</button></a>\n'
sharesStr += ' </form>\n'
sharesStr += ' </center>\n'
sharesStr += ' </div>\n'
sharesStr += '</div>\n'
2020-04-05 09:17:19 +00:00
sharesStr += htmlFooter()
return sharesStr
2019-08-27 12:47:11 +00:00
2020-04-05 09:17:19 +00:00
2020-10-29 12:48:58 +00:00
def htmlDeletePost(cssCache: {},
recentPostsCache: {}, maxRecentPosts: int,
2020-04-05 09:17:19 +00:00
translate, pageNumber: int,
session, baseDir: str, messageId: str,
httpPrefix: str, projectVersion: str,
wfRequest: {}, personCache: {},
callingDomain: str,
YTReplacementDomain: str,
showPublishedDateOnly: bool) -> str:
2019-08-27 12:47:11 +00:00
"""Shows a screen asking to confirm the deletion of a post
"""
if '/statuses/' not in messageId:
return None
2020-04-05 09:17:19 +00:00
iconsDir = getIconsDir(baseDir)
actor = messageId.split('/statuses/')[0]
nickname = getNicknameFromActor(actor)
domain, port = getDomainFromActor(actor)
domainFull = domain
if port:
if port != 80 and port != 443:
domainFull = domain + ':' + str(port)
2019-08-27 12:47:11 +00:00
2020-04-05 09:17:19 +00:00
postFilename = locatePost(baseDir, nickname, domain, messageId)
2019-08-27 12:47:11 +00:00
if not postFilename:
return None
2020-04-05 09:17:19 +00:00
postJsonObject = loadJson(postFilename)
if not postJsonObject:
return None
2019-08-27 12:47:11 +00:00
2020-04-05 09:17:19 +00:00
if os.path.isfile(baseDir + '/img/delete-background.png'):
if not os.path.isfile(baseDir + '/accounts/delete-background.png'):
copyfile(baseDir + '/img/delete-background.png',
baseDir + '/accounts/delete-background.png')
2019-08-27 12:47:11 +00:00
2020-04-05 09:17:19 +00:00
deletePostStr = None
cssFilename = baseDir + '/epicyon-profile.css'
if os.path.isfile(baseDir + '/epicyon.css'):
cssFilename = baseDir + '/epicyon.css'
2020-10-29 12:48:58 +00:00
2020-11-12 17:05:38 +00:00
deletePostStr = htmlHeaderWithExternalStyle(cssFilename)
deletePostStr += \
individualPostAsHtml(True, recentPostsCache, maxRecentPosts,
iconsDir, translate, pageNumber,
baseDir, session, wfRequest, personCache,
nickname, domain, port, postJsonObject,
None, True, False,
httpPrefix, projectVersion, 'outbox',
YTReplacementDomain,
showPublishedDateOnly,
False, False, False, False, False)
deletePostStr += '<center>'
deletePostStr += \
' <p class="followText">' + \
translate['Delete this post?'] + '</p>'
postActor = getAltPath(actor, domainFull, callingDomain)
deletePostStr += \
' <form method="POST" action="' + postActor + '/rmpost">\n'
deletePostStr += \
' <input type="hidden" name="pageNumber" value="' + \
str(pageNumber) + '">\n'
deletePostStr += \
' <input type="hidden" name="messageId" value="' + \
messageId + '">\n'
deletePostStr += \
' <button type="submit" class="button" name="submitYes">' + \
translate['Yes'] + '</button>\n'
deletePostStr += \
' <a href="' + actor + '/inbox"><button class="button">' + \
translate['No'] + '</button></a>\n'
deletePostStr += ' </form>\n'
deletePostStr += '</center>\n'
deletePostStr += htmlFooter()
2019-08-27 12:47:11 +00:00
return deletePostStr
2020-04-05 09:17:19 +00:00
2020-10-29 12:48:58 +00:00
def htmlFollowConfirm(cssCache: {}, translate: {}, baseDir: str,
2020-04-05 09:17:19 +00:00
originPathStr: str,
followActor: str,
2019-09-07 08:57:52 +00:00
followProfileUrl: str) -> str:
2019-07-29 09:49:46 +00:00
"""Asks to confirm a follow
"""
2020-04-05 09:17:19 +00:00
followDomain, port = getDomainFromActor(followActor)
2020-03-22 21:16:02 +00:00
2020-07-25 19:07:06 +00:00
if os.path.isfile(baseDir + '/accounts/follow-background-custom.jpg'):
if not os.path.isfile(baseDir + '/accounts/follow-background.jpg'):
copyfile(baseDir + '/accounts/follow-background-custom.jpg',
baseDir + '/accounts/follow-background.jpg')
2019-07-29 09:49:46 +00:00
2020-04-05 09:17:19 +00:00
cssFilename = baseDir + '/epicyon-follow.css'
if os.path.isfile(baseDir + '/follow.css'):
cssFilename = baseDir + '/follow.css'
2020-10-29 12:48:58 +00:00
2020-11-12 17:05:38 +00:00
followStr = htmlHeaderWithExternalStyle(cssFilename)
2020-07-28 10:31:36 +00:00
followStr += '<div class="follow">\n'
followStr += ' <div class="followAvatar">\n'
followStr += ' <center>\n'
followStr += ' <a href="' + followActor + '">\n'
followStr += ' <img loading="lazy" src="' + followProfileUrl + '"/></a>\n'
2020-04-05 09:17:19 +00:00
followStr += \
' <p class="followText">' + translate['Follow'] + ' ' + \
2020-07-28 10:31:36 +00:00
getNicknameFromActor(followActor) + '@' + followDomain + ' ?</p>\n'
2020-04-05 09:17:19 +00:00
followStr += ' <form method="POST" action="' + \
2020-07-28 10:31:36 +00:00
originPathStr + '/followconfirm">\n'
2020-04-05 09:17:19 +00:00
followStr += ' <input type="hidden" name="actor" value="' + \
2020-07-28 10:31:36 +00:00
followActor + '">\n'
2020-04-05 09:17:19 +00:00
followStr += \
' <button type="submit" class="button" name="submitYes">' + \
2020-07-28 10:31:36 +00:00
translate['Yes'] + '</button>\n'
2020-04-05 09:17:19 +00:00
followStr += \
' <a href="' + originPathStr + '"><button class="button">' + \
2020-07-28 10:31:36 +00:00
translate['No'] + '</button></a>\n'
followStr += ' </form>\n'
followStr += '</center>\n'
followStr += '</div>\n'
followStr += '</div>\n'
2020-04-05 09:17:19 +00:00
followStr += htmlFooter()
2019-07-29 09:49:46 +00:00
return followStr
2019-07-29 20:36:26 +00:00
2020-04-05 09:17:19 +00:00
2020-10-29 12:48:58 +00:00
def htmlUnfollowConfirm(cssCache: {}, translate: {}, baseDir: str,
2020-04-05 09:17:19 +00:00
originPathStr: str,
followActor: str,
2019-09-07 08:57:52 +00:00
followProfileUrl: str) -> str:
2019-07-29 20:36:26 +00:00
"""Asks to confirm unfollowing an actor
"""
2020-04-05 09:17:19 +00:00
followDomain, port = getDomainFromActor(followActor)
2020-03-22 21:16:02 +00:00
2020-07-25 19:07:06 +00:00
if os.path.isfile(baseDir + '/accounts/follow-background-custom.jpg'):
if not os.path.isfile(baseDir + '/accounts/follow-background.jpg'):
copyfile(baseDir + '/accounts/follow-background-custom.jpg',
baseDir + '/accounts/follow-background.jpg')
2019-07-29 20:36:26 +00:00
2020-04-05 09:17:19 +00:00
cssFilename = baseDir + '/epicyon-follow.css'
if os.path.isfile(baseDir + '/follow.css'):
cssFilename = baseDir + '/follow.css'
2020-10-29 12:48:58 +00:00
2020-11-12 17:05:38 +00:00
followStr = htmlHeaderWithExternalStyle(cssFilename)
2020-07-28 10:31:36 +00:00
followStr += '<div class="follow">\n'
followStr += ' <div class="followAvatar">\n'
followStr += ' <center>\n'
followStr += ' <a href="' + followActor + '">\n'
followStr += ' <img loading="lazy" src="' + followProfileUrl + '"/></a>\n'
2020-04-05 09:17:19 +00:00
followStr += \
' <p class="followText">' + translate['Stop following'] + \
2020-07-28 10:31:36 +00:00
' ' + getNicknameFromActor(followActor) + \
'@' + followDomain + ' ?</p>\n'
2020-04-05 09:17:19 +00:00
followStr += ' <form method="POST" action="' + \
2020-07-28 10:31:36 +00:00
originPathStr + '/unfollowconfirm">\n'
2020-04-05 09:17:19 +00:00
followStr += ' <input type="hidden" name="actor" value="' + \
2020-07-28 10:31:36 +00:00
followActor + '">\n'
2020-04-05 09:17:19 +00:00
followStr += \
' <button type="submit" class="button" name="submitYes">' + \
2020-07-28 10:31:36 +00:00
translate['Yes'] + '</button>\n'
2020-04-05 09:17:19 +00:00
followStr += \
' <a href="' + originPathStr + '"><button class="button">' + \
2020-07-28 10:31:36 +00:00
translate['No'] + '</button></a>\n'
followStr += ' </form>\n'
followStr += '</center>\n'
followStr += '</div>\n'
followStr += '</div>\n'
2020-04-05 09:17:19 +00:00
followStr += htmlFooter()
2019-07-29 20:36:26 +00:00
return followStr
2019-07-30 22:34:04 +00:00
2020-04-05 09:17:19 +00:00
2020-10-29 12:48:58 +00:00
def htmlUnblockConfirm(cssCache: {}, translate: {}, baseDir: str,
2020-04-05 09:17:19 +00:00
originPathStr: str,
blockActor: str,
2019-09-07 08:57:52 +00:00
blockProfileUrl: str) -> str:
"""Asks to confirm unblocking an actor
"""
2020-04-05 09:17:19 +00:00
blockDomain, port = getDomainFromActor(blockActor)
2020-03-22 21:16:02 +00:00
2020-04-05 09:17:19 +00:00
if os.path.isfile(baseDir + '/img/block-background.png'):
if not os.path.isfile(baseDir + '/accounts/block-background.png'):
copyfile(baseDir + '/img/block-background.png',
baseDir + '/accounts/block-background.png')
2020-04-05 09:17:19 +00:00
cssFilename = baseDir + '/epicyon-follow.css'
if os.path.isfile(baseDir + '/follow.css'):
cssFilename = baseDir + '/follow.css'
2020-10-29 12:48:58 +00:00
2020-11-12 17:05:38 +00:00
blockStr = htmlHeaderWithExternalStyle(cssFilename)
2020-07-28 10:31:36 +00:00
blockStr += '<div class="block">\n'
blockStr += ' <div class="blockAvatar">\n'
blockStr += ' <center>\n'
blockStr += ' <a href="' + blockActor + '">\n'
blockStr += ' <img loading="lazy" src="' + blockProfileUrl + '"/></a>\n'
2020-04-05 09:17:19 +00:00
blockStr += \
' <p class="blockText">' + translate['Stop blocking'] + ' ' + \
2020-07-28 10:31:36 +00:00
getNicknameFromActor(blockActor) + '@' + blockDomain + ' ?</p>\n'
2020-04-05 09:17:19 +00:00
blockStr += ' <form method="POST" action="' + \
2020-07-28 10:31:36 +00:00
originPathStr + '/unblockconfirm">\n'
2020-04-05 09:17:19 +00:00
blockStr += ' <input type="hidden" name="actor" value="' + \
2020-07-28 10:31:36 +00:00
blockActor + '">\n'
2020-04-05 09:17:19 +00:00
blockStr += \
' <button type="submit" class="button" name="submitYes">' + \
2020-07-28 10:31:36 +00:00
translate['Yes'] + '</button>\n'
2020-04-05 09:17:19 +00:00
blockStr += \
' <a href="' + originPathStr + '"><button class="button">' + \
2020-07-28 10:31:36 +00:00
translate['No'] + '</button></a>\n'
blockStr += ' </form>\n'
blockStr += '</center>\n'
blockStr += '</div>\n'
blockStr += '</div>\n'
2020-04-05 09:17:19 +00:00
blockStr += htmlFooter()
return blockStr