__filename__ = "webinterface.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.0.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import json
import time
import os
from collections import OrderedDict
from datetime import datetime
from datetime import date
from dateutil.parser import parse
from shutil import copyfile
from shutil import copyfileobj
from pprint import pprint
from person import personBoxJson
from person import isPersonSnoozed
from donate import getDonationUrl
from utils import getNicknameFromActor
from utils import getDomainFromActor
from utils import locatePost
from utils import noOfAccounts
from utils import isPublicPost
from utils import getDisplayName
from utils import getCachedPostDirectory
from utils import getCachedPostFilename
from utils import loadJson
from utils import saveJson
from follow import isFollowingActor
from webfinger import webfingerHandle
from posts import isDM
from posts import getPersonBox
from posts import getUserUrl
from posts import parseUserFeed
from posts import populateRepliesJson
from posts import isModerator
from posts import outboxMessageCreateWrap
from posts import downloadAnnounce
from session import getJson
from auth import createPassword
from like import likedByPerson
from like import noOfLikes
from bookmarks import bookmarkedByPerson
from announce import announcedByPerson
from blocking import isBlocked
from content import getMentionsFromHtml
from content import addHtmlTags
from content import replaceEmojiFromTags
from content import removeLongWords
from config import getConfigParam
from skills import getSkills
from cache import getPersonFromCache
from cache import storePersonInCache
from shares import getValidSharedItemID
def updateAvatarImageCache(session,baseDir: str,httpPrefix: str,actor: str,avatarUrl: str,personCache: {},force=False) -> str:
"""Updates the cached avatar for the given actor
"""
if not avatarUrl:
return None
avatarImagePath=baseDir+'/cache/avatars/'+actor.replace('/','-')
if avatarUrl.endswith('.png') or '.png?' in avatarUrl:
sessionHeaders = {'Accept': 'image/png'}
avatarImageFilename=avatarImagePath+'.png'
elif avatarUrl.endswith('.jpg') or avatarUrl.endswith('.jpeg') or \
'.jpg?' in avatarUrl or '.jpeg?' in avatarUrl:
sessionHeaders = {'Accept': 'image/jpeg'}
avatarImageFilename=avatarImagePath+'.jpg'
elif avatarUrl.endswith('.gif') or '.gif?' in avatarUrl:
sessionHeaders = {'Accept': 'image/gif'}
avatarImageFilename=avatarImagePath+'.gif'
elif avatarUrl.endswith('.webp') or '.webp?' in avatarUrl:
sessionHeaders = {'Accept': 'image/webp'}
avatarImageFilename=avatarImagePath+'.webp'
else:
return None
if not os.path.isfile(avatarImageFilename) or force:
try:
print('avatar image url: '+avatarUrl)
result=session.get(avatarUrl, headers=sessionHeaders, params=None)
if result.status_code<200 or result.status_code>202:
print('Avatar image download failed with status '+str(result.status_code))
# remove partial download
if os.path.isfile(avatarImageFilename):
os.remove(avatarImageFilename)
else:
with open(avatarImageFilename, 'wb') as f:
f.write(result.content)
print('avatar image downloaded for '+actor)
return avatarImageFilename.replace(baseDir+'/cache','')
except Exception as e:
print('Failed to download avatar image: '+str(avatarUrl))
print(e)
if '/channel/' not in actor:
sessionHeaders = {'Accept': 'application/activity+json; profile="https://www.w3.org/ns/activitystreams"'}
else:
sessionHeaders = {'Accept': 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'}
personJson = getJson(session,actor,sessionHeaders,None,__version__,httpPrefix,None)
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)
return getPersonAvatarUrl(baseDir,actor,personCache)
return None
return avatarImageFilename.replace(baseDir+'/cache','')
def getPersonAvatarUrl(baseDir: str,personUrl: str,personCache: {}) -> str:
"""Returns the avatar url for the person
"""
personJson = getPersonFromCache(baseDir,personUrl,personCache)
if not personJson:
return None
# get from locally stored image
actorStr=personJson['id'].replace('/','-')
avatarImagePath=baseDir+'/cache/avatars/'+actorStr
if os.path.isfile(avatarImagePath+'.png'):
return '/avatars/'+actorStr+'.png'
if os.path.isfile(avatarImagePath+'.jpg'):
return '/avatars/'+actorStr+'.jpg'
if os.path.isfile(avatarImagePath+'.gif'):
return '/avatars/'+actorStr+'.gif'
if os.path.isfile(avatarImagePath+'.webp'):
return '/avatars/'+actorStr+'.webp'
if os.path.isfile(avatarImagePath):
return '/avatars/'+actorStr
if personJson.get('icon'):
if personJson['icon'].get('url'):
return personJson['icon']['url']
return None
def htmlSearchEmoji(translate: {},baseDir: str,searchStr: str) -> str:
"""Search results for emoji
"""
# emoji.json is generated so that it can be customized and the changes
# will be retained even if default_emoji.json is subsequently updated
if not os.path.isfile(baseDir+'/emoji/emoji.json'):
copyfile(baseDir+'/emoji/default_emoji.json',baseDir+'/emoji/emoji.json')
searchStr=searchStr.lower().replace(':','').strip('\n')
cssFilename=baseDir+'/epicyon-profile.css'
if os.path.isfile(baseDir+'/epicyon.css'):
cssFilename=baseDir+'/epicyon.css'
with open(cssFilename, 'r') as cssFile:
emojiCSS=cssFile.read()
emojiLookupFilename=baseDir+'/emoji/emoji.json'
# create header
emojiForm=htmlHeader(cssFilename,emojiCSS)
emojiForm+='
'+translate['Emoji Search']+'
'
# does the lookup file exist?
if not os.path.isfile(emojiLookupFilename):
emojiForm+='
'+translate['No results']+'
'
emojiForm+=htmlFooter()
return emojiForm
emojiJson=loadJson(emojiLookupFilename)
if emojiJson:
results={}
for emojiName,filename in emojiJson.items():
if searchStr in emojiName:
results[emojiName] = filename+'.png'
for emojiName,filename in emojiJson.items():
if emojiName in searchStr:
results[emojiName] = filename+'.png'
headingShown=False
emojiForm+='
'
for emojiName,filename in results.items():
if os.path.isfile(baseDir+'/emoji/'+filename):
if not headingShown:
emojiForm+='
'+translate['Copy the text then paste it into your post']+'
'
headingShown=True
emojiForm+='
:'+emojiName+':
'
emojiForm+='
'
emojiForm+=htmlFooter()
return emojiForm
def getIconsDir(baseDir: str) -> str:
"""Returns the directory where icons exist
"""
iconsDir='icons'
theme=getConfigParam(baseDir,'theme')
if theme:
if os.path.isdir(baseDir+'/img/icons/'+theme):
iconsDir='icons/'+theme
return iconsDir
def htmlSearchSharedItems(translate: {}, \
baseDir: str,searchStr: str, \
pageNumber: int, \
resultsPerPage: int, \
httpPrefix: str, \
domainFull: str,actor: str) -> str:
"""Search results for shared items
"""
iconsDir=getIconsDir(baseDir)
currPage=1
ctr=0
sharedItemsForm=''
searchStrLower=searchStr.replace('%2B','+').replace('%40','@').replace('%3A',':').replace('%23','#').lower().strip('\n')
searchStrLowerList=searchStrLower.split('+')
cssFilename=baseDir+'/epicyon-profile.css'
if os.path.isfile(baseDir+'/epicyon.css'):
cssFilename=baseDir+'/epicyon.css'
with open(cssFilename, 'r') as cssFile:
sharedItemsCSS=cssFile.read()
sharedItemsForm=htmlHeader(cssFilename,sharedItemsCSS)
sharedItemsForm+='
'+translate['Shared Items Search']+'
'
resultsExist=False
for subdir, dirs, files in os.walk(baseDir+'/accounts'):
for handle in dirs:
if '@' not in handle:
continue
contactNickname=handle.split('@')[0]
sharesFilename=baseDir+'/accounts/'+handle+'/shares.json'
if not os.path.isfile(sharesFilename):
continue
sharesJson=loadJson(sharesFilename)
if not sharesJson:
continue
for name,sharedItem in sharesJson.items():
matched=True
for searchSubstr in searchStrLowerList:
subStrMatched=False
searchSubstr=searchSubstr.strip()
if searchSubstr in sharedItem['location'].lower():
subStrMatched=True
elif searchSubstr in sharedItem['summary'].lower():
subStrMatched=True
elif searchSubstr in sharedItem['displayName'].lower():
subStrMatched=True
elif searchSubstr in sharedItem['category'].lower():
subStrMatched=True
if not subStrMatched:
matched=False
break
if matched:
if currPage==pageNumber:
sharedItemsForm+='
'
sharedItemsForm+='
'+sharedItem['displayName']+'
'
if sharedItem.get('imageUrl'):
sharedItemsForm+=''
sharedItemsForm+=''
sharedItemsForm+='
'
if actor.endswith('/users/'+contactNickname):
sharedItemsForm+=' '
sharedItemsForm+='
'
if not resultsExist and currPage>1:
# previous page link, needs to be a POST
sharedItemsForm+=''
resultsExist=True
ctr+=1
if ctr>=resultsPerPage:
currPage+=1
if currPage>pageNumber:
# next page link, needs to be a POST
sharedItemsForm+=''
break
ctr=0
if not resultsExist:
sharedItemsForm+='
'+translate['No results']+'
'
sharedItemsForm+=htmlFooter()
return sharedItemsForm
def htmlModerationInfo(translate: {},baseDir: str) -> str:
infoForm=''
cssFilename=baseDir+'/epicyon-profile.css'
if os.path.isfile(baseDir+'/epicyon.css'):
cssFilename=baseDir+'/epicyon.css'
with open(cssFilename, 'r') as cssFile:
infoCSS=cssFile.read()
infoForm=htmlHeader(cssFilename,infoCSS)
infoForm+='
'+translate['Moderation Information']+'
'
infoShown=False
suspendedFilename=baseDir+'/accounts/suspended.txt'
if os.path.isfile(suspendedFilename):
with open(suspendedFilename, "r") as f:
suspendedStr = f.read()
infoForm+='
'
infoForm+=' '+translate['Suspended accounts']+''
infoForm+=' '+translate['These are currently suspended']
infoForm+=' '
infoForm+='
'
infoShown=True
blockingFilename=baseDir+'/accounts/blocking.txt'
if os.path.isfile(blockingFilename):
with open(blockingFilename, "r") as f:
blockedStr = f.read()
infoForm+='
'
infoForm+=' '+translate['Blocked accounts and hashtags']+''
infoForm+=' '+translate['These are globally blocked for all accounts on this instance']
infoForm+=' '
infoForm+='
'
infoShown=True
if not infoShown:
infoForm+='
'+translate['Any blocks or suspensions made by moderators will be shown here.']+'
'
infoForm+=htmlFooter()
return infoForm
def htmlHashtagSearch(translate: {}, \
baseDir: str,hashtag: str,pageNumber: int,postsPerPage: int, \
session,wfRequest: {},personCache: {}, \
httpPrefix: str,projectVersion: str) -> str:
"""Show a page containing search results for a hashtag
"""
iconsDir=getIconsDir(baseDir)
if hashtag.startswith('#'):
hashtag=hashtag[1:]
hashtagIndexFile=baseDir+'/tags/'+hashtag+'.txt'
if not os.path.isfile(hashtagIndexFile):
return None
# read the index
with open(hashtagIndexFile, "r") as f:
lines = f.readlines()
cssFilename=baseDir+'/epicyon-profile.css'
if os.path.isfile(baseDir+'/epicyon.css'):
cssFilename=baseDir+'/epicyon.css'
with open(cssFilename, 'r') as cssFile:
hashtagSearchCSS = cssFile.read()
startIndex=len(lines)-1-int(pageNumber*postsPerPage)
if startIndex<0:
startIndex=len(lines)-1
endIndex=startIndex-postsPerPage
if endIndex<0:
endIndex=0
hashtagSearchForm=htmlHeader(cssFilename,hashtagSearchCSS)
hashtagSearchForm+='
#'+hashtag+'
'
if startIndex!=len(lines)-1:
# previous page link
hashtagSearchForm+='
'
index=startIndex
while index>=endIndex:
postId=lines[index].strip('\n')
nickname=getNicknameFromActor(postId)
if not nickname:
index-=1
continue
domain,port=getDomainFromActor(postId)
if not domain:
index-=1
continue
postFilename=locatePost(baseDir,nickname,domain,postId)
if not postFilename:
index-=1
continue
postJsonObject=loadJson(postFilename)
if postJsonObject:
if not isPublicPost(postJsonObject):
index-=1
continue
hashtagSearchForm+= \
individualPostAsHtml(iconsDir,translate,None, \
baseDir,session,wfRequest,personCache, \
nickname,domain,port,postJsonObject, \
None,True,False, \
httpPrefix,projectVersion,'inbox', \
False,False,False,False,False)
index-=1
if endIndex>0:
# next page link
hashtagSearchForm+='
'
hashtagSearchForm+=htmlFooter()
return hashtagSearchForm
def htmlSkillsSearch(translate: {},baseDir: str, \
skillsearch: str,instanceOnly: bool, \
postsPerPage: int) -> str:
"""Show a page containing search results for a skill
"""
if skillsearch.startswith('*'):
skillsearch=skillsearch[1:].strip()
skillsearch=skillsearch.lower().strip('\n')
results=[]
# search instance accounts
for subdir, dirs, files in os.walk(baseDir+'/accounts/'):
for f in files:
if not f.endswith('.json'):
continue
if '@' not in f:
continue
if f.startswith('inbox@'):
continue
actorFilename = os.path.join(subdir, f)
actorJson=loadJson(actorFilename)
if actorJson:
if actorJson.get('id') and \
actorJson.get('skills') and \
actorJson.get('name') and \
actorJson.get('icon'):
actor=actorJson['id']
for skillName,skillLevel in actorJson['skills'].items():
skillName=skillName.lower()
if skillName in skillsearch or skillsearch in skillName:
skillLevelStr=str(skillLevel)
if skillLevel<100:
skillLevelStr='0'+skillLevelStr
if skillLevel<10:
skillLevelStr='0'+skillLevelStr
indexStr=skillLevelStr+';'+actor+';'+actorJson['name']+';'+actorJson['icon']['url']
if indexStr not in results:
results.append(indexStr)
if not instanceOnly:
# search actor cache
for subdir, dirs, files in os.walk(baseDir+'/cache/actors/'):
for f in files:
if not f.endswith('.json'):
continue
if '@' not in f:
continue
if f.startswith('inbox@'):
continue
actorFilename = os.path.join(subdir, f)
cachedActorJson=loadJson(actorFilename)
if cachedActorJson:
if cachedActorJson.get('actor'):
actorJson=cachedActorJson['actor']
if actorJson.get('id') and \
actorJson.get('skills') and \
actorJson.get('name') and \
actorJson.get('icon'):
actor=actorJson['id']
for skillName,skillLevel in actorJson['skills'].items():
skillName=skillName.lower()
if skillName in skillsearch or skillsearch in skillName:
skillLevelStr=str(skillLevel)
if skillLevel<100:
skillLevelStr='0'+skillLevelStr
if skillLevel<10:
skillLevelStr='0'+skillLevelStr
indexStr=skillLevelStr+';'+actor+';'+actorJson['name']+';'+actorJson['icon']['url']
if indexStr not in results:
results.append(indexStr)
results.sort(reverse=True)
cssFilename=baseDir+'/epicyon-profile.css'
if os.path.isfile(baseDir+'/epicyon.css'):
cssFilename=baseDir+'/epicyon.css'
with open(cssFilename, 'r') as cssFile:
skillSearchCSS = cssFile.read()
skillSearchForm=htmlHeader(cssFilename,skillSearchCSS)
skillSearchForm+='
'+translate['Skills search']+': '+skillsearch+'
'
if len(results)==0:
skillSearchForm+='
'+translate['No results']+'
'
else:
skillSearchForm+='
'
ctr=0
for skillMatch in results:
skillMatchFields=skillMatch.split(';')
if len(skillMatchFields)==4:
actor=skillMatchFields[1]
actorName=skillMatchFields[2]
avatarUrl=skillMatchFields[3]
skillSearchForm+='
'
ctr+=1
if ctr>=postsPerPage:
break
skillSearchForm+='
'
skillSearchForm+=htmlFooter()
return skillSearchForm
def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int) -> str:
"""Shows the edit profile screen
"""
imageFormats='.png, .jpg, .jpeg, .gif, .webp'
pathOriginal=path
path=path.replace('/inbox','').replace('/outbox','').replace('/shares','')
nickname=getNicknameFromActor(path)
if not nickname:
return ''
domainFull=domain
if port:
if port!=80 and port!=443:
if ':' not in domain:
domainFull=domain+':'+str(port)
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if not os.path.isfile(actorFilename):
return ''
isBot=''
isGroup=''
followDMs=''
displayNickname=nickname
bioStr=''
donateUrl=''
manuallyApprovesFollowers=''
actorJson=loadJson(actorFilename)
if actorJson:
donateUrl=getDonationUrl(actorJson)
if actorJson.get('name'):
displayNickname=actorJson['name']
if actorJson.get('summary'):
bioStr=actorJson['summary'].replace('
','').replace('
','')
if actorJson.get('manuallyApprovesFollowers'):
if actorJson['manuallyApprovesFollowers']:
manuallyApprovesFollowers='checked'
else:
manuallyApprovesFollowers=''
if actorJson.get('type'):
if actorJson['type']=='Service':
isBot='checked'
isGroup=''
elif actorJson['type']=='Group':
isGroup='checked'
isBot=''
if os.path.isfile(baseDir+'/accounts/'+nickname+'@'+domain+'/.followDMs'):
followDMs='checked'
filterStr=''
filterFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/filters.txt'
if os.path.isfile(filterFilename):
with open(filterFilename, 'r') as filterfile:
filterStr=filterfile.read()
blockedStr=''
blockedFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt'
if os.path.isfile(blockedFilename):
with open(blockedFilename, 'r') as blockedfile:
blockedStr=blockedfile.read()
allowedInstancesStr=''
allowedInstancesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/allowedinstances.txt'
if os.path.isfile(allowedInstancesFilename):
with open(allowedInstancesFilename, 'r') as allowedInstancesFile:
allowedInstancesStr=allowedInstancesFile.read()
skills=getSkills(baseDir,nickname,domain)
skillsStr=''
skillCtr=1
if skills:
for skillDesc,skillValue in skills.items():
skillsStr+='
'
skillsStr+='
'
skillCtr+=1
skillsStr+='
'
skillsStr+='
' \
cssFilename=baseDir+'/epicyon-profile.css'
if os.path.isfile(baseDir+'/epicyon.css'):
cssFilename=baseDir+'/epicyon.css'
with open(cssFilename, 'r') as cssFile:
editProfileCSS = cssFile.read()
instanceStr=''
moderatorsStr=''
adminNickname=getConfigParam(baseDir,'admin')
if path.startswith('/users/'+adminNickname+'/'):
instanceDescription=getConfigParam(baseDir,'instanceDescription')
instanceDescriptionShort=getConfigParam(baseDir,'instanceDescriptionShort')
instanceTitle=getConfigParam(baseDir,'instanceTitle')
instanceStr='
'
moderators=''
moderatorsFile=baseDir+'/accounts/moderators.txt'
if os.path.isfile(moderatorsFile):
with open(moderatorsFile, "r") as f:
moderators = f.read()
moderatorsStr='
'
moderatorsStr+=' '+translate['Moderators']+' '
moderatorsStr+=' '+translate['A list of moderator nicknames. One per line.']
moderatorsStr+=' '
moderatorsStr+='
'
editProfileForm=htmlHeader(cssFilename,editProfileCSS)
editProfileForm+=''
editProfileForm+=htmlFooter()
return editProfileForm
def htmlGetLoginCredentials(loginParams: str,lastLoginTime: int) -> (str,str,bool):
"""Receives login credentials via HTTPServer POST
"""
if not loginParams.startswith('username='):
return None,None,None
# minimum time between login attempts
currTime=int(time.time())
if currTime str:
"""Shows the login screen
"""
accounts=noOfAccounts(baseDir)
loginImage='login.png'
loginImageFilename=None
if os.path.isfile(baseDir+'/accounts/'+loginImage):
loginImageFilename=baseDir+'/accounts/'+loginImage
if os.path.isfile(baseDir+'/accounts/login.jpg'):
loginImage='login.jpg'
loginImageFilename=baseDir+'/accounts/'+loginImage
if os.path.isfile(baseDir+'/accounts/login.jpeg'):
loginImage='login.jpeg'
loginImageFilename=baseDir+'/accounts/'+loginImage
if os.path.isfile(baseDir+'/accounts/login.gif'):
loginImage='login.gif'
loginImageFilename=baseDir+'/accounts/'+loginImage
if os.path.isfile(baseDir+'/accounts/login.webp'):
loginImage='login.webp'
loginImageFilename=baseDir+'/accounts/'+loginImage
if not loginImageFilename:
loginImageFilename=baseDir+'/accounts/'+loginImage
copyfile(baseDir+'/img/login.png',loginImageFilename)
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')
if accounts>0:
loginText='
'+translate['Welcome. Please enter your login details below.']+'
'
else:
loginText='
'+translate['Please enter some credentials']+'
'
loginText+='
'+translate['You will become the admin of this site.']+'
'
if os.path.isfile(baseDir+'/accounts/login.txt'):
# custom login message
with open(baseDir+'/accounts/login.txt', 'r') as file:
loginText = '
'+file.read()+'
'
cssFilename=baseDir+'/epicyon-login.css'
if os.path.isfile(baseDir+'/login.css'):
cssFilename=baseDir+'/login.css'
with open(cssFilename, 'r') as cssFile:
loginCSS = cssFile.read()
# show the register button
registerButtonStr=''
if getConfigParam(baseDir,'registration')=='open':
if int(getConfigParam(baseDir,'registrationsRemaining'))>0:
if accounts>0:
loginText='
'+translate['Welcome. Please login or register a new account.']+'
'
loginButtonStr=''
if accounts>0:
loginButtonStr=''
autocompleteStr=''
if not autocomplete:
autocompleteStr='autocomplete="off" value=""'
loginForm=htmlHeader(cssFilename,loginCSS)
loginForm+=''
loginForm+=''
loginForm+=htmlFooter()
return loginForm
def htmlTermsOfService(baseDir: str,httpPrefix: str,domainFull: str) -> str:
"""Show the terms of service screen
"""
adminNickname = getConfigParam(baseDir,'admin')
if not os.path.isfile(baseDir+'/accounts/tos.txt'):
copyfile(baseDir+'/default_tos.txt',baseDir+'/accounts/tos.txt')
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')
TOSText='Terms of Service go here.'
if os.path.isfile(baseDir+'/accounts/tos.txt'):
with open(baseDir+'/accounts/tos.txt', 'r') as file:
TOSText = file.read()
TOSForm=''
cssFilename=baseDir+'/epicyon-profile.css'
if os.path.isfile(baseDir+'/epicyon.css'):
cssFilename=baseDir+'/epicyon.css'
with open(cssFilename, 'r') as cssFile:
termsCSS = cssFile.read()
TOSForm=htmlHeader(cssFilename,termsCSS)
TOSForm+='
'+TOSText+'
'
if adminNickname:
adminActor=httpPrefix+'://'+domainFull+'/users/'+adminNickname
TOSForm+='
'
TOSForm+=htmlFooter()
return TOSForm
def htmlAbout(baseDir: str,httpPrefix: str,domainFull: str) -> str:
"""Show the about screen
"""
adminNickname = getConfigParam(baseDir,'admin')
if not os.path.isfile(baseDir+'/accounts/about.txt'):
copyfile(baseDir+'/default_about.txt',baseDir+'/accounts/about.txt')
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')
aboutText='Information about this instance goes here.'
if os.path.isfile(baseDir+'/accounts/about.txt'):
with open(baseDir+'/accounts/about.txt', 'r') as file:
aboutText = file.read()
aboutForm=''
cssFilename=baseDir+'/epicyon-profile.css'
if os.path.isfile(baseDir+'/epicyon.css'):
cssFilename=baseDir+'/epicyon.css'
with open(cssFilename, 'r') as cssFile:
termsCSS = cssFile.read()
aboutForm=htmlHeader(cssFilename,termsCSS)
aboutForm+='
'+aboutText+'
'
if adminNickname:
adminActor=httpPrefix+'://'+domainFull+'/users/'+adminNickname
aboutForm+='
'
aboutForm+=htmlFooter()
return aboutForm
def htmlHashtagBlocked(baseDir: str) -> 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'
with open(cssFilename, 'r') as cssFile:
blockedHashtagCSS=cssFile.read()
blockedHashtagForm=htmlHeader(cssFilename,blockedHashtagCSS)
blockedHashtagForm+='
'
# custom report header with any additional instructions
if os.path.isfile(baseDir+'/accounts/report.txt'):
with open(baseDir+'/accounts/report.txt', 'r') as file:
customReportText=file.read()
if '' not in customReportText:
customReportText='
'+customReportText+'
'
customReportText=customReportText.replace('
','
')
newPostText+=customReportText
newPostText+='
'+translate['This message only goes to moderators, even if it mentions other fediverse addresses.']+'
'+translate['Enter the details for your shared item below.']+'
'
if os.path.isfile(baseDir+'/accounts/newpost.txt'):
with open(baseDir+'/accounts/newpost.txt', 'r') as file:
newPostText = '
'+file.read()+'
'
cssFilename=baseDir+'/epicyon-profile.css'
if os.path.isfile(baseDir+'/epicyon.css'):
cssFilename=baseDir+'/epicyon.css'
with open(cssFilename, 'r') as cssFile:
newPostCSS = cssFile.read()
if '?' in path:
path=path.split('?')[0]
pathBase=path.replace('/newreport','').replace('/newpost','').replace('/newshare','').replace('/newunlisted','').replace('/newfollowers','').replace('/newdm','')
scopeIcon='scope_public.png'
scopeDescription=translate['Public']
placeholderSubject=translate['Subject or Content Warning (optional)']+'...'
placeholderMessage=translate['Write something']+'...'
extraFields=''
endpoint='newpost'
if path.endswith('/newunlisted'):
scopeIcon='scope_unlisted.png'
scopeDescription=translate['Unlisted']
endpoint='newunlisted'
if path.endswith('/newfollowers'):
scopeIcon='scope_followers.png'
scopeDescription=translate['Followers']
endpoint='newfollowers'
if path.endswith('/newdm'):
scopeIcon='scope_dm.png'
scopeDescription=translate['DM']
endpoint='newdm'
if path.endswith('/newreport'):
scopeIcon='scope_report.png'
scopeDescription=translate['Report']
endpoint='newreport'
if path.endswith('/newshare'):
scopeIcon='scope_share.png'
scopeDescription=translate['Shared Item']
placeholderSubject=translate['Name of the shared item']+'...'
placeholderMessage=translate['Description of the item being shared']+'...'
endpoint='newshare'
extraFields='
'
newPostForm=htmlHeader(cssFilename,newPostCSS)
# only show the share option if this is not a reply
shareOptionOnDropdown=''
if not replyStr:
shareOptionOnDropdown='Share '+translate['Describe a shared item']+''
mentionsStr=''
for m in mentions:
mentionNickname=getNicknameFromActor(m)
if not mentionNickname:
continue
mentionDomain,mentionPort=getDomainFromActor(m)
if not mentionDomain:
continue
if mentionPort:
mentionsHandle='@'+mentionNickname+'@'+mentionDomain+':'+str(mentionPort)
else:
mentionsHandle='@'+mentionNickname+'@'+mentionDomain
if mentionsHandle not in mentionsStr:
mentionsStr+=mentionsHandle+' '
# build suffixes so that any replies or mentions are preserved when switching between scopes
dropdownNewPostSuffix='/newpost'
dropdownUnlistedSuffix='/newunlisted'
dropdownFollowersSuffix='/newfollowers'
dropdownDMSuffix='/newdm'
dropdownReportSuffix='/newreport'
if inReplyTo or mentions:
dropdownNewPostSuffix=''
dropdownUnlistedSuffix=''
dropdownFollowersSuffix=''
dropdownDMSuffix=''
dropdownReportSuffix=''
if inReplyTo:
dropdownNewPostSuffix+='?replyto='+inReplyTo
dropdownUnlistedSuffix+='?replyto='+inReplyTo
dropdownFollowersSuffix+='?replyfollowers='+inReplyTo
dropdownDMSuffix+='?replydm='+inReplyTo
for mentionedActor in mentions:
dropdownNewPostSuffix+='?mention='+mentionedActor
dropdownUnlistedSuffix+='?mention='+mentionedActor
dropdownFollowersSuffix+='?mention='+mentionedActor
dropdownDMSuffix+='?mention='+mentionedActor
dropdownReportSuffix+='?mention='+mentionedActor
dropDownContent=''
if not reportUrl:
dropDownContent+='
'
else:
mentionsStr='Re: '+reportUrl+'\n\n'+mentionsStr
newPostForm+=''
if not reportUrl:
newPostForm+=''
newPostForm=newPostForm.replace('','')
newPostForm+=htmlFooter()
return newPostForm
def htmlHeader(cssFilename: str,css=None,refreshSec=0,lang='en') -> str:
if refreshSec==0:
meta=' \n'
else:
meta=' \n'
if not css:
if '/' in cssFilename:
cssFilename=cssFilename.split('/')[-1]
htmlStr='\n'
htmlStr+='\n'
htmlStr+=meta
htmlStr+=' \n'
htmlStr+=' \n'
else:
htmlStr='\n'
htmlStr+='\n'
htmlStr+=meta
htmlStr+=' \n'
htmlStr+=' \n'
return htmlStr
def htmlFooter() -> str:
htmlStr=' \n'
htmlStr+='\n'
return htmlStr
def htmlProfilePosts(translate: {}, \
baseDir: str,httpPrefix: str, \
authorized: bool,ocapAlways: bool, \
nickname: str,domain: str,port: int, \
session,wfRequest: {},personCache: {}, \
projectVersion: str) -> str:
"""Shows posts on the profile screen
These should only be public posts
"""
iconsDir=getIconsDir(baseDir)
profileStr=''
maxItems=4
profileStr+=''
ctr=0
currPage=1
while ctr=maxItems:
break
currPage+=1
return profileStr
def htmlProfileFollowing(translate: {},baseDir: str,httpPrefix: str, \
authorized: bool,ocapAlways: bool, \
nickname: str,domain: str,port: int, \
session,wfRequest: {},personCache: {}, \
followingJson: {},projectVersion: str, \
buttons: [], \
feedName: str,actor: str, \
pageNumber: int, \
maxItemsPerPage: int) -> str:
"""Shows following on the profile screen
"""
profileStr=''
iconsDir=getIconsDir(baseDir)
if authorized and pageNumber:
if authorized and pageNumber>1:
# page up arrow
profileStr+= \
'
'
for item in followingJson['orderedItems']:
profileStr+= \
individualFollowAsHtml(translate,baseDir,session, \
wfRequest,personCache, \
domain,item,authorized,nickname, \
httpPrefix,projectVersion, \
buttons)
if authorized and maxItemsPerPage and pageNumber:
if len(followingJson['orderedItems'])>=maxItemsPerPage:
# page down arrow
profileStr+= \
'
'
return profileStr
def htmlProfileRoles(translate: {},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(translate: {},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='
'+profileStr+'
'
return profileStr
def htmlIndividualShare(actor: str,item: {},translate: {},showContact: bool,removeButton: bool) -> str:
"""Returns an individual shared item as html
"""
profileStr='
'
profileStr+='
'+item['displayName']+'
'
if item.get('imageUrl'):
profileStr+=''
profileStr+=''
profileStr+='
'
if showContact:
contactActor=item['actor']
profileStr+='
'
if removeButton:
profileStr+=' '
profileStr+='
'
return profileStr
def htmlProfileShares(actor: str,translate: {},nickname: str,domain: str,sharesJson: {}) -> str:
"""Shows shares on the profile screen
"""
profileStr=''
for item in sharesJson['orderedItems']:
profileStr+=htmlIndividualShare(actor,item,translate,False,False)
if len(profileStr)>0:
profileStr='
'+profileStr+'
'
return profileStr
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:
if '@' in handle:
accountDir=baseDir+'/accounts/'+handle
sharesFilename=accountDir+'/shares.json'
if os.path.isfile(sharesFilename):
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
# 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=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 str:
"""Show shared items timeline as html
"""
sharesJson,lastPage= \
sharesTimelineJson(actor,pageNumber,itemsPerPage, \
baseDir,maxSharesPerAccount)
domainFull=domain
if port!=80 and port!=443:
if ':' not in domain:
domainFull=domain+':'+str(port)
actor=httpPrefix+'://'+domainFull+'/users/'+nickname
timelineStr=''
if pageNumber>1:
timelineStr+='
'
for published,item in sharesJson.items():
showContactButton=False
if item['actor']!=actor:
showContactButton=True
showRemoveButton=False
if item['actor']==actor:
showRemoveButton=True
timelineStr+=htmlIndividualShare(actor,item,translate,showContactButton,showRemoveButton)
if not lastPage:
timelineStr+='
\n'
if not authorized:
loginButton=' '
else:
editProfileStr=''
logoutStr=''
linkToTimelineStart=''
linkToTimelineEnd=''
# are there any follow requests?
followRequestsFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/followrequests.txt'
if os.path.isfile(followRequestsFilename):
with open(followRequestsFilename,'r') as f:
for line in f:
if len(line)>0:
followApprovals=True
followersButton='buttonhighlighted'
if selected=='followers':
followersButton='buttonselectedhighlighted'
break
if selected=='followers':
if followApprovals:
with open(followRequestsFilename,'r') as f:
for followerHandle in f:
if len(line)>0:
if '://' in followerHandle:
followerActor=followerHandle
else:
followerActor=httpPrefix+'://'+followerHandle.split('@')[1]+'/users/'+followerHandle.split('@')[0]
basePath=httpPrefix+'://'+domainFull+'/users/'+nickname
followApprovalsSection+='
'
profileDescriptionShort=profileDescription
if '\n' in profileDescription:
if len(profileDescription.split('\n'))>2:
profileDescriptionShort=''
else:
if ' ' in profileDescription:
if len(profileDescription.split(' '))>2:
profileDescriptionShort=''
profileDescription=profileDescription.replace(' ','\n')
# keep the profile description short
if len(profileDescriptionShort)>256:
profileDescriptionShort=''
# remove formatting from profile description used on title
avatarDescription=''
if profileJson.get('summary'):
avatarDescription=profileJson['summary'].replace(' ','\n').replace('
\n'
return resultStr
def clickToDropDownScript() -> str:
"""Function run onclick to create a dropdown
"""
script='function dropdown() {\n'
script+=' document.getElementById("myDropdown").classList.toggle("show");\n'
script+='}\n'
return script
def cursorToEndOfMessageScript() -> str:
"""Moves the cursor to the end of the text in a textarea
This avoids the cursor being in the wrong position when replying
"""
script='function focusOnMessage() {\n'
script+=" var replyTextArea = document.getElementById('message');\n"
script+=' val = replyTextArea.value;\n'
script+=' if ((val.length>0) && (val.charAt(val.length-1) != " ")) {\n'
script+=' val += " ";\n'
script+=' }\n'
script+=' replyTextArea.focus();\n'
script+=' replyTextArea.value="";\n'
script+=' replyTextArea.value=val;\n'
script+='}\n'
script+="var replyTextArea = document.getElementById('message')\n"
script+='replyTextArea.onFocus = function() {\n'
script+=' focusOnMessage();'
script+='}\n'
return script
def contentWarningScript() -> str:
"""Returns a script used for content warnings
"""
script='function showContentWarning(postID) {\n'
script+=' var x = document.getElementById(postID);\n'
script+=' if (x.style.display !== "block") {\n'
script+=' x.style.display = "block";\n'
script+=' } else {\n'
script+=' x.style.display = "none";\n'
script+=' }\n'
script+='}\n'
return script
def addEmbeddedAudio(translate: {},content: str) -> str:
"""Adds embedded audio for mp3/ogg
"""
if not ('.mp3' in content or '.ogg' in content):
return content
if '