__filename__ = "webapp_person_options.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.6.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Web Interface"
import os
from shutil import copyfile
from petnames import get_pet_name
from person import is_person_snoozed
from posts import is_moderator
from flags import is_featured_writer
from flags import is_dormant
from utils import data_dir
from utils import quote_toots_allowed
from utils import get_full_domain
from utils import get_config_param
from utils import remove_html
from utils import get_domain_from_actor
from utils import get_nickname_from_actor
from utils import acct_dir
from utils import text_in_file
from utils import remove_domain_port
from blocking import is_blocked
from blocking import sending_is_blocked2
from follow import is_follower_of_person
from follow import is_following_actor
from followingCalendar import receiving_calendar_events
from notifyOnPost import notify_when_person_posts
from person import get_person_notes
from webapp_utils import mitm_warning_html
from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer
from webapp_utils import get_broken_link_substitute
from webapp_utils import html_keyboard_navigation
from webapp_utils import get_banner_file
from webapp_utils import html_hide_from_screen_reader
from webapp_utils import minimizing_attached_images
from blocking import allowed_announce
def _minimize_attached_images(base_dir: str, nickname: str, domain: str,
                              following_nickname: str,
                              following_domain: str,
                              add: bool) -> None:
    """Adds or removes a handle from the following.txt list into a list
    indicating whether to minimize images from that account
    """
    # check that a following file exists
    domain = remove_domain_port(domain)
    following_filename = \
        acct_dir(base_dir, nickname, domain) + '/following.txt'
    if not os.path.isfile(following_filename):
        print("WARN: following.txt doesn't exist for " +
              nickname + '@' + domain)
        return
    handle = following_nickname + '@' + following_domain
    # check that you are following this handle (not case sensitive)
    if not text_in_file(handle + '\n', following_filename, False):
        print('WARN: ' + handle + ' is not in ' + following_filename)
        return
    minimize_filename = \
        acct_dir(base_dir, nickname, domain) + '/followingMinimizeImages.txt'
    # get the contents of the minimize file, which is
    # a set of handles
    minimize_handles = ''
    if os.path.isfile(minimize_filename):
        print('Minimize file exists')
        try:
            with open(minimize_filename, 'r',
                      encoding='utf-8') as fp_minimize:
                minimize_handles = fp_minimize.read()
        except OSError:
            print('EX: minimize_attached_images ' + minimize_filename)
    else:
        # create a new minimize file from the following file
        print('Creating minimize file ' + minimize_filename)
        if add:
            try:
                with open(minimize_filename, 'w+',
                          encoding='utf-8') as fp_min:
                    fp_min.write('')
            except OSError:
                print('EX: minimize_attached_images unable to write ' +
                      minimize_filename)
    # already in the minimize file?
    if handle + '\n' in minimize_handles:
        print(handle + ' exists in followingMinimizeImages.txt')
        if add:
            # already added
            return
        # remove from minimize file
        minimize_handles = minimize_handles.replace(handle + '\n', '')
        try:
            with open(minimize_filename, 'w+',
                      encoding='utf-8') as fp_min:
                fp_min.write(minimize_handles)
        except OSError:
            print('EX: minimize_attached_images 3 ' + minimize_filename)
    else:
        print(handle + ' not in followingMinimizeImages.txt')
        # not already in the minimize file
        if add:
            # append to the list of handles
            minimize_handles += handle + '\n'
            try:
                with open(minimize_filename, 'w+',
                          encoding='utf-8') as fp_min:
                    fp_min.write(minimize_handles)
            except OSError:
                print('EX: minimize_attached_images 4 ' + minimize_filename)
def person_minimize_images(base_dir: str, nickname: str, domain: str,
                           following_nickname: str,
                           following_domain: str) -> None:
    """Images from this person are minimized by default
    """
    _minimize_attached_images(base_dir, nickname, domain,
                              following_nickname, following_domain, True)
def person_undo_minimize_images(base_dir: str, nickname: str, domain: str,
                                following_nickname: str,
                                following_domain: str) -> None:
    """Images from this person are no longer minimized by default
    """
    _minimize_attached_images(base_dir, nickname, domain,
                              following_nickname, following_domain, False)
