refactor: make template generic; put all config in one place

This commit is contained in:
Ayo Ayco 2024-04-25 15:43:35 +02:00
parent d25a2dd6b3
commit 6914f7e479
2 changed files with 35 additions and 15 deletions

View file

@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ayo's Threads</title> <title>{{ title }}</title>
<link rel="stylesheet" href="https://webcomponent.io/reset.css" /> <link rel="stylesheet" href="https://webcomponent.io/reset.css" />
<style> <style>
body { body {
@ -61,6 +61,14 @@
border: 1px solid rgba(34, 34, 34, 0.15); border: 1px solid rgba(34, 34, 34, 0.15);
} }
& .author a {
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
& .meta { & .meta {
margin-bottom: 5px; margin-bottom: 5px;
@ -94,11 +102,10 @@
</head> </head>
<body> <body>
<header> <header>
<h1>Ayo's Threads</h1> <h1>{{ title }}</h1>
<p>See the <a href="https://ayco.io/sh/threads">source code</a>. Or <a href="/">go home</a>.</p> <a href="/">go home</a></nav>
</header> </header>
<main> <main>
<ul> <ul>
{% for thread in threads %} {% for thread in threads %}
<li class="card"> <li class="card">
@ -112,6 +119,10 @@
<div class="card_content"> <div class="card_content">
<h3 class="author">
<a rel="author" href="{{thread.account.url}}">{{thread.account.display_name}}</a>
</h3>
{{thread.content | safe}} {{thread.content | safe}}
{% for media in thread.media_attachments %} {% for media in thread.media_attachments %}
@ -169,7 +180,8 @@
</ul> </ul>
</main> </main>
<footer> <footer>
&copy; 2024 Ayo Ayco &copy; {{ attribution.year }} {{ attribution.owner }}
<p>See the <a href="https://ayco.io/sh/threads">source code</a>. Or <a href="/">go home</a>.</p>
</footer> </footer>
</body> </body>
</html> </html>

View file

@ -1,27 +1,34 @@
from flask import Blueprint, render_template from flask import Blueprint, render_template
import requests import requests
threads = Blueprint('threads', __name__, template_folder='template') threads = Blueprint('threads', __name__, template_folder='template')
# TODO: move following to an app config or sqlite
server = 'https://social.ayco.io' server = 'https://social.ayco.io'
thread_ids = ['112319729193615365', '112258065967208438'] thread_ids = ['112319729193615365', '112258065967208438']
title = "Ayo's Threads"
attribution = {
"owner": "Ayo Ayco",
"year": "2024"
}
# TODO: fetch only parent statuses
@threads.route('/') @threads.route('/')
def home(): def home():
statuses = fetch_statuses() statuses = fetch_statuses()
return render_template('threads.html', threads=statuses) return render_template('threads.html', threads=statuses, title=title, attribution=attribution)
# TODO: given parent status id, show page for full thread
@threads.route('/<path:id>') @threads.route('/<path:id>')
def thread(id): def thread(id):
thread = fetch_thread(id) status = fetch_thread(id)
return thread return render_template('threads.html', threads=[status], title=title, attribution=attribution)
@threads.route('/api') @threads.route('/api')
def api(): def api():
return fetch_threads(); return fetch_statuses();
@threads.route('/api/<path:id>')
def api_thread(id):
return fetch_thread(id)
def fetch_statuses(): def fetch_statuses():
statuses = [] statuses = []
@ -31,24 +38,25 @@ def fetch_statuses():
statuses.append(status) statuses.append(status)
return statuses return statuses
def fetch_thread(id): def fetch_thread(id):
status = requests.get(server + '/api/v1/statuses/' + id ).json() status = requests.get(server + '/api/v1/statuses/' + id ).json()
status = clean_status(status) status = clean_status(status)
status['descendants'] = get_descendants(server, status) status['descendants'] = get_descendants(server, status)
return render_template('threads.html', threads=[status]) return status
def get_descendants(server, status): def get_descendants(server, status):
author_id = status['account']['id'] author_id = status['account']['id']
context = requests.get(server + '/api/v1/statuses/' + status['id'] + '/context').json() context = requests.get(server + '/api/v1/statuses/' + status['id'] + '/context').json()
descendants = [] descendants = []
for reply in context['descendants']: for reply in context['descendants']:
# TODO: the following condition will include a reply to a reply of the author
# - edge case: a different author replies in the thread and the author replies then replies again
if reply['account']['id'] == author_id and reply['in_reply_to_account_id'] == author_id: if reply['account']['id'] == author_id and reply['in_reply_to_account_id'] == author_id:
descendants.append(clean_status(reply)) descendants.append(clean_status(reply))
return descendants return descendants
def clean_author(account): def clean_author(account):
return clean_dict(account, ['avatar', 'display_name', 'id']) return clean_dict(account, ['avatar', 'display_name', 'id', 'url'])
def clean_status(status): def clean_status(status):
clean = clean_dict(status, ['id', 'content', 'created_at', 'url', 'media_attachments', 'card']) clean = clean_dict(status, ['id', 'content', 'created_at', 'url', 'media_attachments', 'card'])