__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
import commentjson
from datetime import datetime
from dateutil.parser import parse
from shutil import copyfile
from pprint import pprint
from person import personBoxJson
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 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 session import getJson
from auth import createPassword
from like import likedByPerson
from like import noOfLikes
from announce import announcedByPerson
from blocking import isBlocked
from content import getMentionsFromHtml
from config import getConfigParam
from skills import getSkills
from cache import getPersonFromCache
def getPersonAvatarUrl(baseDir: str,personUrl: str,personCache: {}) -> str:
"""Returns the avatar url for the person
"""
personJson = getPersonFromCache(baseDir,personUrl,personCache)
if personJson:
if personJson.get('icon'):
if personJson['icon'].get('url'):
return personJson['icon']['url']
return None
def htmlSearchEmoji(baseDir: str,searchStr: str) -> str:
"""Search results for emoji
"""
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')
with open(baseDir+'/epicyon-profile.css', 'r') as cssFile:
emojiCSS=cssFile.read()
emojiLookupFilename=baseDir+'/emoji/emoji.json'
# create header
emojiForm=htmlHeader(emojiCSS)
emojiForm+='
Emoji Search
'
# does the lookup file exist?
if not os.path.isfile(emojiLookupFilename):
emojiForm+='
No results
'
emojiForm+=htmlFooter()
return emojiForm
with open(emojiLookupFilename, 'r') as fp:
emojiJson=commentjson.load(fp)
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+='
'
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
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
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+='
No results
'
sharedItemsForm+=htmlFooter()
return sharedItemsForm
def htmlModerationInfo(baseDir: str) -> str:
infoForm=''
with open(baseDir+'/epicyon-profile.css', 'r') as cssFile:
infoCSS=cssFile.read()
infoForm=htmlHeader(infoCSS)
infoForm+='
Moderation Information
'
infoShown=False
suspendedFilename=baseDir+'/accounts/suspended.txt'
if os.path.isfile(suspendedFilename):
with open(suspendedFilename, "r") as f:
suspendedStr = f.read()
infoForm+= \
'
' \
' Suspended accounts' \
' These are currently suspended' \
' ' \
'
'
infoShown=True
blockingFilename=baseDir+'/accounts/blocking.txt'
if os.path.isfile(blockingFilename):
with open(blockingFilename, "r") as f:
blockedStr = f.read()
infoForm+= \
'
' \
' Blocked accounts and hashtags' \
' These are globally blocked for all accounts on this instance' \
' ' \
'
'
infoShown=True
if not infoShown:
infoForm+='
Any blocks or suspensions made by moderators will be shown here.
'
infoForm+=htmlFooter()
return infoForm
def htmlHashtagSearch(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
"""
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()
with open(baseDir+'/epicyon-profile.css', '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(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
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
if not isPublicPost(postJsonObject):
index-=1
continue
hashtagSearchForm+= \
individualPostAsHtml(baseDir,session,wfRequest,personCache, \
nickname,domain,port,postJsonObject, \
None,True,False, \
httpPrefix,projectVersion, \
False,False)
index-=1
if endIndex>0:
# next page link
hashtagSearchForm+='
'
hashtagSearchForm+=htmlFooter()
return hashtagSearchForm
def htmlSkillsSearch(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)
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
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)
with open(actorFilename, 'r') as fp:
cachedActorJson=commentjson.load(fp)
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)
with open(baseDir+'/epicyon-profile.css', 'r') as cssFile:
skillSearchCSS = cssFile.read()
skillSearchForm=htmlHeader(skillSearchCSS)
skillSearchForm+='
Skills search: '+skillsearch+'
'
if len(results)==0:
skillSearchForm+='
No matches
'
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(baseDir: str,path: str,domain: str,port: int) -> str:
"""Shows the edit profile screen
"""
pathOriginal=path
path=path.replace('/inbox','').replace('/outbox','').replace('/shares','')
nickname=getNicknameFromActor(path)
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=''
displayNickname=nickname
bioStr=''
manuallyApprovesFollowers=''
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
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'
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+='
' \
with open(baseDir+'/epicyon-profile.css', 'r') as cssFile:
editProfileCSS = cssFile.read()
moderatorsStr=''
adminNickname=getConfigParam(baseDir,'admin')
if path.startswith('/users/'+adminNickname+'/'):
moderators=''
moderatorsFile=baseDir+'/accounts/moderators.txt'
if os.path.isfile(moderatorsFile):
with open(moderatorsFile, "r") as f:
moderators = f.read()
moderatorsStr= \
'
' \
' Moderators ' \
' A list of moderator nicknames. One per line.' \
' ' \
'
'
editProfileForm=htmlHeader(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)
if not os.path.isfile(baseDir+'/accounts/login.png'):
copyfile(baseDir+'/img/login.png',baseDir+'/accounts/login.png')
if os.path.isfile(baseDir+'/img/login-background.png'):
if not os.path.isfile(baseDir+'/accounts/login-background.png'):
copyfile(baseDir+'/img/login-background.png',baseDir+'/accounts/login-background.png')
if accounts>0:
loginText='
Welcome. Please enter your login details below.
'
else:
loginText='
Please enter some credentials
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()+'
'
with open(baseDir+'/epicyon-login.css', '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='
'
loginButtonStr=''
if accounts>0:
loginButtonStr=''
loginForm=htmlHeader(loginCSS)
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=''
with open(baseDir+'/epicyon-profile.css', 'r') as cssFile:
termsCSS = cssFile.read()
TOSForm=htmlHeader(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=''
with open(baseDir+'/epicyon-profile.css', 'r') as cssFile:
termsCSS = cssFile.read()
aboutForm=htmlHeader(termsCSS)
aboutForm+='
'+aboutText+'
'
if adminNickname:
adminActor=httpPrefix+'://'+domainFull+'/users/'+adminNickname
aboutForm+='
'
suspendedForm+=htmlFooter()
return suspendedForm
def htmlNewPost(baseDir: str,path: str,inReplyTo: str,mentions: [],reportUrl: str) -> str:
"""New post screen
"""
replyStr=''
if not path.endswith('/newshare'):
if not path.endswith('/newreport'):
if not inReplyTo:
newPostText='
'
# 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+='
This message only goes to moderators, even if it mentions other fediverse addresses.
You can also refer to points within the Terms of Service if necessary.
'
else:
newPostText='
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()+'
'
with open(baseDir+'/epicyon-profile.css', '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='Public'
placeholderSubject='Subject or Content Warning (optional)...'
placeholderMessage='Write something...'
extraFields=''
endpoint='newpost'
if path.endswith('/newunlisted'):
scopeIcon='scope_unlisted.png'
scopeDescription='Unlisted'
endpoint='newunlisted'
if path.endswith('/newfollowers'):
scopeIcon='scope_followers.png'
scopeDescription='Followers'
endpoint='newfollowers'
if path.endswith('/newdm'):
scopeIcon='scope_dm.png'
scopeDescription='DM'
endpoint='newdm'
if path.endswith('/newreport'):
scopeIcon='scope_report.png'
scopeDescription='Report'
endpoint='newreport'
if path.endswith('/newshare'):
scopeIcon='scope_share.png'
scopeDescription='Shared Item'
placeholderSubject='Name of the shared item...'
placeholderMessage='Description of the item being shared...'
endpoint='newshare'
extraFields= \
'
' \
' ' \
' ' \
' ' \
'
' \
''
newPostForm=htmlHeader(newPostCSS)
# only show the share option if this is not a reply
shareOptionOnDropdown=''
if not replyStr:
shareOptionOnDropdown='Share 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:
mentionsStr+='@'+mentionNickname+'@'+mentionDomain+':'+str(mentionPort)+' '
else:
mentionsStr+='@'+mentionNickname+'@'+mentionDomain+' '
dropDownContent=''
if not reportUrl:
dropDownContent= \
'
'
return profileStr
def htmlProfile(projectVersion: str, \
baseDir: str,httpPrefix: str,authorized: bool, \
ocapAlways: bool,profileJson: {},selected: str, \
session,wfRequest: {},personCache: {}, \
extraJson=None) -> str:
"""Show the profile page as html
"""
nickname=profileJson['preferredUsername']
if not nickname:
return ""
displayName=profileJson['name']
domain,port=getDomainFromActor(profileJson['id'])
if not domain:
return ""
domainFull=domain
if port:
domainFull=domain+':'+str(port)
profileDescription=profileJson['summary']
postsButton='button'
followingButton='button'
followersButton='button'
rolesButton='button'
skillsButton='button'
sharesButton='button'
if selected=='posts':
postsButton='buttonselected'
elif selected=='following':
followingButton='buttonselected'
elif selected=='followers':
followersButton='buttonselected'
elif selected=='roles':
rolesButton='buttonselected'
elif selected=='skills':
skillsButton='buttonselected'
elif selected=='shares':
sharesButton='buttonselected'
loginButton=''
followApprovalsSection=''
followApprovals=False
linkToTimelineStart=''
linkToTimelineEnd=''
editProfileStr=''
actor=profileJson['id']
if not authorized:
loginButton=' '
else:
editProfileStr=''
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+='
\n'
def clickToDropDownScript() -> str:
"""Function run onclick to create a dropdown
"""
script= \
'function dropdown() {' \
' document.getElementById("myDropdown").classList.toggle("show");' \
'}'
#'window.onclick = function(event) {' \
#" if (!event.target.matches('.dropbtn')) {" \
#' var dropdowns = document.getElementsByClassName("dropdown-content");' \
#' var i;' \
#' for (i = 0; i < dropdowns.length; i++) {' \
#' var openDropdown = dropdowns[i];' \
#" if (openDropdown.classList.contains('show')) {" \
#" openDropdown.classList.remove('show');" \
#' }' \
#' }' \
#' }' \
#'}'
return script
def contentWarningScript() -> str:
"""Returns a script used for content warnings
"""
script= \
'function showContentWarning(postID) {' \
' var x = document.getElementById(postID);' \
' if (x.style.display === "none") {' \
' x.style.display = "block";' \
' } else {' \
' x.style.display = "none";' \
' }' \
'}'
return script
def htmlRemplaceEmojiFromTags(content: str,tag: {}) -> str:
"""Uses the tags to replace :emoji: with html image markup
"""
for tagItem in tag:
if not tagItem.get('type'):
continue
if tagItem['type']!='Emoji':
continue
if not tagItem.get('name'):
continue
if not tagItem.get('icon'):
continue
if not tagItem['icon'].get('url'):
continue
if tagItem['name'] not in content:
continue
emojiHtml=""
content=content.replace(tagItem['name'],emojiHtml)
return content
def addEmbeddedAudio(content: str) -> str:
"""Adds embedded audio for mp3/ogg
"""
if not ('.mp3' in content or '.ogg' in content):
return content
if '