def html_person_options(default_timeline: str,
                        translate: {}, base_dir: str,
                        domain: str, domain_full: str,
                        origin_path_str: str,
                        options_actor: str,
                        options_profile_url: str,
                        options_link: str,
                        page_number: int,
                        donate_url: str,
                        web_address: str,
                        gemini_link: str,
                        pronouns: str,
                        xmpp_address: str,
                        matrix_address: str,
                        ssb_address: str,
                        blog_address: str,
                        tox_address: str,
                        briar_address: str,
                        cwtch_address: str,
                        enigma_pub_key: str,
                        pgp_pub_key: str,
                        pgp_fingerprint: str,
                        email_address: str,
                        deltachat_invite: str,
                        dormant_months: int,
                        back_to_path: str,
                        locked_account: bool,
                        moved_to: str,
                        also_known_as: [],
                        text_mode_banner: str,
                        news_instance: bool,
                        authorized: bool,
                        access_keys: {},
                        is_group: bool,
                        theme: str,
                        blocked_cache: [],
                        repo_url: str,
                        sites_unavailable: [],
                        youtube: str, peertube: str,
                        pixelfed: str,
                        discord: str,
                        music_site_url: str,
                        art_site_url: str,
                        mitm_servers: []) -> str:
    """Show options for a person: view/follow/block/report
    """
    options_link_str = ''
    options_domain, options_port = get_domain_from_actor(options_actor)
    if not options_domain:
        return None
    options_domain_full = get_full_domain(options_domain, options_port)
    dir_str = data_dir(base_dir)
    if os.path.isfile(dir_str + '/options-background-custom.jpg'):
        if not os.path.isfile(dir_str + '/options-background.jpg'):
            copyfile(dir_str + '/options-background.jpg',
                     dir_str + '/options-background.jpg')
    dormant = False
    offline = False
    if options_domain in sites_unavailable:
        offline = True
    follow_str = 'Follow'
    if is_group:
        follow_str = 'Join'
    block_str = 'Block'
    nickname = None
    options_nickname = None
    follows_you = False
    if origin_path_str.startswith('/users/'):
        nickname = origin_path_str.split('/users/')[1]
        if '/' in nickname:
            nickname = nickname.split('/')[0]
        if '?' in nickname:
            nickname = nickname.split('?')[0]
#        follower_domain, follower_port = get_domain_from_actor(options_actor)
        if is_following_actor(base_dir, nickname, domain, options_actor):
            follow_str = 'Unfollow'
            if is_group:
                follow_str = 'Leave'
            if not offline:
                dormant = \
                    is_dormant(base_dir, nickname, domain, options_actor,
                               dormant_months)
        options_nickname = get_nickname_from_actor(options_actor)
        if not options_nickname:
            return None
        options_domain_full = get_full_domain(options_domain, options_port)
        follows_you = \
            is_follower_of_person(base_dir,
                                  nickname, domain,
                                  options_nickname, options_domain_full)
        if is_blocked(base_dir, nickname, domain,
                      options_nickname, options_domain_full,
                      None, None):
            block_str = 'Unblock'
    if options_link:
        options_link_str += \
            '    \n'
    css_filename = base_dir + '/epicyon-options.css'
    if os.path.isfile(base_dir + '/options.css'):
        css_filename = base_dir + '/options.css'
    # To snooze, or not to snooze? That is the question
    snooze_button_str = 'Snooze'
    if nickname:
        if is_person_snoozed(base_dir, nickname, domain, options_actor):
            snooze_button_str = 'Unsnooze'
    donate_str = ''
    if donate_url:
        donate_str = \
            '    ' + translate['Donate'] + '\n'
    banner_file, _ = \
        get_banner_file(base_dir, nickname, domain, theme)
    banner_path = '/users/' + nickname + '/' + banner_file
    instance_title = \
        get_config_param(base_dir, 'instanceTitle')
    preload_images = [banner_path]
    options_str = \
        html_header_with_external_style(css_filename, instance_title, None,
                                        preload_images)
    # show banner
    back_path = '/'
    if nickname:
        back_path = '/users/' + nickname + '/' + default_timeline
        if 'moderation' in back_to_path:
            back_path = '/users/' + nickname + '/moderation'
    if authorized and origin_path_str == '/users/' + nickname:
        banner_link = back_path
    else:
        banner_link = origin_path_str
    options_str += \
        '\n' + \
        '
\n'
    nav_links = {}
    timeline_link_str = html_hide_from_screen_reader('🏠') + ' ' + \
        translate['Switch to timeline view']
    nav_links[timeline_link_str] = \
        '/users/' + nickname + '/' + default_timeline
    nav_access_keys = {
    }
    options_str += \
        html_keyboard_navigation(text_mode_banner, nav_links, nav_access_keys,
                                 None, None, None, False)
    options_str += '