__filename__ = "webapp_profile.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.2.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
__module_group__ = "Web Interface"
import os
from pprint import pprint
from utils import hasObjectDict
from utils import getOccupationName
from utils import getLockedAccount
from utils import getFullDomain
from utils import isArtist
from utils import isDormant
from utils import getNicknameFromActor
from utils import getDomainFromActor
from utils import isSystemAccount
from utils import removeHtml
from utils import loadJson
from utils import getConfigParam
from utils import getImageFormats
from utils import acctDir
from languages import getActorLanguages
from skills import getSkills
from theme import getThemesList
from person import personBoxJson
from person import getActorJson
from person import getPersonAvatarUrl
from webfinger import webfingerHandle
from posts import parseUserFeed
from posts import getPersonBox
from donate import getDonationUrl
from xmpp import getXmppAddress
from matrix import getMatrixAddress
from ssb import getSSBAddress
from pgp import getEmailAddress
from pgp import getPGPfingerprint
from pgp import getPGPpubKey
from tox import getToxAddress
from briar import getBriarAddress
from jami import getJamiAddress
from cwtch import getCwtchAddress
from filters import isFiltered
from follow import isFollowerOfPerson
from webapp_frontscreen import htmlFrontScreen
from webapp_utils import htmlKeyboardNavigation
from webapp_utils import htmlHideFromScreenReader
from webapp_utils import scheduledPostsExist
from webapp_utils import htmlHeaderWithExternalStyle
from webapp_utils import htmlHeaderWithPersonMarkup
from webapp_utils import htmlFooter
from webapp_utils import addEmojiToDisplayName
from webapp_utils import getBannerFile
from webapp_utils import htmlPostSeparator
from webapp_utils import editCheckBox
from webapp_utils import editTextField
from webapp_utils import editTextArea
from webapp_utils import beginEditSection
from webapp_utils import endEditSection
from blog import getBlogAddress
from webapp_post import individualPostAsHtml
from webapp_timeline import htmlIndividualShare
def htmlProfileAfterSearch(cssCache: {},
                           recentPostsCache: {}, maxRecentPosts: int,
                           translate: {},
                           baseDir: str, path: str, httpPrefix: str,
                           nickname: str, domain: str, port: int,
                           profileHandle: str,
                           session, cachedWebfingers: {}, personCache: {},
                           debug: bool, projectVersion: str,
                           YTReplacementDomain: str,
                           showPublishedDateOnly: bool,
                           defaultTimeline: str,
                           peertubeInstances: [],
                           allowLocalNetworkAccess: bool,
                           themeName: str,
                           accessKeys: {},
                           systemLanguage: str) -> str:
    """Show a profile page after a search for a fediverse address
    """
    http = False
    gnunet = False
    if httpPrefix == 'http':
        http = True
    elif httpPrefix == 'gnunet':
        gnunet = True
    profileJson, asHeader = \
        getActorJson(domain, profileHandle, http, gnunet, debug, False)
    if not profileJson:
        return None
    personUrl = profileJson['id']
    searchDomain, searchPort = getDomainFromActor(personUrl)
    searchNickname = getNicknameFromActor(personUrl)
    searchDomainFull = getFullDomain(searchDomain, searchPort)
    profileStr = ''
    cssFilename = baseDir + '/epicyon-profile.css'
    if os.path.isfile(baseDir + '/epicyon.css'):
        cssFilename = baseDir + '/epicyon.css'
    avatarUrl = ''
    if profileJson.get('icon'):
        if profileJson['icon'].get('url'):
            avatarUrl = profileJson['icon']['url']
    if not avatarUrl:
        avatarUrl = getPersonAvatarUrl(baseDir, personUrl,
                                       personCache, True)
    displayName = searchNickname
    if profileJson.get('name'):
        displayName = profileJson['name']
    lockedAccount = getLockedAccount(profileJson)
    if lockedAccount:
        displayName += '🔒'
    movedTo = ''
    if profileJson.get('movedTo'):
        movedTo = profileJson['movedTo']
        displayName += ' ⌂'
    followsYou = \
        isFollowerOfPerson(baseDir,
                           nickname, domain,
                           searchNickname,
                           searchDomainFull)
    profileDescription = ''
    if profileJson.get('summary'):
        profileDescription = profileJson['summary']
    outboxUrl = None
    if not profileJson.get('outbox'):
        if debug:
            pprint(profileJson)
            print('DEBUG: No outbox found')
        return None
    outboxUrl = profileJson['outbox']
    # profileBackgroundImage = ''
    # if profileJson.get('image'):
    #     if profileJson['image'].get('url'):
    #         profileBackgroundImage = profileJson['image']['url']
    # url to return to
    backUrl = path
    if not backUrl.endswith('/inbox'):
        backUrl += '/inbox'
    profileDescriptionShort = profileDescription
    if '\n' in profileDescription:
        if len(profileDescription.split('\n')) > 2:
            profileDescriptionShort = ''
    else:
        if '
