Skip to content

Commit 5faa12b

Browse files
committed
chg: [forum] sort threads by last post date
1 parent 6eb77c3 commit 5faa12b

5 files changed

Lines changed: 49 additions & 4 deletions

File tree

bin/lib/forums_viewer.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from lib.objects import ail_objects
2323
from lib import crawlers
2424
from lib import Language
25+
from packages import Date
2526
from lib.crawlers import Cookiejar
2627

2728
# config_loader = ConfigLoader()
@@ -299,10 +300,43 @@ def _children_meta(parent, child_type):
299300
children.append(_subforum_meta(obj))
300301
elif obj_type == 'forum-thread':
301302
children.append(_thread_meta(obj))
302-
# TODO CHANGE SORT
303303
return sorted(children, key=lambda m: ((m.get('name') or m.get('title') or m.get('id')).lower(), m.get('id')))
304304

305305

306+
def _subforum_threads_meta(subforum):
307+
threads = []
308+
thread_ids = set()
309+
310+
for thread_id, last_post_timestamp in subforum.get_threads_by_last_post():
311+
thread = ForumThreads.ForumThread(thread_id, subforum.subtype)
312+
if not thread.exists():
313+
continue
314+
meta = _thread_meta(thread)
315+
meta['last_post_timestamp'] = int(last_post_timestamp)
316+
meta['last_post_date'] = Date.get_utc_datetime_from_timestamp(last_post_timestamp)
317+
threads.append(meta)
318+
thread_ids.add(thread_id)
319+
320+
# Threads without posts are not present in the last-post zset. Keep them visible
321+
# after active threads, using the same stable alphabetical ordering as before.
322+
inactive_threads = []
323+
for thread_id in subforum.get_threads():
324+
if thread_id in thread_ids:
325+
continue
326+
thread = ForumThreads.ForumThread(thread_id, subforum.subtype)
327+
if not thread.exists():
328+
continue
329+
meta = _thread_meta(thread)
330+
meta['last_post_timestamp'] = 0
331+
meta['last_post_date'] = None
332+
inactive_threads.append(meta)
333+
334+
return threads + sorted(
335+
inactive_threads,
336+
key=lambda m: ((m.get('name') or m.get('title') or m.get('id')).lower(), m.get('id')),
337+
)
338+
339+
306340
def get_forums():
307341
"""Return metadata for all imported Forum objects."""
308342
forums = []
@@ -456,7 +490,7 @@ def api_get_subforum(subtype, subforum_id):
456490
'subforum': _subforum_meta(subforum),
457491
'breadcrumb': get_breadcrumb_for_object(subforum),
458492
'subforums': _children_meta(subforum, 'subforum'),
459-
'threads': _children_meta(subforum, 'forum-thread'),
493+
'threads': _subforum_threads_meta(subforum),
460494
}, 200
461495

462496
def api_get_forum_thread(subtype, thread_id, page=1, nb=50, translation_target=None):

bin/lib/objects/Subforums.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def get_threads(self):
5959
def get_nb_threads(self):
6060
return len(self.get_threads())
6161

62+
def get_threads_by_last_post(self):
63+
return r_object.zrevrange(f'last:subforum:{self.subtype}:{self.id}', 0, -1, withscores=True)
64+
6265
def get_thread_last_post_timestamp(self, thread_id):
6366
return r_object.zscore(f'last:subforum:{self.subtype}:{self.id}', thread_id)
6467

bin/packages/Date.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ def get_current_utc_full_time():
114114
timestamp = datetime.datetime.now(datetime.timezone.utc)
115115
return timestamp.strftime('%Y-%m-%d %H:%M:%S')
116116

117+
def get_utc_datetime_from_timestamp(timestamp):
118+
timestamp = datetime.datetime.fromtimestamp(float(timestamp), datetime.timezone.utc)
119+
return timestamp.strftime('%Y-%m-%d %H:%M:%S')
120+
117121
def get_date_from_timestamp(timestamp):
118122
return datetime.datetime.fromtimestamp(timestamp).strftime('%Y%m%d')
119123

var/www/blueprints/forums_explorer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def objects_post_detect_language():
250250
return redirect(request.referrer)
251251
return redirect(url_for('forums_explorer.forum_explorer_forums'))
252252

253-
@forums_explorer.route("/chats/explorer/forum/subforum", methods=['GET'])
253+
@forums_explorer.route("/forums/explorer/subforum", methods=['GET'])
254254
@login_required
255255
@login_read_only
256256
def forum_explorer_subforum():

var/www/templates/forums_explorer/forums_explorer_subforum.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ <h5 class="mb-1">{{ thread.get('name') or thread['id'] }}</h5>
7575
</div>
7676
<div class="forum-stats">
7777
<div><strong>{{ thread.get('nb_posts') or 0 }}</strong> posts</div>
78-
<div class="small">{{ thread.get('last_seen') or '' }}</div>
78+
<div class="small">
79+
{% if thread.get('last_post_date') %}
80+
Last: {{ thread['last_post_date'] }} UTC
81+
{% endif %}
82+
</div>
7983
</div>
8084
</a>
8185
{% endfor %}

0 commit comments

Comments
 (0)