epicyon/blog.py

954 lines
37 KiB
Python
Raw Normal View History

2020-04-01 21:29:04 +00:00
__filename__ = "blog.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-04-01 21:29:04 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-01 21:29:04 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "ActivityPub"
2020-04-01 21:29:04 +00:00
2020-02-25 13:35:41 +00:00
import os
from datetime import datetime
from content import replaceEmojiFromTags
2020-11-28 10:54:48 +00:00
from webapp_utils import htmlHeaderWithExternalStyle
2021-05-15 19:39:34 +00:00
from webapp_utils import htmlHeaderWithBlogMarkup
2020-11-28 10:54:48 +00:00
from webapp_utils import htmlFooter
2020-11-09 19:41:01 +00:00
from webapp_utils import getPostAttachmentsAsHtml
2021-07-22 18:35:45 +00:00
from webapp_utils import editTextArea
2020-11-28 10:54:48 +00:00
from webapp_media import addEmbeddedElements
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
2021-12-26 10:22:19 +00:00
from utils import get_actor_languages_list
2021-12-26 11:29:40 +00:00
from utils import get_base_content_from_post
2021-12-26 10:50:49 +00:00
from utils import get_content_from_post
2021-12-26 18:46:43 +00:00
from utils import is_account_dir
2021-05-15 19:39:34 +00:00
from utils import removeHtml
2021-12-26 14:08:58 +00:00
from utils import get_config_param
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2020-11-21 11:54:29 +00:00
from utils import getMediaFormats
2020-02-25 13:35:41 +00:00
from utils import getNicknameFromActor
from utils import getDomainFromActor
2021-12-26 20:36:08 +00:00
from utils import locate_post
2021-12-26 15:13:34 +00:00
from utils import load_json
from utils import firstParagraphFromString
2021-12-26 18:22:20 +00:00
from utils import get_actor_property_url
2021-12-26 12:02:29 +00:00
from utils import acct_dir
2020-02-25 13:35:41 +00:00
from posts import createBlogsTimeline
2020-10-04 12:29:07 +00:00
from newswire import rss2Header
from newswire import rss2Footer
from cache import getPersonFromCache
2020-02-25 13:35:41 +00:00
2021-12-25 17:09:22 +00:00
def _noOfBlogReplies(base_dir: str, http_prefix: str, translate: {},
2021-12-26 10:00:46 +00:00
nickname: str, domain: str, domain_full: str,
2021-12-26 19:47:06 +00:00
post_id: str, depth=0) -> int:
2020-02-25 20:31:37 +00:00
"""Returns the number of replies on the post
2020-02-26 10:03:09 +00:00
This is recursive, so can handle replies to replies
2020-02-25 20:31:37 +00:00
"""
2020-04-01 21:29:04 +00:00
if depth > 4:
2020-02-26 10:03:09 +00:00
return 0
2021-12-26 19:47:06 +00:00
if not post_id:
2020-02-25 20:31:37 +00:00
return 0
2020-02-26 10:10:16 +00:00
2020-04-01 21:29:04 +00:00
tryPostBox = ('tlblogs', 'inbox', 'outbox')
boxFound = False
2020-02-26 10:10:16 +00:00
for postBox in tryPostBox:
2021-12-26 23:41:34 +00:00
post_filename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \
2021-12-26 19:47:06 +00:00
post_id.replace('/', '#') + '.replies'
2021-12-26 23:41:34 +00:00
if os.path.isfile(post_filename):
2020-04-01 21:29:04 +00:00
boxFound = True
2020-02-26 10:10:16 +00:00
break
if not boxFound:
2020-02-26 10:26:04 +00:00
# post may exist but has no replies
for postBox in tryPostBox:
2021-12-26 23:41:34 +00:00
post_filename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \
2021-12-26 19:47:06 +00:00
post_id.replace('/', '#')
2021-12-26 23:41:34 +00:00
if os.path.isfile(post_filename):
2020-02-26 10:26:04 +00:00
return 1
2020-02-25 20:31:37 +00:00
return 0
2020-02-26 10:10:16 +00:00
removals = []
2020-04-01 21:29:04 +00:00
replies = 0
lines = []
2021-11-26 12:28:20 +00:00
try:
2021-12-26 23:41:34 +00:00
with open(post_filename, 'r') as f:
2021-11-26 12:28:20 +00:00
lines = f.readlines()
except OSError:
2021-12-26 23:41:34 +00:00
print('EX: failed to read blog ' + post_filename)
2021-11-26 12:28:20 +00:00
for replyPostId in lines:
replyPostId = replyPostId.replace('\n', '').replace('\r', '')
replyPostId = replyPostId.replace('.json', '')
2021-12-26 20:36:08 +00:00
if locate_post(base_dir, nickname, domain, replyPostId):
2021-11-26 12:28:20 +00:00
replyPostId = replyPostId.replace('.replies', '')
replies += \
2021-12-25 17:09:22 +00:00
1 + _noOfBlogReplies(base_dir, http_prefix, translate,
2021-12-26 10:00:46 +00:00
nickname, domain, domain_full,
2021-11-26 12:28:20 +00:00
replyPostId, depth+1)
else:
# remove post which no longer exists
removals.append(replyPostId)
# remove posts from .replies file if they don't exist
if lines and removals:
2021-12-26 23:41:34 +00:00
print('Rewriting ' + post_filename + ' to remove ' +
str(len(removals)) + ' entries')
2021-11-25 18:42:38 +00:00
try:
2021-12-26 23:41:34 +00:00
with open(post_filename, 'w+') as f:
2021-11-25 18:42:38 +00:00
for replyPostId in lines:
replyPostId = \
replyPostId.replace('\n', '').replace('\r', '')
if replyPostId not in removals:
f.write(replyPostId + '\n')
2021-12-25 15:28:52 +00:00
except OSError as ex:
2021-11-25 22:22:54 +00:00
print('EX: unable to remove replies from post ' +
2021-12-26 23:41:34 +00:00
post_filename + ' ' + str(ex))
2020-02-26 10:03:09 +00:00
return replies
2020-02-25 20:31:37 +00:00
2021-12-25 17:09:22 +00:00
def _getBlogReplies(base_dir: str, http_prefix: str, translate: {},
2021-12-26 10:00:46 +00:00
nickname: str, domain: str, domain_full: str,
2021-12-26 19:47:06 +00:00
post_id: str, depth=0) -> str:
2020-05-18 14:00:47 +00:00
"""Returns a string containing html blog posts
2020-02-25 20:53:02 +00:00
"""
2020-04-01 21:29:04 +00:00
if depth > 4:
2020-02-26 10:03:09 +00:00
return ''
2021-12-26 19:47:06 +00:00
if not post_id:
2020-02-25 20:53:02 +00:00
return ''
2020-02-26 10:10:16 +00:00
2020-04-01 21:29:04 +00:00
tryPostBox = ('tlblogs', 'inbox', 'outbox')
boxFound = False
2020-02-26 10:10:16 +00:00
for postBox in tryPostBox:
2021-12-26 23:41:34 +00:00
post_filename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \
2021-12-26 19:47:06 +00:00
post_id.replace('/', '#') + '.replies'
2021-12-26 23:41:34 +00:00
if os.path.isfile(post_filename):
2020-04-01 21:29:04 +00:00
boxFound = True
2020-02-26 10:10:16 +00:00
break
if not boxFound:
2020-02-26 10:26:04 +00:00
# post may exist but has no replies
for postBox in tryPostBox:
2021-12-26 23:41:34 +00:00
post_filename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/' + postBox + '/' + \
2021-12-26 19:47:06 +00:00
post_id.replace('/', '#') + '.json'
2021-12-26 23:41:34 +00:00
if os.path.isfile(post_filename):
post_filename = acct_dir(base_dir, nickname, domain) + \
2020-04-01 21:29:04 +00:00
'/postcache/' + \
2021-12-26 19:47:06 +00:00
post_id.replace('/', '#') + '.html'
2021-12-26 23:41:34 +00:00
if os.path.isfile(post_filename):
2021-11-26 12:28:20 +00:00
try:
2021-12-26 23:41:34 +00:00
with open(post_filename, 'r') as postFile:
2021-11-26 12:28:20 +00:00
return postFile.read() + '\n'
except OSError:
2021-12-26 23:41:34 +00:00
print('EX: unable to read blog 3 ' + post_filename)
2020-02-25 20:53:02 +00:00
return ''
2020-02-26 10:10:16 +00:00
2021-11-26 12:28:20 +00:00
lines = []
try:
2021-12-26 23:41:34 +00:00
with open(post_filename, 'r') as f:
2021-11-26 12:28:20 +00:00
lines = f.readlines()
except OSError:
2021-12-26 23:41:34 +00:00
print('EX: unable to read blog 4 ' + post_filename)
2021-11-26 12:28:20 +00:00
if lines:
2020-04-01 21:29:04 +00:00
repliesStr = ''
2020-02-26 10:04:38 +00:00
for replyPostId in lines:
2020-05-22 11:32:38 +00:00
replyPostId = replyPostId.replace('\n', '').replace('\r', '')
replyPostId = replyPostId.replace('.json', '')
2020-04-01 21:29:04 +00:00
replyPostId = replyPostId.replace('.replies', '')
2021-12-26 23:41:34 +00:00
post_filename = acct_dir(base_dir, nickname, domain) + \
2020-04-01 21:29:04 +00:00
'/postcache/' + \
2020-05-22 11:32:38 +00:00
replyPostId.replace('/', '#') + '.html'
2021-12-26 23:41:34 +00:00
if not os.path.isfile(post_filename):
2020-02-25 20:53:02 +00:00
continue
2021-11-26 12:28:20 +00:00
try:
2021-12-26 23:41:34 +00:00
with open(post_filename, 'r') as postFile:
2021-11-26 12:28:20 +00:00
repliesStr += postFile.read() + '\n'
except OSError:
2021-12-26 23:41:34 +00:00
print('EX: unable to read blog replies ' + post_filename)
2021-12-25 17:09:22 +00:00
rply = _getBlogReplies(base_dir, http_prefix, translate,
2021-12-26 10:00:46 +00:00
nickname, domain, domain_full,
replyPostId, depth+1)
2020-05-18 16:12:57 +00:00
if rply not in repliesStr:
repliesStr += rply
2020-02-26 10:03:09 +00:00
# indicate the reply indentation level
2020-04-01 21:29:04 +00:00
indentStr = '>'
2020-02-26 10:03:09 +00:00
for indentLevel in range(depth):
2020-04-01 21:29:04 +00:00
indentStr += ' >'
2020-02-26 10:03:09 +00:00
2020-04-01 21:29:04 +00:00
repliesStr = repliesStr.replace(translate['SHOW MORE'], indentStr)
return repliesStr.replace('?tl=outbox', '?tl=tlblogs')
2020-02-25 20:53:02 +00:00
return ''
2021-11-01 17:12:17 +00:00
def _htmlBlogPostContent(debug: bool, session, authorized: bool,
2021-12-25 17:09:22 +00:00
base_dir: str, http_prefix: str, translate: {},
2021-12-26 10:00:46 +00:00
nickname: str, domain: str, domain_full: str,
2021-12-25 22:09:19 +00:00
post_json_object: {},
handle: str, restrictToDomain: bool,
2021-12-25 23:38:53 +00:00
peertube_instances: [],
2021-12-25 23:03:28 +00:00
system_language: str,
2021-12-25 22:17:49 +00:00
person_cache: {},
blogSeparator: str = '<hr>') -> str:
2020-02-25 13:35:41 +00:00
"""Returns the content for a single blog post
"""
2020-04-01 21:29:04 +00:00
linkedAuthor = False
actor = ''
blogStr = ''
messageLink = ''
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('id'):
messageLink = \
post_json_object['object']['id'].replace('/statuses/', '/')
2020-04-01 21:29:04 +00:00
titleStr = ''
2020-11-08 13:01:01 +00:00
articleAdded = False
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('summary'):
titleStr = post_json_object['object']['summary']
2020-11-08 13:01:01 +00:00
blogStr += '<article><h1><a href="' + messageLink + '">' + \
2020-04-01 21:29:04 +00:00
titleStr + '</a></h1>\n'
2020-11-08 13:01:01 +00:00
articleAdded = True
2020-02-25 13:35:41 +00:00
# get the handle of the author
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('attributedTo'):
2020-08-06 16:21:46 +00:00
authorNickname = None
2021-12-25 22:09:19 +00:00
if isinstance(post_json_object['object']['attributedTo'], str):
actor = post_json_object['object']['attributedTo']
2020-08-06 16:21:46 +00:00
authorNickname = getNicknameFromActor(actor)
2020-02-25 13:35:41 +00:00
if authorNickname:
2020-04-01 21:29:04 +00:00
authorDomain, authorPort = getDomainFromActor(actor)
2020-02-25 13:35:41 +00:00
if authorDomain:
# author must be from the given domain
if restrictToDomain and authorDomain != domain:
return ''
2020-04-01 21:29:04 +00:00
handle = authorNickname + '@' + authorDomain
2020-02-25 13:35:41 +00:00
else:
# posts from the domain are expected to have an attributedTo field
if restrictToDomain:
return ''
2020-03-22 21:16:02 +00:00
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('published'):
if 'T' in post_json_object['object']['published']:
2020-04-01 21:29:04 +00:00
blogStr += '<h3>' + \
2021-12-25 22:09:19 +00:00
post_json_object['object']['published'].split('T')[0]
2020-02-25 13:35:41 +00:00
if handle:
2020-04-01 21:29:04 +00:00
if handle.startswith(nickname + '@' + domain):
2021-12-25 17:09:22 +00:00
blogStr += ' <a href="' + http_prefix + '://' + \
2021-12-26 10:00:46 +00:00
domain_full + \
2020-04-01 21:29:04 +00:00
'/users/' + nickname + '">' + handle + '</a>'
linkedAuthor = True
2020-02-25 13:35:41 +00:00
else:
2020-04-01 21:29:04 +00:00
if actor:
blogStr += ' <a href="' + actor + '">' + \
handle + '</a>'
linkedAuthor = True
2020-02-25 13:35:41 +00:00
else:
2020-04-01 21:29:04 +00:00
blogStr += ' ' + handle
blogStr += '</h3>\n'
avatarLink = ''
replyStr = ''
announceStr = ''
likeStr = ''
bookmarkStr = ''
deleteStr = ''
muteStr = ''
isMuted = False
2021-12-25 22:09:19 +00:00
attachmentStr, galleryStr = getPostAttachmentsAsHtml(post_json_object,
2020-04-01 21:29:04 +00:00
'tlblogs', translate,
isMuted, avatarLink,
replyStr, announceStr,
likeStr, bookmarkStr,
deleteStr, muteStr)
2020-02-25 16:57:25 +00:00
if attachmentStr:
2020-04-01 21:29:04 +00:00
blogStr += '<br><center>' + attachmentStr + '</center>'
2020-02-25 16:57:25 +00:00
2021-12-26 10:19:59 +00:00
personUrl = local_actor_url(http_prefix, nickname, domain_full)
2021-12-26 10:29:52 +00:00
actor_json = \
2021-12-25 22:17:49 +00:00
getPersonFromCache(base_dir, personUrl, person_cache, False)
2021-12-26 10:52:54 +00:00
languages_understood = []
2021-12-26 10:29:52 +00:00
if actor_json:
2021-12-26 10:52:54 +00:00
languages_understood = get_actor_languages_list(actor_json)
2021-12-26 10:50:49 +00:00
jsonContent = get_content_from_post(post_json_object, system_language,
2021-12-26 10:52:54 +00:00
languages_understood)
if jsonContent:
contentStr = addEmbeddedElements(translate, jsonContent,
2021-12-25 23:38:53 +00:00
peertube_instances)
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('tag'):
post_json_object_tags = post_json_object['object']['tag']
2021-12-25 16:17:53 +00:00
contentStr = replaceEmojiFromTags(session, base_dir, contentStr,
2021-12-25 22:09:19 +00:00
post_json_object_tags,
2021-11-01 17:12:17 +00:00
'content', debug)
2020-11-08 13:01:01 +00:00
if articleAdded:
blogStr += '<br>' + contentStr + '</article>\n'
else:
blogStr += '<br><article>' + contentStr + '</article>\n'
2020-02-25 13:35:41 +00:00
2020-11-06 14:04:09 +00:00
citationsStr = ''
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('tag'):
for tagJson in post_json_object['object']['tag']:
2020-11-06 14:04:09 +00:00
if not isinstance(tagJson, dict):
continue
if not tagJson.get('type'):
continue
if tagJson['type'] != 'Article':
continue
if not tagJson.get('name'):
continue
if not tagJson.get('url'):
continue
citationsStr += \
'<li><a href="' + tagJson['url'] + '">' + \
'<cite>' + tagJson['name'] + '</cite></a></li>\n'
if citationsStr:
citationsStr = '<p><b>' + translate['Citations'] + \
':</b></p>' + \
'<ul>\n' + citationsStr + '</ul>\n'
blogStr += '<br>\n' + citationsStr
2020-02-25 20:31:37 +00:00
2020-02-25 20:57:16 +00:00
if not linkedAuthor:
2020-04-01 21:29:04 +00:00
blogStr += '<p class="about"><a class="about" href="' + \
2021-12-26 10:19:59 +00:00
local_actor_url(http_prefix, nickname, domain_full) + \
2021-08-14 11:13:39 +00:00
'">' + translate['About the author'] + \
2020-04-01 21:29:04 +00:00
'</a></p>\n'
2021-12-25 17:09:22 +00:00
replies = _noOfBlogReplies(base_dir, http_prefix, translate,
2021-12-26 10:00:46 +00:00
nickname, domain, domain_full,
2021-12-25 22:09:19 +00:00
post_json_object['object']['id'])
2020-05-18 11:32:28 +00:00
# separator between blogs should be centered
if '<center>' not in blogSeparator:
blogSeparator = '<center>' + blogSeparator + '</center>'
if replies == 0:
blogStr += blogSeparator + '\n'
return blogStr
if not authorized:
blogStr += '<p class="blogreplies">' + \
translate['Replies'].lower() + ': ' + str(replies) + '</p>'
blogStr += '<br><br><br>' + blogSeparator + '\n'
else:
blogStr += blogSeparator + '<h1>' + translate['Replies'] + '</h1>\n'
if not titleStr:
2021-12-25 17:09:22 +00:00
blogStr += _getBlogReplies(base_dir, http_prefix, translate,
2021-12-26 10:00:46 +00:00
nickname, domain, domain_full,
2021-12-25 22:09:19 +00:00
post_json_object['object']['id'])
2020-02-25 21:40:13 +00:00
else:
2021-12-25 17:09:22 +00:00
blogRepliesStr = _getBlogReplies(base_dir, http_prefix, translate,
2021-12-26 10:00:46 +00:00
nickname, domain, domain_full,
2021-12-25 22:09:19 +00:00
post_json_object['object']['id'])
2020-05-18 11:32:28 +00:00
blogStr += blogRepliesStr.replace('>' + titleStr + '<', '')
2020-02-25 13:35:41 +00:00
return blogStr
def _htmlBlogPostRSS2(authorized: bool,
2021-12-25 17:09:22 +00:00
base_dir: str, http_prefix: str, translate: {},
2021-12-26 10:00:46 +00:00
nickname: str, domain: str, domain_full: str,
2021-12-25 22:09:19 +00:00
post_json_object: {},
handle: str, restrictToDomain: bool,
2021-12-25 23:03:28 +00:00
system_language: str) -> str:
"""Returns the RSS version 2 feed for a single blog post
2020-02-27 20:23:27 +00:00
"""
2020-11-01 17:18:05 +00:00
rssStr = ''
2020-04-01 21:29:04 +00:00
messageLink = ''
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('id'):
messageLink = \
post_json_object['object']['id'].replace('/statuses/', '/')
2020-02-27 20:23:27 +00:00
if not restrictToDomain or \
2020-04-01 21:29:04 +00:00
(restrictToDomain and '/' + domain in messageLink):
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('summary') and \
post_json_object['object'].get('published'):
published = post_json_object['object']['published']
2020-04-01 21:29:04 +00:00
pubDate = datetime.strptime(published, "%Y-%m-%dT%H:%M:%SZ")
2021-12-25 22:09:19 +00:00
titleStr = post_json_object['object']['summary']
2020-04-01 21:29:04 +00:00
rssDateStr = pubDate.strftime("%a, %d %b %Y %H:%M:%S UT")
content = \
2021-12-26 11:29:40 +00:00
get_base_content_from_post(post_json_object,
system_language)
description = firstParagraphFromString(content)
2020-04-01 21:29:04 +00:00
rssStr = ' <item>'
rssStr += ' <title>' + titleStr + '</title>'
rssStr += ' <link>' + messageLink + '</link>'
2020-11-08 10:30:25 +00:00
rssStr += \
' <description>' + description + '</description>'
2020-04-01 21:29:04 +00:00
rssStr += ' <pubDate>' + rssDateStr + '</pubDate>'
rssStr += ' </item>'
2020-02-27 20:23:27 +00:00
return rssStr
def _htmlBlogPostRSS3(authorized: bool,
2021-12-25 17:09:22 +00:00
base_dir: str, http_prefix: str, translate: {},
2021-12-26 10:00:46 +00:00
nickname: str, domain: str, domain_full: str,
2021-12-25 22:09:19 +00:00
post_json_object: {},
handle: str, restrictToDomain: bool,
2021-12-25 23:03:28 +00:00
system_language: str) -> str:
2020-05-23 09:41:50 +00:00
"""Returns the RSS version 3 feed for a single blog post
"""
2020-11-01 17:18:05 +00:00
rssStr = ''
2020-05-23 09:41:50 +00:00
messageLink = ''
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('id'):
messageLink = \
post_json_object['object']['id'].replace('/statuses/', '/')
2020-05-23 09:41:50 +00:00
if not restrictToDomain or \
(restrictToDomain and '/' + domain in messageLink):
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('summary') and \
post_json_object['object'].get('published'):
published = post_json_object['object']['published']
2020-05-23 09:41:50 +00:00
pubDate = datetime.strptime(published, "%Y-%m-%dT%H:%M:%SZ")
2021-12-25 22:09:19 +00:00
titleStr = post_json_object['object']['summary']
2020-05-23 09:41:50 +00:00
rssDateStr = pubDate.strftime("%a, %d %b %Y %H:%M:%S UT")
content = \
2021-12-26 11:29:40 +00:00
get_base_content_from_post(post_json_object,
system_language)
description = firstParagraphFromString(content)
2020-05-23 09:41:50 +00:00
rssStr = 'title: ' + titleStr + '\n'
rssStr += 'link: ' + messageLink + '\n'
2020-11-08 10:30:25 +00:00
rssStr += 'description: ' + description + '\n'
2020-05-23 09:41:50 +00:00
rssStr += 'created: ' + rssDateStr + '\n\n'
return rssStr
def _htmlBlogRemoveCwButton(blogStr: str, translate: {}) -> str:
2020-07-03 10:07:11 +00:00
"""Removes the CW button from blog posts, where the
summary field is instead used as the blog title
"""
blogStr = blogStr.replace('<details>', '<b>')
blogStr = blogStr.replace('</details>', '</b>')
blogStr = blogStr.replace('<summary>', '')
blogStr = blogStr.replace('</summary>', '')
blogStr = blogStr.replace(translate['SHOW MORE'], '')
return blogStr
2021-12-25 22:09:19 +00:00
def _getSnippetFromBlogContent(post_json_object: {},
2021-12-25 23:03:28 +00:00
system_language: str) -> str:
2021-05-15 19:39:34 +00:00
"""Returns a snippet of text from the blog post as a preview
"""
2021-12-26 11:29:40 +00:00
content = get_base_content_from_post(post_json_object, system_language)
2021-05-15 19:39:34 +00:00
if '<p>' in content:
content = content.split('<p>', 1)[1]
if '</p>' in content:
content = content.split('</p>', 1)[0]
content = removeHtml(content)
2021-05-15 19:43:42 +00:00
if '\n' in content:
content = content.split('\n')[0]
2021-05-15 19:39:34 +00:00
if len(content) >= 256:
content = content[:252] + '...'
return content
2021-11-01 17:12:17 +00:00
def htmlBlogPost(session, authorized: bool,
2021-12-25 17:09:22 +00:00
base_dir: str, http_prefix: str, translate: {},
2021-12-26 10:00:46 +00:00
nickname: str, domain: str, domain_full: str,
2021-12-25 22:09:19 +00:00
post_json_object: {},
2021-12-25 23:38:53 +00:00
peertube_instances: [],
2021-12-25 23:03:28 +00:00
system_language: str, person_cache: {},
2021-12-25 17:13:38 +00:00
debug: bool, content_license_url: str) -> str:
2020-02-25 13:35:41 +00:00
"""Returns a html blog post
"""
2020-04-01 21:29:04 +00:00
blogStr = ''
2020-02-25 13:35:41 +00:00
2021-12-25 16:17:53 +00:00
cssFilename = base_dir + '/epicyon-blog.css'
if os.path.isfile(base_dir + '/blog.css'):
cssFilename = base_dir + '/blog.css'
2021-01-11 19:46:21 +00:00
instanceTitle = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2021-12-25 22:09:19 +00:00
published = post_json_object['object']['published']
2021-11-08 13:20:06 +00:00
modified = published
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('updated'):
modified = post_json_object['object']['updated']
title = post_json_object['object']['summary']
2021-11-08 13:20:06 +00:00
url = ''
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('url'):
url = post_json_object['object']['url']
2021-12-25 23:03:28 +00:00
snippet = _getSnippetFromBlogContent(post_json_object, system_language)
2021-05-15 19:39:34 +00:00
blogStr = htmlHeaderWithBlogMarkup(cssFilename, instanceTitle,
2021-12-26 10:00:46 +00:00
http_prefix, domain_full, nickname,
2021-12-25 23:03:28 +00:00
system_language, published, modified,
2021-11-08 16:17:07 +00:00
title, snippet, translate, url,
2021-12-25 17:13:38 +00:00
content_license_url)
_htmlBlogRemoveCwButton(blogStr, translate)
2020-02-25 13:35:41 +00:00
2021-12-25 16:17:53 +00:00
blogStr += _htmlBlogPostContent(debug, session, authorized, base_dir,
2021-12-25 17:09:22 +00:00
http_prefix, translate,
nickname, domain,
2021-12-26 10:00:46 +00:00
domain_full, post_json_object,
2020-12-23 23:59:49 +00:00
None, False,
2021-12-25 23:38:53 +00:00
peertube_instances, system_language,
2021-12-25 22:17:49 +00:00
person_cache)
2020-11-12 16:05:45 +00:00
# show rss links
blogStr += '<p class="rssfeed">'
2020-05-23 09:41:50 +00:00
2021-12-25 17:09:22 +00:00
blogStr += '<a href="' + http_prefix + '://' + \
2021-12-26 10:00:46 +00:00
domain_full + '/blog/' + nickname + '/rss.xml">'
2020-11-12 16:05:45 +00:00
blogStr += '<img style="width:3%;min-width:50px" ' + \
'loading="lazy" alt="RSS 2.0" ' + \
'title="RSS 2.0" src="/' + \
2020-12-09 13:08:26 +00:00
'icons/logorss.png" /></a>'
2020-05-23 09:41:50 +00:00
2021-12-25 17:09:22 +00:00
# blogStr += '<a href="' + http_prefix + '://' + \
2021-12-26 10:00:46 +00:00
# domain_full + '/blog/' + nickname + '/rss.txt">'
2020-11-12 16:05:45 +00:00
# blogStr += '<img style="width:3%;min-width:50px" ' + \
# 'loading="lazy" alt="RSS 3.0" ' + \
# 'title="RSS 3.0" src="/' + \
2020-12-09 13:08:26 +00:00
# 'icons/rss3.png" /></a>'
2020-05-23 09:41:50 +00:00
2020-11-12 16:05:45 +00:00
blogStr += '</p>'
2020-04-01 21:29:04 +00:00
2020-11-12 16:05:45 +00:00
return blogStr + htmlFooter()
2020-02-25 13:35:41 +00:00
2020-04-01 21:29:04 +00:00
def htmlBlogPage(authorized: bool, session,
2021-12-25 17:09:22 +00:00
base_dir: str, http_prefix: str, translate: {},
2020-04-01 21:29:04 +00:00
nickname: str, domain: str, port: int,
2020-12-23 23:59:49 +00:00
noOfItems: int, pageNumber: int,
2021-12-25 23:38:53 +00:00
peertube_instances: [], system_language: str,
2021-12-25 22:17:49 +00:00
person_cache: {}, debug: bool) -> str:
2020-02-25 13:35:41 +00:00
"""Returns a html blog page containing posts
"""
2020-05-22 11:32:38 +00:00
if ' ' in nickname or '@' in nickname or \
'\n' in nickname or '\r' in nickname:
2020-02-25 13:35:41 +00:00
return None
2020-04-01 21:29:04 +00:00
blogStr = ''
2020-02-25 13:35:41 +00:00
2021-12-25 16:17:53 +00:00
cssFilename = base_dir + '/epicyon-profile.css'
if os.path.isfile(base_dir + '/epicyon.css'):
cssFilename = base_dir + '/epicyon.css'
2021-01-11 19:46:21 +00:00
instanceTitle = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
blogStr = htmlHeaderWithExternalStyle(cssFilename, instanceTitle, None)
_htmlBlogRemoveCwButton(blogStr, translate)
2020-02-27 20:23:27 +00:00
2021-12-26 12:02:29 +00:00
blogsIndex = acct_dir(base_dir, nickname, domain) + '/tlblogs.index'
2020-11-12 16:05:45 +00:00
if not os.path.isfile(blogsIndex):
return blogStr + htmlFooter()
2020-02-25 13:35:41 +00:00
2021-12-25 16:17:53 +00:00
timelineJson = createBlogsTimeline(session, base_dir,
2020-11-12 16:05:45 +00:00
nickname, domain, port,
2021-12-25 17:09:22 +00:00
http_prefix,
2020-11-12 16:05:45 +00:00
noOfItems, False,
pageNumber)
if not timelineJson:
return blogStr + htmlFooter()
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2020-02-25 13:35:41 +00:00
2020-11-12 16:05:45 +00:00
# show previous and next buttons
if pageNumber is not None:
navigateStr = '<p>'
if pageNumber > 1:
# show previous button
2021-12-25 17:09:22 +00:00
navigateStr += '<a href="' + http_prefix + '://' + \
2021-12-26 10:00:46 +00:00
domain_full + '/blog/' + \
2020-11-12 16:05:45 +00:00
nickname + '?page=' + str(pageNumber-1) + '">' + \
'<img loading="lazy" alt="<" title="<" ' + \
2020-12-09 13:08:26 +00:00
'src="/icons' + \
2020-11-12 16:05:45 +00:00
'/prev.png" class="buttonprev"/></a>\n'
2020-04-01 21:29:04 +00:00
if len(timelineJson['orderedItems']) >= noOfItems:
2020-11-12 16:05:45 +00:00
# show next button
2021-12-25 17:09:22 +00:00
navigateStr += '<a href="' + http_prefix + '://' + \
2021-12-26 10:00:46 +00:00
domain_full + '/blog/' + nickname + \
2020-11-12 16:05:45 +00:00
'?page=' + str(pageNumber + 1) + '">' + \
'<img loading="lazy" alt=">" title=">" ' + \
2020-12-09 13:08:26 +00:00
'src="/icons' + \
2020-11-12 16:05:45 +00:00
'/prev.png" class="buttonnext"/></a>\n'
navigateStr += '</p>'
blogStr += navigateStr
for item in timelineJson['orderedItems']:
if item['type'] != 'Create':
continue
2020-02-25 18:19:43 +00:00
2021-12-25 16:17:53 +00:00
blogStr += _htmlBlogPostContent(debug, session, authorized, base_dir,
2021-12-25 17:09:22 +00:00
http_prefix, translate,
nickname, domain,
2021-12-26 10:00:46 +00:00
domain_full, item,
2020-12-23 23:59:49 +00:00
None, True,
2021-12-25 23:38:53 +00:00
peertube_instances,
2021-12-25 23:03:28 +00:00
system_language,
2021-12-25 22:17:49 +00:00
person_cache)
2020-05-23 09:50:10 +00:00
2020-11-12 16:05:45 +00:00
if len(timelineJson['orderedItems']) >= noOfItems:
blogStr += navigateStr
2020-05-23 09:50:10 +00:00
2020-11-12 16:05:45 +00:00
# show rss link
blogStr += '<p class="rssfeed">'
2020-05-23 09:50:10 +00:00
2021-12-25 17:09:22 +00:00
blogStr += '<a href="' + http_prefix + '://' + \
2021-12-26 10:00:46 +00:00
domain_full + '/blog/' + nickname + '/rss.xml">'
2020-11-12 16:05:45 +00:00
blogStr += '<img loading="lazy" alt="RSS 2.0" ' + \
'title="RSS 2.0" src="/' + \
2020-12-09 13:08:26 +00:00
'icons/logorss.png" /></a>'
2020-04-01 21:29:04 +00:00
2021-12-25 17:09:22 +00:00
# blogStr += '<a href="' + http_prefix + '://' + \
2021-12-26 10:00:46 +00:00
# domain_full + '/blog/' + nickname + '/rss.txt">'
2020-11-12 16:05:45 +00:00
# blogStr += '<img loading="lazy" alt="RSS 3.0" ' + \
# 'title="RSS 3.0" src="/' + \
2020-12-09 13:08:26 +00:00
# 'icons/rss3.png" /></a>'
2020-11-12 16:05:45 +00:00
blogStr += '</p>'
return blogStr + htmlFooter()
2020-02-25 13:35:41 +00:00
2020-04-01 21:29:04 +00:00
def htmlBlogPageRSS2(authorized: bool, session,
2021-12-25 17:09:22 +00:00
base_dir: str, http_prefix: str, translate: {},
nickname: str, domain: str, port: int,
2020-10-13 16:58:45 +00:00
noOfItems: int, pageNumber: int,
2021-12-25 23:03:28 +00:00
includeHeader: bool, system_language: str) -> str:
"""Returns an RSS version 2 feed containing posts
2020-02-27 20:23:27 +00:00
"""
2020-05-22 11:32:38 +00:00
if ' ' in nickname or '@' in nickname or \
'\n' in nickname or '\r' in nickname:
2020-02-27 20:23:27 +00:00
return None
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2020-02-27 20:23:27 +00:00
2020-10-13 16:58:45 +00:00
blogRSS2 = ''
if includeHeader:
2021-12-26 10:00:46 +00:00
blogRSS2 = rss2Header(http_prefix, nickname, domain_full,
2020-10-13 16:58:45 +00:00
'Blog', translate)
2020-02-27 20:23:27 +00:00
2021-12-26 12:02:29 +00:00
blogsIndex = acct_dir(base_dir, nickname, domain) + '/tlblogs.index'
2020-02-27 20:23:27 +00:00
if not os.path.isfile(blogsIndex):
2020-10-13 17:09:22 +00:00
if includeHeader:
return blogRSS2 + rss2Footer()
else:
return blogRSS2
2020-02-27 20:23:27 +00:00
2021-12-25 16:17:53 +00:00
timelineJson = createBlogsTimeline(session, base_dir,
2020-04-01 21:29:04 +00:00
nickname, domain, port,
2021-12-25 17:09:22 +00:00
http_prefix,
2020-10-02 12:39:22 +00:00
noOfItems, False,
2020-04-01 21:29:04 +00:00
pageNumber)
2020-02-27 20:23:27 +00:00
if not timelineJson:
2020-10-13 16:58:45 +00:00
if includeHeader:
return blogRSS2 + rss2Footer()
else:
return blogRSS2
2020-02-27 20:23:27 +00:00
2020-04-01 21:29:04 +00:00
if pageNumber is not None:
2020-02-27 20:23:27 +00:00
for item in timelineJson['orderedItems']:
2020-04-01 21:29:04 +00:00
if item['type'] != 'Create':
2020-02-27 20:23:27 +00:00
continue
blogRSS2 += \
2021-12-25 16:17:53 +00:00
_htmlBlogPostRSS2(authorized, base_dir,
2021-12-25 17:09:22 +00:00
http_prefix, translate,
nickname, domain,
2021-12-26 10:00:46 +00:00
domain_full, item,
2021-12-25 23:03:28 +00:00
None, True, system_language)
2020-02-27 20:23:27 +00:00
2020-10-13 17:09:22 +00:00
if includeHeader:
return blogRSS2 + rss2Footer()
else:
return blogRSS2
2020-02-27 20:23:27 +00:00
2020-02-25 13:35:41 +00:00
2020-05-23 09:41:50 +00:00
def htmlBlogPageRSS3(authorized: bool, session,
2021-12-25 17:09:22 +00:00
base_dir: str, http_prefix: str, translate: {},
2020-05-23 09:41:50 +00:00
nickname: str, domain: str, port: int,
noOfItems: int, pageNumber: int,
2021-12-25 23:03:28 +00:00
system_language: str) -> str:
2020-05-23 09:41:50 +00:00
"""Returns an RSS version 3 feed containing posts
"""
if ' ' in nickname or '@' in nickname or \
'\n' in nickname or '\r' in nickname:
return None
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2020-05-23 09:41:50 +00:00
blogRSS3 = ''
2021-12-26 12:02:29 +00:00
blogsIndex = acct_dir(base_dir, nickname, domain) + '/tlblogs.index'
2020-05-23 09:41:50 +00:00
if not os.path.isfile(blogsIndex):
return blogRSS3
2021-12-25 16:17:53 +00:00
timelineJson = createBlogsTimeline(session, base_dir,
2020-05-23 09:41:50 +00:00
nickname, domain, port,
2021-12-25 17:09:22 +00:00
http_prefix,
2020-10-02 12:39:22 +00:00
noOfItems, False,
2020-05-23 09:41:50 +00:00
pageNumber)
if not timelineJson:
return blogRSS3
if pageNumber is not None:
for item in timelineJson['orderedItems']:
if item['type'] != 'Create':
continue
blogRSS3 += \
2021-12-25 16:17:53 +00:00
_htmlBlogPostRSS3(authorized, base_dir,
2021-12-25 17:09:22 +00:00
http_prefix, translate,
nickname, domain,
2021-12-26 10:00:46 +00:00
domain_full, item,
None, True,
2021-12-25 23:03:28 +00:00
system_language)
2020-05-23 09:41:50 +00:00
return blogRSS3
2021-12-25 16:17:53 +00:00
def _noOfBlogAccounts(base_dir: str) -> int:
2020-02-25 13:35:41 +00:00
"""Returns the number of blog accounts
"""
2020-04-01 21:29:04 +00:00
ctr = 0
2021-12-25 16:17:53 +00:00
for subdir, dirs, files in os.walk(base_dir + '/accounts'):
2020-02-25 13:35:41 +00:00
for acct in dirs:
2021-12-26 18:46:43 +00:00
if not is_account_dir(acct):
2020-02-25 13:35:41 +00:00
continue
2021-12-25 16:17:53 +00:00
accountDir = os.path.join(base_dir + '/accounts', acct)
2020-04-01 21:29:04 +00:00
blogsIndex = accountDir + '/tlblogs.index'
2020-02-25 13:35:41 +00:00
if os.path.isfile(blogsIndex):
2020-04-01 21:29:04 +00:00
ctr += 1
2020-12-13 22:13:45 +00:00
break
2020-02-25 13:35:41 +00:00
return ctr
2020-04-01 21:29:04 +00:00
2021-12-25 16:17:53 +00:00
def _singleBlogAccountNickname(base_dir: str) -> str:
2020-02-25 13:35:41 +00:00
"""Returns the nickname of a single blog account
"""
2021-12-25 16:17:53 +00:00
for subdir, dirs, files in os.walk(base_dir + '/accounts'):
2020-02-25 13:35:41 +00:00
for acct in dirs:
2021-12-26 18:46:43 +00:00
if not is_account_dir(acct):
2020-02-25 13:35:41 +00:00
continue
2021-12-25 16:17:53 +00:00
accountDir = os.path.join(base_dir + '/accounts', acct)
2020-04-01 21:29:04 +00:00
blogsIndex = accountDir + '/tlblogs.index'
2020-02-25 13:35:41 +00:00
if os.path.isfile(blogsIndex):
return acct.split('@')[0]
2020-12-13 22:13:45 +00:00
break
2020-02-25 13:35:41 +00:00
return None
2020-04-01 21:29:04 +00:00
def htmlBlogView(authorized: bool,
2021-12-25 17:09:22 +00:00
session, base_dir: str, http_prefix: str,
2020-04-01 21:29:04 +00:00
translate: {}, domain: str, port: int,
2020-12-23 23:59:49 +00:00
noOfItems: int,
2021-12-25 23:38:53 +00:00
peertube_instances: [], system_language: str,
2021-12-25 22:17:49 +00:00
person_cache: {}, debug: bool) -> str:
2020-02-25 13:35:41 +00:00
"""Show the blog main page
"""
2020-04-01 21:29:04 +00:00
blogStr = ''
2020-02-25 13:35:41 +00:00
2021-12-25 16:17:53 +00:00
cssFilename = base_dir + '/epicyon-profile.css'
if os.path.isfile(base_dir + '/epicyon.css'):
cssFilename = base_dir + '/epicyon.css'
2021-01-11 19:46:21 +00:00
instanceTitle = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
blogStr = htmlHeaderWithExternalStyle(cssFilename, instanceTitle, None)
2020-04-01 21:29:04 +00:00
2021-12-25 16:17:53 +00:00
if _noOfBlogAccounts(base_dir) <= 1:
nickname = _singleBlogAccountNickname(base_dir)
2020-11-12 16:05:45 +00:00
if nickname:
return htmlBlogPage(authorized, session,
2021-12-25 17:09:22 +00:00
base_dir, http_prefix, translate,
2020-11-12 16:05:45 +00:00
nickname, domain, port,
2021-12-25 23:38:53 +00:00
noOfItems, 1, peertube_instances,
2021-12-25 23:03:28 +00:00
system_language, person_cache, debug)
2020-11-12 16:05:45 +00:00
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2020-11-12 16:05:45 +00:00
2021-12-25 16:17:53 +00:00
for subdir, dirs, files in os.walk(base_dir + '/accounts'):
2020-11-12 16:05:45 +00:00
for acct in dirs:
2021-12-26 18:46:43 +00:00
if not is_account_dir(acct):
2020-11-12 16:05:45 +00:00
continue
2021-12-25 16:17:53 +00:00
accountDir = os.path.join(base_dir + '/accounts', acct)
2020-11-12 16:05:45 +00:00
blogsIndex = accountDir + '/tlblogs.index'
if os.path.isfile(blogsIndex):
blogStr += '<p class="blogaccount">'
blogStr += '<a href="' + \
2021-12-26 10:00:46 +00:00
http_prefix + '://' + domain_full + '/blog/' + \
2020-11-12 16:05:45 +00:00
acct.split('@')[0] + '">' + acct + '</a>'
blogStr += '</p>'
2020-12-13 22:13:45 +00:00
break
2020-11-12 16:05:45 +00:00
return blogStr + htmlFooter()
2020-02-29 20:34:44 +00:00
2020-04-01 21:29:04 +00:00
2021-12-25 20:25:07 +00:00
def htmlEditBlog(media_instance: bool, translate: {},
2021-12-25 17:09:22 +00:00
base_dir: str, http_prefix: str,
2020-04-01 21:29:04 +00:00
path: str,
pageNumber: int,
nickname: str, domain: str,
2021-12-25 23:03:28 +00:00
postUrl: str, system_language: str) -> str:
2020-02-29 20:34:44 +00:00
"""Edit a blog post after it was created
"""
2021-12-26 23:41:34 +00:00
post_filename = locate_post(base_dir, nickname, domain, postUrl)
if not post_filename:
2020-04-01 21:29:04 +00:00
print('Edit blog: Filename not found for ' + postUrl)
2020-03-01 12:33:20 +00:00
return None
2021-12-26 23:41:34 +00:00
post_json_object = load_json(post_filename)
2021-12-25 22:09:19 +00:00
if not post_json_object:
2021-12-26 23:41:34 +00:00
print('Edit blog: json not loaded for ' + post_filename)
2020-03-01 12:33:20 +00:00
return None
2020-11-21 09:56:45 +00:00
editBlogText = '<h1">' + translate['Write your post text below.'] + '</h1>'
2020-02-29 20:34:44 +00:00
2021-12-25 16:17:53 +00:00
if os.path.isfile(base_dir + '/accounts/newpost.txt'):
2021-11-26 12:28:20 +00:00
try:
2021-12-25 16:17:53 +00:00
with open(base_dir + '/accounts/newpost.txt', 'r') as file:
2021-11-26 12:28:20 +00:00
editBlogText = '<p>' + file.read() + '</p>'
except OSError:
2021-12-25 16:17:53 +00:00
print('EX: unable to read ' + base_dir + '/accounts/newpost.txt')
2020-02-29 20:34:44 +00:00
2021-12-25 16:17:53 +00:00
cssFilename = base_dir + '/epicyon-profile.css'
if os.path.isfile(base_dir + '/epicyon.css'):
cssFilename = base_dir + '/epicyon.css'
2020-02-29 20:34:44 +00:00
if '?' in path:
2020-04-01 21:29:04 +00:00
path = path.split('?')[0]
pathBase = path
editBlogImageSection = ' <div class="container">'
editBlogImageSection += ' <label class="labels">' + \
translate['Image description'] + '</label>'
editBlogImageSection += ' <input type="text" name="imageDescription">'
2020-04-05 09:33:23 +00:00
editBlogImageSection += \
' <input type="file" id="attachpic" name="attachpic"'
editBlogImageSection += \
2020-11-21 11:54:29 +00:00
' accept="' + getMediaFormats() + '">'
2020-04-01 21:29:04 +00:00
editBlogImageSection += ' </div>'
placeholderMessage = translate['Write something'] + '...'
endpoint = 'editblogpost'
placeholderSubject = translate['Title']
scopeIcon = 'scope_blog.png'
scopeDescription = translate['Blog']
dateAndLocation = ''
dateAndLocation = '<div class="container">'
2020-04-05 09:33:23 +00:00
dateAndLocation += \
'<p><input type="checkbox" class="profilecheckbox" ' + \
'name="schedulePost"><label class="labels">' + \
2020-04-01 21:29:04 +00:00
translate['This is a scheduled post.'] + '</label></p>'
2020-04-05 09:33:23 +00:00
dateAndLocation += \
'<p><img loading="lazy" alt="" title="" ' + \
2020-12-09 13:08:26 +00:00
'class="emojicalendar" src="/icons/calendar.png"/>'
2020-04-05 09:33:23 +00:00
dateAndLocation += \
'<label class="labels">' + translate['Date'] + ': </label>'
2020-04-01 21:29:04 +00:00
dateAndLocation += '<input type="date" name="eventDate">'
dateAndLocation += '<label class="labelsright">' + translate['Time'] + ':'
dateAndLocation += '<input type="time" name="eventTime"></label></p>'
dateAndLocation += '</div>'
dateAndLocation += '<div class="container">'
2020-04-05 09:33:23 +00:00
dateAndLocation += \
'<br><label class="labels">' + translate['Location'] + ': </label>'
2020-04-01 21:29:04 +00:00
dateAndLocation += '<input type="text" name="location">'
dateAndLocation += '</div>'
2021-01-11 19:46:21 +00:00
instanceTitle = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
editBlogForm = \
htmlHeaderWithExternalStyle(cssFilename, instanceTitle, None)
2020-04-01 21:29:04 +00:00
2020-04-05 09:33:23 +00:00
editBlogForm += \
'<form enctype="multipart/form-data" method="POST" ' + \
'accept-charset="UTF-8" action="' + \
2020-04-01 21:29:04 +00:00
pathBase + '?' + endpoint + '?page=' + str(pageNumber) + '">'
2020-04-05 09:33:23 +00:00
editBlogForm += \
' <input type="hidden" name="postUrl" value="' + postUrl + '">'
editBlogForm += \
' <input type="hidden" name="pageNumber" value="' + \
str(pageNumber) + '">'
2020-04-01 21:29:04 +00:00
editBlogForm += ' <div class="vertical-center">'
2020-04-05 09:33:23 +00:00
editBlogForm += \
' <label for="nickname"><b>' + editBlogText + '</b></label>'
2020-04-01 21:29:04 +00:00
editBlogForm += ' <div class="container">'
editBlogForm += ' <div class="dropbtn">'
2020-04-05 09:33:23 +00:00
editBlogForm += \
2020-12-09 13:08:26 +00:00
' <img loading="lazy" alt="" title="" src="/icons' + \
2020-04-05 09:33:23 +00:00
'/' + scopeIcon + '"/><b class="scope-desc">' + \
scopeDescription + '</b>'
2020-04-01 21:29:04 +00:00
editBlogForm += ' </div>'
editBlogForm += ' <a href="' + pathBase + \
'/searchemoji"><img loading="lazy" ' + \
'class="emojisearch" src="/emoji/1F601.png" title="' + \
translate['Search for emoji'] + '" alt="' + \
translate['Search for emoji'] + '"/></a>'
editBlogForm += ' </div>'
editBlogForm += ' <div class="container"><center>'
editBlogForm += ' <a href="' + pathBase + \
'/inbox"><button class="cancelbtn">' + \
translate['Cancel'] + '</button></a>'
editBlogForm += ' <input type="submit" name="submitPost" value="' + \
translate['Submit'] + '">'
editBlogForm += ' </center></div>'
2021-12-25 20:25:07 +00:00
if media_instance:
2020-04-01 21:29:04 +00:00
editBlogForm += editBlogImageSection
2020-04-05 09:33:23 +00:00
editBlogForm += \
' <label class="labels">' + placeholderSubject + '</label><br>'
2020-04-01 21:29:04 +00:00
titleStr = ''
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('summary'):
titleStr = post_json_object['object']['summary']
2020-04-05 09:33:23 +00:00
editBlogForm += \
' <input type="text" name="subject" value="' + titleStr + '">'
2020-04-01 21:29:04 +00:00
editBlogForm += ''
2021-07-22 18:35:45 +00:00
editBlogForm += ' <br>'
2020-04-01 21:29:04 +00:00
messageBoxHeight = 800
2021-12-26 11:29:40 +00:00
contentStr = get_base_content_from_post(post_json_object, system_language)
2020-04-05 09:33:23 +00:00
contentStr = contentStr.replace('<p>', '').replace('</p>', '\n')
2020-04-01 21:29:04 +00:00
2020-04-05 09:33:23 +00:00
editBlogForm += \
2021-07-22 18:35:45 +00:00
editTextArea(placeholderMessage, 'message', contentStr,
messageBoxHeight, '', True)
2020-04-01 21:29:04 +00:00
editBlogForm += dateAndLocation
2021-12-25 20:25:07 +00:00
if not media_instance:
2020-04-01 21:29:04 +00:00
editBlogForm += editBlogImageSection
editBlogForm += ' </div>'
editBlogForm += '</form>'
2020-02-29 20:34:44 +00:00
2020-04-01 21:29:04 +00:00
editBlogForm = editBlogForm.replace('<body>',
'<body onload="focusOnMessage()">')
2020-02-29 20:34:44 +00:00
2020-04-01 21:29:04 +00:00
editBlogForm += htmlFooter()
2020-02-29 20:34:44 +00:00
return editBlogForm
2021-06-07 19:23:48 +00:00
2021-12-25 16:17:53 +00:00
def pathContainsBlogLink(base_dir: str,
2021-12-25 17:09:22 +00:00
http_prefix: str, domain: str,
2021-12-26 10:00:46 +00:00
domain_full: str, path: str) -> (str, str):
2021-06-07 19:23:48 +00:00
"""If the path contains a blog entry then return its filename
"""
if '/users/' not in path:
return None, None
userEnding = path.split('/users/', 1)[1]
if '/' not in userEnding:
return None, None
userEnding2 = userEnding.split('/')
nickname = userEnding2[0]
if len(userEnding2) != 2:
return None, None
if len(userEnding2[1]) < 14:
return None, None
userEnding2[1] = userEnding2[1].strip()
if not userEnding2[1].isdigit():
return None, None
# check for blog posts
2021-12-26 12:02:29 +00:00
blogIndexFilename = acct_dir(base_dir, nickname, domain) + '/tlblogs.index'
2021-06-07 19:23:48 +00:00
if not os.path.isfile(blogIndexFilename):
return None, None
if '#' + userEnding2[1] + '.' not in open(blogIndexFilename).read():
return None, None
2021-12-26 10:19:59 +00:00
messageId = local_actor_url(http_prefix, nickname, domain_full) + \
2021-08-14 11:13:39 +00:00
'/statuses/' + userEnding2[1]
2021-12-26 20:36:08 +00:00
return locate_post(base_dir, nickname, domain, messageId), nickname
2021-06-26 11:16:41 +00:00
2021-12-26 10:29:52 +00:00
def getBlogAddress(actor_json: {}) -> str:
2021-06-26 11:16:41 +00:00
"""Returns blog address for the given actor
"""
2021-12-26 18:22:20 +00:00
return get_actor_property_url(actor_json, 'Blog')