__filename__ = "webapp_create_post.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.3.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Web Interface"
import os
from utils import get_new_post_endpoints
from utils import is_public_post_from_url
from utils import get_nickname_from_actor
from utils import get_domain_from_actor
from utils import get_media_formats
from utils import get_config_param
from utils import acct_dir
from utils import get_currencies
from utils import get_category_types
from utils import get_account_timezone
from utils import get_supported_languages
from webapp_utils import html_common_emoji
from webapp_utils import begin_edit_section
from webapp_utils import end_edit_section
from webapp_utils import get_banner_file
from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer
from webapp_utils import edit_text_field
from webapp_utils import edit_number_field
from webapp_utils import edit_currency_field
from webapp_post import individual_post_as_html
from maps import get_map_preferences_url
from maps import get_map_preferences_coords
def _html_following_data_list(base_dir: str, nickname: str,
domain: str, domain_full: str) -> str:
"""Returns a datalist of handles being followed
"""
list_str = '\n'
following_filename = \
acct_dir(base_dir, nickname, domain) + '/following.txt'
msg = None
if os.path.isfile(following_filename):
with open(following_filename, 'r') as following_file:
msg = following_file.read()
# add your own handle, so that you can send DMs
# to yourself as reminders
msg += nickname + '@' + domain_full + '\n'
if msg:
# include petnames
petnames_filename = \
acct_dir(base_dir, nickname, domain) + '/petnames.txt'
if os.path.isfile(petnames_filename):
following_list = []
with open(petnames_filename, 'r') as petnames_file:
pet_str = petnames_file.read()
# extract each petname and append it
petnames_list = pet_str.split('\n')
for pet in petnames_list:
following_list.append(pet.split(' ')[0])
# add the following.txt entries
following_list += msg.split('\n')
else:
# no petnames list exists - just use following.txt
following_list = msg.split('\n')
following_list.sort()
if following_list:
for following_address in following_list:
if following_address:
list_str += '@' + following_address + ' \n'
list_str += ' \n'
return list_str
def _html_new_post_drop_down(scope_icon: str, scope_description: str,
reply_str: str,
translate: {},
show_public_on_dropdown: bool,
default_timeline: str,
path_base: str,
dropdown_new_post_suffix: str,
dropdown_new_blog_suffix: str,
dropdown_unlisted_suffix: str,
dropdown_followers_suffix: str,
dropdown_dm_suffix: str,
dropdown_reminder_suffix: str,
dropdown_report_suffix: str,
no_drop_down: bool,
access_keys: {}) -> str:
"""Returns the html for a drop down list of new post types
"""
drop_down_content = '\n'
if not no_drop_down:
drop_down_content += '
\n'
drop_down_content += '
\n'
drop_down_content += ' ' + scope_description + ' \n'
if no_drop_down:
drop_down_content += '
\n'
return drop_down_content
drop_down_content += '
\n'
drop_down_content += '\n'
return drop_down_content
def html_new_post(css_cache: {}, media_instance: bool, translate: {},
base_dir: str, http_prefix: str,
path: str, in_reply_to: str,
mentions: [],
share_description: str,
report_url: str, page_number: int,
category: str,
nickname: str, domain: str,
domain_full: str,
default_timeline: str, newswire: {},
theme: str, no_drop_down: bool,
access_keys: {}, custom_submit_text: str,
conversation_id: str,
recent_posts_cache: {}, max_recent_posts: int,
session, cached_webfingers: {},
person_cache: {}, port: int,
post_json_object: {},
project_version: str,
yt_replace_domain: str,
twitter_replacement_domain: str,
show_published_date_only: bool,
peertube_instances: [],
allow_local_network_access: bool,
system_language: str,
max_like_count: int, signing_priv_key_pem: str,
cw_lists: {}, lists_enabled: str,
box_name: str,
reply_is_chat: bool, bold_reading: bool) -> str:
"""New post screen
"""
reply_str = ''
is_new_reminder = False
if path.endswith('/newreminder'):
is_new_reminder = True
# the date and time
date_and_time_str = '\n'
if not is_new_reminder:
date_and_time_str += \
' \n'
# select a date and time for this post
date_and_time_str += '' + \
translate['Date'] + ': \n'
date_and_time_str += ' \n'
date_and_time_str += '' + \
translate['Start Time'] + ': '
date_and_time_str += \
' \n \n'
date_and_time_str += '' + \
translate['End Time'] + ': '
date_and_time_str += \
' \n
\n'
show_public_on_dropdown = True
message_box_height = 400
image_description_height = 100
# filename of the banner shown at the top
banner_file, _ = \
get_banner_file(base_dir, nickname, domain, theme)
if not path.endswith('/newshare') and not path.endswith('/newwanted'):
if not path.endswith('/newreport'):
if not in_reply_to or is_new_reminder:
new_post_text = '' + \
translate['Write your post text below.'] + ' \n'
else:
new_post_text = ''
if category != 'accommodation':
new_post_text = \
'' + \
translate['Write your reply to'] + \
' ' + \
translate['this post'] + '
\n'
if post_json_object:
timezone = \
get_account_timezone(base_dir, nickname, domain)
new_post_text += \
individual_post_as_html(signing_priv_key_pem,
True, recent_posts_cache,
max_recent_posts,
translate, None,
base_dir, session,
cached_webfingers,
person_cache,
nickname, domain, port,
post_json_object,
None, True, False,
http_prefix,
project_version,
box_name,
yt_replace_domain,
twitter_replacement_domain,
show_published_date_only,
peertube_instances,
allow_local_network_access,
theme, system_language,
max_like_count,
False, False, False,
False, False, False,
cw_lists, lists_enabled,
timezone, False,
bold_reading)
reply_str = ' \n'
# if replying to a non-public post then also make
# this post non-public
if not is_public_post_from_url(base_dir, nickname, domain,
in_reply_to):
new_post_path = path
if '?' in new_post_path:
new_post_path = new_post_path.split('?')[0]
if new_post_path.endswith('/newpost'):
path = path.replace('/newpost', '/newfollowers')
show_public_on_dropdown = False
else:
new_post_text = \
'' + translate['Write your report below.'] + ' \n'
# custom report header with any additional instructions
if os.path.isfile(base_dir + '/accounts/report.txt'):
with open(base_dir + '/accounts/report.txt', 'r') as file:
custom_report_text = file.read()
if '' not in custom_report_text:
custom_report_text = \
'' + \
custom_report_text + '
\n'
rep_str = ''
custom_report_text = \
custom_report_text.replace('
', rep_str)
new_post_text += custom_report_text
idx = 'This message only goes to moderators, even if it ' + \
'mentions other fediverse addresses.'
new_post_text += \
'
' + translate[idx] + '
\n' + \
'' + translate['Also see'] + \
' ' + \
translate['Terms of Service'] + '
\n'
else:
if path.endswith('/newshare'):
new_post_text = \
'' + \
translate['Enter the details for your shared item below.'] + \
' \n'
else:
new_post_text = \
'' + \
translate['Enter the details for your wanted item below.'] + \
' \n'
if path.endswith('/newquestion'):
new_post_text = \
'' + \
translate['Enter the choices for your question below.'] + \
' \n'
if os.path.isfile(base_dir + '/accounts/newpost.txt'):
with open(base_dir + '/accounts/newpost.txt', 'r') as file:
new_post_text = \
'' + file.read() + '
\n'
css_filename = base_dir + '/epicyon-profile.css'
if os.path.isfile(base_dir + '/epicyon.css'):
css_filename = base_dir + '/epicyon.css'
if '?' in path:
path = path.split('?')[0]
new_post_endpoints = get_new_post_endpoints()
path_base = path
for curr_post_type in new_post_endpoints:
path_base = path_base.replace('/' + curr_post_type, '')
attach_str = 'Attach an image, video or audio file'
new_post_image_section = begin_edit_section('π· ' + translate[attach_str])
new_post_image_section += \
' \n'
new_post_image_section += \
' \n'
new_post_image_section += end_edit_section()
new_post_emoji_section = ''
common_emoji_str = html_common_emoji(base_dir, 16)
if common_emoji_str:
new_post_emoji_section = \
begin_edit_section('π ' + translate['Common emoji'])
new_post_emoji_section += \
'' + \
translate['Copy and paste into your text'] + ' \n'
new_post_emoji_section += common_emoji_str
new_post_emoji_section += end_edit_section()
scope_icon = 'scope_public.png'
scope_description = translate['Public']
if share_description:
if category == 'accommodation':
placeholder_subject = translate['Request to stay']
else:
placeholder_subject = translate['Ask about a shared item.'] + '..'
else:
placeholder_subject = \
translate['Subject or Content Warning (optional)'] + '...'
placeholder_mentions = ''
if in_reply_to:
placeholder_mentions = \
translate['Replying to'] + '...'
placeholder_message = ''
if category != 'accommodation':
if default_timeline == 'tlfeatures':
placeholder_message = translate['Write your news report'] + '...'
else:
placeholder_message = translate['Write something'] + '...'
else:
idx = 'Introduce yourself and specify the date ' + \
'and time when you wish to stay'
placeholder_message = translate[idx]
extra_fields = ''
endpoint = 'newpost'
if path.endswith('/newblog'):
placeholder_subject = translate['Title']
scope_icon = 'scope_blog.png'
if default_timeline != 'tlfeatures':
scope_description = translate['Blog']
else:
scope_description = translate['Article']
endpoint = 'newblog'
elif path.endswith('/newunlisted'):
scope_icon = 'scope_unlisted.png'
scope_description = translate['Unlisted']
endpoint = 'newunlisted'
elif path.endswith('/newfollowers'):
scope_icon = 'scope_followers.png'
scope_description = translate['Followers']
endpoint = 'newfollowers'
elif path.endswith('/newdm'):
scope_icon = 'scope_dm.png'
scope_description = translate['DM']
endpoint = 'newdm'
placeholder_message = 'β οΈ ' + translate['DM warning']
elif is_new_reminder:
scope_icon = 'scope_reminder.png'
scope_description = translate['Reminder']
endpoint = 'newreminder'
elif path.endswith('/newreport'):
scope_icon = 'scope_report.png'
scope_description = translate['Report']
endpoint = 'newreport'
elif path.endswith('/newquestion'):
scope_icon = 'scope_question.png'
scope_description = translate['Question']
placeholder_message = translate['Enter your question'] + '...'
endpoint = 'newquestion'
extra_fields = '\n'
extra_fields += ' ' + \
translate['Possible answers'] + ': \n'
for question_ctr in range(8):
extra_fields += \
' \n'
extra_fields += \
' ' + \
translate['Duration of listing in days'] + \
': \n'
extra_fields += '
'
elif path.endswith('/newshare'):
scope_icon = 'scope_share.png'
scope_description = translate['Shared Item']
placeholder_subject = translate['Name of the shared item'] + '...'
placeholder_message = \
translate['Description of the item being shared'] + '...'
endpoint = 'newshare'
extra_fields = '\n'
extra_fields += \
edit_number_field(translate['Quantity'],
'itemQty', 1, 1, 999999, 1)
extra_fields += ' ' + \
edit_text_field(translate['Type of shared item. eg. hat'] + ':',
'itemType', '', '', True)
category_types = get_category_types(base_dir)
cat_str = translate['Category of shared item. eg. clothing']
extra_fields += '' + cat_str + ' \n'
extra_fields += ' \n'
for cat in category_types:
translated_category = "food"
if translate.get(cat):
translated_category = translate[cat]
extra_fields += ' ' + \
translated_category + ' \n'
extra_fields += ' \n'
extra_fields += \
edit_number_field(translate['Duration of listing in days'],
'duration', 14, 1, 365, 1)
extra_fields += '
\n'
extra_fields += '\n'
city_or_loc_str = translate['City or location of the shared item']
extra_fields += edit_text_field(city_or_loc_str + ':', 'location', '')
extra_fields += '
\n'
extra_fields += '\n'
extra_fields += \
edit_currency_field(translate['Price'] + ':', 'itemPrice', '0.00',
'0.00', True)
extra_fields += ' '
extra_fields += \
'' + translate['Currency'] + ' \n'
currencies = get_currencies()
extra_fields += ' \n'
currency_list = []
for symbol, curr_name in currencies.items():
currency_list.append(curr_name + ' ' + symbol)
currency_list.sort()
default_currency = get_config_param(base_dir, 'defaultCurrency')
if not default_currency:
default_currency = "EUR"
for curr_name in currency_list:
if default_currency not in curr_name:
extra_fields += ' ' + curr_name + ' \n'
else:
extra_fields += ' ' + \
curr_name + ' \n'
extra_fields += ' \n'
extra_fields += '
\n'
elif path.endswith('/newwanted'):
scope_icon = 'scope_wanted.png'
scope_description = translate['Wanted']
placeholder_subject = translate['Name of the wanted item'] + '...'
placeholder_message = \
translate['Description of the item wanted'] + '...'
endpoint = 'newwanted'
extra_fields = '\n'
extra_fields += \
edit_number_field(translate['Quantity'],
'itemQty', 1, 1, 999999, 1)
extra_fields += ' ' + \
edit_text_field(translate['Type of wanted item. eg. hat'] + ':',
'itemType', '', '', True)
category_types = get_category_types(base_dir)
cat_str = translate['Category of wanted item. eg. clothes']
extra_fields += '' + cat_str + ' \n'
extra_fields += ' \n'
for cat in category_types:
translated_category = "food"
if translate.get(cat):
translated_category = translate[cat]
extra_fields += ' ' + \
translated_category + ' \n'
extra_fields += ' \n'
extra_fields += \
edit_number_field(translate['Duration of listing in days'],
'duration', 14, 1, 365, 1)
extra_fields += '
\n'
extra_fields += '\n'
city_or_loc_str = translate['City or location of the wanted item']
extra_fields += edit_text_field(city_or_loc_str + ':', 'location', '')
extra_fields += '
\n'
extra_fields += '\n'
extra_fields += \
edit_currency_field(translate['Maximum Price'] + ':',
'itemPrice', '0.00', '0.00', True)
extra_fields += ' '
extra_fields += \
'' + translate['Currency'] + ' \n'
currencies = get_currencies()
extra_fields += ' \n'
currency_list = []
for symbol, curr_name in currencies.items():
currency_list.append(curr_name + ' ' + symbol)
currency_list.sort()
default_currency = get_config_param(base_dir, 'defaultCurrency')
if not default_currency:
default_currency = "EUR"
for curr_name in currency_list:
if default_currency not in curr_name:
extra_fields += ' ' + curr_name + ' \n'
else:
extra_fields += ' ' + \
curr_name + ' \n'
extra_fields += ' \n'
extra_fields += '
\n'
citations_str = ''
if endpoint == 'newblog':
citations_filename = \
acct_dir(base_dir, nickname, domain) + '/.citations.txt'
if os.path.isfile(citations_filename):
citations_str = '\n'
citations_str += '
' + \
translate['Citations'] + ':
\n'
citations_str += '
\n'
citations_separator = '#####'
with open(citations_filename, 'r') as cit_file:
citations = cit_file.readlines()
for line in citations:
if citations_separator not in line:
continue
sections = line.strip().split(citations_separator)
if len(sections) != 3:
continue
title = sections[1]
link = sections[2]
citations_str += \
' ' + \
title + ' '
citations_str += ' \n'
citations_str += '
\n'
replies_section = ''
date_and_location = ''
if endpoint not in ('newshare', 'newwanted', 'newreport', 'newquestion'):
if not is_new_reminder:
replies_section = \
'\n'
if category != 'accommodation':
replies_section += \
'
' + \
translate['Allow replies.'] + '
\n'
else:
replies_section += \
'
\n'
supported_languages = get_supported_languages(base_dir)
languages_dropdown = '
'
for lang_name in supported_languages:
translated_lang_name = lang_name
if translate.get('lang_' + lang_name):
translated_lang_name = translate['lang_' + lang_name]
languages_dropdown += ' ' + \
translated_lang_name + ' '
languages_dropdown += ' '
languages_dropdown = \
languages_dropdown.replace('
',
' ')
replies_section += \
' ' + \
translate['Language used'] + ' \n'
replies_section += languages_dropdown
replies_section += ' \n'
date_and_location = \
begin_edit_section('ποΈ ' + translate['Set a place and time'])
if endpoint == 'newpost':
date_and_location += \
' ' + \
translate['Pin this post to your profile.'] + \
'
\n'
if not in_reply_to:
date_and_location += \
' ' + \
translate['This is a scheduled post.'] + '
\n'
date_and_location += date_and_time_str
maps_url = get_map_preferences_url(base_dir, nickname, domain)
if not maps_url:
maps_url = 'https://www.openstreetmap.org'
if '://' not in maps_url:
maps_url = 'https://' + maps_url
maps_latitude, maps_longitude, maps_zoom = \
get_map_preferences_coords(base_dir, nickname, domain)
if maps_latitude and maps_longitude and maps_zoom:
if 'openstreetmap.org' in maps_url:
maps_url = \
'https://www.openstreetmap.org/#map=' + \
str(maps_zoom) + '/' + \
str(maps_latitude) + '/' + \
str(maps_longitude)
elif '.google.co' in maps_url:
maps_url = \
'https://www.google.com/maps/@' + \
str(maps_latitude) + ',' + \
str(maps_longitude) + ',' + \
str(maps_zoom) + 'z'
elif '.bing.co' in maps_url:
maps_url = \
'https://www.bing.com/maps?cp=' + \
str(maps_latitude) + '~' + \
str(maps_longitude) + '&lvl=' + \
str(maps_zoom)
elif '.waze.co' in maps_url:
maps_url = \
'https://ul.waze.com/ul?ll=' + \
str(maps_latitude) + '%2C' + \
str(maps_longitude) + '&zoom=' + \
str(maps_zoom)
elif 'wego.here.co' in maps_url:
maps_url = \
'https://wego.here.com/?x=ep&map=' + \
str(maps_latitude) + ',' + \
str(maps_longitude) + ',' + \
str(maps_zoom) + ',normal'
location_label_with_link = \
'πΊοΈ ' + \
translate['Location'] + ' '
date_and_location += '\n' + \
edit_text_field(location_label_with_link, 'location', '',
'https://www.openstreetmap.org/#map=') + '
\n'
date_and_location += end_edit_section()
instance_title = get_config_param(base_dir, 'instanceTitle')
new_post_form = html_header_with_external_style(css_filename,
instance_title, None)
new_post_form += \
'\n'
mentions_str = ''
for ment in mentions:
mention_nickname = get_nickname_from_actor(ment)
if not mention_nickname:
continue
mention_domain, mention_port = get_domain_from_actor(ment)
if not mention_domain:
continue
if mention_port:
mentions_handle = \
'@' + mention_nickname + '@' + \
mention_domain + ':' + str(mention_port)
else:
mentions_handle = '@' + mention_nickname + '@' + mention_domain
if mentions_handle not in mentions_str:
mentions_str += mentions_handle + ' '
# build suffixes so that any replies or mentions are
# preserved when switching between scopes
dropdown_new_post_suffix = '/newpost'
dropdown_new_blog_suffix = '/newblog'
dropdown_unlisted_suffix = '/newunlisted'
dropdown_followers_suffix = '/newfollowers'
dropdown_dm_suffix = '/newdm'
dropdown_reminder_suffix = '/newreminder'
dropdown_report_suffix = '/newreport'
if in_reply_to or mentions:
dropdown_new_post_suffix = ''
dropdown_new_blog_suffix = ''
dropdown_unlisted_suffix = ''
dropdown_followers_suffix = ''
dropdown_dm_suffix = ''
dropdown_reminder_suffix = ''
dropdown_report_suffix = ''
if in_reply_to:
dropdown_new_post_suffix += '?replyto=' + in_reply_to
dropdown_new_blog_suffix += '?replyto=' + in_reply_to
dropdown_unlisted_suffix += '?replyunlisted=' + in_reply_to
dropdown_followers_suffix += '?replyfollowers=' + in_reply_to
if reply_is_chat:
dropdown_dm_suffix += '?replychat=' + in_reply_to
else:
dropdown_dm_suffix += '?replydm=' + in_reply_to
for mentioned_actor in mentions:
dropdown_new_post_suffix += '?mention=' + mentioned_actor
dropdown_new_blog_suffix += '?mention=' + mentioned_actor
dropdown_unlisted_suffix += '?mention=' + mentioned_actor
dropdown_followers_suffix += '?mention=' + mentioned_actor
dropdown_dm_suffix += '?mention=' + mentioned_actor
dropdown_report_suffix += '?mention=' + mentioned_actor
if conversation_id and in_reply_to:
dropdown_new_post_suffix += '?conversationId=' + conversation_id
dropdown_new_blog_suffix += '?conversationId=' + conversation_id
dropdown_unlisted_suffix += '?conversationId=' + conversation_id
dropdown_followers_suffix += '?conversationId=' + conversation_id
dropdown_dm_suffix += '?conversationId=' + conversation_id
drop_down_content = ''
if not report_url and not share_description:
drop_down_content = \
_html_new_post_drop_down(scope_icon, scope_description,
reply_str,
translate,
show_public_on_dropdown,
default_timeline,
path_base,
dropdown_new_post_suffix,
dropdown_new_blog_suffix,
dropdown_unlisted_suffix,
dropdown_followers_suffix,
dropdown_dm_suffix,
dropdown_reminder_suffix,
dropdown_report_suffix,
no_drop_down, access_keys)
else:
if not share_description:
# reporting a post to moderator
mentions_str = 'Re: ' + report_url + '\n\n' + mentions_str
new_post_form += \
'\n'
new_post_form += html_footer()
return new_post_form