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

View file

@ -1,27 +1,34 @@
from flask import Blueprint, render_template
import requests
threads = Blueprint('threads', __name__, template_folder='template')
# TODO: move following to an app config or sqlite
server = 'https://social.ayco.io'
thread_ids = ['112319729193615365', '112258065967208438']
title = "Ayo's Threads"
attribution = {
"owner": "Ayo Ayco",
"year": "2024"
}
# TODO: fetch only parent statuses
@threads.route('/')
def home():
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>')
def thread(id):
thread = fetch_thread(id)
return thread
status = fetch_thread(id)
return render_template('threads.html', threads=[status], title=title, attribution=attribution)
@threads.route('/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():
statuses = []
@ -31,24 +38,25 @@ def fetch_statuses():
statuses.append(status)
return statuses
def fetch_thread(id):
status = requests.get(server + '/api/v1/statuses/' + id ).json()
status = clean_status(status)
status['descendants'] = get_descendants(server, status)
return render_template('threads.html', threads=[status])
return status
def get_descendants(server, status):
author_id = status['account']['id']
context = requests.get(server + '/api/v1/statuses/' + status['id'] + '/context').json()
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:
descendants.append(clean_status(reply))
return descendants
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):
clean = clean_dict(status, ['id', 'content', 'created_at', 'url', 'media_attachments', 'card'])