geminispace.info

gemini search engine
git clone https://git.clttr.info/geminispace.info.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

commit b6ddd91524cf60db0e7cd93516b78c8f0f38ae4a
parent 3d014404a2f9f01a348a516ad1c9ed03fc603d4f
Author: Natalie Pendragon <natpen@natpen.net>
Date:   Wed,  5 Aug 2020 09:03:27 -0400

[threads] Add different sort orders for threads

Diffstat:
Mserve/models.py | 34+++++++++++++++++++++++++++++++---
Mserve/templates/threads.gmi | 10++++++++++
Mserve/views.py | 4+++-
3 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/serve/models.py b/serve/models.py @@ -88,8 +88,10 @@ ORDER BY l.is_cross_host_like, p_from.url ASC""", u, f"{u}/", resource.normalize return internal_backlink_urls, external_backlink_urls - def get_threads(self): - threads_query = Thread.raw("""SELECT t.* + def get_threads(self, sort="recency"): + sort = sort.lower() + if sort == "recency": + threads_query = Thread.raw("""SELECT t.* , tp.address , tp.friendly_author , tp.friendly_title @@ -110,6 +112,33 @@ ON c.page_id == p.id WHERE c.status == 20 GROUP BY tp.id ORDER BY t.updated_at DESC, t.id ASC, tp.address ASC""") + elif sort == "length": + threads_query = Thread.raw("""SELECT t.* + , tp.address + , tp.friendly_author + , tp.friendly_title + , p.fetchable_url + , p.url + , MIN(c.timestamp) AS first_seen +FROM ( + SELECT t.*, COUNT(tp.id) AS thread_length + FROM thread AS t + JOIN threadpage AS tp + ON tp.thread_id == t.id + GROUP BY t.id + ORDER BY thread_length DESC + LIMIT 50) AS t +JOIN threadpage AS tp +ON tp.thread_id == t.id +JOIN page AS p +ON p.id == tp.page_id +JOIN crawl AS c +ON c.page_id == p.id +WHERE c.status == 20 +GROUP BY tp.id +ORDER BY t.thread_length DESC, t.updated_at DESC, t.id ASC, tp.address ASC""") + else: + threads_query = "" threads = [] last_date = None last_id = None @@ -165,7 +194,6 @@ OR p.content_type IN ('application/atom+xml', 'application/rss+xml') return feeds_query.execute() - def get_search_suggestions(self, query): suggestions = [] corrector = self.searcher.corrector("content") diff --git a/serve/templates/threads.gmi b/serve/templates/threads.gmi @@ -3,6 +3,16 @@ ## Threads +{% if sort == "recency" %} +### Sort +Most recent (current) +=> /threads?length Switch to longest +{% elif sort == "length" %} +### Sort +Longest (current) +=> /threads?recency Switch to most recent +{% endif %} + {% for date in threads %} ### {{ date["date"] | datetimeformat("%Y, %b %d") }} diff --git a/serve/views.py b/serve/views.py @@ -152,8 +152,10 @@ def backlinks(request): @app.route("/threads") def threads(request): - threads = gus.get_threads() + sort = request.query or "recency" + threads = gus.get_threads(sort) body = render_template("threads.gmi", threads=threads, + sort=sort, index_modification_time=gus.statistics["index_modification_time"]) return Response(Status.SUCCESS, "text/gemini", body)