Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2045,6 +2045,7 @@ async def _serialize_post(db: aiosqlite.Connection, row: dict, viewer_id: int,
|
|
| 2045 |
"quote": quote,
|
| 2046 |
"created_at_ms": row["created_at_ms"],
|
| 2047 |
"edited_at_ms": row["edited_at_ms"],
|
|
|
|
| 2048 |
"reposter_id": reposter_id,
|
| 2049 |
"reposter": reposter,
|
| 2050 |
"like_count": await _count_post(db, "social_likes", row["id"]),
|
|
@@ -2340,17 +2341,15 @@ async def get_social_feed_rest(
|
|
| 2340 |
try:
|
| 2341 |
blocked = await _blocked_user_ids(db, user["id"])
|
| 2342 |
if feed == "home":
|
|
|
|
|
|
|
| 2343 |
author_ids = await _social_author_ids(db, user["id"])
|
| 2344 |
-
|
| 2345 |
-
|
| 2346 |
-
|
| 2347 |
-
|
| 2348 |
-
f"SELECT * FROM social_posts WHERE author_id IN ({placeholders}) "
|
| 2349 |
-
f"AND is_deleted = 0 ORDER BY created_at_ms DESC LIMIT ?",
|
| 2350 |
-
author_ids + [limit * 3])
|
| 2351 |
-
rows = [dict(r) for r in await cur.fetchall()]
|
| 2352 |
-
# Reposts by followed accounts + self, merged with original posts.
|
| 2353 |
reposter_ids = list(author_ids)
|
|
|
|
| 2354 |
if reposter_ids:
|
| 2355 |
ph = ",".join("?" * len(reposter_ids))
|
| 2356 |
cur = await db.execute(
|
|
@@ -2359,7 +2358,10 @@ async def get_social_feed_rest(
|
|
| 2359 |
f"WHERE r.user_id IN ({ph}) AND p.is_deleted = 0",
|
| 2360 |
reposter_ids)
|
| 2361 |
reposts = [dict(r) for r in await cur.fetchall()]
|
| 2362 |
-
|
|
|
|
|
|
|
|
|
|
| 2363 |
rows = sorted(rows, key=lambda r: r.get("reposted_at_ms") or r["created_at_ms"], reverse=True)
|
| 2364 |
elif feed == "explore":
|
| 2365 |
cur = await db.execute(
|
|
@@ -2368,11 +2370,15 @@ async def get_social_feed_rest(
|
|
| 2368 |
rows = [dict(r) for r in await cur.fetchall()]
|
| 2369 |
else:
|
| 2370 |
raise HTTPException(400, "Unknown feed")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2371 |
if cursor >= 0:
|
| 2372 |
rows = [r for r in rows if r["created_at_ms"] < cursor]
|
| 2373 |
rows = rows[:limit]
|
| 2374 |
posts = await _serialize_post_rows(db, rows, user["id"])
|
| 2375 |
-
posts.sort(key=lambda p: p["created_at_ms"], reverse=True)
|
| 2376 |
return {"posts": posts, "cursor": posts[-1]["created_at_ms"] if posts else None}
|
| 2377 |
finally:
|
| 2378 |
await db.close()
|
|
|
|
| 2045 |
"quote": quote,
|
| 2046 |
"created_at_ms": row["created_at_ms"],
|
| 2047 |
"edited_at_ms": row["edited_at_ms"],
|
| 2048 |
+
"reposted_at_ms": row.get("reposted_at_ms"),
|
| 2049 |
"reposter_id": reposter_id,
|
| 2050 |
"reposter": reposter,
|
| 2051 |
"like_count": await _count_post(db, "social_likes", row["id"]),
|
|
|
|
| 2341 |
try:
|
| 2342 |
blocked = await _blocked_user_ids(db, user["id"])
|
| 2343 |
if feed == "home":
|
| 2344 |
+
# Home shows all public posts (so other users can see them) mixed
|
| 2345 |
+
# with reposts made by accounts the viewer follows / themself.
|
| 2346 |
author_ids = await _social_author_ids(db, user["id"])
|
| 2347 |
+
cur = await db.execute(
|
| 2348 |
+
"SELECT * FROM social_posts WHERE is_deleted = 0 "
|
| 2349 |
+
"ORDER BY created_at_ms DESC LIMIT ?", (limit * 3,))
|
| 2350 |
+
rows = [dict(r) for r in await cur.fetchall()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2351 |
reposter_ids = list(author_ids)
|
| 2352 |
+
reposts = []
|
| 2353 |
if reposter_ids:
|
| 2354 |
ph = ",".join("?" * len(reposter_ids))
|
| 2355 |
cur = await db.execute(
|
|
|
|
| 2358 |
f"WHERE r.user_id IN ({ph}) AND p.is_deleted = 0",
|
| 2359 |
reposter_ids)
|
| 2360 |
reposts = [dict(r) for r in await cur.fetchall()]
|
| 2361 |
+
repost_ids = {r["id"] for r in reposts}
|
| 2362 |
+
# Prefer the repost entry when a followed account reposts a post;
|
| 2363 |
+
# the original post is still visible to everyone below when not reposted.
|
| 2364 |
+
rows = [r for r in rows if r["id"] not in repost_ids] + reposts
|
| 2365 |
rows = sorted(rows, key=lambda r: r.get("reposted_at_ms") or r["created_at_ms"], reverse=True)
|
| 2366 |
elif feed == "explore":
|
| 2367 |
cur = await db.execute(
|
|
|
|
| 2370 |
rows = [dict(r) for r in await cur.fetchall()]
|
| 2371 |
else:
|
| 2372 |
raise HTTPException(400, "Unknown feed")
|
| 2373 |
+
# Never let blocked users crowd out visible posts before pagination.
|
| 2374 |
+
rows = [r for r in rows
|
| 2375 |
+
if r["author_id"] not in blocked
|
| 2376 |
+
and (r.get("reposter_id") is None or r["reposter_id"] not in blocked)]
|
| 2377 |
if cursor >= 0:
|
| 2378 |
rows = [r for r in rows if r["created_at_ms"] < cursor]
|
| 2379 |
rows = rows[:limit]
|
| 2380 |
posts = await _serialize_post_rows(db, rows, user["id"])
|
| 2381 |
+
posts.sort(key=lambda p: p.get("reposted_at_ms") or p["created_at_ms"], reverse=True)
|
| 2382 |
return {"posts": posts, "cursor": posts[-1]["created_at_ms"] if posts else None}
|
| 2383 |
finally:
|
| 2384 |
await db.close()
|