epicyon/reaction.py

595 lines
22 KiB
Python
Raw Normal View History

2021-11-10 12:16:03 +00:00
__filename__ = "reaction.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.2.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "ActivityPub"
import os
2021-11-10 13:10:02 +00:00
import re
2021-11-10 21:43:48 +00:00
import urllib.parse
2021-11-10 12:16:03 +00:00
from pprint import pprint
2021-12-26 17:12:07 +00:00
from utils import has_object_string
2021-12-26 15:54:46 +00:00
from utils import has_object_string_object
2021-12-26 17:12:07 +00:00
from utils import has_object_stringType
2021-12-26 18:17:37 +00:00
from utils import remove_domain_port
2021-12-26 10:57:03 +00:00
from utils import has_object_dict
2021-12-26 12:19:00 +00:00
from utils import has_users_path
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2021-12-27 11:20:57 +00:00
from utils import remove_id_ending
2021-12-27 20:47:05 +00:00
from utils import url_permitted
2021-12-27 22:19:18 +00:00
from utils import get_nickname_from_actor
2021-12-27 19:05:25 +00:00
from utils import get_domain_from_actor
2021-12-26 20:36:08 +00:00
from utils import locate_post
2021-12-27 23:02:50 +00:00
from utils import undo_reaction_collection_entry
2021-12-26 17:53:07 +00:00
from utils import has_group_type
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
2021-12-26 15:13:34 +00:00
from utils import load_json
2021-12-26 14:47:21 +00:00
from utils import save_json
2021-12-27 11:05:24 +00:00
from utils import remove_post_from_cache
2021-12-26 23:41:34 +00:00
from utils import get_cached_post_filename
2021-12-27 17:53:41 +00:00
from utils import contains_invalid_chars
2021-12-29 21:55:09 +00:00
from posts import send_signed_json
from session import post_json
from webfinger import webfinger_handle
2021-12-28 21:36:27 +00:00
from auth import create_basic_auth_header
2021-12-29 21:55:09 +00:00
from posts import get_person_box
2021-11-10 12:16:03 +00:00
2021-11-10 17:35:54 +00:00
# the maximum number of reactions from individual actors which can be
# added to a post. Hence an adversary can't bombard you with sockpuppet
# generated reactions and make the post infeasibly large
maxActorReactionsPerPost = 64
2021-11-10 12:16:03 +00:00
2021-11-10 17:35:54 +00:00
# regex defining permissable emoji icon range
2021-11-10 13:10:02 +00:00
emojiRegex = re.compile(r'[\u263a-\U0001f645]')
2021-12-29 21:55:09 +00:00
def valid_emoji_content(emojiContent: str) -> bool:
2021-11-10 13:10:02 +00:00
"""Is the given emoji content valid?
"""
if not emojiContent:
return False
if len(emojiContent) > 2:
2021-11-10 13:10:02 +00:00
return False
if len(emojiRegex.findall(emojiContent)) == 0:
return False
2021-12-27 17:53:41 +00:00
if contains_invalid_chars(emojiContent):
return False
2021-11-10 13:10:02 +00:00
return True
2021-12-29 21:55:09 +00:00
def _reactionpost(recent_posts_cache: {},
session, base_dir: str, federation_list: [],
nickname: str, domain: str, port: int,
ccList: [], http_prefix: str,
objectUrl: str, emojiContent: str,
actorReaction: str,
client_to_server: bool,
send_threads: [], postLog: [],
person_cache: {}, cached_webfingers: {},
debug: bool, project_version: str,
signing_priv_key_pem: str) -> {}:
2021-11-10 12:16:03 +00:00
"""Creates an emoji reaction
actor is the person doing the reacting
'to' might be a specific person (actor) whose post was reaction
object is typically the url of the message which was reaction
"""
2021-12-27 20:47:05 +00:00
if not url_permitted(objectUrl, federation_list):
2021-11-10 12:16:03 +00:00
return None
2021-12-29 21:55:09 +00:00
if not valid_emoji_content(emojiContent):
2021-11-10 13:10:02 +00:00
print('_reaction: Invalid emoji reaction: "' + emojiContent + '"')
return
2021-11-10 12:16:03 +00:00
2021-12-26 12:45:03 +00:00
fullDomain = get_full_domain(domain, port)
2021-11-10 12:16:03 +00:00
newReactionJson = {
"@context": "https://www.w3.org/ns/activitystreams",
'type': 'EmojiReact',
2021-12-26 10:19:59 +00:00
'actor': local_actor_url(http_prefix, nickname, fullDomain),
2021-11-10 12:16:03 +00:00
'object': objectUrl,
'content': emojiContent
}
if ccList:
if len(ccList) > 0:
newReactionJson['cc'] = ccList
# Extract the domain and nickname from a statuses link
2021-12-29 21:55:09 +00:00
reaction_postNickname = None
reaction_postDomain = None
reaction_postPort = None
2021-12-26 00:07:44 +00:00
group_account = False
2021-11-10 12:16:03 +00:00
if actorReaction:
2021-12-29 21:55:09 +00:00
reaction_postNickname = get_nickname_from_actor(actorReaction)
reaction_postDomain, reaction_postPort = \
2021-12-27 19:05:25 +00:00
get_domain_from_actor(actorReaction)
2021-12-26 17:53:07 +00:00
group_account = has_group_type(base_dir, actorReaction, person_cache)
2021-11-10 12:16:03 +00:00
else:
2021-12-26 12:19:00 +00:00
if has_users_path(objectUrl):
2021-12-29 21:55:09 +00:00
reaction_postNickname = get_nickname_from_actor(objectUrl)
reaction_postDomain, reaction_postPort = \
2021-12-27 19:05:25 +00:00
get_domain_from_actor(objectUrl)
2021-12-29 21:55:09 +00:00
if '/' + str(reaction_postNickname) + '/' in objectUrl:
2021-11-10 12:16:03 +00:00
actorReaction = \
2021-12-29 21:55:09 +00:00
objectUrl.split('/' + reaction_postNickname + '/')[0] + \
'/' + reaction_postNickname
2021-12-26 00:07:44 +00:00
group_account = \
2021-12-26 17:53:07 +00:00
has_group_type(base_dir, actorReaction, person_cache)
2021-11-10 12:16:03 +00:00
2021-12-29 21:55:09 +00:00
if reaction_postNickname:
2021-12-26 23:41:34 +00:00
post_filename = locate_post(base_dir, nickname, domain, objectUrl)
if not post_filename:
2021-12-25 16:17:53 +00:00
print('DEBUG: reaction base_dir: ' + base_dir)
2021-11-10 12:16:03 +00:00
print('DEBUG: reaction nickname: ' + nickname)
print('DEBUG: reaction domain: ' + domain)
print('DEBUG: reaction objectUrl: ' + objectUrl)
return None
2021-12-29 21:55:09 +00:00
update_reaction_collection(recent_posts_cache,
base_dir, post_filename, objectUrl,
newReactionJson['actor'],
nickname, domain, debug, None,
emojiContent)
send_signed_json(newReactionJson, session, base_dir,
nickname, domain, port,
reaction_postNickname,
reaction_postDomain, reaction_postPort,
'https://www.w3.org/ns/activitystreams#Public',
http_prefix, True, client_to_server, federation_list,
send_threads, postLog, cached_webfingers,
person_cache,
debug, project_version, None, group_account,
signing_priv_key_pem, 7165392)
2021-11-10 12:16:03 +00:00
return newReactionJson
2021-12-29 21:55:09 +00:00
def reaction_post(recent_posts_cache: {},
session, base_dir: str, federation_list: [],
nickname: str, domain: str, port: int, http_prefix: str,
reactionNickname: str, reactionDomain: str,
reactionPort: int, ccList: [],
reactionStatusNumber: int, emojiContent: str,
client_to_server: bool,
send_threads: [], postLog: [],
person_cache: {}, cached_webfingers: {},
debug: bool, project_version: str,
signing_priv_key_pem: str) -> {}:
2021-11-10 12:16:03 +00:00
"""Adds a reaction to a given status post. This is only used by unit tests
"""
2021-12-26 12:45:03 +00:00
reactionDomain = get_full_domain(reactionDomain, reactionPort)
2021-11-10 12:16:03 +00:00
2021-12-25 17:09:22 +00:00
actorReaction = \
2021-12-26 10:19:59 +00:00
local_actor_url(http_prefix, reactionNickname, reactionDomain)
2021-11-10 12:16:03 +00:00
objectUrl = actorReaction + '/statuses/' + str(reactionStatusNumber)
2021-12-29 21:55:09 +00:00
return _reactionpost(recent_posts_cache,
session, base_dir, federation_list,
nickname, domain, port,
ccList, http_prefix, objectUrl, emojiContent,
actorReaction, client_to_server,
send_threads, postLog, person_cache,
cached_webfingers,
debug, project_version, signing_priv_key_pem)
def send_reaction_via_server(base_dir: str, session,
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
http_prefix: str, reactionUrl: str,
emojiContent: str,
cached_webfingers: {}, person_cache: {},
debug: bool, project_version: str,
signing_priv_key_pem: str) -> {}:
2021-11-10 12:16:03 +00:00
"""Creates a reaction via c2s
"""
if not session:
2021-12-29 21:55:09 +00:00
print('WARN: No session for send_reaction_via_server')
2021-11-10 12:16:03 +00:00
return 6
2021-12-29 21:55:09 +00:00
if not valid_emoji_content(emojiContent):
print('send_reaction_via_server: Invalid emoji reaction: "' +
2021-11-10 13:10:02 +00:00
emojiContent + '"')
return 7
2021-11-10 12:16:03 +00:00
2021-12-26 12:45:03 +00:00
fromDomainFull = get_full_domain(fromDomain, fromPort)
2021-11-10 12:16:03 +00:00
2021-12-26 10:19:59 +00:00
actor = local_actor_url(http_prefix, fromNickname, fromDomainFull)
2021-11-10 12:16:03 +00:00
newReactionJson = {
"@context": "https://www.w3.org/ns/activitystreams",
'type': 'EmojiReact',
'actor': actor,
'object': reactionUrl,
'content': emojiContent
}
2021-12-25 17:09:22 +00:00
handle = http_prefix + '://' + fromDomainFull + '/@' + fromNickname
2021-11-10 12:16:03 +00:00
# lookup the inbox for the To handle
2021-12-29 21:55:09 +00:00
wfRequest = webfinger_handle(session, handle, http_prefix,
cached_webfingers,
fromDomain, project_version, debug, False,
signing_priv_key_pem)
2021-11-10 12:16:03 +00:00
if not wfRequest:
if debug:
print('DEBUG: reaction webfinger failed for ' + handle)
return 1
if not isinstance(wfRequest, dict):
print('WARN: reaction webfinger for ' + handle +
' did not return a dict. ' + str(wfRequest))
return 1
postToBox = 'outbox'
# get the actor inbox for the To handle
originDomain = fromDomain
(inboxUrl, pubKeyId, pubKey, fromPersonId, sharedInbox, avatarUrl,
2021-12-29 21:55:09 +00:00
displayName, _) = get_person_box(signing_priv_key_pem,
originDomain,
base_dir, session, wfRequest,
person_cache,
project_version, http_prefix,
fromNickname, fromDomain,
postToBox, 72873)
2021-11-10 12:16:03 +00:00
if not inboxUrl:
if debug:
print('DEBUG: reaction no ' + postToBox +
' was found for ' + handle)
return 3
if not fromPersonId:
if debug:
print('DEBUG: reaction no actor was found for ' + handle)
return 4
2021-12-28 21:36:27 +00:00
authHeader = create_basic_auth_header(fromNickname, password)
2021-11-10 12:16:03 +00:00
headers = {
'host': fromDomain,
'Content-type': 'application/json',
'Authorization': authHeader
}
2021-12-29 21:55:09 +00:00
postResult = post_json(http_prefix, fromDomainFull,
session, newReactionJson, [], inboxUrl,
headers, 3, True)
2021-11-10 12:16:03 +00:00
if not postResult:
if debug:
print('WARN: POST reaction failed for c2s to ' + inboxUrl)
return 5
if debug:
print('DEBUG: c2s POST reaction success')
return newReactionJson
2021-12-29 21:55:09 +00:00
def send_undo_reaction_via_server(base_dir: str, session,
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
http_prefix: str, reactionUrl: str,
emojiContent: str,
cached_webfingers: {}, person_cache: {},
debug: bool, project_version: str,
signing_priv_key_pem: str) -> {}:
2021-11-10 12:16:03 +00:00
"""Undo a reaction via c2s
"""
if not session:
2021-12-29 21:55:09 +00:00
print('WARN: No session for send_undo_reaction_via_server')
2021-11-10 12:16:03 +00:00
return 6
2021-12-26 12:45:03 +00:00
fromDomainFull = get_full_domain(fromDomain, fromPort)
2021-11-10 12:16:03 +00:00
2021-12-26 10:19:59 +00:00
actor = local_actor_url(http_prefix, fromNickname, fromDomainFull)
2021-11-10 12:16:03 +00:00
newUndoReactionJson = {
"@context": "https://www.w3.org/ns/activitystreams",
'type': 'Undo',
'actor': actor,
'object': {
'type': 'EmojiReact',
'actor': actor,
'object': reactionUrl,
'content': emojiContent
}
}
2021-12-25 17:09:22 +00:00
handle = http_prefix + '://' + fromDomainFull + '/@' + fromNickname
2021-11-10 12:16:03 +00:00
# lookup the inbox for the To handle
2021-12-29 21:55:09 +00:00
wfRequest = webfinger_handle(session, handle, http_prefix,
cached_webfingers,
fromDomain, project_version, debug, False,
signing_priv_key_pem)
2021-11-10 12:16:03 +00:00
if not wfRequest:
if debug:
print('DEBUG: unreaction webfinger failed for ' + handle)
return 1
if not isinstance(wfRequest, dict):
if debug:
print('WARN: unreaction webfinger for ' + handle +
' did not return a dict. ' + str(wfRequest))
return 1
postToBox = 'outbox'
# get the actor inbox for the To handle
originDomain = fromDomain
(inboxUrl, pubKeyId, pubKey, fromPersonId, sharedInbox, avatarUrl,
2021-12-29 21:55:09 +00:00
displayName, _) = get_person_box(signing_priv_key_pem,
originDomain,
base_dir, session, wfRequest,
person_cache, project_version,
http_prefix, fromNickname,
fromDomain, postToBox,
72625)
2021-11-10 12:16:03 +00:00
if not inboxUrl:
if debug:
print('DEBUG: unreaction no ' + postToBox +
' was found for ' + handle)
return 3
if not fromPersonId:
if debug:
print('DEBUG: unreaction no actor was found for ' + handle)
return 4
2021-12-28 21:36:27 +00:00
authHeader = create_basic_auth_header(fromNickname, password)
2021-11-10 12:16:03 +00:00
headers = {
'host': fromDomain,
'Content-type': 'application/json',
'Authorization': authHeader
}
2021-12-29 21:55:09 +00:00
postResult = post_json(http_prefix, fromDomainFull,
session, newUndoReactionJson, [], inboxUrl,
headers, 3, True)
2021-11-10 12:16:03 +00:00
if not postResult:
if debug:
print('WARN: POST unreaction failed for c2s to ' + inboxUrl)
return 5
if debug:
print('DEBUG: c2s POST unreaction success')
return newUndoReactionJson
2021-12-29 21:55:09 +00:00
def outbox_reaction(recent_posts_cache: {},
base_dir: str, http_prefix: str,
nickname: str, domain: str, port: int,
message_json: {}, debug: bool) -> None:
2021-11-10 12:16:03 +00:00
""" When a reaction request is received by the outbox from c2s
"""
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
2021-11-10 12:16:03 +00:00
if debug:
print('DEBUG: reaction - no type')
return
2021-12-25 23:51:19 +00:00
if not message_json['type'] == 'EmojiReact':
2021-11-10 12:16:03 +00:00
if debug:
print('DEBUG: not a reaction')
return
2021-12-26 17:12:07 +00:00
if not has_object_string(message_json, debug):
2021-11-10 12:16:03 +00:00
return
2021-12-25 23:51:19 +00:00
if not message_json.get('content'):
2021-11-10 12:16:03 +00:00
return
2021-12-25 23:51:19 +00:00
if not isinstance(message_json['content'], str):
2021-11-10 12:16:03 +00:00
return
2021-12-29 21:55:09 +00:00
if not valid_emoji_content(message_json['content']):
print('outbox_reaction: Invalid emoji reaction: "' +
2021-12-25 23:51:19 +00:00
message_json['content'] + '"')
2021-11-10 13:10:02 +00:00
return
2021-11-10 12:16:03 +00:00
if debug:
print('DEBUG: c2s reaction request arrived in outbox')
2021-12-27 11:20:57 +00:00
messageId = remove_id_ending(message_json['object'])
2021-12-26 18:17:37 +00:00
domain = remove_domain_port(domain)
2021-12-25 23:51:19 +00:00
emojiContent = message_json['content']
2021-12-26 23:41:34 +00:00
post_filename = locate_post(base_dir, nickname, domain, messageId)
if not post_filename:
2021-11-10 12:16:03 +00:00
if debug:
print('DEBUG: c2s reaction post not found in inbox or outbox')
print(messageId)
return True
2021-12-29 21:55:09 +00:00
update_reaction_collection(recent_posts_cache,
base_dir, post_filename, messageId,
message_json['actor'],
nickname, domain, debug, None, emojiContent)
2021-11-10 12:16:03 +00:00
if debug:
2021-12-26 23:41:34 +00:00
print('DEBUG: post reaction via c2s - ' + post_filename)
2021-11-10 12:16:03 +00:00
2021-12-29 21:55:09 +00:00
def outbox_undo_reaction(recent_posts_cache: {},
base_dir: str, http_prefix: str,
nickname: str, domain: str, port: int,
message_json: {}, debug: bool) -> None:
2021-11-10 12:16:03 +00:00
""" When an undo reaction request is received by the outbox from c2s
"""
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
2021-11-10 12:16:03 +00:00
return
2021-12-25 23:51:19 +00:00
if not message_json['type'] == 'Undo':
2021-11-10 12:16:03 +00:00
return
2021-12-26 17:12:07 +00:00
if not has_object_stringType(message_json, debug):
2021-11-10 12:16:03 +00:00
return
2021-12-25 23:51:19 +00:00
if not message_json['object']['type'] == 'EmojiReact':
2021-11-10 12:16:03 +00:00
if debug:
print('DEBUG: not a undo reaction')
return
2021-12-25 23:51:19 +00:00
if not message_json['object'].get('content'):
2021-11-10 12:16:03 +00:00
return
2021-12-25 23:51:19 +00:00
if not isinstance(message_json['object']['content'], str):
2021-11-10 12:16:03 +00:00
return
2021-12-26 15:54:46 +00:00
if not has_object_string_object(message_json, debug):
2021-11-10 12:16:03 +00:00
return
if debug:
print('DEBUG: c2s undo reaction request arrived in outbox')
2021-12-27 11:20:57 +00:00
messageId = remove_id_ending(message_json['object']['object'])
2021-12-25 23:51:19 +00:00
emojiContent = message_json['object']['content']
2021-12-26 18:17:37 +00:00
domain = remove_domain_port(domain)
2021-12-26 23:41:34 +00:00
post_filename = locate_post(base_dir, nickname, domain, messageId)
if not post_filename:
2021-11-10 12:16:03 +00:00
if debug:
print('DEBUG: c2s undo reaction post not found in inbox or outbox')
print(messageId)
return True
2021-12-27 23:02:50 +00:00
undo_reaction_collection_entry(recent_posts_cache, base_dir, post_filename,
messageId, message_json['actor'],
domain, debug, None, emojiContent)
2021-11-10 12:16:03 +00:00
if debug:
2021-12-26 23:41:34 +00:00
print('DEBUG: post undo reaction via c2s - ' + post_filename)
2021-11-10 12:16:03 +00:00
2021-12-29 21:55:09 +00:00
def update_reaction_collection(recent_posts_cache: {},
base_dir: str, post_filename: str,
objectUrl: str, actor: str,
nickname: str, domain: str, debug: bool,
post_json_object: {},
emojiContent: str) -> None:
2021-11-10 12:16:03 +00:00
"""Updates the reactions collection within a post
"""
2021-12-25 22:09:19 +00:00
if not post_json_object:
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-11-10 12:16:03 +00:00
return
# remove any cached version of this post so that the
# reaction icon is changed
2021-12-27 11:05:24 +00:00
remove_post_from_cache(post_json_object, recent_posts_cache)
2021-12-26 23:41:34 +00:00
cachedPostFilename = \
get_cached_post_filename(base_dir, nickname,
domain, post_json_object)
2021-11-10 12:16:03 +00:00
if cachedPostFilename:
if os.path.isfile(cachedPostFilename):
try:
os.remove(cachedPostFilename)
2021-11-25 18:42:38 +00:00
except OSError:
2021-12-29 21:55:09 +00:00
print('EX: update_reaction_collection unable to delete ' +
2021-11-10 12:16:03 +00:00
cachedPostFilename)
2021-12-25 22:09:19 +00:00
obj = post_json_object
2021-12-26 10:57:03 +00:00
if has_object_dict(post_json_object):
2021-12-25 22:09:19 +00:00
obj = post_json_object['object']
2021-11-10 12:16:03 +00:00
if not objectUrl.endswith('/reactions'):
objectUrl = objectUrl + '/reactions'
if not obj.get('reactions'):
if debug:
print('DEBUG: Adding initial emoji reaction to ' + objectUrl)
reactionsJson = {
"@context": "https://www.w3.org/ns/activitystreams",
'id': objectUrl,
'type': 'Collection',
"totalItems": 1,
'items': [{
'type': 'EmojiReact',
'actor': actor,
'content': emojiContent
}]
}
obj['reactions'] = reactionsJson
else:
if not obj['reactions'].get('items'):
obj['reactions']['items'] = []
2021-11-10 17:35:54 +00:00
# upper limit for the number of reactions on a post
if len(obj['reactions']['items']) >= maxActorReactionsPerPost:
return
2021-11-10 12:16:03 +00:00
for reactionItem in obj['reactions']['items']:
if reactionItem.get('actor') and reactionItem.get('content'):
if reactionItem['actor'] == actor and \
reactionItem['content'] == emojiContent:
# already reaction
return
newReaction = {
'type': 'EmojiReact',
'actor': actor,
'content': emojiContent
}
obj['reactions']['items'].append(newReaction)
itlen = len(obj['reactions']['items'])
obj['reactions']['totalItems'] = itlen
if debug:
print('DEBUG: saving post with emoji reaction added')
2021-12-25 22:09:19 +00:00
pprint(post_json_object)
2021-12-26 23:41:34 +00:00
save_json(post_json_object, post_filename)
2021-11-10 17:14:51 +00:00
2021-12-29 21:55:09 +00:00
def html_emoji_reactions(post_json_object: {}, interactive: bool,
actor: str, maxReactionTypes: int,
boxName: str, pageNumber: int) -> str:
2021-11-10 17:14:51 +00:00
"""html containing row of emoji reactions
displayed at the bottom of posts, above the icons
2021-11-10 17:14:51 +00:00
"""
2021-12-26 10:57:03 +00:00
if not has_object_dict(post_json_object):
2021-11-10 17:14:51 +00:00
return ''
2021-12-25 22:09:19 +00:00
if not post_json_object.get('actor'):
2021-11-12 11:40:27 +00:00
return ''
2021-12-25 22:09:19 +00:00
if not post_json_object['object'].get('reactions'):
2021-11-10 17:14:51 +00:00
return ''
2021-12-25 22:09:19 +00:00
if not post_json_object['object']['reactions'].get('items'):
2021-11-10 17:14:51 +00:00
return ''
reactions = {}
2021-11-10 21:55:56 +00:00
reactedToByThisActor = []
2021-12-25 22:09:19 +00:00
for item in post_json_object['object']['reactions']['items']:
2021-11-10 17:14:51 +00:00
emojiContent = item['content']
2021-11-10 21:55:56 +00:00
emojiActor = item['actor']
2021-12-27 22:19:18 +00:00
emojiNickname = get_nickname_from_actor(emojiActor)
2021-12-27 19:05:25 +00:00
emojiDomain, _ = get_domain_from_actor(emojiActor)
emojiHandle = emojiNickname + '@' + emojiDomain
2021-11-10 21:55:56 +00:00
if emojiActor == actor:
if emojiContent not in reactedToByThisActor:
reactedToByThisActor.append(emojiContent)
2021-11-10 17:14:51 +00:00
if not reactions.get(emojiContent):
if len(reactions.items()) < maxReactionTypes:
reactions[emojiContent] = {
"handles": [emojiHandle],
"count": 1
}
2021-11-10 17:14:51 +00:00
else:
reactions[emojiContent]['count'] += 1
if len(reactions[emojiContent]['handles']) < 32:
reactions[emojiContent]['handles'].append(emojiHandle)
2021-11-10 17:14:51 +00:00
if len(reactions.items()) == 0:
return ''
2021-12-27 11:20:57 +00:00
reactBy = remove_id_ending(post_json_object['object']['id'])
2021-11-10 17:14:51 +00:00
htmlStr = '<div class="emojiReactionBar">\n'
for emojiContent, item in reactions.items():
count = item['count']
# get the handles of actors who reacted
handlesStr = ''
item['handles'].sort()
for handle in item['handles']:
if handlesStr:
handlesStr += '&#10;'
handlesStr += handle
2021-11-10 21:55:56 +00:00
if emojiContent not in reactedToByThisActor:
2021-11-12 11:40:27 +00:00
baseUrl = actor + '?react=' + reactBy
2021-11-10 21:55:56 +00:00
else:
2021-11-12 11:40:27 +00:00
baseUrl = actor + '?unreact=' + reactBy
2021-12-25 22:09:19 +00:00
baseUrl += '?actor=' + post_json_object['actor']
2021-11-12 11:40:27 +00:00
baseUrl += '?tl=' + boxName
baseUrl += '?page=' + str(pageNumber)
baseUrl += '?emojreact='
2021-11-10 21:55:56 +00:00
2021-11-10 17:14:51 +00:00
htmlStr += ' <div class="emojiReactionButton">\n'
if count < 100:
countStr = str(count)
else:
countStr = '99+'
emojiContentStr = emojiContent + countStr
if interactive:
# urlencode the emoji
2021-11-10 21:43:48 +00:00
emojiContentEncoded = urllib.parse.quote_plus(emojiContent)
2021-11-10 17:14:51 +00:00
emojiContentStr = \
' <a href="' + baseUrl + emojiContentEncoded + \
'" title="' + handlesStr + '">' + \
2021-11-10 17:14:51 +00:00
emojiContentStr + '</a>\n'
htmlStr += emojiContentStr
htmlStr += ' </div>\n'
htmlStr += '</div>\n'
return htmlStr