2020-11-09 15:22:59 +00:00
|
|
|
__filename__ = "webapp_utils.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2021-01-26 10:07:42 +00:00
|
|
|
__version__ = "1.2.0"
|
2020-11-09 15:22:59 +00:00
|
|
|
__maintainer__ = "Bob Mottram"
|
|
|
|
__email__ = "bob@freedombone.net"
|
|
|
|
__status__ = "Production"
|
2021-06-15 15:08:12 +00:00
|
|
|
__module_group__ = "Web Interface"
|
2020-11-09 15:22:59 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
from collections import OrderedDict
|
2020-11-09 15:25:18 +00:00
|
|
|
from session import getJson
|
2020-12-12 15:31:28 +00:00
|
|
|
from utils import removeHtml
|
2020-11-21 11:21:05 +00:00
|
|
|
from utils import getImageExtensions
|
2020-11-09 15:22:59 +00:00
|
|
|
from utils import getProtocolPrefixes
|
|
|
|
from utils import loadJson
|
2020-11-09 19:41:01 +00:00
|
|
|
from utils import getCachedPostFilename
|
2020-11-09 15:22:59 +00:00
|
|
|
from utils import getConfigParam
|
2021-07-13 21:59:53 +00:00
|
|
|
from utils import acctDir
|
2020-11-09 15:22:59 +00:00
|
|
|
from cache import storePersonInCache
|
2020-11-09 19:41:01 +00:00
|
|
|
from content import addHtmlTags
|
|
|
|
from content import replaceEmojiFromTags
|
2021-06-25 14:33:16 +00:00
|
|
|
from person import getPersonAvatarUrl
|
2020-11-09 15:22:59 +00:00
|
|
|
|
|
|
|
|
2020-12-12 10:38:48 +00:00
|
|
|
def getBrokenLinkSubstitute() -> str:
|
|
|
|
"""Returns html used to show a default image if the link to
|
|
|
|
an image is broken
|
|
|
|
"""
|
|
|
|
return " onerror=\"this.onerror=null; this.src='" + \
|
|
|
|
"/icons/avatar_default.png'\""
|
|
|
|
|
|
|
|
|
2020-11-28 10:49:10 +00:00
|
|
|
def htmlFollowingList(cssCache: {}, baseDir: str,
|
|
|
|
followingFilename: str) -> str:
|
|
|
|
"""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'
|
|
|
|
|
2021-01-11 19:46:21 +00:00
|
|
|
instanceTitle = \
|
|
|
|
getConfigParam(baseDir, 'instanceTitle')
|
|
|
|
followingListHtml = htmlHeaderWithExternalStyle(cssFilename,
|
|
|
|
instanceTitle)
|
2020-11-28 10:49:10 +00:00
|
|
|
for followingAddress in followingList:
|
|
|
|
if followingAddress:
|
|
|
|
followingListHtml += \
|
|
|
|
'<h3>@' + followingAddress + '</h3>'
|
|
|
|
followingListHtml += htmlFooter()
|
|
|
|
msg = followingListHtml
|
|
|
|
return msg
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
|
|
def htmlHashtagBlocked(cssCache: {}, baseDir: str, translate: {}) -> str:
|
|
|
|
"""Show the screen for a blocked hashtag
|
|
|
|
"""
|
|
|
|
blockedHashtagForm = ''
|
|
|
|
cssFilename = baseDir + '/epicyon-suspended.css'
|
|
|
|
if os.path.isfile(baseDir + '/suspended.css'):
|
|
|
|
cssFilename = baseDir + '/suspended.css'
|
|
|
|
|
2021-01-11 19:46:21 +00:00
|
|
|
instanceTitle = \
|
|
|
|
getConfigParam(baseDir, 'instanceTitle')
|
|
|
|
blockedHashtagForm = htmlHeaderWithExternalStyle(cssFilename,
|
|
|
|
instanceTitle)
|
2020-11-28 10:49:10 +00:00
|
|
|
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()
|
|
|
|
return blockedHashtagForm
|
|
|
|
|
|
|
|
|
2020-11-28 10:19:59 +00:00
|
|
|
def headerButtonsFrontScreen(translate: {},
|
|
|
|
nickname: str, boxName: str,
|
|
|
|
authorized: bool,
|
2020-12-09 13:31:54 +00:00
|
|
|
iconsAsButtons: bool) -> str:
|
2020-11-28 10:19:59 +00:00
|
|
|
"""Returns the header buttons for the front page of a news instance
|
|
|
|
"""
|
|
|
|
headerStr = ''
|
|
|
|
if nickname == 'news':
|
|
|
|
buttonFeatures = 'buttonMobile'
|
|
|
|
buttonNewswire = 'buttonMobile'
|
|
|
|
buttonLinks = 'buttonMobile'
|
|
|
|
if boxName == 'features':
|
|
|
|
buttonFeatures = 'buttonselected'
|
|
|
|
elif boxName == 'newswire':
|
|
|
|
buttonNewswire = 'buttonselected'
|
|
|
|
elif boxName == 'links':
|
|
|
|
buttonLinks = 'buttonselected'
|
|
|
|
|
|
|
|
headerStr += \
|
|
|
|
' <a href="/">' + \
|
|
|
|
'<button class="' + buttonFeatures + '">' + \
|
|
|
|
'<span>' + translate['Features'] + \
|
|
|
|
'</span></button></a>'
|
|
|
|
if not authorized:
|
|
|
|
headerStr += \
|
|
|
|
' <a href="/login">' + \
|
|
|
|
'<button class="buttonMobile">' + \
|
|
|
|
'<span>' + translate['Login'] + \
|
|
|
|
'</span></button></a>'
|
|
|
|
if iconsAsButtons:
|
|
|
|
headerStr += \
|
|
|
|
' <a href="/users/news/newswiremobile">' + \
|
|
|
|
'<button class="' + buttonNewswire + '">' + \
|
|
|
|
'<span>' + translate['Newswire'] + \
|
|
|
|
'</span></button></a>'
|
|
|
|
headerStr += \
|
|
|
|
' <a href="/users/news/linksmobile">' + \
|
|
|
|
'<button class="' + buttonLinks + '">' + \
|
|
|
|
'<span>' + translate['Links'] + \
|
|
|
|
'</span></button></a>'
|
|
|
|
else:
|
|
|
|
headerStr += \
|
|
|
|
' <a href="' + \
|
|
|
|
'/users/news/newswiremobile">' + \
|
2020-12-09 13:08:26 +00:00
|
|
|
'<img loading="lazy" src="/icons' + \
|
2020-11-28 10:19:59 +00:00
|
|
|
'/newswire.png" title="' + translate['Newswire'] + \
|
|
|
|
'" alt="| ' + translate['Newswire'] + '"/></a>\n'
|
|
|
|
headerStr += \
|
|
|
|
' <a href="' + \
|
|
|
|
'/users/news/linksmobile">' + \
|
2020-12-09 13:08:26 +00:00
|
|
|
'<img loading="lazy" src="/icons' + \
|
2020-11-28 10:19:59 +00:00
|
|
|
'/links.png" title="' + translate['Links'] + \
|
|
|
|
'" alt="| ' + translate['Links'] + '"/></a>\n'
|
|
|
|
else:
|
|
|
|
if not authorized:
|
|
|
|
headerStr += \
|
|
|
|
' <a href="/login">' + \
|
|
|
|
'<button class="buttonMobile">' + \
|
|
|
|
'<span>' + translate['Login'] + \
|
|
|
|
'</span></button></a>'
|
|
|
|
|
|
|
|
if headerStr:
|
|
|
|
headerStr = \
|
|
|
|
'\n <div class="frontPageMobileButtons">\n' + \
|
|
|
|
headerStr + \
|
|
|
|
' </div>\n'
|
|
|
|
return headerStr
|
|
|
|
|
|
|
|
|
2020-11-09 15:22:59 +00:00
|
|
|
def getContentWarningButton(postID: str, translate: {},
|
|
|
|
content: str) -> str:
|
|
|
|
"""Returns the markup for a content warning button
|
|
|
|
"""
|
2021-01-19 19:27:32 +00:00
|
|
|
return ' <details><summary class="cw">' + \
|
2021-01-19 19:24:16 +00:00
|
|
|
translate['SHOW MORE'] + '</summary>' + \
|
2020-11-09 15:22:59 +00:00
|
|
|
'<div id="' + postID + '">' + content + \
|
|
|
|
'</div></details>\n'
|
|
|
|
|
|
|
|
|
2020-12-22 18:06:23 +00:00
|
|
|
def _setActorPropertyUrl(actorJson: {}, propertyName: str, url: str) -> None:
|
2020-11-09 15:22:59 +00:00
|
|
|
"""Sets a url for the given actor property
|
|
|
|
"""
|
|
|
|
if not actorJson.get('attachment'):
|
|
|
|
actorJson['attachment'] = []
|
|
|
|
|
|
|
|
propertyNameLower = propertyName.lower()
|
|
|
|
|
|
|
|
# remove any existing value
|
|
|
|
propertyFound = None
|
|
|
|
for propertyValue in actorJson['attachment']:
|
|
|
|
if not propertyValue.get('name'):
|
|
|
|
continue
|
|
|
|
if not propertyValue.get('type'):
|
|
|
|
continue
|
|
|
|
if not propertyValue['name'].lower().startswith(propertyNameLower):
|
|
|
|
continue
|
|
|
|
propertyFound = propertyValue
|
|
|
|
break
|
|
|
|
if propertyFound:
|
|
|
|
actorJson['attachment'].remove(propertyFound)
|
|
|
|
|
|
|
|
prefixes = getProtocolPrefixes()
|
|
|
|
prefixFound = False
|
|
|
|
for prefix in prefixes:
|
|
|
|
if url.startswith(prefix):
|
|
|
|
prefixFound = True
|
|
|
|
break
|
|
|
|
if not prefixFound:
|
|
|
|
return
|
|
|
|
if '.' not in url:
|
|
|
|
return
|
|
|
|
if ' ' in url:
|
|
|
|
return
|
|
|
|
if ',' in url:
|
|
|
|
return
|
|
|
|
|
|
|
|
for propertyValue in actorJson['attachment']:
|
|
|
|
if not propertyValue.get('name'):
|
|
|
|
continue
|
|
|
|
if not propertyValue.get('type'):
|
|
|
|
continue
|
|
|
|
if not propertyValue['name'].lower().startswith(propertyNameLower):
|
|
|
|
continue
|
|
|
|
if propertyValue['type'] != 'PropertyValue':
|
|
|
|
continue
|
|
|
|
propertyValue['value'] = url
|
|
|
|
return
|
|
|
|
|
|
|
|
newAddress = {
|
|
|
|
"name": propertyName,
|
|
|
|
"type": "PropertyValue",
|
|
|
|
"value": url
|
|
|
|
}
|
|
|
|
actorJson['attachment'].append(newAddress)
|
|
|
|
|
|
|
|
|
|
|
|
def setBlogAddress(actorJson: {}, blogAddress: str) -> None:
|
|
|
|
"""Sets an blog address for the given actor
|
|
|
|
"""
|
2020-12-22 18:06:23 +00:00
|
|
|
_setActorPropertyUrl(actorJson, 'Blog', removeHtml(blogAddress))
|
2020-11-09 15:22:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def updateAvatarImageCache(session, baseDir: str, httpPrefix: str,
|
|
|
|
actor: str, avatarUrl: str,
|
|
|
|
personCache: {}, allowDownloads: bool,
|
2021-06-20 11:28:35 +00:00
|
|
|
force: bool = False, debug: bool = False) -> str:
|
2020-11-09 15:22:59 +00:00
|
|
|
"""Updates the cached avatar for the given actor
|
|
|
|
"""
|
|
|
|
if not avatarUrl:
|
|
|
|
return None
|
|
|
|
actorStr = actor.replace('/', '-')
|
|
|
|
avatarImagePath = baseDir + '/cache/avatars/' + actorStr
|
2020-12-12 14:23:14 +00:00
|
|
|
|
|
|
|
# try different image types
|
|
|
|
imageFormats = {
|
|
|
|
'png': 'png',
|
|
|
|
'jpg': 'jpeg',
|
|
|
|
'jpeg': 'jpeg',
|
|
|
|
'gif': 'gif',
|
2021-01-11 22:27:57 +00:00
|
|
|
'svg': 'svg+xml',
|
2020-12-12 14:23:14 +00:00
|
|
|
'webp': 'webp',
|
|
|
|
'avif': 'avif'
|
|
|
|
}
|
|
|
|
avatarImageFilename = None
|
|
|
|
for imFormat, mimeType in imageFormats.items():
|
|
|
|
if avatarUrl.endswith('.' + imFormat) or \
|
|
|
|
'.' + imFormat + '?' in avatarUrl:
|
|
|
|
sessionHeaders = {
|
|
|
|
'Accept': 'image/' + mimeType
|
|
|
|
}
|
|
|
|
avatarImageFilename = avatarImagePath + '.' + imFormat
|
|
|
|
|
|
|
|
if not avatarImageFilename:
|
2020-11-09 15:22:59 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
if (not os.path.isfile(avatarImageFilename) or force) and allowDownloads:
|
|
|
|
try:
|
2021-03-14 21:29:40 +00:00
|
|
|
if debug:
|
|
|
|
print('avatar image url: ' + avatarUrl)
|
2020-11-09 15:22:59 +00:00
|
|
|
result = session.get(avatarUrl,
|
|
|
|
headers=sessionHeaders,
|
|
|
|
params=None)
|
|
|
|
if result.status_code < 200 or \
|
|
|
|
result.status_code > 202:
|
2021-03-14 21:29:40 +00:00
|
|
|
if debug:
|
|
|
|
print('Avatar image download failed with status ' +
|
|
|
|
str(result.status_code))
|
2020-11-09 15:22:59 +00:00
|
|
|
# remove partial download
|
|
|
|
if os.path.isfile(avatarImageFilename):
|
|
|
|
os.remove(avatarImageFilename)
|
|
|
|
else:
|
|
|
|
with open(avatarImageFilename, 'wb') as f:
|
|
|
|
f.write(result.content)
|
2021-03-14 21:29:40 +00:00
|
|
|
if debug:
|
|
|
|
print('avatar image downloaded for ' + actor)
|
2020-11-09 15:22:59 +00:00
|
|
|
return avatarImageFilename.replace(baseDir + '/cache', '')
|
|
|
|
except Exception as e:
|
2021-05-20 12:04:05 +00:00
|
|
|
print('WARN: Failed to download avatar image: ' +
|
|
|
|
str(avatarUrl) + ' ' + str(e))
|
2020-11-09 15:22:59 +00:00
|
|
|
prof = 'https://www.w3.org/ns/activitystreams'
|
|
|
|
if '/channel/' not in actor or '/accounts/' not in actor:
|
|
|
|
sessionHeaders = {
|
|
|
|
'Accept': 'application/activity+json; profile="' + prof + '"'
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
sessionHeaders = {
|
|
|
|
'Accept': 'application/ld+json; profile="' + prof + '"'
|
|
|
|
}
|
|
|
|
personJson = \
|
2021-03-14 20:55:37 +00:00
|
|
|
getJson(session, actor, sessionHeaders, None,
|
|
|
|
debug, __version__, httpPrefix, None)
|
2020-11-09 15:22:59 +00:00
|
|
|
if personJson:
|
|
|
|
if not personJson.get('id'):
|
|
|
|
return None
|
|
|
|
if not personJson.get('publicKey'):
|
|
|
|
return None
|
|
|
|
if not personJson['publicKey'].get('publicKeyPem'):
|
|
|
|
return None
|
|
|
|
if personJson['id'] != actor:
|
|
|
|
return None
|
|
|
|
if not personCache.get(actor):
|
|
|
|
return None
|
|
|
|
if personCache[actor]['actor']['publicKey']['publicKeyPem'] != \
|
|
|
|
personJson['publicKey']['publicKeyPem']:
|
|
|
|
print("ERROR: " +
|
|
|
|
"public keys don't match when downloading actor for " +
|
|
|
|
actor)
|
|
|
|
return None
|
|
|
|
storePersonInCache(baseDir, actor, personJson, personCache,
|
|
|
|
allowDownloads)
|
|
|
|
return getPersonAvatarUrl(baseDir, actor, personCache,
|
|
|
|
allowDownloads)
|
|
|
|
return None
|
|
|
|
return avatarImageFilename.replace(baseDir + '/cache', '')
|
|
|
|
|
|
|
|
|
|
|
|
def scheduledPostsExist(baseDir: str, nickname: str, domain: str) -> bool:
|
|
|
|
"""Returns true if there are posts scheduled to be delivered
|
|
|
|
"""
|
|
|
|
scheduleIndexFilename = \
|
2021-07-13 21:59:53 +00:00
|
|
|
acctDir(baseDir, nickname, domain) + '/schedule.index'
|
2020-11-09 15:22:59 +00:00
|
|
|
if not os.path.isfile(scheduleIndexFilename):
|
|
|
|
return False
|
|
|
|
if '#users#' in open(scheduleIndexFilename).read():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def sharesTimelineJson(actor: str, pageNumber: int, itemsPerPage: int,
|
|
|
|
baseDir: str, maxSharesPerAccount: int) -> ({}, bool):
|
|
|
|
"""Get a page on the shared items timeline as json
|
|
|
|
maxSharesPerAccount helps to avoid one person dominating the timeline
|
|
|
|
by sharing a large number of things
|
|
|
|
"""
|
|
|
|
allSharesJson = {}
|
|
|
|
for subdir, dirs, files in os.walk(baseDir + '/accounts'):
|
|
|
|
for handle in dirs:
|
2020-12-06 14:42:42 +00:00
|
|
|
if '@' not in handle:
|
|
|
|
continue
|
|
|
|
accountDir = baseDir + '/accounts/' + handle
|
|
|
|
sharesFilename = accountDir + '/shares.json'
|
|
|
|
if not os.path.isfile(sharesFilename):
|
|
|
|
continue
|
|
|
|
sharesJson = loadJson(sharesFilename)
|
|
|
|
if not sharesJson:
|
|
|
|
continue
|
|
|
|
nickname = handle.split('@')[0]
|
|
|
|
# actor who owns this share
|
|
|
|
owner = actor.split('/users/')[0] + '/users/' + nickname
|
|
|
|
ctr = 0
|
|
|
|
for itemID, item in sharesJson.items():
|
|
|
|
# assign owner to the item
|
|
|
|
item['actor'] = owner
|
|
|
|
allSharesJson[str(item['published'])] = item
|
|
|
|
ctr += 1
|
|
|
|
if ctr >= maxSharesPerAccount:
|
|
|
|
break
|
2020-12-13 22:13:45 +00:00
|
|
|
break
|
2020-11-09 15:22:59 +00:00
|
|
|
# sort the shared items in descending order of publication date
|
|
|
|
sharesJson = OrderedDict(sorted(allSharesJson.items(), reverse=True))
|
|
|
|
lastPage = False
|
|
|
|
startIndex = itemsPerPage * pageNumber
|
|
|
|
maxIndex = len(sharesJson.items())
|
|
|
|
if maxIndex < itemsPerPage:
|
|
|
|
lastPage = True
|
|
|
|
if startIndex >= maxIndex - itemsPerPage:
|
|
|
|
lastPage = True
|
|
|
|
startIndex = maxIndex - itemsPerPage
|
|
|
|
if startIndex < 0:
|
|
|
|
startIndex = 0
|
|
|
|
ctr = 0
|
|
|
|
resultJson = {}
|
|
|
|
for published, item in sharesJson.items():
|
|
|
|
if ctr >= startIndex + itemsPerPage:
|
|
|
|
break
|
|
|
|
if ctr < startIndex:
|
|
|
|
ctr += 1
|
|
|
|
continue
|
|
|
|
resultJson[published] = item
|
|
|
|
ctr += 1
|
|
|
|
return resultJson, lastPage
|
|
|
|
|
|
|
|
|
|
|
|
def postContainsPublic(postJsonObject: {}) -> bool:
|
|
|
|
"""Does the given post contain #Public
|
|
|
|
"""
|
|
|
|
containsPublic = False
|
|
|
|
if not postJsonObject['object'].get('to'):
|
|
|
|
return containsPublic
|
|
|
|
|
|
|
|
for toAddress in postJsonObject['object']['to']:
|
|
|
|
if toAddress.endswith('#Public'):
|
|
|
|
containsPublic = True
|
|
|
|
break
|
|
|
|
if not containsPublic:
|
|
|
|
if postJsonObject['object'].get('cc'):
|
|
|
|
for toAddress in postJsonObject['object']['cc']:
|
|
|
|
if toAddress.endswith('#Public'):
|
|
|
|
containsPublic = True
|
|
|
|
break
|
|
|
|
return containsPublic
|
|
|
|
|
|
|
|
|
2020-12-22 18:06:23 +00:00
|
|
|
def _getImageFile(baseDir: str, name: str, directory: str,
|
|
|
|
nickname: str, domain: str, theme: str) -> (str, str):
|
2020-11-09 15:22:59 +00:00
|
|
|
"""
|
2020-11-09 15:40:24 +00:00
|
|
|
returns the filenames for an image with the given name
|
|
|
|
"""
|
|
|
|
bannerExtensions = getImageExtensions()
|
|
|
|
bannerFile = ''
|
|
|
|
bannerFilename = ''
|
|
|
|
for ext in bannerExtensions:
|
2020-12-20 17:47:34 +00:00
|
|
|
bannerFileTest = name + '.' + ext
|
|
|
|
bannerFilenameTest = directory + '/' + bannerFileTest
|
|
|
|
if os.path.isfile(bannerFilenameTest):
|
2020-12-20 17:43:55 +00:00
|
|
|
bannerFile = name + '_' + theme + '.' + ext
|
2020-12-20 17:59:09 +00:00
|
|
|
bannerFilename = bannerFilenameTest
|
2020-11-09 15:40:24 +00:00
|
|
|
break
|
|
|
|
return bannerFile, bannerFilename
|
|
|
|
|
|
|
|
|
|
|
|
def getBannerFile(baseDir: str,
|
2020-12-20 17:26:38 +00:00
|
|
|
nickname: str, domain: str, theme: str) -> (str, str):
|
2021-07-13 21:59:53 +00:00
|
|
|
accountDir = acctDir(baseDir, nickname, domain)
|
|
|
|
return _getImageFile(baseDir, 'banner', accountDir,
|
2020-12-22 18:06:23 +00:00
|
|
|
nickname, domain, theme)
|
2020-11-09 15:40:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def getSearchBannerFile(baseDir: str,
|
2020-12-20 17:26:38 +00:00
|
|
|
nickname: str, domain: str, theme: str) -> (str, str):
|
2021-07-13 21:59:53 +00:00
|
|
|
accountDir = acctDir(baseDir, nickname, domain)
|
|
|
|
return _getImageFile(baseDir, 'search_banner', accountDir,
|
2020-12-22 18:06:23 +00:00
|
|
|
nickname, domain, theme)
|
2020-11-09 15:40:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def getLeftImageFile(baseDir: str,
|
2020-12-20 17:26:38 +00:00
|
|
|
nickname: str, domain: str, theme: str) -> (str, str):
|
2021-07-13 21:59:53 +00:00
|
|
|
accountDir = acctDir(baseDir, nickname, domain)
|
|
|
|
return _getImageFile(baseDir, 'left_col_image', accountDir,
|
2020-12-22 18:06:23 +00:00
|
|
|
nickname, domain, theme)
|
2020-11-09 15:40:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def getRightImageFile(baseDir: str,
|
2020-12-20 17:26:38 +00:00
|
|
|
nickname: str, domain: str, theme: str) -> (str, str):
|
2021-07-13 21:59:53 +00:00
|
|
|
accountDir = acctDir(baseDir, nickname, domain)
|
2020-12-22 18:06:23 +00:00
|
|
|
return _getImageFile(baseDir, 'right_col_image',
|
2021-07-13 21:59:53 +00:00
|
|
|
accountDir, nickname, domain, theme)
|
2020-11-09 19:41:01 +00:00
|
|
|
|
|
|
|
|
2021-01-11 19:46:21 +00:00
|
|
|
def htmlHeaderWithExternalStyle(cssFilename: str, instanceTitle: str,
|
|
|
|
lang='en') -> str:
|
2020-11-12 17:18:14 +00:00
|
|
|
cssFile = '/' + cssFilename.split('/')[-1]
|
2021-07-06 12:50:38 +00:00
|
|
|
htmlStr = \
|
|
|
|
'<!DOCTYPE html>\n' + \
|
|
|
|
'<html lang="' + lang + '">\n' + \
|
|
|
|
' <head>\n' + \
|
|
|
|
' <meta charset="utf-8">\n' + \
|
|
|
|
' <link rel="stylesheet" href="' + cssFile + '">\n' + \
|
|
|
|
' <link rel="manifest" href="/manifest.json">\n' + \
|
|
|
|
' <meta name="theme-color" content="grey">\n' + \
|
|
|
|
' <title>' + instanceTitle + '</title>\n' + \
|
|
|
|
' </head>\n' + \
|
|
|
|
' <body>\n'
|
2020-11-09 19:41:01 +00:00
|
|
|
return htmlStr
|
|
|
|
|
|
|
|
|
2021-05-12 15:49:53 +00:00
|
|
|
def htmlHeaderWithPersonMarkup(cssFilename: str, instanceTitle: str,
|
2021-05-16 09:20:24 +00:00
|
|
|
actorJson: {}, city: str,
|
|
|
|
lang='en') -> str:
|
2021-05-12 15:49:53 +00:00
|
|
|
"""html header which includes person markup
|
|
|
|
https://schema.org/Person
|
|
|
|
"""
|
|
|
|
htmlStr = htmlHeaderWithExternalStyle(cssFilename, instanceTitle, lang)
|
|
|
|
if not actorJson:
|
|
|
|
return htmlStr
|
|
|
|
|
2021-05-17 10:46:31 +00:00
|
|
|
cityMarkup = ''
|
|
|
|
if city:
|
|
|
|
city = city.lower().title()
|
|
|
|
addComma = ''
|
|
|
|
countryMarkup = ''
|
|
|
|
if ',' in city:
|
|
|
|
country = city.split(',', 1)[1].strip().title()
|
|
|
|
city = city.split(',', 1)[0]
|
|
|
|
countryMarkup = \
|
|
|
|
' "addressCountry": "' + country + '"\n'
|
|
|
|
addComma = ','
|
|
|
|
cityMarkup = \
|
|
|
|
' "address": {\n' + \
|
|
|
|
' "@type": "PostalAddress",\n' + \
|
|
|
|
' "addressLocality": "' + city + '"' + addComma + '\n' + \
|
|
|
|
countryMarkup + \
|
|
|
|
' },\n'
|
|
|
|
|
2021-05-12 15:49:53 +00:00
|
|
|
skillsMarkup = ''
|
2021-05-13 13:27:35 +00:00
|
|
|
if actorJson.get('hasOccupation'):
|
2021-05-16 16:07:02 +00:00
|
|
|
if isinstance(actorJson['hasOccupation'], list):
|
2021-05-16 16:11:48 +00:00
|
|
|
skillsMarkup = ' "hasOccupation": [\n'
|
2021-05-16 16:07:02 +00:00
|
|
|
firstEntry = True
|
|
|
|
for skillDict in actorJson['hasOccupation']:
|
|
|
|
if skillDict['@type'] == 'Role':
|
|
|
|
if not firstEntry:
|
|
|
|
skillsMarkup += ',\n'
|
|
|
|
sk = skillDict['hasOccupation']
|
|
|
|
roleName = sk['name']
|
2021-05-16 16:37:03 +00:00
|
|
|
if not roleName:
|
|
|
|
roleName = 'member'
|
2021-05-16 16:07:02 +00:00
|
|
|
category = \
|
|
|
|
sk['occupationalCategory']['codeValue']
|
|
|
|
categoryUrl = \
|
2021-05-16 16:25:16 +00:00
|
|
|
'https://www.onetonline.org/link/summary/' + category
|
2021-05-16 16:07:02 +00:00
|
|
|
skillsMarkup += \
|
2021-07-06 12:50:38 +00:00
|
|
|
' {\n' + \
|
|
|
|
' "@type": "Role",\n' + \
|
|
|
|
' "hasOccupation": {\n' + \
|
|
|
|
' "@type": "Occupation",\n' + \
|
|
|
|
' "name": "' + roleName + '",\n' + \
|
|
|
|
' "description": ' + \
|
|
|
|
'"Fediverse instance role",\n' + \
|
|
|
|
' "occupationLocation": {\n' + \
|
|
|
|
' "@type": "City",\n' + \
|
|
|
|
' "name": "' + city + '"\n' + \
|
|
|
|
' },\n' + \
|
|
|
|
' "occupationalCategory": {\n' + \
|
|
|
|
' "@type": "CategoryCode",\n' + \
|
|
|
|
' "inCodeSet": {\n' + \
|
|
|
|
' "@type": "CategoryCodeSet",\n' + \
|
|
|
|
' "name": "O*Net-SOC",\n' + \
|
|
|
|
' "dateModified": "2019",\n' + \
|
2021-05-16 16:20:38 +00:00
|
|
|
' ' + \
|
2021-07-06 12:50:38 +00:00
|
|
|
'"url": "https://www.onetonline.org/"\n' + \
|
|
|
|
' },\n' + \
|
|
|
|
' "codeValue": "' + category + '",\n' + \
|
|
|
|
' "url": "' + categoryUrl + '"\n' + \
|
|
|
|
' }\n' + \
|
|
|
|
' }\n' + \
|
|
|
|
' }'
|
2021-05-16 16:07:02 +00:00
|
|
|
elif skillDict['@type'] == 'Occupation':
|
|
|
|
if not firstEntry:
|
|
|
|
skillsMarkup += ',\n'
|
2021-05-16 16:09:24 +00:00
|
|
|
ocName = skillDict['name']
|
2021-05-16 17:08:17 +00:00
|
|
|
if not ocName:
|
|
|
|
ocName = 'member'
|
2021-05-16 16:09:24 +00:00
|
|
|
skillsList = skillDict['skills']
|
2021-05-16 16:07:02 +00:00
|
|
|
skillsListStr = '['
|
|
|
|
for skillStr in skillsList:
|
|
|
|
if skillsListStr != '[':
|
|
|
|
skillsListStr += ', '
|
|
|
|
skillsListStr += '"' + skillStr + '"'
|
|
|
|
skillsListStr += ']'
|
2021-05-16 16:13:46 +00:00
|
|
|
skillsMarkup += \
|
2021-07-06 12:50:38 +00:00
|
|
|
' {\n' + \
|
|
|
|
' "@type": "Occupation",\n' + \
|
|
|
|
' "name": "' + ocName + '",\n' + \
|
|
|
|
' "description": ' + \
|
|
|
|
'"Fediverse instance occupation",\n' + \
|
|
|
|
' "occupationLocation": {\n' + \
|
|
|
|
' "@type": "City",\n' + \
|
|
|
|
' "name": "' + city + '"\n' + \
|
|
|
|
' },\n' + \
|
|
|
|
' "skills": ' + skillsListStr + '\n' + \
|
|
|
|
' }'
|
2021-05-16 16:07:02 +00:00
|
|
|
firstEntry = False
|
2021-05-16 16:20:38 +00:00
|
|
|
skillsMarkup += '\n ],\n'
|
2021-05-16 16:07:02 +00:00
|
|
|
|
2021-05-16 11:16:50 +00:00
|
|
|
description = removeHtml(actorJson['summary'])
|
|
|
|
nameStr = removeHtml(actorJson['name'])
|
2021-05-12 15:49:53 +00:00
|
|
|
personMarkup = \
|
|
|
|
' <script type="application/ld+json">\n' + \
|
|
|
|
' {\n' + \
|
2021-05-12 15:52:07 +00:00
|
|
|
' "@context" : "http://schema.org",\n' + \
|
|
|
|
' "@type" : "Person",\n' + \
|
2021-05-16 11:16:50 +00:00
|
|
|
' "name": "' + nameStr + '",\n' + \
|
2021-05-12 15:52:07 +00:00
|
|
|
' "image": "' + actorJson['icon']['url'] + '",\n' + \
|
2021-05-16 11:16:50 +00:00
|
|
|
' "description": "' + description + '",\n' + \
|
2021-05-16 09:20:24 +00:00
|
|
|
cityMarkup + skillsMarkup + \
|
2021-05-12 15:52:07 +00:00
|
|
|
' "url": "' + actorJson['id'] + '"\n' + \
|
2021-05-12 15:49:53 +00:00
|
|
|
' }\n' + \
|
|
|
|
' </script>\n'
|
|
|
|
htmlStr = htmlStr.replace('<head>\n', '<head>\n' + personMarkup)
|
|
|
|
return htmlStr
|
|
|
|
|
|
|
|
|
2021-05-14 11:27:08 +00:00
|
|
|
def htmlHeaderWithWebsiteMarkup(cssFilename: str, instanceTitle: str,
|
|
|
|
httpPrefix: str, domain: str,
|
|
|
|
systemLanguage: str) -> str:
|
|
|
|
"""html header which includes website markup
|
|
|
|
https://schema.org/WebSite
|
|
|
|
"""
|
|
|
|
htmlStr = htmlHeaderWithExternalStyle(cssFilename, instanceTitle,
|
|
|
|
systemLanguage)
|
|
|
|
|
2021-05-15 14:25:51 +00:00
|
|
|
licenseUrl = 'https://www.gnu.org/licenses/agpl-3.0.rdf'
|
2021-05-15 09:08:01 +00:00
|
|
|
|
|
|
|
# social networking category
|
|
|
|
genreUrl = 'http://vocab.getty.edu/aat/300312270'
|
|
|
|
|
2021-05-14 11:27:08 +00:00
|
|
|
websiteMarkup = \
|
|
|
|
' <script type="application/ld+json">\n' + \
|
|
|
|
' {\n' + \
|
|
|
|
' "@context" : "http://schema.org",\n' + \
|
|
|
|
' "@type" : "WebSite",\n' + \
|
|
|
|
' "name": "' + instanceTitle + '",\n' + \
|
2021-05-14 12:24:21 +00:00
|
|
|
' "url": "' + httpPrefix + '://' + domain + '",\n' + \
|
2021-05-14 11:27:08 +00:00
|
|
|
' "license": "' + licenseUrl + '",\n' + \
|
|
|
|
' "inLanguage": "' + systemLanguage + '",\n' + \
|
|
|
|
' "isAccessibleForFree": true,\n' + \
|
2021-05-15 09:08:01 +00:00
|
|
|
' "genre": "' + genreUrl + '",\n' + \
|
2021-05-14 11:27:08 +00:00
|
|
|
' "accessMode": ["textual", "visual"],\n' + \
|
|
|
|
' "accessModeSufficient": ["textual"],\n' + \
|
2021-05-14 11:30:05 +00:00
|
|
|
' "accessibilityAPI" : ["ARIA"],\n' + \
|
2021-05-14 11:27:08 +00:00
|
|
|
' "accessibilityControl" : [\n' + \
|
|
|
|
' "fullKeyboardControl",\n' + \
|
|
|
|
' "fullTouchControl",\n' + \
|
|
|
|
' "fullMouseControl"\n' + \
|
|
|
|
' ],\n' + \
|
|
|
|
' "encodingFormat" : [\n' + \
|
|
|
|
' "text/html", "image/png", "image/webp",\n' + \
|
|
|
|
' "image/jpeg", "image/gif", "text/css"\n' + \
|
2021-05-14 11:29:20 +00:00
|
|
|
' ]\n' + \
|
2021-05-14 11:27:08 +00:00
|
|
|
' }\n' + \
|
|
|
|
' </script>\n'
|
|
|
|
htmlStr = htmlStr.replace('<head>\n', '<head>\n' + websiteMarkup)
|
|
|
|
return htmlStr
|
|
|
|
|
|
|
|
|
2021-05-15 19:39:34 +00:00
|
|
|
def htmlHeaderWithBlogMarkup(cssFilename: str, instanceTitle: str,
|
|
|
|
httpPrefix: str, domain: str, nickname: str,
|
|
|
|
systemLanguage: str, published: str,
|
|
|
|
title: str, snippet: str) -> str:
|
|
|
|
"""html header which includes blog post markup
|
|
|
|
https://schema.org/BlogPosting
|
|
|
|
"""
|
|
|
|
htmlStr = htmlHeaderWithExternalStyle(cssFilename, instanceTitle,
|
|
|
|
systemLanguage)
|
|
|
|
|
|
|
|
authorUrl = httpPrefix + '://' + domain + '/users/' + nickname
|
2021-05-16 10:42:52 +00:00
|
|
|
aboutUrl = httpPrefix + '://' + domain + '/about.html'
|
2021-05-17 14:25:46 +00:00
|
|
|
|
|
|
|
# license for content on the site may be different from
|
|
|
|
# the software license
|
|
|
|
contentLicenseUrl = 'https://creativecommons.org/licenses/by/3.0'
|
|
|
|
|
2021-05-15 19:39:34 +00:00
|
|
|
blogMarkup = \
|
|
|
|
' <script type="application/ld+json">\n' + \
|
|
|
|
' {\n' + \
|
|
|
|
' "@context" : "http://schema.org",\n' + \
|
|
|
|
' "@type" : "BlogPosting",\n' + \
|
|
|
|
' "headline": "' + title + '",\n' + \
|
|
|
|
' "datePublished": "' + published + '",\n' + \
|
|
|
|
' "dateModified": "' + published + '",\n' + \
|
|
|
|
' "author": {\n' + \
|
|
|
|
' "@type": "Person",\n' + \
|
|
|
|
' "name": "' + nickname + '",\n' + \
|
2021-05-16 10:42:52 +00:00
|
|
|
' "sameAs": "' + authorUrl + '"\n' + \
|
2021-05-15 19:39:34 +00:00
|
|
|
' },\n' + \
|
|
|
|
' "publisher": {\n' + \
|
|
|
|
' "@type": "WebSite",\n' + \
|
|
|
|
' "name": "' + instanceTitle + '",\n' + \
|
2021-05-16 10:42:52 +00:00
|
|
|
' "sameAs": "' + aboutUrl + '"\n' + \
|
2021-05-15 19:39:34 +00:00
|
|
|
' },\n' + \
|
2021-05-17 14:25:46 +00:00
|
|
|
' "license": "' + contentLicenseUrl + '",\n' + \
|
2021-05-15 19:39:34 +00:00
|
|
|
' "description": "' + snippet + '"\n' + \
|
|
|
|
' }\n' + \
|
|
|
|
' </script>\n'
|
|
|
|
htmlStr = htmlStr.replace('<head>\n', '<head>\n' + blogMarkup)
|
|
|
|
return htmlStr
|
|
|
|
|
|
|
|
|
2020-11-09 19:41:01 +00:00
|
|
|
def htmlFooter() -> str:
|
|
|
|
htmlStr = ' </body>\n'
|
|
|
|
htmlStr += '</html>\n'
|
|
|
|
return htmlStr
|
|
|
|
|
|
|
|
|
|
|
|
def loadIndividualPostAsHtmlFromCache(baseDir: str,
|
|
|
|
nickname: str, domain: str,
|
|
|
|
postJsonObject: {}) -> str:
|
|
|
|
"""If a cached html version of the given post exists then load it and
|
|
|
|
return the html text
|
|
|
|
This is much quicker than generating the html from the json object
|
|
|
|
"""
|
|
|
|
cachedPostFilename = \
|
|
|
|
getCachedPostFilename(baseDir, nickname, domain, postJsonObject)
|
|
|
|
|
|
|
|
postHtml = ''
|
|
|
|
if not cachedPostFilename:
|
|
|
|
return postHtml
|
|
|
|
|
|
|
|
if not os.path.isfile(cachedPostFilename):
|
|
|
|
return postHtml
|
|
|
|
|
|
|
|
tries = 0
|
|
|
|
while tries < 3:
|
|
|
|
try:
|
|
|
|
with open(cachedPostFilename, 'r') as file:
|
|
|
|
postHtml = file.read()
|
|
|
|
break
|
|
|
|
except Exception as e:
|
2021-05-20 12:04:05 +00:00
|
|
|
print('ERROR: loadIndividualPostAsHtmlFromCache ' +
|
|
|
|
str(tries) + ' ' + str(e))
|
2020-11-09 19:41:01 +00:00
|
|
|
# no sleep
|
|
|
|
tries += 1
|
|
|
|
if postHtml:
|
|
|
|
return postHtml
|
|
|
|
|
|
|
|
|
|
|
|
def addEmojiToDisplayName(baseDir: str, httpPrefix: str,
|
|
|
|
nickname: str, domain: str,
|
|
|
|
displayName: str, inProfileName: bool) -> str:
|
2020-12-29 09:52:52 +00:00
|
|
|
"""Adds emoji icons to display names or CW on individual posts
|
2020-11-09 19:41:01 +00:00
|
|
|
"""
|
|
|
|
if ':' not in displayName:
|
|
|
|
return displayName
|
|
|
|
|
|
|
|
displayName = displayName.replace('<p>', '').replace('</p>', '')
|
|
|
|
emojiTags = {}
|
2021-01-31 10:48:21 +00:00
|
|
|
# print('TAG: displayName before tags: ' + displayName)
|
2020-11-09 19:41:01 +00:00
|
|
|
displayName = \
|
|
|
|
addHtmlTags(baseDir, httpPrefix,
|
|
|
|
nickname, domain, displayName, [], emojiTags)
|
|
|
|
displayName = displayName.replace('<p>', '').replace('</p>', '')
|
2021-01-31 10:48:21 +00:00
|
|
|
# print('TAG: displayName after tags: ' + displayName)
|
2020-11-09 19:41:01 +00:00
|
|
|
# convert the emoji dictionary to a list
|
|
|
|
emojiTagsList = []
|
|
|
|
for tagName, tag in emojiTags.items():
|
|
|
|
emojiTagsList.append(tag)
|
2021-01-31 10:48:21 +00:00
|
|
|
# print('TAG: emoji tags list: ' + str(emojiTagsList))
|
2020-11-09 19:41:01 +00:00
|
|
|
if not inProfileName:
|
|
|
|
displayName = \
|
|
|
|
replaceEmojiFromTags(displayName, emojiTagsList, 'post header')
|
|
|
|
else:
|
|
|
|
displayName = \
|
|
|
|
replaceEmojiFromTags(displayName, emojiTagsList, 'profile')
|
2021-01-31 10:48:21 +00:00
|
|
|
# print('TAG: displayName after tags 2: ' + displayName)
|
2020-11-09 19:41:01 +00:00
|
|
|
|
|
|
|
# remove any stray emoji
|
|
|
|
while ':' in displayName:
|
|
|
|
if '://' in displayName:
|
|
|
|
break
|
|
|
|
emojiStr = displayName.split(':')[1]
|
|
|
|
prevDisplayName = displayName
|
|
|
|
displayName = displayName.replace(':' + emojiStr + ':', '').strip()
|
|
|
|
if prevDisplayName == displayName:
|
|
|
|
break
|
2021-01-31 10:48:21 +00:00
|
|
|
# print('TAG: displayName after tags 3: ' + displayName)
|
|
|
|
# print('TAG: displayName after tag replacements: ' + displayName)
|
2020-11-09 19:41:01 +00:00
|
|
|
|
|
|
|
return displayName
|
|
|
|
|
|
|
|
|
2021-03-07 10:15:17 +00:00
|
|
|
def _isImageMimeType(mimeType: str) -> bool:
|
|
|
|
"""Is the given mime type an image?
|
|
|
|
"""
|
|
|
|
imageMimeTypes = (
|
|
|
|
'image/png',
|
|
|
|
'image/jpeg',
|
|
|
|
'image/webp',
|
|
|
|
'image/avif',
|
|
|
|
'image/svg+xml',
|
|
|
|
'image/gif'
|
|
|
|
)
|
|
|
|
if mimeType in imageMimeTypes:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _isVideoMimeType(mimeType: str) -> bool:
|
|
|
|
"""Is the given mime type a video?
|
|
|
|
"""
|
|
|
|
videoMimeTypes = (
|
|
|
|
'video/mp4',
|
|
|
|
'video/webm',
|
|
|
|
'video/ogv'
|
|
|
|
)
|
|
|
|
if mimeType in videoMimeTypes:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _isAudioMimeType(mimeType: str) -> bool:
|
|
|
|
"""Is the given mime type an audio file?
|
|
|
|
"""
|
|
|
|
audioMimeTypes = (
|
|
|
|
'audio/mpeg',
|
|
|
|
'audio/ogg'
|
|
|
|
)
|
|
|
|
if mimeType in audioMimeTypes:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _isAttachedImage(attachmentFilename: str) -> bool:
|
|
|
|
"""Is the given attachment filename an image?
|
|
|
|
"""
|
|
|
|
if '.' not in attachmentFilename:
|
|
|
|
return False
|
|
|
|
imageExt = (
|
|
|
|
'png', 'jpg', 'jpeg', 'webp', 'avif', 'svg', 'gif'
|
|
|
|
)
|
|
|
|
ext = attachmentFilename.split('.')[-1]
|
|
|
|
if ext in imageExt:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-03-07 10:24:27 +00:00
|
|
|
def _isAttachedVideo(attachmentFilename: str) -> bool:
|
|
|
|
"""Is the given attachment filename a video?
|
|
|
|
"""
|
|
|
|
if '.' not in attachmentFilename:
|
|
|
|
return False
|
|
|
|
videoExt = (
|
|
|
|
'mp4', 'webm', 'ogv'
|
|
|
|
)
|
|
|
|
ext = attachmentFilename.split('.')[-1]
|
|
|
|
if ext in videoExt:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-11-09 19:41:01 +00:00
|
|
|
def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {},
|
|
|
|
isMuted: bool, avatarLink: str,
|
|
|
|
replyStr: str, announceStr: str, likeStr: str,
|
|
|
|
bookmarkStr: str, deleteStr: str,
|
|
|
|
muteStr: str) -> (str, str):
|
|
|
|
"""Returns a string representing any attachments
|
|
|
|
"""
|
|
|
|
attachmentStr = ''
|
|
|
|
galleryStr = ''
|
|
|
|
if not postJsonObject['object'].get('attachment'):
|
|
|
|
return attachmentStr, galleryStr
|
|
|
|
|
|
|
|
if not isinstance(postJsonObject['object']['attachment'], list):
|
|
|
|
return attachmentStr, galleryStr
|
|
|
|
|
|
|
|
attachmentCtr = 0
|
2021-03-07 12:43:31 +00:00
|
|
|
attachmentStr = ''
|
|
|
|
mediaStyleAdded = False
|
2020-11-09 19:41:01 +00:00
|
|
|
for attach in postJsonObject['object']['attachment']:
|
|
|
|
if not (attach.get('mediaType') and attach.get('url')):
|
|
|
|
continue
|
|
|
|
|
|
|
|
mediaType = attach['mediaType']
|
|
|
|
imageDescription = ''
|
|
|
|
if attach.get('name'):
|
|
|
|
imageDescription = attach['name'].replace('"', "'")
|
2021-03-07 10:15:17 +00:00
|
|
|
if _isImageMimeType(mediaType):
|
|
|
|
if _isAttachedImage(attach['url']):
|
2021-03-07 12:43:31 +00:00
|
|
|
if not attachmentStr:
|
|
|
|
attachmentStr += '<div class="media">\n'
|
|
|
|
mediaStyleAdded = True
|
|
|
|
|
2020-11-09 19:41:01 +00:00
|
|
|
if attachmentCtr > 0:
|
|
|
|
attachmentStr += '<br>'
|
|
|
|
if boxName == 'tlmedia':
|
|
|
|
galleryStr += '<div class="gallery">\n'
|
|
|
|
if not isMuted:
|
|
|
|
galleryStr += ' <a href="' + attach['url'] + '">\n'
|
|
|
|
galleryStr += \
|
|
|
|
' <img loading="lazy" src="' + \
|
|
|
|
attach['url'] + '" alt="" title="">\n'
|
|
|
|
galleryStr += ' </a>\n'
|
|
|
|
if postJsonObject['object'].get('url'):
|
|
|
|
imagePostUrl = postJsonObject['object']['url']
|
|
|
|
else:
|
|
|
|
imagePostUrl = postJsonObject['object']['id']
|
|
|
|
if imageDescription and not isMuted:
|
|
|
|
galleryStr += \
|
|
|
|
' <a href="' + imagePostUrl + \
|
|
|
|
'" class="gallerytext"><div ' + \
|
|
|
|
'class="gallerytext">' + \
|
|
|
|
imageDescription + '</div></a>\n'
|
|
|
|
else:
|
|
|
|
galleryStr += \
|
|
|
|
'<label class="transparent">---</label><br>'
|
|
|
|
galleryStr += ' <div class="mediaicons">\n'
|
|
|
|
galleryStr += \
|
|
|
|
' ' + replyStr+announceStr + likeStr + \
|
|
|
|
bookmarkStr + deleteStr + muteStr + '\n'
|
|
|
|
galleryStr += ' </div>\n'
|
|
|
|
galleryStr += ' <div class="mediaavatar">\n'
|
|
|
|
galleryStr += ' ' + avatarLink + '\n'
|
|
|
|
galleryStr += ' </div>\n'
|
|
|
|
galleryStr += '</div>\n'
|
|
|
|
|
|
|
|
attachmentStr += '<a href="' + attach['url'] + '">'
|
|
|
|
attachmentStr += \
|
|
|
|
'<img loading="lazy" src="' + attach['url'] + \
|
|
|
|
'" alt="' + imageDescription + '" title="' + \
|
|
|
|
imageDescription + '" class="attachment"></a>\n'
|
|
|
|
attachmentCtr += 1
|
2021-03-07 10:15:17 +00:00
|
|
|
elif _isVideoMimeType(mediaType):
|
2021-03-07 10:24:27 +00:00
|
|
|
if _isAttachedVideo(attach['url']):
|
|
|
|
extension = attach['url'].split('.')[-1]
|
2020-11-09 19:41:01 +00:00
|
|
|
if attachmentCtr > 0:
|
|
|
|
attachmentStr += '<br>'
|
|
|
|
if boxName == 'tlmedia':
|
|
|
|
galleryStr += '<div class="gallery">\n'
|
|
|
|
if not isMuted:
|
|
|
|
galleryStr += ' <a href="' + attach['url'] + '">\n'
|
|
|
|
galleryStr += \
|
2021-03-07 11:55:06 +00:00
|
|
|
' <figure id="videoContainer" ' + \
|
|
|
|
'data-fullscreen="false">\n' + \
|
|
|
|
' <video id="video" controls ' + \
|
|
|
|
'preload="metadata">\n'
|
2020-11-09 19:41:01 +00:00
|
|
|
galleryStr += \
|
|
|
|
' <source src="' + attach['url'] + \
|
|
|
|
'" alt="' + imageDescription + \
|
|
|
|
'" title="' + imageDescription + \
|
|
|
|
'" class="attachment" type="video/' + \
|
2021-03-07 10:24:27 +00:00
|
|
|
extension + '">'
|
2020-11-09 19:41:01 +00:00
|
|
|
idx = 'Your browser does not support the video tag.'
|
|
|
|
galleryStr += translate[idx]
|
|
|
|
galleryStr += ' </video>\n'
|
2021-03-07 11:55:06 +00:00
|
|
|
galleryStr += ' </figure>\n'
|
2020-11-09 19:41:01 +00:00
|
|
|
galleryStr += ' </a>\n'
|
|
|
|
if postJsonObject['object'].get('url'):
|
|
|
|
videoPostUrl = postJsonObject['object']['url']
|
|
|
|
else:
|
|
|
|
videoPostUrl = postJsonObject['object']['id']
|
|
|
|
if imageDescription and not isMuted:
|
|
|
|
galleryStr += \
|
|
|
|
' <a href="' + videoPostUrl + \
|
|
|
|
'" class="gallerytext"><div ' + \
|
|
|
|
'class="gallerytext">' + \
|
|
|
|
imageDescription + '</div></a>\n'
|
|
|
|
else:
|
|
|
|
galleryStr += \
|
|
|
|
'<label class="transparent">---</label><br>'
|
|
|
|
galleryStr += ' <div class="mediaicons">\n'
|
|
|
|
galleryStr += \
|
|
|
|
' ' + replyStr + announceStr + likeStr + \
|
|
|
|
bookmarkStr + deleteStr + muteStr + '\n'
|
|
|
|
galleryStr += ' </div>\n'
|
|
|
|
galleryStr += ' <div class="mediaavatar">\n'
|
|
|
|
galleryStr += ' ' + avatarLink + '\n'
|
|
|
|
galleryStr += ' </div>\n'
|
|
|
|
galleryStr += '</div>\n'
|
|
|
|
|
|
|
|
attachmentStr += \
|
2021-03-07 12:01:33 +00:00
|
|
|
'<center><figure id="videoContainer" ' + \
|
|
|
|
'data-fullscreen="false">\n' + \
|
|
|
|
' <video id="video" controls ' + \
|
|
|
|
'preload="metadata">\n'
|
2020-11-09 19:41:01 +00:00
|
|
|
attachmentStr += \
|
|
|
|
'<source src="' + attach['url'] + '" alt="' + \
|
|
|
|
imageDescription + '" title="' + imageDescription + \
|
|
|
|
'" class="attachment" type="video/' + \
|
2021-03-07 10:24:27 +00:00
|
|
|
extension + '">'
|
2020-11-09 19:41:01 +00:00
|
|
|
attachmentStr += \
|
|
|
|
translate['Your browser does not support the video tag.']
|
2021-03-07 12:01:33 +00:00
|
|
|
attachmentStr += '</video></figure></center>'
|
2020-11-09 19:41:01 +00:00
|
|
|
attachmentCtr += 1
|
2021-03-07 10:15:17 +00:00
|
|
|
elif _isAudioMimeType(mediaType):
|
2020-11-09 19:41:01 +00:00
|
|
|
extension = '.mp3'
|
|
|
|
if attach['url'].endswith('.ogg'):
|
|
|
|
extension = '.ogg'
|
|
|
|
if attach['url'].endswith(extension):
|
|
|
|
if attachmentCtr > 0:
|
|
|
|
attachmentStr += '<br>'
|
|
|
|
if boxName == 'tlmedia':
|
|
|
|
galleryStr += '<div class="gallery">\n'
|
|
|
|
if not isMuted:
|
|
|
|
galleryStr += ' <a href="' + attach['url'] + '">\n'
|
|
|
|
galleryStr += ' <audio controls>\n'
|
|
|
|
galleryStr += \
|
|
|
|
' <source src="' + attach['url'] + \
|
|
|
|
'" alt="' + imageDescription + \
|
|
|
|
'" title="' + imageDescription + \
|
|
|
|
'" class="attachment" type="audio/' + \
|
|
|
|
extension.replace('.', '') + '">'
|
|
|
|
idx = 'Your browser does not support the audio tag.'
|
|
|
|
galleryStr += translate[idx]
|
|
|
|
galleryStr += ' </audio>\n'
|
|
|
|
galleryStr += ' </a>\n'
|
|
|
|
if postJsonObject['object'].get('url'):
|
|
|
|
audioPostUrl = postJsonObject['object']['url']
|
|
|
|
else:
|
|
|
|
audioPostUrl = postJsonObject['object']['id']
|
|
|
|
if imageDescription and not isMuted:
|
|
|
|
galleryStr += \
|
|
|
|
' <a href="' + audioPostUrl + \
|
|
|
|
'" class="gallerytext"><div ' + \
|
|
|
|
'class="gallerytext">' + \
|
|
|
|
imageDescription + '</div></a>\n'
|
|
|
|
else:
|
|
|
|
galleryStr += \
|
|
|
|
'<label class="transparent">---</label><br>'
|
|
|
|
galleryStr += ' <div class="mediaicons">\n'
|
|
|
|
galleryStr += \
|
|
|
|
' ' + replyStr + announceStr + \
|
|
|
|
likeStr + bookmarkStr + \
|
2021-06-22 12:29:17 +00:00
|
|
|
deleteStr + muteStr + '\n'
|
2020-11-09 19:41:01 +00:00
|
|
|
galleryStr += ' </div>\n'
|
|
|
|
galleryStr += ' <div class="mediaavatar">\n'
|
|
|
|
galleryStr += ' ' + avatarLink + '\n'
|
|
|
|
galleryStr += ' </div>\n'
|
|
|
|
galleryStr += '</div>\n'
|
|
|
|
|
|
|
|
attachmentStr += '<center>\n<audio controls>\n'
|
|
|
|
attachmentStr += \
|
|
|
|
'<source src="' + attach['url'] + '" alt="' + \
|
|
|
|
imageDescription + '" title="' + imageDescription + \
|
|
|
|
'" class="attachment" type="audio/' + \
|
|
|
|
extension.replace('.', '') + '">'
|
|
|
|
attachmentStr += \
|
|
|
|
translate['Your browser does not support the audio tag.']
|
|
|
|
attachmentStr += '</audio>\n</center>\n'
|
|
|
|
attachmentCtr += 1
|
2021-03-07 12:43:31 +00:00
|
|
|
if mediaStyleAdded:
|
|
|
|
attachmentStr += '</div>'
|
2020-11-09 19:41:01 +00:00
|
|
|
return attachmentStr, galleryStr
|
|
|
|
|
|
|
|
|
|
|
|
def htmlPostSeparator(baseDir: str, column: str) -> str:
|
|
|
|
"""Returns the html for a timeline post separator image
|
|
|
|
"""
|
2020-11-14 11:49:29 +00:00
|
|
|
theme = getConfigParam(baseDir, 'theme')
|
2020-11-09 19:41:01 +00:00
|
|
|
filename = 'separator.png'
|
2020-11-18 21:56:19 +00:00
|
|
|
separatorClass = "postSeparatorImage"
|
2020-11-09 19:41:01 +00:00
|
|
|
if column:
|
2020-11-18 21:56:19 +00:00
|
|
|
separatorClass = "postSeparatorImage" + column.title()
|
2020-11-09 19:41:01 +00:00
|
|
|
filename = 'separator_' + column + '.png'
|
2020-11-14 11:49:29 +00:00
|
|
|
separatorImageFilename = baseDir + '/theme/' + theme + '/icons/' + filename
|
2020-11-09 19:41:01 +00:00
|
|
|
separatorStr = ''
|
|
|
|
if os.path.isfile(separatorImageFilename):
|
|
|
|
separatorStr = \
|
2020-11-18 21:56:19 +00:00
|
|
|
'<div class="' + separatorClass + '"><center>' + \
|
2021-02-01 18:38:08 +00:00
|
|
|
'<img src="/icons/' + filename + '" ' + \
|
|
|
|
'alt="" /></center></div>\n'
|
2020-11-09 19:41:01 +00:00
|
|
|
return separatorStr
|
2020-11-09 22:44:03 +00:00
|
|
|
|
|
|
|
|
2020-11-17 20:40:36 +00:00
|
|
|
def htmlHighlightLabel(label: str, highlight: bool) -> str:
|
|
|
|
"""If the given text should be highlighted then return
|
|
|
|
the appropriate markup.
|
|
|
|
This is so that in shell browsers, like lynx, it's possible
|
|
|
|
to see if the replies or DM button are highlighted.
|
|
|
|
"""
|
|
|
|
if not highlight:
|
|
|
|
return label
|
|
|
|
return '*' + str(label) + '*'
|
2020-12-20 11:00:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def getAvatarImageUrl(session,
|
|
|
|
baseDir: str, httpPrefix: str,
|
|
|
|
postActor: str, personCache: {},
|
|
|
|
avatarUrl: str, allowDownloads: bool) -> str:
|
|
|
|
"""Returns the avatar image url
|
|
|
|
"""
|
|
|
|
# get the avatar image url for the post actor
|
|
|
|
if not avatarUrl:
|
|
|
|
avatarUrl = \
|
|
|
|
getPersonAvatarUrl(baseDir, postActor, personCache,
|
|
|
|
allowDownloads)
|
|
|
|
avatarUrl = \
|
|
|
|
updateAvatarImageCache(session, baseDir, httpPrefix,
|
|
|
|
postActor, avatarUrl, personCache,
|
|
|
|
allowDownloads)
|
|
|
|
else:
|
|
|
|
updateAvatarImageCache(session, baseDir, httpPrefix,
|
|
|
|
postActor, avatarUrl, personCache,
|
|
|
|
allowDownloads)
|
|
|
|
|
|
|
|
if not avatarUrl:
|
|
|
|
avatarUrl = postActor + '/avatar.png'
|
|
|
|
|
|
|
|
return avatarUrl
|
2021-02-05 17:05:53 +00:00
|
|
|
|
|
|
|
|
2021-02-06 10:35:47 +00:00
|
|
|
def htmlHideFromScreenReader(htmlStr: str) -> str:
|
|
|
|
"""Returns html which is hidden from screen readers
|
|
|
|
"""
|
|
|
|
return '<span aria-hidden="true">' + htmlStr + '</span>'
|
|
|
|
|
|
|
|
|
2021-04-22 11:51:19 +00:00
|
|
|
def htmlKeyboardNavigation(banner: str, links: {}, accessKeys: {},
|
2021-06-20 11:28:35 +00:00
|
|
|
subHeading: str = None,
|
|
|
|
usersPath: str = None, translate: {} = None,
|
|
|
|
followApprovals: bool = False) -> str:
|
2021-02-05 17:05:53 +00:00
|
|
|
"""Given a set of links return the html for keyboard navigation
|
|
|
|
"""
|
2021-02-15 12:09:21 +00:00
|
|
|
htmlStr = '<div class="transparent"><ul>\n'
|
2021-02-05 18:46:42 +00:00
|
|
|
|
2021-02-05 19:15:52 +00:00
|
|
|
if banner:
|
2021-02-15 12:13:31 +00:00
|
|
|
htmlStr += '<pre aria-label="">\n' + banner + '\n<br><br></pre>\n'
|
2021-02-05 19:15:52 +00:00
|
|
|
|
2021-02-12 15:28:11 +00:00
|
|
|
if subHeading:
|
2021-02-12 15:31:47 +00:00
|
|
|
htmlStr += '<strong><label class="transparent">' + \
|
2021-02-15 12:09:21 +00:00
|
|
|
subHeading + '</label></strong><br>\n'
|
2021-02-12 15:28:11 +00:00
|
|
|
|
2021-02-05 18:46:42 +00:00
|
|
|
# show new follower approvals
|
|
|
|
if usersPath and translate and followApprovals:
|
2021-02-06 11:39:32 +00:00
|
|
|
htmlStr += '<strong><label class="transparent">' + \
|
2021-02-05 18:46:42 +00:00
|
|
|
'<a href="' + usersPath + '/followers#timeline">' + \
|
|
|
|
translate['Approve follow requests'] + '</a>' + \
|
2021-02-15 12:09:21 +00:00
|
|
|
'</label></strong><br><br>\n'
|
2021-02-05 18:46:42 +00:00
|
|
|
|
|
|
|
# show the list of links
|
2021-02-05 17:05:53 +00:00
|
|
|
for title, url in links.items():
|
2021-04-22 11:51:19 +00:00
|
|
|
accessKeyStr = ''
|
|
|
|
if accessKeys.get(title):
|
|
|
|
accessKeyStr = 'accesskey="' + accessKeys[title] + '"'
|
|
|
|
|
2021-02-05 17:33:31 +00:00
|
|
|
htmlStr += '<li><label class="transparent">' + \
|
2021-04-22 11:51:19 +00:00
|
|
|
'<a href="' + str(url) + '" ' + accessKeyStr + '>' + \
|
2021-02-15 12:09:21 +00:00
|
|
|
str(title) + '</a></label></li>\n'
|
|
|
|
htmlStr += '</ul></div>\n'
|
2021-02-05 17:05:53 +00:00
|
|
|
return htmlStr
|
2021-07-22 16:58:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def beginEditSection(label: str) -> str:
|
|
|
|
"""returns the html for begining a dropdown section on edit profile screen
|
|
|
|
"""
|
|
|
|
return \
|
|
|
|
' <details><summary class="cw">' + label + '</summary>\n' + \
|
|
|
|
'<div class="container">'
|
|
|
|
|
|
|
|
|
|
|
|
def endEditSection() -> str:
|
|
|
|
"""returns the html for ending a dropdown section on edit profile screen
|
|
|
|
"""
|
|
|
|
return ' </div></details>\n'
|
|
|
|
|
|
|
|
|
|
|
|
def editTextField(label: str, name: str, value: str = "",
|
|
|
|
placeholder: str = "") -> str:
|
|
|
|
"""Returns html for editing a text field
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
value = ''
|
|
|
|
placeholderStr = ''
|
|
|
|
if placeholder:
|
|
|
|
placeholderStr = ' placeholder="' + placeholder + '"'
|
|
|
|
return \
|
|
|
|
'<label class="labels">' + label + '</label><br>\n' + \
|
|
|
|
' <input type="text" name="' + name + '" value="' + \
|
|
|
|
value + '"' + placeholderStr + '>\n'
|
|
|
|
|
|
|
|
|
2021-07-24 11:47:51 +00:00
|
|
|
def editNumberField(label: str, name: str, value: int = 1,
|
|
|
|
minValue: int = 1, maxValue: int = 999999,
|
|
|
|
placeholder: int = 1) -> str:
|
|
|
|
"""Returns html for editing an integer number field
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
value = ''
|
|
|
|
placeholderStr = ''
|
|
|
|
if placeholder:
|
|
|
|
placeholderStr = ' placeholder="' + str(placeholder) + '"'
|
|
|
|
return \
|
|
|
|
'<label class="labels">' + label + '</label><br>\n' + \
|
|
|
|
' <input type="number" name="' + name + '" value="' + \
|
|
|
|
str(value) + '"' + placeholderStr + ' ' + \
|
|
|
|
'min="' + str(minValue) + '" max="' + str(maxValue) + '" step="1">\n'
|
|
|
|
|
|
|
|
|
2021-07-24 22:08:11 +00:00
|
|
|
def editCurrencyField(label: str, name: str, value: str = "0.00",
|
|
|
|
placeholder: str = "0.00") -> str:
|
|
|
|
"""Returns html for editing a currency field
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
value = '0.00'
|
|
|
|
placeholderStr = '0.00'
|
|
|
|
if placeholder:
|
|
|
|
if placeholder.isdigit():
|
|
|
|
placeholderStr = ' placeholder="' + str(placeholder) + '"'
|
|
|
|
return \
|
|
|
|
'<label class="labels">' + label + '</label><br>\n' + \
|
|
|
|
' <input type="text" name="' + name + '" value="' + \
|
|
|
|
str(value) + '"' + placeholderStr + ' ' + \
|
|
|
|
'" pattern="^\\d{1,3}(,\\d{3})*(\\.\\d+)?" data-type="currency">\n'
|
|
|
|
|
|
|
|
|
2021-07-22 16:58:59 +00:00
|
|
|
def editCheckBox(label: str, name: str, checked: bool = False) -> str:
|
|
|
|
"""Returns html for editing a checkbox field
|
|
|
|
"""
|
|
|
|
checkedStr = ''
|
|
|
|
if checked:
|
|
|
|
checkedStr = ' checked'
|
|
|
|
|
|
|
|
return \
|
|
|
|
' <input type="checkbox" class="profilecheckbox" ' + \
|
|
|
|
'name="' + name + '"' + checkedStr + '> ' + label + '<br>\n'
|
|
|
|
|
|
|
|
|
|
|
|
def editTextArea(label: str, name: str, value: str = "",
|
|
|
|
height: int = 600,
|
|
|
|
placeholder: str = "",
|
|
|
|
spellcheck: bool = False) -> str:
|
|
|
|
"""Returns html for editing a textarea field
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
value = ''
|
2021-07-22 18:35:45 +00:00
|
|
|
text = ''
|
|
|
|
if label:
|
|
|
|
text = '<label class="labels">' + label + '</label><br>\n'
|
|
|
|
text += \
|
2021-07-22 16:58:59 +00:00
|
|
|
' <textarea id="message" placeholder=' + \
|
2021-07-22 18:50:31 +00:00
|
|
|
'"' + placeholder + '" '
|
|
|
|
text += 'name="' + name + '" '
|
2021-07-22 18:52:47 +00:00
|
|
|
text += 'style="height:' + str(height) + 'px" '
|
2021-07-22 18:50:31 +00:00
|
|
|
text += 'spellcheck="' + str(spellcheck).lower() + '">'
|
|
|
|
text += value + '</textarea>\n'
|
2021-07-22 18:35:45 +00:00
|
|
|
return text
|