', '')
            avatarDescription = avatarDescription.replace('
', '')
            if '<' in avatarDescription:
                avatarDescription = removeHtml(avatarDescription)
    imageUrl = ''
    if profileJson.get('image'):
        if profileJson['image'].get('url'):
            imageUrl = profileJson['image']['url']
    alsoKnownAs = None
    if profileJson.get('alsoKnownAs'):
        alsoKnownAs = profileJson['alsoKnownAs']
    joinedDate = None
    if profileJson.get('published'):
        if 'T' in profileJson['published']:
            joinedDate = profileJson['published']
    profileStr = \
        _getProfileHeaderAfterSearch(baseDir,
                                     nickname, defaultTimeline,
                                     searchNickname,
                                     searchDomainFull,
                                     translate,
                                     displayName, followsYou,
                                     profileDescriptionShort,
                                     avatarUrl, imageUrl,
                                     movedTo, profileJson['id'],
                                     alsoKnownAs, accessKeys,
                                     joinedDate)
    domainFull = getFullDomain(domain, port)
    followIsPermitted = True
    if searchNickname == 'news' and searchDomainFull == domainFull:
        # currently the news actor is not something you can follow
        followIsPermitted = False
    elif searchNickname == nickname and searchDomainFull == domainFull:
        # don't follow yourself!
        followIsPermitted = False
    if followIsPermitted:
        profileStr += \
            '\n' + \
            '  \n' + \
            '
\n'
    i = 0
    for item in parseUserFeed(session, outboxUrl, asHeader,
                              projectVersion, httpPrefix, domain):
        if not item.get('actor'):
            continue
        if item['actor'] != personUrl:
            continue
        if not item.get('type'):
            continue
        if item['type'] != 'Create':
            continue
        if not hasObjectDict(item):
            continue
        profileStr += \
            individualPostAsHtml(True, recentPostsCache, maxRecentPosts,
                                 translate, None, baseDir,
                                 session, cachedWebfingers, personCache,
                                 nickname, domain, port,
                                 item, avatarUrl, False, False,
                                 httpPrefix, projectVersion, 'inbox',
                                 YTReplacementDomain,
                                 showPublishedDateOnly,
                                 peertubeInstances, allowLocalNetworkAccess,
                                 themeName, systemLanguage,
                                 False, False, False, False, False)
        i += 1
        if i >= 20:
            break
    instanceTitle = \
        getConfigParam(baseDir, 'instanceTitle')
    return htmlHeaderWithExternalStyle(cssFilename, instanceTitle) + \
        profileStr + htmlFooter()
def _getProfileHeader(baseDir: str, httpPrefix: str,
                      nickname: str, domain: str,
                      domainFull: str, translate: {},
                      defaultTimeline: str,
                      displayName: str,
                      avatarDescription: str,
                      profileDescriptionShort: str,
                      loginButton: str, avatarUrl: str,
                      theme: str, movedTo: str,
                      alsoKnownAs: [],
                      pinnedContent: str,
                      accessKeys: {},
                      joinedDate: str,
                      occupationName: str) -> str:
    """The header of the profile screen, containing background
    image and avatar
    """
    htmlStr = \
        '\n\n    \n\n'
    return htmlStr
def _getProfileHeaderAfterSearch(baseDir: str,
                                 nickname: str, defaultTimeline: str,
                                 searchNickname: str,
                                 searchDomainFull: str,
                                 translate: {},
                                 displayName: str,
                                 followsYou: bool,
                                 profileDescriptionShort: str,
                                 avatarUrl: str, imageUrl: str,
                                 movedTo: str, actor: str,
                                 alsoKnownAs: [],
                                 accessKeys: {},
                                 joinedDate: str) -> str:
    """The header of a searched for handle, containing background
    image and avatar
    """
    htmlStr = \
        '\n\n    \n\n'
    return htmlStr
def htmlProfile(rssIconAtTop: bool,
                cssCache: {}, iconsAsButtons: bool,
                defaultTimeline: str,
                recentPostsCache: {}, maxRecentPosts: int,
                translate: {}, projectVersion: str,
                baseDir: str, httpPrefix: str, authorized: bool,
                profileJson: {}, selected: str,
                session, cachedWebfingers: {}, personCache: {},
                YTReplacementDomain: str,
                showPublishedDateOnly: bool,
                newswire: {}, theme: str, dormantMonths: int,
                peertubeInstances: [],
                allowLocalNetworkAccess: bool,
                textModeBanner: str,
                debug: bool, accessKeys: {}, city: str,
                systemLanguage: str,
                extraJson: {} = None, pageNumber: int = None,
                maxItemsPerPage: int = None) -> str:
    """Show the profile page as html
    """
    nickname = profileJson['preferredUsername']
    if not nickname:
        return ""
    if isSystemAccount(nickname):
        return htmlFrontScreen(rssIconAtTop,
                               cssCache, iconsAsButtons,
                               defaultTimeline,
                               recentPostsCache, maxRecentPosts,
                               translate, projectVersion,
                               baseDir, httpPrefix, authorized,
                               profileJson, selected,
                               session, cachedWebfingers, personCache,
                               YTReplacementDomain,
                               showPublishedDateOnly,
                               newswire, theme, extraJson,
                               allowLocalNetworkAccess, accessKeys,
                               systemLanguage,
                               pageNumber, maxItemsPerPage)
    domain, port = getDomainFromActor(profileJson['id'])
    if not domain:
        return ""
    displayName = \
        addEmojiToDisplayName(baseDir, httpPrefix,
                              nickname, domain,
                              profileJson['name'], True)
    domainFull = getFullDomain(domain, port)
    profileDescription = \
        addEmojiToDisplayName(baseDir, httpPrefix,
                              nickname, domain,
                              profileJson['summary'], False)
    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
    editProfileStr = ''
    logoutStr = ''
    actor = profileJson['id']
    usersPath = '/users/' + actor.split('/users/')[1]
    donateSection = ''
    donateUrl = getDonationUrl(profileJson)
    PGPpubKey = getPGPpubKey(profileJson)
    PGPfingerprint = getPGPfingerprint(profileJson)
    emailAddress = getEmailAddress(profileJson)
    xmppAddress = getXmppAddress(profileJson)
    matrixAddress = getMatrixAddress(profileJson)
    ssbAddress = getSSBAddress(profileJson)
    toxAddress = getToxAddress(profileJson)
    briarAddress = getBriarAddress(profileJson)
    jamiAddress = getJamiAddress(profileJson)
    cwtchAddress = getCwtchAddress(profileJson)
    if donateUrl or xmppAddress or matrixAddress or \
       ssbAddress or toxAddress or briarAddress or \
       jamiAddress or cwtchAddress or PGPpubKey or \
       PGPfingerprint or emailAddress:
        donateSection = '\n'
        donateSection += '  
\n'
        if donateUrl and not isSystemAccount(nickname):
            donateSection += \
                '    ' + translate['Donate'] + \
                ' 
\n'
        if emailAddress:
            donateSection += \
                '' + translate['Email'] + ': ' + emailAddress + ' 
\n'
        if xmppAddress:
            donateSection += \
                '' + translate['XMPP'] + ': ' + xmppAddress + ' 
\n'
        if matrixAddress:
            donateSection += \
                '' + translate['Matrix'] + ': ' + matrixAddress + '
\n'
        if ssbAddress:
            donateSection += \
                'SSB: ' + \
                ssbAddress + ' 
\n'
        if toxAddress:
            donateSection += \
                'Tox: ' + \
                toxAddress + ' 
\n'
        if briarAddress:
            if briarAddress.startswith('briar://'):
                donateSection += \
                    '' + \
                    briarAddress + ' 
\n'
            else:
                donateSection += \
                    'briar://' + \
                    briarAddress + ' 
\n'
        if jamiAddress:
            donateSection += \
                'Jami: ' + \
                jamiAddress + ' 
\n'
        if cwtchAddress:
            donateSection += \
                'Cwtch: ' + \
                cwtchAddress + ' 
\n'
        if PGPfingerprint:
            donateSection += \
                'PGP: ' + \
                PGPfingerprint.replace('\n', '
\n'
        if PGPpubKey:
            donateSection += \
                '' + PGPpubKey.replace('\n', '
\n'
        donateSection += '   \n'
        donateSection += '
' + \
            ' \n'
        logoutStr = \
            '' + \
            ' \n'
        # are there any follow requests?
        followRequestsFilename = \
            acctDir(baseDir, 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 = '/users/' + nickname
                            followApprovalsSection += ''
    profileDescriptionShort = profileDescription
    if '\n' in profileDescription:
        if len(profileDescription.split('\n')) > 2:
            profileDescriptionShort = ''
    else:
        if '', '')
        avatarDescription = avatarDescription.replace('
', '')
    movedTo = ''
    if profileJson.get('movedTo'):
        movedTo = profileJson['movedTo']
    alsoKnownAs = None
    if profileJson.get('alsoKnownAs'):
        alsoKnownAs = profileJson['alsoKnownAs']
    joinedDate = None
    if profileJson.get('published'):
        if 'T' in profileJson['published']:
            joinedDate = profileJson['published']
    occupationName = None
    if profileJson.get('hasOccupation'):
        occupationName = getOccupationName(profileJson)
    avatarUrl = profileJson['icon']['url']
    # use alternate path for local avatars to avoid any caching issues
    if '://' + domainFull + '/accounts/avatars/' in avatarUrl:
        avatarUrl = \
            avatarUrl.replace('://' + domainFull + '/accounts/avatars/',
                              '://' + domainFull + '/users/')
    # get pinned post content
    accountDir = acctDir(baseDir, nickname, domain)
    pinnedFilename = accountDir + '/pinToProfile.txt'
    pinnedContent = None
    if os.path.isfile(pinnedFilename):
        with open(pinnedFilename, 'r') as pinFile:
            pinnedContent = pinFile.read()
    profileHeaderStr = \
        _getProfileHeader(baseDir, httpPrefix,
                          nickname, domain,
                          domainFull, translate,
                          defaultTimeline, displayName,
                          avatarDescription,
                          profileDescriptionShort,
                          loginButton, avatarUrl, theme,
                          movedTo, alsoKnownAs,
                          pinnedContent, accessKeys,
                          joinedDate, occupationName)
    # keyboard navigation
    userPathStr = '/users/' + nickname
    deft = defaultTimeline
    menuTimeline = \
        htmlHideFromScreenReader('🏠') + ' ' + \
        translate['Switch to timeline view']
    menuEdit = \
        htmlHideFromScreenReader('✍') + ' ' + translate['Edit']
    menuFollowing = \
        htmlHideFromScreenReader('👥') + ' ' + translate['Following']
    menuFollowers = \
        htmlHideFromScreenReader('👪') + ' ' + translate['Followers']
    menuRoles = \
        htmlHideFromScreenReader('🤚') + ' ' + translate['Roles']
    menuSkills = \
        htmlHideFromScreenReader('🛠') + ' ' + translate['Skills']
    menuShares = \
        htmlHideFromScreenReader('🤝') + ' ' + translate['Shares']
    menuLogout = \
        htmlHideFromScreenReader('❎') + ' ' + translate['Logout']
    navLinks = {
        menuTimeline: userPathStr + '/' + deft,
        menuEdit: userPathStr + '/editprofile',
        menuFollowing: userPathStr + '/following#timeline',
        menuFollowers: userPathStr + '/followers#timeline',
        menuRoles: userPathStr + '/roles#timeline',
        menuSkills: userPathStr + '/skills#timeline',
        menuShares: userPathStr + '/shares#timeline',
        menuLogout: '/logout'
    }
    navAccessKeys = {}
    for variableName, key in accessKeys.items():
        if not locals().get(variableName):
            continue
        navAccessKeys[locals()[variableName]] = key
    profileStr = htmlKeyboardNavigation(textModeBanner,
                                        navLinks, navAccessKeys)
    profileStr += profileHeaderStr + donateSection
    profileStr += ''
    # start of #timeline
    profileStr += '\n'
    profileStr += followApprovalsSection
    cssFilename = baseDir + '/epicyon-profile.css'
    if os.path.isfile(baseDir + '/epicyon.css'):
        cssFilename = baseDir + '/epicyon.css'
    licenseStr = \
        '
' + \
        ' '
    if selected == 'posts':
        profileStr += \
            _htmlProfilePosts(recentPostsCache, maxRecentPosts,
                              translate,
                              baseDir, httpPrefix, authorized,
                              nickname, domain, port,
                              session, cachedWebfingers, personCache,
                              projectVersion,
                              YTReplacementDomain,
                              showPublishedDateOnly,
                              peertubeInstances,
                              allowLocalNetworkAccess,
                              theme, systemLanguage) + licenseStr
    elif selected == 'following':
        profileStr += \
            _htmlProfileFollowing(translate, baseDir, httpPrefix,
                                  authorized, nickname,
                                  domain, port, session,
                                  cachedWebfingers, personCache, extraJson,
                                  projectVersion, ["unfollow"], selected,
                                  usersPath, pageNumber, maxItemsPerPage,
                                  dormantMonths, debug)
    elif selected == 'followers':
        profileStr += \
            _htmlProfileFollowing(translate, baseDir, httpPrefix,
                                  authorized, nickname,
                                  domain, port, session,
                                  cachedWebfingers, personCache, extraJson,
                                  projectVersion, ["block"],
                                  selected, usersPath, pageNumber,
                                  maxItemsPerPage, dormantMonths, debug)
    elif selected == 'roles':
        profileStr += \
            _htmlProfileRoles(translate, nickname, domainFull,
                              extraJson)
    elif selected == 'skills':
        profileStr += \
            _htmlProfileSkills(translate, nickname, domainFull, extraJson)
    elif selected == 'shares':
        profileStr += \
            _htmlProfileShares(actor, translate,
                               nickname, domainFull,
                               extraJson) + licenseStr
    # end of #timeline
    profileStr += '
\n' + \
                '     \n'
    for followingActor in followingJson['orderedItems']:
        # is this a dormant followed account?
        dormant = False
        if authorized and feedName == 'following':
            dormant = \
                isDormant(baseDir, nickname, domain, followingActor,
                          dormantMonths)
        profileStr += \
            _individualFollowAsHtml(translate, baseDir, session,
                                    cachedWebfingers, personCache,
                                    domain, followingActor,
                                    authorized, nickname,
                                    httpPrefix, projectVersion, dormant,
                                    debug, buttons)
    if authorized and maxItemsPerPage and pageNumber:
        if len(followingJson['orderedItems']) >= maxItemsPerPage:
            # page down arrow
            profileStr += \
                '  \n' + \
                '     \n'
    return profileStr
def _htmlProfileRoles(translate: {}, nickname: str, domain: str,
                      rolesList: []) -> str:
    """Shows roles on the profile screen
    """
    profileStr = ''
    profileStr += \
        '\n
\n'
    for role in rolesList:
        if translate.get(role):
            profileStr += '
' + translate[role] + ' \n'
        else:
            profileStr += '' + role + ' \n'
    profileStr += '@' + nickname + '@' + domain + ' has no roles assigned
\n'
    else:
        profileStr = '' + profileStr + '
\n'
    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 += \
            '\n' + \
            profileStr + '
' + profileStr + '
\n'
    return profileStr
def _grayscaleEnabled(baseDir: str) -> bool:
    """Is grayscale UI enabled?
    """
    return os.path.isfile(baseDir + '/accounts/.grayscale')
def _htmlThemesDropdown(baseDir: str, translate: {}) -> str:
    """Returns the html for theme selection dropdown
    """
    # Themes section
    themes = getThemesList(baseDir)
    themesDropdown = '  ' + \
        translate['Theme'] + ' '
    for themeName in themes:
        translatedThemeName = themeName
        if translate.get(themeName):
            translatedThemeName = translate[themeName]
        themesDropdown += '    ' + \
            translatedThemeName + ' '
    themesDropdown += '   ',
                               ' ')
    return themesDropdown
def _htmlEditProfileGraphicDesign(baseDir: str, translate: {}) -> str:
    """Graphic design section on Edit Profile screen
    """
    themeFormats = '.zip, .gz'
    graphicsStr = beginEditSection(translate['Graphic Design'])
    graphicsStr += _htmlThemesDropdown(baseDir, translate)
    graphicsStr += \
        '      ' + \
        translate['Import Theme'] + ' \n'
    graphicsStr += '      ' + \
        translate['Export Theme'] + ' ➤ \n'
    graphicsStr += endEditSection()
    return graphicsStr
def _htmlEditProfileInstance(baseDir: str, translate: {},
                             peertubeInstances: [],
                             mediaInstanceStr: str,
                             blogsInstanceStr: str,
                             newsInstanceStr: str) -> (str, str, str, str):
    """Edit profile instance settings
    """
    imageFormats = getImageFormats()
    # Instance details section
    instanceDescription = \
        getConfigParam(baseDir, 'instanceDescription')
    customSubmitText = \
        getConfigParam(baseDir, 'customSubmitText')
    instanceDescriptionShort = \
        getConfigParam(baseDir, 'instanceDescriptionShort')
    instanceTitle = \
        getConfigParam(baseDir, 'instanceTitle')
    instanceStr = beginEditSection(translate['Instance Settings'])
    instanceStr += \
        editTextField(translate['Instance Title'],
                      'instanceTitle', instanceTitle)
    instanceStr += '' + \
        translate['Instance Logo'] + ' ' + \
        '  ' + \
        translate['Security'] + ' ' + \
        translate['Type of instance'] + ' ' + \
        translate['Moderators'] + ' ' + \
        translate['Site Editors'] + ' ' + \
        editTextArea(translate['Counselors'], 'counselors', counselors,
                     200, '', False)
    # artists
    artists = ''
    artistsFile = baseDir + '/accounts/artists.txt'
    if os.path.isfile(artistsFile):
        with open(artistsFile, 'r') as f:
            artists = f.read()
    roleAssignStr += \
        '  ' + \
        editTextArea(translate['Artists'], 'artists', artists,
                     200, '', False)
    roleAssignStr += endEditSection()
    # Video section
    peertubeStr = beginEditSection(translate['Video Settings'])
    peertubeInstancesStr = ''
    for url in peertubeInstances:
        peertubeInstancesStr += url + '\n'
    peertubeStr += \
        editTextArea(translate['Peertube Instances'], 'ptInstances',
                     peertubeInstancesStr, 200, '', False)
    peertubeStr += \
        '      ' + \
        translate['Danger Zone'] + '   
'
            skillCtr += 1
    skillsStr += \
        '
' + endEditSection()
    idx = 'If you want to participate within organizations then you ' + \
        'can indicate some skills that you have and approximate ' + \
        'proficiency levels. This helps organizers to construct ' + \
        'teams with an appropriate combination of skills.'
    editProfileForm = \
        beginEditSection(translate['Skills']) + \
        '      ' + \
        translate['Skills'] + ' ' + \
        translate[idx] + ' \n' + skillsStr
    return editProfileForm
def _htmlEditProfileGitProjects(baseDir: str, nickname: str, domain: str,
                                translate: {}) -> str:
    """git projects section of edit profile screen
    """
    gitProjectsStr = ''
    gitProjectsFilename = \
        acctDir(baseDir, nickname, domain) + '/gitprojects.txt'
    if os.path.isfile(gitProjectsFilename):
        with open(gitProjectsFilename, 'r') as gitProjectsFile:
            gitProjectsStr = gitProjectsFile.read()
    idx = 'List of project names that you wish to receive git patches for'
    editProfileForm = beginEditSection(translate['Git Projects'])
    editProfileForm = \
        editTextArea(translate[idx], 'gitProjects', gitProjectsStr,
                     100, '', False)
    editProfileForm = endEditSection()
    return editProfileForm
def _htmlEditProfileFiltering(baseDir: str, nickname: str, domain: str,
                              userAgentsBlocked: str, translate: {}) -> str:
    """Filtering and blocking section of edit profile screen
    """
    filterStr = ''
    filterFilename = \
        acctDir(baseDir, nickname, domain) + '/filters.txt'
    if os.path.isfile(filterFilename):
        with open(filterFilename, 'r') as filterfile:
            filterStr = filterfile.read()
    switchStr = ''
    switchFilename = \
        acctDir(baseDir, nickname, domain) + '/replacewords.txt'
    if os.path.isfile(switchFilename):
        with open(switchFilename, 'r') as switchfile:
            switchStr = switchfile.read()
    autoTags = ''
    autoTagsFilename = \
        acctDir(baseDir, nickname, domain) + '/autotags.txt'
    if os.path.isfile(autoTagsFilename):
        with open(autoTagsFilename, 'r') as autoTagsFile:
            autoTags = autoTagsFile.read()
    autoCW = ''
    autoCWFilename = \
        acctDir(baseDir, nickname, domain) + '/autocw.txt'
    if os.path.isfile(autoCWFilename):
        with open(autoCWFilename, 'r') as autoCWFile:
            autoCW = autoCWFile.read()
    blockedStr = ''
    blockedFilename = \
        acctDir(baseDir, nickname, domain) + '/blocking.txt'
    if os.path.isfile(blockedFilename):
        with open(blockedFilename, 'r') as blockedfile:
            blockedStr = blockedfile.read()
    dmAllowedInstancesStr = ''
    dmAllowedInstancesFilename = \
        acctDir(baseDir, nickname, domain) + '/dmAllowedInstances.txt'
    if os.path.isfile(dmAllowedInstancesFilename):
        with open(dmAllowedInstancesFilename, 'r') as dmAllowedInstancesFile:
            dmAllowedInstancesStr = dmAllowedInstancesFile.read()
    allowedInstancesStr = ''
    allowedInstancesFilename = \
        acctDir(baseDir, nickname, domain) + '/allowedinstances.txt'
    if os.path.isfile(allowedInstancesFilename):
        with open(allowedInstancesFilename, 'r') as allowedInstancesFile:
            allowedInstancesStr = allowedInstancesFile.read()
    editProfileForm = beginEditSection(translate['Filtering and Blocking'])
    editProfileForm += \
        '' + \
        translate['City for spoofed GPS image metadata'] + \
        ' \n'
    city = city.lower()
    for cityName in cities:
        if ':' not in cityName:
            continue
        citySelected = ''
        cityName = cityName.split(':')[0]
        cityName = cityName.lower()
        if city:
            if city in cityName:
                citySelected = ' selected'
        editProfileForm += \
            '    ' + \
            cityName + ' \n'
    editProfileForm += '   ' + \
        translate['Filtered words'] + ' ' + \
        translate['One per line'] + ' \n' + \
        '      \n' + \
        '      ' + \
        translate['Word Replacements'] + ' A -> B \n' + \
        '      \n' + \
        '      ' + \
        translate['Autogenerated Hashtags'] + ' A -> #B \n' + \
        '      \n' + \
        '      ' + \
        translate['Autogenerated Content Warnings'] + ' A -> B \n' + \
        '      \n'
    idx = 'Blocked accounts, one per line, in the form ' + \
        'nickname@domain or *@blockeddomain'
    editProfileForm += \
        editTextArea(translate['Blocked accounts'], 'blocked', blockedStr,
                     200, '', False)
    idx = 'Direct messages are always allowed from these instances.'
    editProfileForm += \
        editTextArea(translate['Direct Message permitted instances'],
                     'dmAllowedInstances', dmAllowedInstancesStr,
                     200, '', False)
    idx = 'Federate only with a defined set of instances. ' + \
        'One domain name per line.'
    editProfileForm += \
        '      ' + \
        translate['Federation list'] + ' ' + \
        translate[idx] + ' \n' + \
        '      \n'
    userAgentsBlockedStr = ''
    for ua in userAgentsBlocked:
        if userAgentsBlockedStr:
            userAgentsBlockedStr += '\n'
        userAgentsBlockedStr += ua
    editProfileForm += \
        editTextArea(translate['Blocked User Agents'],
                     'userAgentsBlockedStr', userAgentsBlockedStr,
                     200, '', False)
    editProfileForm += endEditSection()
    return editProfileForm
def _htmlEditProfileChangePassword(translate: {}) -> str:
    """Change password section of edit profile screen
    """
    editProfileForm = \
        beginEditSection(translate['Change Password']) + \
        '' + translate['Change Password'] + \
        ' ' + translate['Confirm Password'] + \
        ' ' + translate[idx] + ' ' + \
            translate['Background image'] + ' \n' + \
            '      ' + \
            translate['Timeline banner image'] + ' \n' + \
            '      ' + \
            translate['Search banner image'] + ' \n' + \
            '      ' + \
            translate['Left column image'] + ' \n' + \
            '      ' + \
            translate['Right column image'] + ' \n' + \
            '      ' + \
        translate['Following'] + ' \n'
    editProfileForm += \
        editCheckBox(translate['Approve follower requests'],
                     'approveFollowers', manuallyApprovesFollowers)
    editProfileForm += \
        editCheckBox(translate['This is a bot account'],
                     'isBot', isBot)
    editProfileForm += \
        editCheckBox(translate['This is a group account'],
                     'isGroup', isGroup)
    editProfileForm += \
        editCheckBox(translate['Only people I follow can send me DMs'],
                     'followDMs', followDMs)
    editProfileForm += \
        editCheckBox(translate['Remove Twitter posts'],
                     'removeTwitter', removeTwitter)
    editProfileForm += \
        editCheckBox(translate['Notify when posts are liked'],
                     'notifyLikes', notifyLikes)
    editProfileForm += \
        editCheckBox(translate["Don't show the Like button"],
                     'hideLikeButton', hideLikeButton)
    editProfileForm += '    
\n'
    return editProfileForm
def _getSupportedLanguages(baseDir: str) -> str:
    """Returns a list of supported languages
    """
    langList = []
    for subdir, dirs, files in os.walk(baseDir + '/translations'):
        for f in files:
            if not f.endswith('.json'):
                continue
            langStr = f.split('.')[0]
            if len(langStr) != 2:
                continue
            langList.append(langStr)
        break
    if not langList:
        return ''
    langList.sort()
    languagesStr = ''
    for lang in langList:
        if languagesStr:
            languagesStr += ' / ' + lang
        else:
            languagesStr = lang
    return languagesStr
def _htmlEditProfileMain(baseDir: str, displayNickname: str, bioStr: str,
                         movedTo: str, donateUrl: str,
                         blogAddress: str, actorJson: {},
                         translate: {}) -> str:
    """main info on edit profile screen
    """
    imageFormats = getImageFormats()
    editProfileForm = '    \n'
    editProfileForm += \
        editTextField(translate['Nickname'], 'displayNickname',
                      displayNickname)
    editProfileForm += \
        editTextArea(translate['Your bio'], 'bio', bioStr, 200, '', True)
    editProfileForm += \
        '      ' + translate['Avatar image'] + \
        ' \n' + \
        '      
\n'
    return editProfileForm
def _htmlEditProfileTopBanner(baseDir: str,
                              nickname: str, domain: str, domainFull: str,
                              defaultTimeline: str, bannerFile: str,
                              path: str, accessKeys: {}, translate: {}) -> str:
    """top banner on edit profile screen
    """
    editProfileForm = \
        '' + \
        ' \n'
    editProfileForm += \
        '\n'
    editProfileForm += htmlFooter()
    return editProfileForm
def _individualFollowAsHtml(translate: {},
                            baseDir: str, session,
                            cachedWebfingers: {},
                            personCache: {}, domain: str,
                            followUrl: str,
                            authorized: bool,
                            actorNickname: str,
                            httpPrefix: str,
                            projectVersion: str,
                            dormant: bool,
                            debug: bool,
                            buttons=[]) -> str:
    """An individual follow entry on the profile screen
    """
    followUrlNickname = getNicknameFromActor(followUrl)
    followUrlDomain, followUrlPort = getDomainFromActor(followUrl)
    followUrlDomainFull = getFullDomain(followUrlDomain, followUrlPort)
    titleStr = '@' + followUrlNickname + '@' + followUrlDomainFull
    avatarUrl = getPersonAvatarUrl(baseDir, followUrl, personCache, True)
    if not avatarUrl:
        avatarUrl = followUrl + '/avatar.png'
    # lookup the correct webfinger for the followUrl
    followUrlHandle = followUrlNickname + '@' + followUrlDomainFull
    followUrlWf = \
        webfingerHandle(session, followUrlHandle, httpPrefix,
                        cachedWebfingers,
                        domain, __version__, debug)
    (inboxUrl, pubKeyId, pubKey,
     fromPersonId, sharedInbox,
     avatarUrl2, displayName) = getPersonBox(baseDir, session,
                                             followUrlWf,
                                             personCache, projectVersion,
                                             httpPrefix, followUrlNickname,
                                             domain, 'outbox', 43036)
    if avatarUrl2:
        avatarUrl = avatarUrl2
    if displayName:
        displayName = \
            addEmojiToDisplayName(baseDir, httpPrefix,
                                  actorNickname, domain,
                                  displayName, False)
        titleStr = displayName
    if dormant:
        titleStr += ' 💤'
    buttonsStr = ''
    if authorized:
        for b in buttons:
            if b == 'block':
                buttonsStr += \
                    '' + \
                    translate['Block'] + ' ' + \
                    translate['Unfollow'] + '