|
22 | 22 | from lib.objects import ail_objects |
23 | 23 | from lib import crawlers |
24 | 24 | from lib import Language |
| 25 | +from packages import Date |
25 | 26 | from lib.crawlers import Cookiejar |
26 | 27 |
|
27 | 28 | # config_loader = ConfigLoader() |
@@ -299,10 +300,43 @@ def _children_meta(parent, child_type): |
299 | 300 | children.append(_subforum_meta(obj)) |
300 | 301 | elif obj_type == 'forum-thread': |
301 | 302 | children.append(_thread_meta(obj)) |
302 | | - # TODO CHANGE SORT |
303 | 303 | return sorted(children, key=lambda m: ((m.get('name') or m.get('title') or m.get('id')).lower(), m.get('id'))) |
304 | 304 |
|
305 | 305 |
|
| 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 | + |
306 | 340 | def get_forums(): |
307 | 341 | """Return metadata for all imported Forum objects.""" |
308 | 342 | forums = [] |
@@ -456,7 +490,7 @@ def api_get_subforum(subtype, subforum_id): |
456 | 490 | 'subforum': _subforum_meta(subforum), |
457 | 491 | 'breadcrumb': get_breadcrumb_for_object(subforum), |
458 | 492 | 'subforums': _children_meta(subforum, 'subforum'), |
459 | | - 'threads': _children_meta(subforum, 'forum-thread'), |
| 493 | + 'threads': _subforum_threads_meta(subforum), |
460 | 494 | }, 200 |
461 | 495 |
|
462 | 496 | def api_get_forum_thread(subtype, thread_id, page=1, nb=50, translation_target=None): |
|
0 commit comments