escape user input and rename class

This commit is contained in:
Daniel Langbein 2022-12-13 13:39:48 +01:00
parent 1e147033af
commit abd03d9154
5 changed files with 25 additions and 18 deletions

View File

@ -2,22 +2,23 @@ from feedgen.feed import FeedGenerator
from flask import url_for
from mastodon_toot_follower import path_util
from mastodon_toot_follower.app import App
from mastodon_toot_follower.mastodon_factory import MastodonFactory
from mastodon_toot_follower.conversation.update import Update as ConversationUpdate
class Conversation:
def __init__(self, app: App, mastodon_instance: str, toot_id: str, seed: str = ''):
self.app = app
self.mastodon = self.app.get_mastodon(mastodon_instance)
def __init__(self, mastodon_factory: MastodonFactory, mastodon_instance: str, toot_id: str, seed: str = ''):
self.mastodon = mastodon_factory.get_mastodon(mastodon_instance)
self.toot_id = toot_id
self.toot = self.mastodon.status(id=self.toot_id)
escaped_uri = path_util.escape(self.toot["uri"])
if len(seed) > 0:
self.file = self.app.toot_cache_dir().joinpath(f'{path_util.escape(self.toot["uri"])}_{seed}.json')
escaped_seed = path_util.escape(seed)
self.file = mastodon_factory.toot_cache_dir().joinpath(f'{escaped_uri}_{escaped_seed}.json')
else:
self.file = self.app.toot_cache_dir().joinpath(f'{path_util.escape(self.toot["uri"])}.json')
self.file = mastodon_factory.toot_cache_dir().joinpath(f'{escaped_uri}.json')
# List of replies to initial toot.
self.replies = self.mastodon.status_context(id=self.toot_id)['descendants']
@ -31,8 +32,8 @@ class Conversation:
"""
Returns a FeedGenerator object "fg".
atomfeed = fg.atom_str(pretty=True) # Get the ATOM feed as string
rssfeed = fg.rss_str(pretty=True) # Get the RSS feed as string
atom_feed = fg.atom_str(pretty=True) # Get the ATOM feed as string
rss_feed = fg.rss_str(pretty=True) # Get the RSS feed as string
"""
fg = FeedGenerator()

View File

@ -81,7 +81,6 @@ class Update:
return f'Edit by {acct}'
def get_date(self):
acct = self.dict['acct']
if self.dict['reason'] == 'new':
return self.dict['created_at']
else:

View File

@ -3,7 +3,7 @@
import sys
from mastodon_toot_follower import mastodon_util
from mastodon_toot_follower.app import App
from mastodon_toot_follower.mastodon_factory import MastodonFactory
from mastodon_toot_follower.conversation.conversation import Conversation
@ -18,8 +18,10 @@ def main():
print(e, file=sys.stderr)
usage()
app = App()
conversation = Conversation(app=app,
instance_url, username, toot_id = None, None, None # Fix IDE Warnings
mastodon_factory = MastodonFactory()
conversation = Conversation(mastodon_factory=mastodon_factory,
mastodon_instance=instance_url,
toot_id=toot_id)
conversation.save_changes()

View File

@ -4,7 +4,7 @@ from mastodon import Mastodon
from mastodon_toot_follower import path_util
class App:
class MastodonFactory:
def __init__(self,
name: str = 'MastodonTootFollower',
url: str = 'https://git.privacy1st.de/langfingaz/MastodonTootFollower',

View File

@ -6,7 +6,7 @@ from flask import Flask, Response, render_template
from flask import request
from mastodon_toot_follower import mastodon_util, path_util
from mastodon_toot_follower.app import App as MastodonApp
from mastodon_toot_follower.mastodon_factory import MastodonFactory as MastodonFactory
from mastodon_toot_follower.conversation.conversation import Conversation
# Create Flask's `app` object
@ -17,7 +17,7 @@ app = Flask(
static_folder=str(path_util.get_static_dir()),
)
mastodon_app = MastodonApp()
mastodon_factory = MastodonFactory()
class Templates(Enum):
@ -35,7 +35,7 @@ def rss():
url = request.args.get('url', 'None')
instance_url, username, toot_id = mastodon_util.parse_toot_url(url=url)
conversation = Conversation(app=mastodon_app, mastodon_instance=instance_url, toot_id=toot_id)
conversation = Conversation(mastodon_factory=mastodon_factory, mastodon_instance=instance_url, toot_id=toot_id)
conversation.save_changes()
fg = conversation.as_feed(request.url)
@ -47,7 +47,9 @@ def html(seed: str):
url = request.args.get('url', 'None')
instance_url, username, toot_id = mastodon_util.parse_toot_url(url=url)
conversation = Conversation(app=mastodon_app, mastodon_instance=instance_url, toot_id=toot_id, seed=seed)
conversation = Conversation(mastodon_factory=mastodon_factory,
mastodon_instance=instance_url,
toot_id=toot_id, seed=seed)
conversation.save_changes()
return render_template(Templates.updates.value, updates=conversation.new_updates)
@ -58,7 +60,10 @@ def json(seed: str):
url = request.args.get('url', 'None')
instance_url, username, toot_id = mastodon_util.parse_toot_url(url=url)
conversation = Conversation(app=mastodon_app, mastodon_instance=instance_url, toot_id=toot_id, seed=seed)
conversation = Conversation(mastodon_factory=mastodon_factory,
mastodon_instance=instance_url,
toot_id=toot_id,
seed=seed)
conversation.save_changes()
# If you return a dict or list from a view, it will be converted to a JSON response.