From e9dc02e868e34c6af2585ef88879550c1165d034 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Fri, 10 Feb 2023 00:46:08 -0600 Subject: [PATCH] Core: Display variables within templates. Closes #515 --- doc/changelog.md | 6 ++++++ src/sessions/mastodon/templates.py | 14 ++------------ src/sessions/twitter/templates.py | 13 +++---------- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/doc/changelog.md b/doc/changelog.md index 6cac16cb..497141bc 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -2,6 +2,12 @@ TWBlue Changelog ## changes in this version +We had to release a 2023.2.8 version of TWblue, which removes the API deprecation code as Twitter has changed again their plans, and set their free API shutdown to February 13. This new release will not enforce any date for stopping support to Twitter sessions, but will skip loading the sessions if there are errors when logging into Twitter. There are also some significant improvements to our Mastodon support, which you can read in the following list of changes: + +* TWBlue should be able to display variables within templates (for example, now it is possible to send a template inside a post's text). Before, it was removing $variables so it was difficult to show how to edit templates from the client. ([#515](https://github.com/MCV-Software/TWBlue/issues/515)) + +## Changes on version 2023.2.6 + This release focuses on fixing some important bugs that have been reported in the previous version. Particularly, TWBlue should be able to authorize on some instances that have blocked the Mastodon.py library, and should be able to avoid repeatedly calling some endpoints that cause excessive connections for some instances. Additionally, it is possible to disable Streaming from the account options in Mastodon. This can be especially useful if TWBlue keeps making a lot of API calls for some instances. * Fixed the update system. diff --git a/src/sessions/mastodon/templates.py b/src/sessions/mastodon/templates.py index 0d5bdea3..d678600b 100644 --- a/src/sessions/mastodon/templates.py +++ b/src/sessions/mastodon/templates.py @@ -45,11 +45,6 @@ def process_image_descriptions(media_attachments): idescriptions = idescriptions + _("Image description: {}").format(image) + "\n" return idescriptions -def remove_unneeded_variables(template, variables): - for variable in variables: - template = re.sub("\$"+variable, "", template) - return template - def render_post(post, template, relative_times=False, offset_hours=0): """ Renders any given post according to the passed template. Available data for posts will be stored in the following variables: @@ -64,7 +59,7 @@ def render_post(post, template, relative_times=False, offset_hours=0): $visibility: post's visibility: public, not listed, followers only or direct. """ global post_variables - available_data = dict() + available_data = dict(source="") created_at = process_date(post.created_at, relative_times, offset_hours) available_data.update(date=created_at) # user. @@ -91,10 +86,8 @@ def render_post(post, template, relative_times=False, offset_hours=0): image_descriptions = process_image_descriptions(post.reblog.media_attachments) else: image_descriptions = process_image_descriptions(post.media_attachments) - if image_descriptions != "": - available_data.update(image_descriptions=image_descriptions) + available_data.update(image_descriptions=image_descriptions) result = Template(_(template)).safe_substitute(**available_data) - result = remove_unneeded_variables(result, post_variables) return result def render_user(user, template, relative_times=True, offset_hours=0): @@ -121,7 +114,6 @@ def render_user(user, template, relative_times=True, offset_hours=0): created_at = process_date(user.created_at, relative_times=relative_times, offset_hours=offset_hours) available_data.update(created_at=created_at) result = Template(_(template)).safe_substitute(**available_data) - result = remove_unneeded_variables(result, person_variables) return result def render_conversation(conversation, template, post_template, relative_times=False, offset_hours=0): @@ -135,7 +127,6 @@ def render_conversation(conversation, template, post_template, relative_times=Fa last_post = render_post(conversation.last_status, post_template, relative_times=relative_times, offset_hours=offset_hours) available_data = dict(users=users, last_post=last_post) result = Template(_(template)).safe_substitute(**available_data) - result = remove_unneeded_variables(result, conversation_variables) return result def render_notification(notification, template, post_template, relative_times=False, offset_hours=0): @@ -178,6 +169,5 @@ def render_notification(notification, template, post_template, relative_times=Fa text = _("wants to follow you.") available_data.update(text=text) result = Template(_(template)).safe_substitute(**available_data) - result = remove_unneeded_variables(result, post_variables) result = result.replace(" . ", "") return result diff --git a/src/sessions/twitter/templates.py b/src/sessions/twitter/templates.py index 7d97c701..83c73758 100644 --- a/src/sessions/twitter/templates.py +++ b/src/sessions/twitter/templates.py @@ -54,11 +54,6 @@ def process_image_descriptions(entities): idescriptions += _("Image description: {}.").format(image) return idescriptions -def remove_unneeded_variables(template, variables): - for variable in variables: - template = re.sub("\$"+variable, "", template) - return template - def render_tweet(tweet, template, session, relative_times=False, offset_seconds=0): """ Renders any given Tweet according to the passed template. Available data for tweets will be stored in the following variables: @@ -96,10 +91,8 @@ def render_tweet(tweet, template, session, relative_times=False, offset_seconds= image_descriptions = process_image_descriptions(tweet.retweeted_status.quoted_status.extended_entities) elif hasattr(tweet, "extended_entities"): image_descriptions = process_image_descriptions(tweet.extended_entities) - if image_descriptions != "": - available_data.update(image_descriptions=image_descriptions) + available_data.update(image_descriptions=image_descriptions) result = Template(_(template)).safe_substitute(**available_data) - result = remove_unneeded_variables(result, tweet_variables) return result def render_dm(dm, template, session, relative_times=False, offset_seconds=0): @@ -127,7 +120,6 @@ def render_dm(dm, template, session, relative_times=False, offset_seconds=0): recipient = session.get_user(dm.message_create["target"]["recipient_id"]) available_data.update(sender_display_name=sender.name, sender_screen_name=sender.screen_name, recipient_display_name=recipient.name, recipient_screen_name=recipient.screen_name) result = Template(_(template)).safe_substitute(**available_data) - result = remove_unneeded_variables(result, dm_variables) return result # Sesion object is not used in this function but we keep compatibility across all rendering functions. @@ -152,8 +144,9 @@ def render_person(user, template, session=None, relative_times=True, offset_seco for nullable in nullables: if hasattr(user, nullable) and getattr(user, nullable) != None: available_data[nullable] = getattr(user, nullable) + else: + available_data[nullable] = "" created_at = process_date(user.created_at, relative_times=relative_times, offset_seconds=offset_seconds) available_data.update(created_at=created_at) result = Template(_(template)).safe_substitute(**available_data) - result = remove_unneeded_variables(result, person_variables) return result \ No newline at end of file