Get the latest push information for a user Args: user_id: User ID Returns: Push information with papers list
(user_id: str)
| 1298 | |
| 1299 | |
| 1300 | def get_latest_push(user_id: str) -> Optional[Dict]: |
| 1301 | """ |
| 1302 | Get the latest push information for a user |
| 1303 | |
| 1304 | Args: |
| 1305 | user_id: User ID |
| 1306 | |
| 1307 | Returns: |
| 1308 | Push information with papers list |
| 1309 | """ |
| 1310 | import json |
| 1311 | conn = get_connection() |
| 1312 | cursor = conn.cursor() |
| 1313 | |
| 1314 | # Get the latest real push_id from behavior logs |
| 1315 | cursor.execute(""" |
| 1316 | SELECT push_id, MAX(timestamp) as timestamp |
| 1317 | FROM behavior_logs |
| 1318 | WHERE user_id = ? |
| 1319 | AND action IN ('pushed', 'push_empty') |
| 1320 | GROUP BY push_id |
| 1321 | ORDER BY timestamp DESC, push_id DESC |
| 1322 | LIMIT 1 |
| 1323 | """, (user_id,)) |
| 1324 | |
| 1325 | row = cursor.fetchone() |
| 1326 | if not row: |
| 1327 | conn.close() |
| 1328 | return None |
| 1329 | |
| 1330 | push_id = row['push_id'] |
| 1331 | push_time = row['timestamp'] |
| 1332 | |
| 1333 | # Get all papers in this push with their category and rank from metadata. |
| 1334 | cursor.execute(""" |
| 1335 | SELECT p.*, bl.id as behavior_log_id, bl.metadata as bl_metadata FROM papers p |
| 1336 | JOIN behavior_logs bl ON p.id = bl.paper_id |
| 1337 | WHERE bl.user_id = ? AND bl.push_id = ? AND bl.action = 'pushed' |
| 1338 | ORDER BY bl.id ASC |
| 1339 | """, (user_id, push_id)) |
| 1340 | |
| 1341 | papers = [_build_paper_dict(row, metadata_key="bl_metadata") for row in cursor.fetchall()] |
| 1342 | papers.sort(key=lambda paper: (paper.get("rank", 10**9), paper.get("behavior_log_id", 10**9))) |
| 1343 | |
| 1344 | cursor.execute( |
| 1345 | """ |
| 1346 | SELECT metadata |
| 1347 | FROM behavior_logs |
| 1348 | WHERE user_id = ? |
| 1349 | AND push_id = ? |
| 1350 | AND action = 'push_empty' |
| 1351 | ORDER BY id DESC |
| 1352 | LIMIT 1 |
| 1353 | """, |
| 1354 | (user_id, push_id), |
| 1355 | ) |
| 1356 | metadata_row = cursor.fetchone() |
| 1357 | push_metadata = _load_json_metadata(metadata_row["metadata"]) if metadata_row else {} |