forked from retoor/devplacepy
Perfect state.
This commit is contained in:
+13
-14
@@ -55,16 +55,6 @@ def init_db():
|
||||
_index(db, "follows", "idx_follows_following", ["following_uid"])
|
||||
_index(db, "password_resets", "idx_password_resets_token", ["token"])
|
||||
|
||||
if "daily_topics" in tables:
|
||||
existing = db["daily_topics"].find_one()
|
||||
if not existing:
|
||||
db["daily_topics"].insert({
|
||||
"uid": "default",
|
||||
"title": "Anthropic details how it improved Claude's safety...",
|
||||
"summary": "New techniques in AI safety research show promising results for alignment of large language models with human values.",
|
||||
"updated_at": datetime.utcnow().isoformat(),
|
||||
})
|
||||
|
||||
if "site_settings" in tables:
|
||||
defaults = {"site_name": "DevPlace", "site_description": "The Developer Social Network", "site_tagline": "Track industry shifts. Discover bold releases. Share what you are building in an open, uncensored environment."}
|
||||
for key, value in defaults.items():
|
||||
@@ -90,6 +80,13 @@ def init_db():
|
||||
"uid": article["uid"],
|
||||
"show_on_landing": 0,
|
||||
}, ["uid"])
|
||||
for article in db["news"].find(slug=None):
|
||||
from devplacepy.utils import make_combined_slug
|
||||
slug = make_combined_slug(article.get("title", "") or "news", article["uid"])
|
||||
db["news"].update({
|
||||
"uid": article["uid"],
|
||||
"slug": slug,
|
||||
}, ["uid"])
|
||||
|
||||
if "news_sync" in tables:
|
||||
for entry in db["news_sync"].find():
|
||||
@@ -130,16 +127,18 @@ def get_users_by_uids(uids):
|
||||
def get_comment_counts_by_post_uids(post_uids):
|
||||
if not post_uids or "comments" not in db.tables:
|
||||
return {}
|
||||
placeholders = ",".join(f"'{u}'" for u in post_uids)
|
||||
rows = db.query(f"SELECT post_uid, COUNT(*) as c FROM comments WHERE post_uid IN ({placeholders}) GROUP BY post_uid")
|
||||
placeholders = ", ".join(f":p{i}" for i in range(len(post_uids)))
|
||||
params = {f"p{i}": u for i, u in enumerate(post_uids)}
|
||||
rows = db.query(f"SELECT post_uid, COUNT(*) as c FROM comments WHERE post_uid IN ({placeholders}) GROUP BY post_uid", **params)
|
||||
return {r["post_uid"]: r["c"] for r in rows}
|
||||
|
||||
|
||||
def get_vote_counts(target_uids):
|
||||
if not target_uids or "votes" not in db.tables:
|
||||
return {}, {}
|
||||
placeholders = ",".join(f"'{u}'" for u in target_uids)
|
||||
rows = db.query(f"SELECT target_uid, value, COUNT(*) as c FROM votes WHERE target_uid IN ({placeholders}) GROUP BY target_uid, value")
|
||||
placeholders = ", ".join(f":p{i}" for i in range(len(target_uids)))
|
||||
params = {f"p{i}": u for i, u in enumerate(target_uids)}
|
||||
rows = db.query(f"SELECT target_uid, value, COUNT(*) as c FROM votes WHERE target_uid IN ({placeholders}) GROUP BY target_uid, value", **params)
|
||||
ups = {}
|
||||
downs = {}
|
||||
for r in rows:
|
||||
|
||||
+33
-8
@@ -4,10 +4,10 @@ import os
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import RedirectResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from devplacepy.config import STATIC_DIR
|
||||
from devplacepy.database import init_db, get_daily_topic, get_table, db
|
||||
from devplacepy.config import STATIC_DIR, PORT
|
||||
from devplacepy.database import init_db, get_table, db, get_users_by_uids, get_comment_counts_by_post_uids, get_vote_counts
|
||||
from devplacepy.templating import templates
|
||||
from devplacepy.utils import get_current_user
|
||||
from devplacepy.utils import get_current_user, time_ago
|
||||
from devplacepy.seo import base_seo_context, site_url, website_schema, breadcrumb_schema, combine
|
||||
from devplacepy.routers import auth, feed, posts, comments, projects, profile, messages, notifications, votes, avatar, follow, admin, seo, bugs, news, services as services_router
|
||||
from devplacepy.services.manager import service_manager
|
||||
@@ -56,7 +56,7 @@ async def startup():
|
||||
news_service = NewsService()
|
||||
service_manager.register(news_service)
|
||||
asyncio.create_task(service_manager.start_all())
|
||||
logger.info("DevPlace started on port 10500")
|
||||
logger.info(f"DevPlace started on port {PORT}")
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
@@ -70,12 +70,11 @@ async def landing(request: Request):
|
||||
user = get_current_user(request)
|
||||
if user:
|
||||
return RedirectResponse(url="/feed", status_code=302)
|
||||
daily_topic = get_daily_topic()
|
||||
|
||||
landing_articles = []
|
||||
if "news" in db.tables:
|
||||
news_table = get_table("news")
|
||||
raw = list(news_table.find(show_on_landing=1, order_by=["-synced_at"], _limit=6))
|
||||
raw = list(news_table.find(order_by=["-synced_at"], _limit=6))
|
||||
images_table = get_table("news_images") if "news_images" in db.tables else None
|
||||
for a in raw:
|
||||
image_url = ""
|
||||
@@ -84,13 +83,38 @@ async def landing(request: Request):
|
||||
if img:
|
||||
image_url = img["url"]
|
||||
landing_articles.append({
|
||||
"uid": a["uid"],
|
||||
"slug": a.get("slug", ""),
|
||||
"title": a.get("title", ""),
|
||||
"description": (a.get("description", "") or "")[:250],
|
||||
"url": a.get("url", ""),
|
||||
"source_name": a.get("source_name", ""),
|
||||
"synced_at": a.get("synced_at", "")[:10] if a.get("synced_at") else "",
|
||||
"grade": a.get("grade", 0),
|
||||
"synced_at": a.get("synced_at", "") or "",
|
||||
"time_ago": time_ago(a["synced_at"]),
|
||||
"image_url": image_url,
|
||||
})
|
||||
|
||||
landing_posts = []
|
||||
if "posts" in db.tables:
|
||||
posts_table = get_table("posts")
|
||||
raw_posts = list(posts_table.find(order_by=["-created_at"], _limit=6))
|
||||
if raw_posts:
|
||||
post_uids = [p["uid"] for p in raw_posts]
|
||||
author_uids = [p["user_uid"] for p in raw_posts]
|
||||
authors = get_users_by_uids(author_uids)
|
||||
comment_counts = get_comment_counts_by_post_uids(post_uids)
|
||||
upvotes, downvotes = get_vote_counts(post_uids)
|
||||
for p in raw_posts:
|
||||
landing_posts.append({
|
||||
"post": p,
|
||||
"author": authors.get(p["user_uid"]),
|
||||
"time_ago": time_ago(p["created_at"]),
|
||||
"comment_count": comment_counts.get(p["uid"], 0),
|
||||
"stars": upvotes.get(p["uid"], 0) - downvotes.get(p["uid"], 0),
|
||||
"slug": p.get("slug", "") or p["uid"],
|
||||
})
|
||||
|
||||
base = site_url(request)
|
||||
seo_ctx = base_seo_context(
|
||||
request,
|
||||
@@ -100,6 +124,7 @@ async def landing(request: Request):
|
||||
schemas=[website_schema(base)],
|
||||
)
|
||||
return templates.TemplateResponse("landing.html", {
|
||||
**seo_ctx, "request": request, "daily_topic": daily_topic,
|
||||
**seo_ctx, "request": request,
|
||||
"landing_articles": landing_articles,
|
||||
"landing_posts": landing_posts,
|
||||
})
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import hashlib
|
||||
import secrets
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
@@ -102,7 +103,7 @@ async def signup(request: Request):
|
||||
|
||||
token = create_session(uid)
|
||||
response = RedirectResponse(url="/feed", status_code=302)
|
||||
response.set_cookie(key="session", value=token, max_age=86400 * 7, httponly=True)
|
||||
response.set_cookie(key="session", value=token, max_age=86400 * 7, httponly=True, samesite="lax")
|
||||
logger.info(f"User {username} signed up")
|
||||
return response
|
||||
|
||||
@@ -134,7 +135,7 @@ async def login(request: Request):
|
||||
token = create_session(user["uid"])
|
||||
max_age = 86400 * 30 if remember_me else 86400 * 7
|
||||
response = RedirectResponse(url="/feed", status_code=302)
|
||||
response.set_cookie(key="session", value=token, max_age=max_age, httponly=True)
|
||||
response.set_cookie(key="session", value=token, max_age=max_age, httponly=True, samesite="lax")
|
||||
logger.info(f"User {user['username']} logged in")
|
||||
return response
|
||||
|
||||
@@ -167,16 +168,17 @@ async def forgot_password(request: Request):
|
||||
user = users.find_one(email=email)
|
||||
if user:
|
||||
token = secrets.token_hex(32)
|
||||
token_hash = hashlib.sha256(token.encode()).hexdigest()
|
||||
resets = get_table("password_resets")
|
||||
resets.insert({
|
||||
"uid": generate_uid(),
|
||||
"user_uid": user["uid"],
|
||||
"token": hash_password(token),
|
||||
"token": token_hash,
|
||||
"expires_at": (datetime.utcnow() + timedelta(hours=1)).isoformat(),
|
||||
"used": False,
|
||||
"created_at": datetime.utcnow().isoformat(),
|
||||
})
|
||||
logger.info(f"Password reset requested for {email}: /auth/reset-password/{token}")
|
||||
logger.info(f"Password reset requested for {email}")
|
||||
|
||||
return templates.TemplateResponse("forgot_password.html", {
|
||||
**seo_ctx, "request": request, "sent": True,
|
||||
@@ -215,15 +217,10 @@ async def reset_password(request: Request, token: str):
|
||||
|
||||
resets = get_table("password_resets")
|
||||
users = get_table("users")
|
||||
all_resets = list(resets.find(used=False))
|
||||
matched = None
|
||||
for r in all_resets:
|
||||
expires = datetime.fromisoformat(r["expires_at"])
|
||||
if expires > datetime.utcnow() and verify_password(token, r["token"]):
|
||||
matched = r
|
||||
break
|
||||
token_hash = hashlib.sha256(token.encode()).hexdigest()
|
||||
matched = resets.find_one(token=token_hash, used=False)
|
||||
|
||||
if not matched:
|
||||
if not matched or datetime.fromisoformat(matched["expires_at"]) < datetime.utcnow():
|
||||
errors.append("Invalid or expired reset token")
|
||||
return templates.TemplateResponse("reset_password.html", {
|
||||
"request": request, "token": token, "errors": errors,
|
||||
|
||||
@@ -22,6 +22,16 @@ def resolve_target_redirect(target_type, target_uid):
|
||||
if not project:
|
||||
project = projects.find_one(slug=target_uid)
|
||||
return f"/projects/{project['slug'] or project['uid']}" if project else "/projects"
|
||||
if target_type == "news":
|
||||
news = get_table("news")
|
||||
article = news.find_one(uid=target_uid)
|
||||
if not article:
|
||||
article = news.find_one(slug=target_uid)
|
||||
if article:
|
||||
return f"/news/{article.get('slug', '') or article['uid']}"
|
||||
return "/news"
|
||||
if target_type == "bug":
|
||||
return f"/bugs?highlight={target_uid}"
|
||||
return "/bugs"
|
||||
|
||||
|
||||
@@ -67,21 +77,36 @@ async def create_comment(request: Request):
|
||||
})
|
||||
|
||||
if target_type == "post":
|
||||
posts = get_table("posts")
|
||||
post = posts.find_one(uid=target_uid)
|
||||
if not post:
|
||||
post = posts.find_one(slug=target_uid)
|
||||
if post and post["user_uid"] != user["uid"]:
|
||||
notifications = get_table("notifications")
|
||||
notifications.insert({
|
||||
"uid": generate_uid(),
|
||||
"user_uid": post["user_uid"],
|
||||
"type": "comment",
|
||||
"message": f"{user['username']} commented on your post",
|
||||
"related_uid": user["uid"],
|
||||
"read": False,
|
||||
"created_at": datetime.utcnow().isoformat(),
|
||||
})
|
||||
if parent_uid:
|
||||
comments_table = get_table("comments")
|
||||
parent = comments_table.find_one(uid=parent_uid)
|
||||
if parent and parent["user_uid"] != user["uid"]:
|
||||
notifications = get_table("notifications")
|
||||
notifications.insert({
|
||||
"uid": generate_uid(),
|
||||
"user_uid": parent["user_uid"],
|
||||
"type": "reply",
|
||||
"message": f"{user['username']} replied to your comment",
|
||||
"related_uid": user["uid"],
|
||||
"read": False,
|
||||
"created_at": datetime.utcnow().isoformat(),
|
||||
})
|
||||
else:
|
||||
posts = get_table("posts")
|
||||
post = posts.find_one(uid=target_uid)
|
||||
if not post:
|
||||
post = posts.find_one(slug=target_uid)
|
||||
if post and post["user_uid"] != user["uid"]:
|
||||
notifications = get_table("notifications")
|
||||
notifications.insert({
|
||||
"uid": generate_uid(),
|
||||
"user_uid": post["user_uid"],
|
||||
"type": "comment",
|
||||
"message": f"{user['username']} commented on your post",
|
||||
"related_uid": user["uid"],
|
||||
"read": False,
|
||||
"created_at": datetime.utcnow().isoformat(),
|
||||
})
|
||||
|
||||
logger.info(f"Comment by {user['username']} on {target_type} {target_uid}")
|
||||
return RedirectResponse(url=redirect_url, status_code=302)
|
||||
@@ -98,6 +123,7 @@ async def delete_comment(request: Request, comment_uid: str):
|
||||
return RedirectResponse(url="/feed", status_code=302)
|
||||
target_type = comment.get("target_type", "post")
|
||||
target_uid = comment.get("target_uid") or comment.get("post_uid", "")
|
||||
get_table("votes").delete(target_uid=comment_uid, target_type="comment")
|
||||
comments.delete(id=comment["id"])
|
||||
logger.info(f"Comment {comment_uid} deleted by {user['username']}")
|
||||
redirect_url = resolve_target_redirect(target_type, target_uid)
|
||||
|
||||
@@ -4,7 +4,7 @@ from fastapi import APIRouter, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from devplacepy.database import get_table, get_daily_topic, get_users_by_uids, get_comment_counts_by_post_uids
|
||||
from devplacepy.templating import templates
|
||||
from devplacepy.utils import require_user, time_ago
|
||||
from devplacepy.utils import get_current_user, time_ago
|
||||
from devplacepy.seo import base_seo_context, site_url, website_schema, breadcrumb_schema, combine
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -21,6 +21,8 @@ def get_feed_posts(user, tab: str = "all", topic: str = None, before: str = None
|
||||
filters["topic"] = topic
|
||||
|
||||
if tab == "following":
|
||||
if not user:
|
||||
return [], None
|
||||
follows = get_table("follows")
|
||||
following = [f["following_uid"] for f in follows.find(follower_uid=user["uid"])]
|
||||
if not following:
|
||||
@@ -64,7 +66,7 @@ def get_feed_posts(user, tab: str = "all", topic: str = None, before: str = None
|
||||
|
||||
@router.get("", response_class=HTMLResponse)
|
||||
async def feed_page(request: Request, tab: str = "all", topic: str = None, before: str = None):
|
||||
user = require_user(request)
|
||||
user = get_current_user(request)
|
||||
posts, next_cursor = get_feed_posts(user, tab, topic, before)
|
||||
users_table = get_table("users")
|
||||
total_members = len(list(users_table.all()))
|
||||
|
||||
@@ -2,10 +2,10 @@ import logging
|
||||
from datetime import datetime, timedelta
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from devplacepy.database import get_table, db
|
||||
from devplacepy.database import get_table, db, load_comments
|
||||
from devplacepy.templating import templates
|
||||
from devplacepy.utils import get_current_user, time_ago
|
||||
from devplacepy.seo import base_seo_context, website_schema, site_url
|
||||
from devplacepy.seo import base_seo_context, website_schema, site_url, discussion_forum_posting, combine
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
@@ -49,7 +49,7 @@ async def news_page(request: Request):
|
||||
request,
|
||||
title="Developer News",
|
||||
description="Curated developer news and industry signals. Stay ahead with hand-picked articles.",
|
||||
breadcrumbs=[{"name": "News", "url": "/news"}],
|
||||
breadcrumbs=[{"name": "Home", "url": "/feed"}, {"name": "News", "url": "/news"}],
|
||||
schemas=[website_schema(base)],
|
||||
)
|
||||
|
||||
@@ -59,3 +59,45 @@ async def news_page(request: Request):
|
||||
"user": user,
|
||||
"articles": enriched,
|
||||
})
|
||||
|
||||
|
||||
@router.get("/{news_slug}", response_class=HTMLResponse)
|
||||
async def news_detail_page(request: Request, news_slug: str):
|
||||
user = get_current_user(request)
|
||||
news_table = get_table("news")
|
||||
article = news_table.find_one(slug=news_slug)
|
||||
if not article:
|
||||
article = news_table.find_one(uid=news_slug)
|
||||
if not article:
|
||||
return HTMLResponse("News article not found", status_code=404)
|
||||
|
||||
image_url = ""
|
||||
if "news_images" in db.tables:
|
||||
img = get_table("news_images").find_one(news_uid=article["uid"])
|
||||
if img:
|
||||
image_url = img["url"]
|
||||
|
||||
canonical_slug = article.get("slug", "") or article["uid"]
|
||||
comments = load_comments("news", article["uid"])
|
||||
|
||||
base = site_url(request)
|
||||
page_url = f"{base}/news/{canonical_slug}"
|
||||
seo_ctx = base_seo_context(
|
||||
request,
|
||||
title=article.get("title", "News Article"),
|
||||
description=(article.get("description", "") or "")[:200],
|
||||
breadcrumbs=[{"name": "Home", "url": "/feed"}, {"name": "News", "url": "/news"}, {"name": article.get("title", "")[:60], "url": page_url}],
|
||||
schemas=[website_schema(base)],
|
||||
)
|
||||
|
||||
return templates.TemplateResponse("news_detail.html", {
|
||||
**seo_ctx,
|
||||
"request": request,
|
||||
"user": user,
|
||||
"article": article,
|
||||
"canonical_slug": canonical_slug,
|
||||
"image_url": image_url,
|
||||
"grade": article.get("grade", 0),
|
||||
"time_ago": time_ago(article["synced_at"]),
|
||||
"comments": comments,
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from devplacepy.database import get_table
|
||||
@@ -10,27 +11,63 @@ logger = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def _group_label(created_at: str) -> str:
|
||||
try:
|
||||
dt = datetime.fromisoformat(created_at)
|
||||
now = datetime.utcnow()
|
||||
today = now.date()
|
||||
date = dt.date()
|
||||
if date == today:
|
||||
return "Today"
|
||||
if date == today - timedelta(days=1):
|
||||
return "Yesterday"
|
||||
if (today - date).days < 7:
|
||||
return "This week"
|
||||
return "Older"
|
||||
except Exception:
|
||||
return "Older"
|
||||
|
||||
|
||||
@router.get("", response_class=HTMLResponse)
|
||||
async def notifications_page(request: Request):
|
||||
user = require_user(request)
|
||||
notifications_table = get_table("notifications")
|
||||
raw_notifications = list(
|
||||
notifications_table.find(user_uid=user["uid"], order_by=["-created_at"])
|
||||
)
|
||||
|
||||
notifications = []
|
||||
if raw_notifications:
|
||||
from devplacepy.database import get_users_by_uids
|
||||
actor_uids = [n.get("related_uid") for n in raw_notifications if n.get("related_uid")]
|
||||
actors = get_users_by_uids(actor_uids)
|
||||
else:
|
||||
actors = {}
|
||||
for n in raw_notifications:
|
||||
notifications.append({
|
||||
"notification": n,
|
||||
"actor": actors.get(n.get("related_uid")),
|
||||
"time_ago": time_ago(n["created_at"]),
|
||||
})
|
||||
try:
|
||||
notifications_table = get_table("notifications")
|
||||
raw_notifications = list(
|
||||
notifications_table.find(user_uid=user["uid"], order_by=["-created_at"])
|
||||
)
|
||||
|
||||
enriched = []
|
||||
if raw_notifications:
|
||||
from devplacepy.database import get_users_by_uids
|
||||
actor_uids = [n.get("related_uid") for n in raw_notifications if n.get("related_uid")]
|
||||
actors = get_users_by_uids(actor_uids)
|
||||
else:
|
||||
actors = {}
|
||||
for n in raw_notifications:
|
||||
enriched.append({
|
||||
"notification": n,
|
||||
"actor": actors.get(n.get("related_uid")),
|
||||
"time_ago": time_ago(n["created_at"]),
|
||||
})
|
||||
|
||||
groups = []
|
||||
current_label = None
|
||||
current_entries = []
|
||||
for item in enriched:
|
||||
label = _group_label(item["notification"]["created_at"])
|
||||
if label != current_label:
|
||||
if current_entries:
|
||||
groups.append({"label": current_label, "entries": current_entries})
|
||||
current_label = label
|
||||
current_entries = []
|
||||
current_entries.append(item)
|
||||
if current_entries:
|
||||
groups.append({"label": current_label, "entries": current_entries})
|
||||
except Exception as e:
|
||||
logger.exception(f"Error loading notifications: {e}")
|
||||
groups = []
|
||||
|
||||
seo_ctx = base_seo_context(
|
||||
request,
|
||||
@@ -41,7 +78,7 @@ async def notifications_page(request: Request):
|
||||
**seo_ctx,
|
||||
"request": request,
|
||||
"user": user,
|
||||
"notifications": notifications,
|
||||
"notification_groups": groups,
|
||||
})
|
||||
|
||||
|
||||
|
||||
@@ -210,6 +210,12 @@ async def delete_post(request: Request, post_slug: str):
|
||||
if post and post["user_uid"] == user["uid"]:
|
||||
get_table("comments").delete(post_uid=post["uid"])
|
||||
get_table("votes").delete(target_uid=post["uid"])
|
||||
image = post.get("image")
|
||||
if image:
|
||||
try:
|
||||
(STATIC_DIR / "uploads" / image).unlink(missing_ok=True)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to delete image {image}: {e}")
|
||||
posts.delete(id=post["id"])
|
||||
logger.info(f"Post {post['uid']} deleted by {user['username']}")
|
||||
return RedirectResponse(url="/feed", status_code=302)
|
||||
|
||||
@@ -36,15 +36,41 @@ async def vote(request: Request, target_type: str, target_uid: str):
|
||||
"created_at": datetime.utcnow().isoformat(),
|
||||
})
|
||||
|
||||
up = len(list(votes.find(target_uid=target_uid, value=1)))
|
||||
down = len(list(votes.find(target_uid=target_uid, value=-1)))
|
||||
up_count = len(list(votes.find(target_uid=target_uid, value=1)))
|
||||
down_count = len(list(votes.find(target_uid=target_uid, value=-1)))
|
||||
net = up_count - down_count
|
||||
|
||||
if target_type == "post":
|
||||
posts = get_table("posts")
|
||||
posts.update({"uid": target_uid, "stars": up - down}, ["uid"])
|
||||
posts.update({"uid": target_uid, "stars": net}, ["uid"])
|
||||
elif target_type == "project":
|
||||
projects = get_table("projects")
|
||||
projects.update({"uid": target_uid, "stars": up - down}, ["uid"])
|
||||
projects.update({"uid": target_uid, "stars": net}, ["uid"])
|
||||
|
||||
if value == 1:
|
||||
target_owner_uid = None
|
||||
if target_type == "post":
|
||||
target_owner = posts.find_one(uid=target_uid)
|
||||
if target_owner:
|
||||
target_owner_uid = target_owner["user_uid"]
|
||||
elif target_type == "comment":
|
||||
comments = get_table("comments")
|
||||
target_comment = comments.find_one(uid=target_uid)
|
||||
if target_comment:
|
||||
target_owner_uid = target_comment["user_uid"]
|
||||
|
||||
if target_owner_uid and target_owner_uid != user["uid"]:
|
||||
label = "post" if target_type == "post" else "comment"
|
||||
notifications = get_table("notifications")
|
||||
notifications.insert({
|
||||
"uid": generate_uid(),
|
||||
"user_uid": target_owner_uid,
|
||||
"type": "vote",
|
||||
"message": f"{user['username']} ++'d your {label}",
|
||||
"related_uid": user["uid"],
|
||||
"read": False,
|
||||
"created_at": datetime.utcnow().isoformat(),
|
||||
})
|
||||
|
||||
referer = request.headers.get("Referer", "/feed")
|
||||
return RedirectResponse(url=referer, status_code=302)
|
||||
|
||||
+4
-4
@@ -182,8 +182,8 @@ def make_sitemap(base_url):
|
||||
urlset.append(url_element(f"{base_url}/projects", changefreq="daily", priority="0.8"))
|
||||
|
||||
if "posts" in db.tables:
|
||||
posts = list(get_table("posts").all())
|
||||
for p in posts[-500:]:
|
||||
posts = list(get_table("posts").find(order_by=["-created_at"], _limit=500))
|
||||
for p in posts:
|
||||
urlset.append(url_element(
|
||||
f"{base_url}/posts/{p.get('slug') or p['uid']}",
|
||||
lastmod=p.get("created_at", ""),
|
||||
@@ -192,7 +192,7 @@ def make_sitemap(base_url):
|
||||
))
|
||||
|
||||
if "projects" in db.tables:
|
||||
projects = list(get_table("projects").all())
|
||||
projects = list(get_table("projects").find(order_by=["-created_at"], _limit=1000))
|
||||
for p in projects:
|
||||
urlset.append(url_element(
|
||||
f"{base_url}/projects/{p.get('slug') or p['uid']}",
|
||||
@@ -202,7 +202,7 @@ def make_sitemap(base_url):
|
||||
))
|
||||
|
||||
if "users" in db.tables:
|
||||
users = list(get_table("users").all())
|
||||
users = list(get_table("users").find(order_by=["-created_at"], _limit=200))
|
||||
for u in users:
|
||||
urlset.append(url_element(
|
||||
f"{base_url}/profile/{u['username']}",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from datetime import datetime
|
||||
|
||||
@@ -6,7 +7,7 @@ import httpx
|
||||
|
||||
from devplacepy.database import get_table
|
||||
from devplacepy.services.base import BaseService
|
||||
from devplacepy.utils import generate_uid
|
||||
from devplacepy.utils import generate_uid, make_combined_slug
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -24,6 +25,23 @@ def _get_setting(key: str, default: str) -> str:
|
||||
return row.get("value", default)
|
||||
|
||||
|
||||
def _get_ai_key() -> str:
|
||||
key = os.environ.get("NEWS_AI_KEY")
|
||||
if key:
|
||||
return key
|
||||
table = get_table("site_settings")
|
||||
row = table.find_one(key="news_ai_key")
|
||||
if row:
|
||||
return row.get("value", "")
|
||||
key = os.environ.get("OPENROUTER_API_KEY")
|
||||
if key:
|
||||
return key
|
||||
key = os.environ.get("OPENAI_API_KEY")
|
||||
if key:
|
||||
return key
|
||||
return ""
|
||||
|
||||
|
||||
def _extract_grade(text: str) -> int | None:
|
||||
match = re.search(r"\d+", text.strip())
|
||||
if match:
|
||||
@@ -79,17 +97,27 @@ class NewsService(BaseService):
|
||||
|
||||
news_table = get_table("news")
|
||||
images_table = get_table("news_images")
|
||||
sync_table = get_table("news_sync")
|
||||
|
||||
synced_ids = set()
|
||||
for entry in sync_table.find():
|
||||
synced_ids.add(entry["external_id"])
|
||||
|
||||
new_count = 0
|
||||
updated_count = 0
|
||||
draft_count = 0
|
||||
failed_count = 0
|
||||
skipped_count = 0
|
||||
|
||||
for article in articles:
|
||||
external_id = article.get("guid", "")
|
||||
if not external_id:
|
||||
continue
|
||||
|
||||
if external_id in synced_ids:
|
||||
skipped_count += 1
|
||||
continue
|
||||
|
||||
grade = await self._grade_article(article, ai_url, ai_model)
|
||||
now = datetime.utcnow().isoformat()
|
||||
|
||||
@@ -97,23 +125,28 @@ class NewsService(BaseService):
|
||||
grade_val = 0
|
||||
auto_published = False
|
||||
failed_count += 1
|
||||
sync_status = "grading_failed"
|
||||
elif grade < threshold:
|
||||
grade_val = grade
|
||||
auto_published = False
|
||||
draft_count += 1
|
||||
sync_status = "graded"
|
||||
else:
|
||||
grade_val = grade
|
||||
auto_published = True
|
||||
sync_status = "graded"
|
||||
|
||||
is_published = "published" if auto_published else "draft"
|
||||
existing = news_table.find_one(external_id=external_id)
|
||||
|
||||
if existing:
|
||||
existing_slug = existing.get("slug", "")
|
||||
news_table.update({
|
||||
"id": existing["id"],
|
||||
"grade": grade_val,
|
||||
"status": is_published,
|
||||
"title": article.get("title", ""),
|
||||
"slug": existing_slug or make_combined_slug(article.get("title", "") or "news", existing["uid"]),
|
||||
"description": (article.get("description", "") or "")[:5000],
|
||||
"url": article.get("link", ""),
|
||||
"source_name": article.get("feed_name", ""),
|
||||
@@ -127,8 +160,10 @@ class NewsService(BaseService):
|
||||
article_uid = existing["uid"]
|
||||
else:
|
||||
article_uid = generate_uid()
|
||||
article_slug = make_combined_slug(article.get("title", "") or "news", article_uid)
|
||||
news_table.insert({
|
||||
"uid": article_uid,
|
||||
"slug": article_slug,
|
||||
"external_id": external_id,
|
||||
"title": article.get("title", ""),
|
||||
"description": (article.get("description", "") or "")[:5000],
|
||||
@@ -144,6 +179,23 @@ class NewsService(BaseService):
|
||||
})
|
||||
new_count += 1
|
||||
|
||||
existing_sync = sync_table.find_one(external_id=external_id)
|
||||
if existing_sync:
|
||||
sync_table.update({
|
||||
"id": existing_sync["id"],
|
||||
"status": sync_status,
|
||||
"synced_at": now,
|
||||
}, ["id"])
|
||||
else:
|
||||
sync_table.insert({
|
||||
"uid": generate_uid(),
|
||||
"external_id": external_id,
|
||||
"status": sync_status,
|
||||
"synced_at": now,
|
||||
})
|
||||
|
||||
synced_ids.add(external_id)
|
||||
|
||||
link = article.get("link", "")
|
||||
if link:
|
||||
fresh_images = await _get_article_images(link)
|
||||
@@ -155,7 +207,8 @@ class NewsService(BaseService):
|
||||
"alt_text": img.get("alt_text", ""),
|
||||
})
|
||||
|
||||
self.log(f"New {new_count}, updated {updated_count}, draft {draft_count}, grading failed {failed_count}")
|
||||
self.log(f"New {new_count}, updated {updated_count}, draft {draft_count}, "
|
||||
f"grading failed {failed_count}, skipped {skipped_count}")
|
||||
|
||||
async def _grade_article(self, article: dict, ai_url: str, ai_model: str) -> int | None:
|
||||
title = (article.get("title", "") or "")[:500]
|
||||
@@ -180,14 +233,25 @@ class NewsService(BaseService):
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
ai_key = _get_ai_key()
|
||||
if ai_key:
|
||||
headers["Authorization"] = f"Bearer {ai_key}"
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=15.0) as client:
|
||||
resp = await client.post(ai_url, json=payload, headers=headers)
|
||||
resp.raise_for_status()
|
||||
if resp.status_code != 200:
|
||||
self.log(f"AI grading returned {resp.status_code}: {resp.text[:200]}")
|
||||
resp.raise_for_status()
|
||||
result = resp.json()
|
||||
text = result.get("choices", [{}])[0].get("message", {}).get("content", "")
|
||||
return _extract_grade(text)
|
||||
if not text:
|
||||
self.log(f"AI grading returned empty content for: {title[:60]}")
|
||||
return None
|
||||
grade = _extract_grade(text)
|
||||
if grade is None:
|
||||
self.log(f"AI grading returned unparseable response: {text[:100]}")
|
||||
return grade
|
||||
except Exception as e:
|
||||
self.log(f"AI grading failed: {e}")
|
||||
self.log(f"AI grading failed for '{title[:50]}': {e}")
|
||||
return None
|
||||
|
||||
@@ -229,6 +229,29 @@ img {
|
||||
.topnav-user-info { display: flex; flex-direction: column; line-height: 1.2; }
|
||||
.topnav-user-name { font-size: 0.8125rem; font-weight: 600; color: var(--text-primary); }
|
||||
.topnav-user-role { font-size: 0.6875rem; color: var(--text-muted); }
|
||||
.topnav-user-dropdown { position: relative; }
|
||||
.topnav-user-dropdown .dropdown-menu {
|
||||
display: none;
|
||||
position: absolute;
|
||||
top: 100%;
|
||||
right: 0;
|
||||
min-width: 140px;
|
||||
background: var(--bg-secondary);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
padding: 0.25rem 0;
|
||||
z-index: 1001;
|
||||
}
|
||||
.topnav-user-dropdown:hover .dropdown-menu { display: block; }
|
||||
.dropdown-item {
|
||||
display: block;
|
||||
padding: 0.5rem 0.875rem;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-secondary);
|
||||
white-space: nowrap;
|
||||
}
|
||||
.dropdown-item:hover { background: var(--bg-card); color: var(--text-primary); }
|
||||
|
||||
.user-avatar-link { display: inline-flex; flex-shrink: 0; line-height: 0; }
|
||||
.user-avatar-link:hover { opacity: 0.85; }
|
||||
@@ -254,7 +277,6 @@ img {
|
||||
max-width: var(--max-content);
|
||||
margin: 0 auto;
|
||||
padding: 1rem;
|
||||
padding-top: calc(var(--nav-height) + 1rem);
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
@@ -430,6 +452,5 @@ img {
|
||||
@media (max-width: 768px) {
|
||||
.page {
|
||||
padding: 0.5rem;
|
||||
padding-top: calc(var(--nav-height) + 0.5rem);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,13 +53,12 @@
|
||||
}
|
||||
|
||||
.landing-hero {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
padding: 4rem 2rem;
|
||||
padding: 4rem 2rem 3rem;
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
}
|
||||
@@ -126,48 +125,6 @@
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.landing-daily-topic {
|
||||
max-width: 800px;
|
||||
margin: 3rem auto 0;
|
||||
padding: 1.5rem;
|
||||
background: var(--bg-card);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius-lg);
|
||||
text-align: left;
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.landing-daily-topic-label {
|
||||
font-size: 0.75rem;
|
||||
font-weight: 700;
|
||||
color: var(--accent);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.1em;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.landing-daily-topic h3 {
|
||||
font-size: 1.125rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.landing-daily-topic p {
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-secondary);
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.landing-daily-topic-links {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.landing-daily-topic-links a {
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.landing-auth-link {
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-secondary);
|
||||
@@ -181,88 +138,170 @@
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.landing-news {
|
||||
.landing-section {
|
||||
max-width: 960px;
|
||||
margin: 2rem auto 3rem;
|
||||
margin: 0 auto 3rem;
|
||||
padding: 0 2rem;
|
||||
width: 100%;
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.landing-news h2 {
|
||||
.landing-section-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 1.25rem;
|
||||
}
|
||||
|
||||
.landing-section-header h2 {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 700;
|
||||
margin-bottom: 1rem;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.landing-news-grid {
|
||||
.landing-section-link {
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
color: var(--accent);
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
.landing-section-link:hover {
|
||||
color: var(--accent-hover);
|
||||
}
|
||||
|
||||
.landing-posts-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.landing-news-card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
.landing-post-card {
|
||||
background: var(--bg-card);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius-lg);
|
||||
overflow: hidden;
|
||||
border-radius: var(--radius-xl);
|
||||
padding: 1.25rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.625rem;
|
||||
transition: transform 0.2s, box-shadow 0.2s;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.landing-news-card:hover {
|
||||
.landing-post-card:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: var(--shadow-lg);
|
||||
}
|
||||
|
||||
.landing-news-img {
|
||||
width: 100%;
|
||||
aspect-ratio: 16 / 9;
|
||||
overflow: hidden;
|
||||
background: var(--bg-secondary);
|
||||
.landing-post-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.landing-news-img img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.landing-news-body {
|
||||
padding: 0.75rem 1rem;
|
||||
.landing-post-author-wrap {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.25rem;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.landing-news-source {
|
||||
font-size: 0.6875rem;
|
||||
.landing-post-author {
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
color: var(--accent);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.landing-news-body h3 {
|
||||
font-size: 0.875rem;
|
||||
.landing-post-time {
|
||||
font-size: 0.6875rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.landing-post-title {
|
||||
font-size: 1rem;
|
||||
font-weight: 700;
|
||||
color: var(--text-primary);
|
||||
line-height: 1.4;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.landing-news-date {
|
||||
font-size: 0.6875rem;
|
||||
color: var(--text-muted);
|
||||
.landing-post-title a {
|
||||
color: var(--text-primary);
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
.landing-post-title a:hover {
|
||||
color: var(--accent);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.landing-post-content {
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-secondary);
|
||||
line-height: 1.5;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 3;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.landing-post-footer {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
padding-top: 0.5rem;
|
||||
border-top: 1px solid var(--border);
|
||||
margin-top: auto;
|
||||
}
|
||||
|
||||
.landing-post-stat {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-muted);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.landing-post-open {
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 600;
|
||||
color: var(--accent);
|
||||
margin-left: auto;
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
.landing-post-open:hover {
|
||||
color: var(--accent-hover);
|
||||
}
|
||||
|
||||
.landing-footer {
|
||||
text-align: center;
|
||||
padding: 2rem;
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
margin-top: auto;
|
||||
}
|
||||
|
||||
.landing-footer p {
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-muted);
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.landing-footer-links {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
.landing-footer-links a {
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-secondary);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.landing-footer-links a:hover {
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.landing-hero h1 {
|
||||
font-size: 2rem;
|
||||
@@ -273,10 +312,10 @@
|
||||
.landing-nav {
|
||||
padding: 0.75rem 1rem;
|
||||
}
|
||||
.landing-news-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
.landing-news {
|
||||
.landing-section {
|
||||
padding: 0 1rem;
|
||||
}
|
||||
.landing-posts-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
.news-page {
|
||||
max-width: 960px;
|
||||
margin: 0 auto;
|
||||
padding: 1.5rem 1rem;
|
||||
}
|
||||
|
||||
.news-header {
|
||||
margin-bottom: 2rem;
|
||||
margin-bottom: 1.5rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
@@ -167,6 +166,105 @@
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.news-detail {
|
||||
max-width: 760px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.news-detail-card {
|
||||
background: var(--bg-card);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius-xl);
|
||||
overflow: hidden;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.news-detail-image {
|
||||
width: 100%;
|
||||
max-height: 400px;
|
||||
overflow: hidden;
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
|
||||
.news-detail-image img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.news-detail-body {
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
.news-detail-meta {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
flex-wrap: wrap;
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.news-detail-title {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 800;
|
||||
line-height: 1.3;
|
||||
color: var(--text-primary);
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.news-detail-desc {
|
||||
font-size: 1rem;
|
||||
color: var(--text-secondary);
|
||||
line-height: 1.6;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.news-detail-content {
|
||||
font-size: 0.9375rem;
|
||||
color: var(--text-primary);
|
||||
line-height: 1.7;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.news-detail-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.75rem;
|
||||
padding-top: 1rem;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.news-detail-read-link {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 0.25rem;
|
||||
padding: 0.5rem 1rem;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
border-radius: var(--radius);
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.news-detail-read-link:hover {
|
||||
background: var(--accent-hover);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.news-detail-back {
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.news-detail-back:hover {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
@media (max-width: 680px) {
|
||||
.news-grid {
|
||||
grid-template-columns: 1fr;
|
||||
@@ -175,4 +273,8 @@
|
||||
.news-header h1 {
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.news-detail-title {
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,4 +73,17 @@
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.notification-group {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.notification-group-label {
|
||||
font-size: 0.75rem;
|
||||
font-weight: 700;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
color: var(--text-muted);
|
||||
padding: 0 0.25rem 0.5rem;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@
|
||||
</td>
|
||||
<td style="white-space: nowrap; font-size: 0.75rem; color: var(--text-muted);">
|
||||
{% if item.synced_at %}
|
||||
{{ item.synced_at[:10] }}
|
||||
{{ format_date(item.synced_at) }}
|
||||
{% else %}
|
||||
{{ item.time_ago }}
|
||||
{% endif %}
|
||||
|
||||
@@ -47,6 +47,12 @@
|
||||
<input type="text" id="news_ai_model" name="news_ai_model" value="{{ settings.get('news_ai_model', 'molodetz') }}" maxlength="200">
|
||||
</div>
|
||||
|
||||
<div class="admin-field">
|
||||
<label for="news_ai_key">AI API Key</label>
|
||||
<input type="password" id="news_ai_key" name="news_ai_key" value="{{ settings.get('news_ai_key', '') }}" maxlength="500" placeholder="Leave empty to use NEWS_AI_KEY env var">
|
||||
<small class="hint-text">Falls back to OPENROUTER_API_KEY or OPENAI_API_KEY env vars if both field and NEWS_AI_KEY are empty.</small>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">Save Settings</button>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
||||
@@ -39,20 +39,23 @@
|
||||
{% endif %}
|
||||
</head>
|
||||
<body>
|
||||
{% if user %}
|
||||
<nav class="topnav">
|
||||
<div class="topnav-inner">
|
||||
<a href="/feed" class="topnav-logo">Dev<span>Place</span></a>
|
||||
<div class="topnav-links">
|
||||
<a href="/feed" class="topnav-link {% if request.url.path == '/feed' %}active{% endif %}">Home</a>
|
||||
<a href="/" class="topnav-link {% if request.url.path == '/' %}active{% endif %}">Home</a>
|
||||
<a href="/feed" class="topnav-link {% if request.url.path == '/feed' %}active{% endif %}">Posts</a>
|
||||
<a href="/news" class="topnav-link {% if 'news' in request.url.path %}active{% endif %}">News</a>
|
||||
<a href="/projects" class="topnav-link {% if 'projects' in request.url.path %}active{% endif %}">Projects</a>
|
||||
{% if user %}
|
||||
<a href="/messages" class="topnav-link {% if 'messages' in request.url.path %}active{% endif %}">Messages</a>
|
||||
{% if user.get('role') == 'Admin' %}
|
||||
<a href="/admin" class="topnav-link {% if 'admin' in request.url.path %}active{% endif %}">Admin</a>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="topnav-right">
|
||||
{% if user %}
|
||||
<a href="/notifications" class="topnav-icon">
|
||||
<span class="nav-bell">🔔</span>
|
||||
{% set unread_count = get_unread_count(user["uid"]) %}
|
||||
@@ -60,17 +63,25 @@
|
||||
<span class="nav-badge">{{ unread_count }}</span>
|
||||
{% endif %}
|
||||
</a>
|
||||
<a href="/profile/{{ user['username'] }}" class="topnav-user">
|
||||
{% set _user = user %}{% set _size = 32 %}{% set _size_class = "sm" %}{% include "_avatar_link.html" %}
|
||||
<div class="topnav-user-info">
|
||||
<span class="topnav-user-name">{{ user['username'] }}</span>
|
||||
<span class="topnav-user-role">{{ user.get('role', 'Member') }}</span>
|
||||
<div class="topnav-user-dropdown">
|
||||
<a href="/profile/{{ user['username'] }}" class="topnav-user">
|
||||
<img src="{{ avatar_url('multiavatar', user['username'], 32) }}" class="avatar-img avatar-sm" alt="{{ user['username'] }}" loading="lazy">
|
||||
<div class="topnav-user-info">
|
||||
<span class="topnav-user-name">{{ user['username'] }}</span>
|
||||
<span class="topnav-user-role">{{ user.get('role', 'Member') }}</span>
|
||||
</div>
|
||||
</a>
|
||||
<div class="dropdown-menu">
|
||||
<a href="/auth/logout" class="dropdown-item">Logout</a>
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
{% else %}
|
||||
<a href="/auth/login" class="topnav-link">Login</a>
|
||||
<a href="/auth/signup" class="btn btn-primary btn-sm">Sign Up</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
{% endif %}
|
||||
|
||||
{% if breadcrumbs and breadcrumbs|length > 1 %}
|
||||
<nav aria-label="Breadcrumb" class="breadcrumb">
|
||||
@@ -92,6 +103,7 @@
|
||||
{% block content %}{% endblock %}
|
||||
</main>
|
||||
|
||||
{% block footer %}
|
||||
<footer class="site-footer">
|
||||
<div class="footer-inner">
|
||||
<span class="footer-copy">© DevPlace</span>
|
||||
@@ -100,6 +112,7 @@
|
||||
</nav>
|
||||
</div>
|
||||
</footer>
|
||||
{% endblock %}
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/marked/lib/marked.umd.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.11.1/build/highlight.min.js"></script>
|
||||
|
||||
@@ -156,6 +156,7 @@
|
||||
</aside>
|
||||
</div>
|
||||
|
||||
{% if user %}
|
||||
<a href="#" class="feed-fab" data-modal="create-post-modal" title="Create New Post">+</a>
|
||||
|
||||
<div class="modal-overlay" id="create-post-modal">
|
||||
@@ -209,5 +210,6 @@
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
@@ -1,19 +1,11 @@
|
||||
{% extends "base.html" %}
|
||||
{% block footer %}{% endblock %}
|
||||
{% block extra_head %}
|
||||
<link rel="stylesheet" href="/static/css/landing.css">
|
||||
<link rel="stylesheet" href="/static/css/news.css">
|
||||
{% endblock %}
|
||||
{% block content %}
|
||||
<div class="landing">
|
||||
<nav class="landing-nav">
|
||||
<div class="landing-logo">Dev<span>Place</span></div>
|
||||
<div class="landing-nav-links">
|
||||
<a href="/">Home</a>
|
||||
<a href="/projects">Projects</a>
|
||||
<a href="/auth/login">Login</a>
|
||||
<a href="/auth/signup" class="btn btn-primary btn-sm">Sign Up</a>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<section class="landing-hero">
|
||||
<h1>Devplace.net — The Developer <span>Social Network</span></h1>
|
||||
<p>Track industry shifts. Discover bold releases. Share what you're building in an open, uncensored environment.</p>
|
||||
@@ -38,41 +30,100 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="landing-daily-topic">
|
||||
<div class="landing-daily-topic-label">Daily Topic</div>
|
||||
<h3>{{ daily_topic.get('title', '') }}</h3>
|
||||
<p>{{ daily_topic.get('summary', '') }}</p>
|
||||
<div class="landing-daily-topic-links">
|
||||
<a href="/feed">Read More</a>
|
||||
<a href="/feed">Source</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="landing-auth-link">
|
||||
Already have an account? <a href="/auth/login">Log In</a>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{% if landing_articles %}
|
||||
<section class="landing-news">
|
||||
<h2>Developer News</h2>
|
||||
<div class="landing-news-grid">
|
||||
{% for article in landing_articles %}
|
||||
<a href="{{ article.url }}" target="_blank" rel="noopener" class="landing-news-card">
|
||||
{% if article.image_url %}
|
||||
<div class="landing-news-img">
|
||||
<img src="{{ article.image_url }}" alt="" loading="lazy">
|
||||
{% if landing_posts %}
|
||||
<section class="landing-section">
|
||||
<div class="landing-section-header">
|
||||
<h2>Latest Posts</h2>
|
||||
<a href="/feed" class="landing-section-link">Browse all posts →</a>
|
||||
</div>
|
||||
<div class="landing-posts-grid">
|
||||
{% for item in landing_posts %}
|
||||
<article class="landing-post-card">
|
||||
<div class="landing-post-header">
|
||||
{% set _user = item.author %}
|
||||
{% set _size = 28 %}
|
||||
{% set _size_class = "sm" %}
|
||||
{% include "_avatar_link.html" %}
|
||||
<div class="landing-post-author-wrap">
|
||||
<a href="/profile/{{ item.author['username'] }}" class="landing-post-author">{{ item.author['username'] }}</a>
|
||||
<span class="landing-post-time">{{ item.time_ago }}</span>
|
||||
</div>
|
||||
<span class="badge badge-{{ item.post['topic'] }}">{{ item.post['topic'] }}</span>
|
||||
</div>
|
||||
{% if item.post.get('title') %}
|
||||
<h3 class="landing-post-title">
|
||||
<a href="/posts/{{ item.slug }}">{{ item.post['title'] }}</a>
|
||||
</h3>
|
||||
{% endif %}
|
||||
<div class="landing-news-body">
|
||||
<span class="landing-news-source">{{ article.source_name }}</span>
|
||||
<h3>{{ article.title[:100] }}{% if article.title|length > 100 %}...{% endif %}</h3>
|
||||
<span class="landing-news-date">{{ article.synced_at }}</span>
|
||||
<div class="landing-post-content rendered-content" data-render>{{ item.post['content'][:200] }}{% if item.post['content']|length > 200 %}...{% endif %}</div>
|
||||
<div class="landing-post-footer">
|
||||
<span class="landing-post-stat">+{{ item.stars }}</span>
|
||||
<span class="landing-post-stat">💬 {{ item.comment_count }}</span>
|
||||
<a href="/posts/{{ item.slug }}" class="landing-post-open">Open →</a>
|
||||
</div>
|
||||
</a>
|
||||
</article>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</section>
|
||||
{% endif %}
|
||||
|
||||
{% if landing_articles %}
|
||||
<section class="landing-section">
|
||||
<div class="landing-section-header">
|
||||
<h2>Developer News</h2>
|
||||
<a href="/news" class="landing-section-link">View all news →</a>
|
||||
</div>
|
||||
<div class="news-grid">
|
||||
{% for item in landing_articles %}
|
||||
<article class="news-card">
|
||||
{% if item.image_url %}
|
||||
<div class="news-card-image">
|
||||
<img src="{{ item.image_url }}" alt="" loading="lazy" onerror="this.parentElement.style.display='none'">
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="news-card-body">
|
||||
<div class="news-card-meta">
|
||||
<span class="news-source">{{ item.source_name }}</span>
|
||||
{% if item.grade >= 9 %}
|
||||
<span class="news-grade news-grade-top">Grade {{ item.grade }}</span>
|
||||
{% elif item.grade >= 7 %}
|
||||
<span class="news-grade news-grade-high">Grade {{ item.grade }}</span>
|
||||
{% else %}
|
||||
<span class="news-grade">Grade {{ item.grade }}</span>
|
||||
{% endif %}
|
||||
<span class="news-time">{{ item.time_ago }}</span>
|
||||
</div>
|
||||
<h3 class="news-card-title">
|
||||
<a href="/news/{{ item.slug or item.uid }}">{{ item.title }}</a>
|
||||
</h3>
|
||||
{% if item.description %}
|
||||
<p class="news-card-desc">{{ item.description[:250] }}{% if item.description|length > 250 %}...{% endif %}</p>
|
||||
{% endif %}
|
||||
<a href="{{ item.url }}" target="_blank" rel="noopener" class="news-read-link">
|
||||
Read on {{ item.source_name }} ↗
|
||||
</a>
|
||||
</div>
|
||||
</article>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</section>
|
||||
{% endif %}
|
||||
|
||||
<footer class="landing-footer">
|
||||
<p>© DevPlace — The Developer Social Network</p>
|
||||
<div class="landing-footer-links">
|
||||
<a href="/feed">Posts</a>
|
||||
<a href="/news">News</a>
|
||||
<a href="/projects">Projects</a>
|
||||
<a href="/auth/login">Login</a>
|
||||
<a href="/auth/signup">Sign Up</a>
|
||||
<a href="/bugs">Bug Report</a>
|
||||
</div>
|
||||
</footer>
|
||||
</div>
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
@@ -19,7 +19,7 @@
|
||||
<div class="conversation-name">{{ conv.other_user['username'] }}</div>
|
||||
<div class="conversation-preview">{{ conv.last_message[:60] }}</div>
|
||||
</div>
|
||||
<span class="conversation-time">{{ conv.last_message_at[:10] }}</span>
|
||||
<span class="conversation-time">{{ format_date(conv.last_message_at) }}</span>
|
||||
</a>
|
||||
{% else %}
|
||||
<div class="empty-state" style="border: none;">No conversations yet</div>
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
<span class="news-time">{{ item.time_ago }}</span>
|
||||
</div>
|
||||
<h3 class="news-card-title">
|
||||
<a href="{{ item.article['url'] }}" target="_blank" rel="noopener">{{ item.article['title'] }}</a>
|
||||
<a href="/news/{{ item.article['slug'] or item.article['uid'] }}">{{ item.article['title'] }}</a>
|
||||
</h3>
|
||||
{% if item.article.get('description') %}
|
||||
<p class="news-card-desc">{{ item.article['description'][:250] }}{% if item.article['description']|length > 250 %}...{% endif %}</p>
|
||||
|
||||
@@ -7,27 +7,32 @@
|
||||
<div class="notifications-header">
|
||||
<h2>Notifications</h2>
|
||||
<form method="POST" action="/notifications/mark-all-read" style="display:inline;">
|
||||
<button type="submit" class="btn btn-ghost btn-sm">Mark all read</button>
|
||||
<button type="submit" class="btn btn-ghost btn-sm">Clear</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="notifications-list">
|
||||
{% for item in notifications %}
|
||||
<div class="notification-card {% if not item.notification['read'] %}unread{% endif %}">
|
||||
<a href="/profile/{{ item.actor['username'] if item.actor else '#' }}">
|
||||
<img src="{{ avatar_url('multiavatar', item.actor['username'] if item.actor else '?', 32) }}" class="avatar-img avatar-sm" alt="{{ item.actor['username'] if item.actor else '?' }}" loading="lazy">
|
||||
</a>
|
||||
<div class="notification-body">
|
||||
<div class="notification-text">{{ item.notification['message'] }}</div>
|
||||
<div class="notification-time">{{ item.time_ago }}</div>
|
||||
{% for group in notification_groups %}
|
||||
<div class="notification-group">
|
||||
<div class="notification-group-label">{{ group.label }}</div>
|
||||
{% for item in group.entries %}
|
||||
<div class="notification-card {% if not item.notification['read'] %}unread{% endif %}">
|
||||
<a href="/profile/{{ item.actor['username'] if item.actor else '#' }}">
|
||||
<img src="{{ avatar_url('multiavatar', item.actor['username'] if item.actor else '?', 32) }}" class="avatar-img avatar-sm" alt="{{ item.actor['username'] if item.actor else '?' }}" loading="lazy">
|
||||
</a>
|
||||
<div class="notification-body">
|
||||
<div class="notification-text">{{ item.notification['message'] }}</div>
|
||||
<div class="notification-time">{{ item.time_ago }}</div>
|
||||
</div>
|
||||
<form method="POST" action="/notifications/mark-read/{{ item.notification['uid'] }}" style="display:inline;">
|
||||
<button type="submit" class="notification-dismiss" data-uid="{{ item.notification['uid'] }}">×</button>
|
||||
</form>
|
||||
</div>
|
||||
<form method="POST" action="/notifications/mark-read/{{ item.notification['uid'] }}" style="display:inline;">
|
||||
<button type="submit" class="notification-dismiss" data-uid="{{ item.notification['uid'] }}">×</button>
|
||||
</form>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="empty-state">No notifications yet</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
@@ -18,10 +18,10 @@
|
||||
<span class="meta-item">Uptime: {{ svc.uptime }}</span>
|
||||
{% endif %}
|
||||
{% if svc.last_run %}
|
||||
<span class="meta-item">Last run: {{ svc.last_run[:19] }}</span>
|
||||
<span class="meta-item">Last run: {{ format_date(svc.last_run, include_time=True) }}</span>
|
||||
{% endif %}
|
||||
{% if svc.next_run %}
|
||||
<span class="meta-item">Next run: {{ svc.next_run[:19] }}</span>
|
||||
<span class="meta-item">Next run: {{ format_date(svc.next_run, include_time=True) }}</span>
|
||||
{% endif %}
|
||||
<span class="meta-item">Interval: every {{ svc.interval_seconds }}s</span>
|
||||
</div>
|
||||
@@ -42,6 +42,25 @@
|
||||
<script type="module">
|
||||
const POLL_INTERVAL = 5000;
|
||||
|
||||
function formatDate(isoStr, includeTime) {
|
||||
if (!isoStr) return '-';
|
||||
try {
|
||||
const d = new Date(isoStr);
|
||||
if (isNaN(d.getTime())) return isoStr.slice(0, 19);
|
||||
const dd = String(d.getDate()).padStart(2, '0');
|
||||
const mm = String(d.getMonth() + 1).padStart(2, '0');
|
||||
const yyyy = d.getFullYear();
|
||||
if (includeTime) {
|
||||
const hh = String(d.getHours()).padStart(2, '0');
|
||||
const mi = String(d.getMinutes()).padStart(2, '0');
|
||||
return `${dd}/${mm}/${yyyy} ${hh}:${mi}`;
|
||||
}
|
||||
return `${dd}/${mm}/${yyyy}`;
|
||||
} catch {
|
||||
return isoStr.slice(0, 19);
|
||||
}
|
||||
}
|
||||
|
||||
async function pollServices() {
|
||||
try {
|
||||
const resp = await fetch('/admin/services/data');
|
||||
@@ -56,8 +75,8 @@ async function pollServices() {
|
||||
const metaItems = card.querySelectorAll('.meta-item');
|
||||
if (metaItems.length >= 3) {
|
||||
metaItems[0].textContent = 'Uptime: ' + (svc.uptime || '-');
|
||||
metaItems[1].textContent = 'Last run: ' + (svc.last_run ? svc.last_run.slice(0, 19) : '-');
|
||||
metaItems[2].textContent = 'Next run: ' + (svc.next_run ? svc.next_run.slice(0, 19) : '-');
|
||||
metaItems[1].textContent = 'Last run: ' + formatDate(svc.last_run, true);
|
||||
metaItems[2].textContent = 'Next run: ' + formatDate(svc.next_run, true);
|
||||
}
|
||||
const logPre = card.querySelector('.log-output');
|
||||
if (logPre) {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import time
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from devplacepy.config import TEMPLATES_DIR
|
||||
from devplacepy.database import get_table
|
||||
from devplacepy.avatar import avatar_url
|
||||
from devplacepy.utils import format_date as _format_date
|
||||
from devplacepy.seo import (
|
||||
site_url, combine,
|
||||
website_schema, breadcrumb_schema,
|
||||
@@ -13,13 +15,18 @@ templates = Jinja2Templates(directory=str(TEMPLATES_DIR))
|
||||
|
||||
|
||||
_unread_cache = {}
|
||||
_UNREAD_CACHE_TTL = 60
|
||||
|
||||
def jinja_unread_count(user_uid: str) -> int:
|
||||
if user_uid in _unread_cache:
|
||||
return _unread_cache[user_uid]
|
||||
cached = _unread_cache.get(user_uid)
|
||||
if cached:
|
||||
entry, expiry = cached
|
||||
if time.time() < expiry:
|
||||
return entry
|
||||
_unread_cache.pop(user_uid, None)
|
||||
notifs = get_table("notifications")
|
||||
count = len(list(notifs.find(user_uid=user_uid, read=False)))
|
||||
_unread_cache[user_uid] = count
|
||||
_unread_cache[user_uid] = (count, time.time() + _UNREAD_CACHE_TTL)
|
||||
return count
|
||||
|
||||
|
||||
@@ -31,3 +38,4 @@ def jinja_user_projects(user_uid: str) -> list:
|
||||
templates.env.globals["get_unread_count"] = jinja_unread_count
|
||||
templates.env.globals["get_user_projects"] = jinja_user_projects
|
||||
templates.env.globals["avatar_url"] = avatar_url
|
||||
templates.env.globals["format_date"] = _format_date
|
||||
|
||||
+20
-5
@@ -1,4 +1,5 @@
|
||||
import secrets
|
||||
import time
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from passlib.hash import pbkdf2_sha256
|
||||
@@ -31,6 +32,7 @@ def create_session(user_uid: str) -> str:
|
||||
|
||||
|
||||
_user_cache = {}
|
||||
_USER_CACHE_TTL = 300
|
||||
|
||||
|
||||
def get_current_user(request: Request):
|
||||
@@ -40,7 +42,10 @@ def get_current_user(request: Request):
|
||||
|
||||
cached = _user_cache.get(token)
|
||||
if cached:
|
||||
return cached
|
||||
entry, expiry = cached
|
||||
if time.time() < expiry:
|
||||
return entry
|
||||
_user_cache.pop(token, None)
|
||||
|
||||
sessions = get_table("sessions")
|
||||
session = sessions.find_one(session_token=token)
|
||||
@@ -57,7 +62,7 @@ def get_current_user(request: Request):
|
||||
_user_cache.pop(token, None)
|
||||
return None
|
||||
if user:
|
||||
_user_cache[token] = user
|
||||
_user_cache[token] = (user, time.time() + _USER_CACHE_TTL)
|
||||
return user
|
||||
|
||||
|
||||
@@ -101,10 +106,8 @@ def time_ago(dt_str: str) -> str:
|
||||
now = datetime.utcnow()
|
||||
diff = now - dt
|
||||
days = diff.days
|
||||
if days > 365:
|
||||
return f"{days // 365}y ago"
|
||||
if days > 30:
|
||||
return f"{days // 30}mo ago"
|
||||
return dt.strftime("%d/%m/%Y")
|
||||
if days > 0:
|
||||
return f"{days}d ago"
|
||||
hours = diff.seconds // 3600
|
||||
@@ -114,3 +117,15 @@ def time_ago(dt_str: str) -> str:
|
||||
if minutes > 0:
|
||||
return f"{minutes}m ago"
|
||||
return "just now"
|
||||
|
||||
|
||||
def format_date(dt_str: str, include_time: bool = False) -> str:
|
||||
if not dt_str:
|
||||
return ""
|
||||
try:
|
||||
dt = datetime.fromisoformat(dt_str)
|
||||
if include_time:
|
||||
return dt.strftime("%d/%m/%Y %H:%M")
|
||||
return dt.strftime("%d/%m/%Y")
|
||||
except (ValueError, TypeError):
|
||||
return dt_str
|
||||
|
||||
Reference in New Issue
Block a user