re-add rss feeds

This commit is contained in:
2026-06-10 22:30:53 +03:00
parent 50c61da8b6
commit bf3028e7d6
9 changed files with 113 additions and 12 deletions

View File

@@ -6,7 +6,7 @@ from pygments.lexers import get_lexer_by_name
from pygments.util import ClassNotFound as PygmentsClassNotFound
import re
BABYCODE_VERSION = 11
BABYCODE_VERSION = 13
class BabycodeError(Exception):
@@ -406,6 +406,13 @@ def tag_quote(children, attr):
return f'<fieldset class="plank minimal no-shadow secondary-bg"><legend>{quotee}</legend><blockquote>{children}</blockquote></fieldset>'
def tag_quote_rss(children, attr):
if attr:
quotee = f'Quoting: {attr.strip()}'
return f'<figure><blockquote>{children}</blockquote><figcaption>{quotee}</figcaption></figure>'
else:
return f'<blockquote>{children}</blockquote>'
TAGS = {
"b": lambda children, attr: f"<strong>{children}</strong>",
"i": lambda children, attr: f"<em>{children}</em>",
@@ -462,6 +469,7 @@ RSS_TAGS = {
'url': tag_url_rss,
'spoiler': lambda children, attr: f'<details><summary>{attr or "Spoiler"} (click to reveal)</summary>{children}</details>',
'code': tag_code_rss,
'quote': tag_quote_rss,
'big': lambda children, attr: f'<span style="font-size: 1.2em">{children}</span>',
'small': lambda children, attr: f'<small>{children}</small>'

View File

@@ -1,8 +1,10 @@
from flask import Blueprint, redirect, url_for, render_template, request, abort
from flask import Blueprint, redirect, url_for, render_template, request, abort, current_app
from functools import wraps
from app import cache
from ..auth import login_required, get_active_user, is_logged_in
from ..db import db
from ..models import Threads, Posts, Topics, Users, Reactions, Subscriptions
from ..lib.render_atom import render_atom_template
from ..util import get_form_checkbox, time_now
import math
@@ -70,9 +72,23 @@ def thread(thread_id, slug):
posts=posts, page=page,
page_count=page_count, topic=topic,
started_by=started_by, topics=Topics.get_list(),
Reactions=Reactions, last_post=last_post
Reactions=Reactions, last_post=last_post,
__feedlink=url_for('.feed', thread_id=thread_id, _external=True),
__feedtitle=f'replies to {thread.title}',
)
@bp.get('/<int:thread_id>/feed.atom/')
@cache.cached(timeout=5 * 60, unless=lambda: current_app.config['DEBUG'])
def feed(thread_id):
thread = Threads.find({'id': thread_id})
if not thread:
abort(404)
topic = Topics.find({'id': thread.topic_id})
posts = thread.get_posts_rss()
return render_atom_template('threads/thread.atom', thread=thread, topic=topic, posts=posts)
@bp.post('/<int:thread_id>/')
@login_required
def reply(thread_id):
@@ -195,10 +211,6 @@ def mark_read():
return redirect(url_for('users.inbox', username=user.username))
@bp.get('/<int:thread_id>/feed.atom/')
def feed(thread_id):
return 'stub'
@bp.get('/new/')
@login_required
def new():

View File

@@ -1,5 +1,6 @@
from flask import Blueprint, redirect, url_for, render_template, request, session, abort
from flask import Blueprint, redirect, url_for, render_template, request, session, abort, current_app
from app import cache
from ..lib.render_atom import render_atom_template
from ..models import Topics, Threads, Subscriptions
from ..auth import get_active_user
import math
@@ -42,8 +43,20 @@ def topic(topic_id, slug):
subscription = Subscriptions.find({'user_id': user.id, 'thread_id': thread['id']})
if subscription:
subscriptions[thread['id']] = subscription.get_unread_count()
return render_template('topics/topic.html', topic=topic, threads=threads, sort_by=sort_by, page=page, page_count=page_count, subscriptions=subscriptions)
return render_template(
'topics/topic.html', topic=topic,
threads=threads, sort_by=sort_by,
page=page, page_count=page_count, subscriptions=subscriptions,
__feedlink=url_for('.feed', topic_id=topic_id, _external=True),
__feedtitle=f'latest threads in {topic.name}',
)
@bp.get('/<int:topic_id>/feed.atom/')
@cache.cached(timeout=5 * 60, unless=lambda: current_app.config['DEBUG'])
def feed(topic_id):
return 'stub'
topic = Topics.find({'id': topic_id})
if not topic:
abort(404)
threads_list = topic.get_threads_with_op_rss()
return render_atom_template('topics/topic.atom', topic=topic, threads_list=threads_list)

20
app/templates/base.atom Normal file
View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
{%- if self.title() -%}
<title>{%- block title -%}{%- endblock -%}</title>
{%- else -%}
<title>{{- config.SITE_NAME -}}</title>
{%- endif -%}
{%- if self.feed_updated() -%}
<updated>{%- block feed_updated -%}{%- endblock -%}</updated>
{%- else -%}
<updated>{{- get_time_now() | iso8601 -}}</updated>
{%- endif -%}
<id>{{- __current_page -}}</id>
<link rel="self" href="{{ __current_page }}" />
<link href="{%- block canonical_link -%}{%- endblock -%}" />
{%- if self.feed_author() -%}
<author>{%- block feed_author -%}{%- endblock -%}</author>
{%- endif -%}
{%- block content -%}{%- endblock -%}
</feed>

View File

@@ -11,6 +11,9 @@
{%- else -%}
<title>{{ config.SITE_NAME }}</title>
{%- endif -%}
{%- if __feedlink -%}
<link rel="alternate" type="application/atom+xml" href="{{ __feedlink }}" title="{{ __feedtitle }}">
{%- endif -%}
</head>
<body>
<bitty-8 data-connect="/static/js/bits/progressive-enhancement.js"></bitty-8>

View File

@@ -302,3 +302,7 @@
<div class="sortable-item-inner {{full and 'full' or ''}}">{{ caller() }}</div>
</li>
{%- endmacro %}
{% macro rss_html_content(html) -%}
<content type="html">{{ html }}</content>
{%- endmacro %}

View File

@@ -0,0 +1,20 @@
{% from 'common/macros.html' import rss_html_content %}
{%- extends 'base.atom' -%}
{%- block title -%}replies to {{thread.title}}{%- endblock -%}
{%- block canonical_link -%}{{ url_for('threads.thread_by_id', thread_id=thread.id, _external=true) }}{%- endblock -%}
{%- block content -%}
{%- for post in posts -%}
{%- set post_url = get_post_url(post.id, _anchor=true, external=true) -%}
<entry>
<title>Re: {{ thread.title | escape }}</title>
<link href="{{ post_url }}"/>
<id>{{ post_url }}</id>
<updated>{{ post.edited_at | iso8601 }}</updated>
{{ rss_html_content(post.content_rss) }}
<author>
<name>{{ post.display_name | escape }} @{{ post.username }}</name>
<uri>{{ url_for('users.user_page', username=post.username, _external=true) }}</uri>
</author>
</entry>
{%- endfor -%}
{%- endblock -%}

View File

@@ -0,0 +1,21 @@
{% from 'common/macros.html' import rss_html_content %}
{%- extends 'base.atom' -%}
{%- block title -%}latest threads in {{topic.name | escape}}{%- endblock -%}
{%- block canonical_link -%}{{ url_for('topics.topic_by_id', topic_id=topic.id, _external=true) }}{%- endblock -%}
{%- block content -%}
<subtitle>{{ topic.description | escape }}</subtitle>
{%- for thread in threads_list -%}
<entry>
<title>{{ thread.title | escape }}</title>
<link href="{{ url_for('threads.thread_by_id', thread_id=thread.id, _external=true) }}"/>
<link rel="replies" type="application/atom+xml" href="{{ url_for('threads.feed', thread_id=thread.id, _external=true) }}"/>
<id>{{ url_for('threads.thread_by_id', thread_id=thread.id, _external=true) }}</id>
{{ rss_html_content(thread.original_post_content) }}
<updated>{{ thread.created_at | iso8601 }}</updated>
<author>
<name>{{ thread.started_by_display_name | escape }} @{{ thread.started_by }}</name>
<uri>{{ url_for('users.user_page', username=thread.started_by, _external=true) }}</uri>
</author>
</entry>
{%- endfor -%}
{%- endblock -%}