From 05d663320415ae72d612288f3cf2f8e395c012c2 Mon Sep 17 00:00:00 2001 From: retoor Date: Thu, 5 Nov 2026 17:59:21 +0100 Subject: [PATCH] Render markdown in gist and project sidebar titles; add content visibility and GIF picker Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 3 + README.md | 12 +- devplacepy/content.py | 145 +++++- devplacepy/database/CLAUDE.md | 19 + devplacepy/database/__init__.py | 24 + devplacepy/database/activity.py | 3 +- devplacepy/database/content.py | 2 + devplacepy/database/engagement.py | 3 +- devplacepy/database/pagination.py | 11 +- devplacepy/database/schema.py | 5 + devplacepy/database/soft_delete.py | 1 + devplacepy/database/stats.py | 24 +- devplacepy/database/visibility.py | 112 +++++ devplacepy/docs_api/_shared.py | 68 +++ devplacepy/docs_api/groups/admin.py | 11 +- devplacepy/docs_api/groups/content.py | 17 +- devplacepy/docs_api/groups/messaging.py | 25 + devplacepy/docs_api/groups/uploads.py | 78 ++- devplacepy/docs_live.py | 2 +- devplacepy/happy404.py | 4 +- devplacepy/lifespan.py | 2 + devplacepy/main.py | 3 + devplacepy/models/__init__.py | 8 + devplacepy/models/community.py | 5 +- devplacepy/models/content.py | 10 +- devplacepy/models/projects.py | 11 +- devplacepy/models/shared.py | 41 ++ devplacepy/models/uploads.py | 5 + devplacepy/routers/CLAUDE.md | 5 + devplacepy/routers/admin/gateway_configs.py | 71 ++- devplacepy/routers/admin/news.py | 24 +- devplacepy/routers/bookmarks.py | 6 +- devplacepy/routers/comments.py | 5 +- devplacepy/routers/devrant/rants.py | 12 +- devplacepy/routers/docs/pages.py | 12 + devplacepy/routers/giphy.py | 126 +++++ devplacepy/routers/gists.py | 12 +- devplacepy/routers/messages.py | 50 +- devplacepy/routers/news.py | 6 +- devplacepy/routers/notes.py | 6 +- devplacepy/routers/posts.py | 12 +- devplacepy/routers/profile/index.py | 20 +- devplacepy/routers/projects/CLAUDE.md | 2 +- devplacepy/routers/projects/index.py | 17 +- devplacepy/routers/reactions.py | 3 + devplacepy/routers/topics.py | 6 +- devplacepy/routers/votes.py | 4 +- devplacepy/schemas/content.py | 18 +- devplacepy/schemas/gateway.py | 2 + devplacepy/seo.py | 10 +- devplacepy/services/backup/CLAUDE.md | 2 +- devplacepy/services/backup/offload.py | 7 + devplacepy/services/dbapi/service.py | 50 +- .../services/devii/actions/catalog/_shared.py | 13 + .../services/devii/actions/catalog/admin.py | 13 +- .../services/devii/actions/catalog/gateway.py | 1 + .../services/devii/actions/catalog/gists.py | 12 +- .../devii/actions/catalog/messages.py | 9 +- .../services/devii/actions/catalog/posts.py | 12 +- .../devii/actions/catalog/projects.py | 12 +- .../services/devii/actions/catalog/uploads.py | 40 ++ .../services/devii/actions/dispatcher.py | 7 + devplacepy/services/devrant/feed.py | 18 +- devplacepy/services/devrant/ids.py | 9 +- devplacepy/services/devrant/profile.py | 7 +- devplacepy/services/giphy/CLAUDE.md | 31 ++ devplacepy/services/giphy/__init__.py | 5 + devplacepy/services/giphy/client.py | 134 ++++++ devplacepy/services/giphy/config.py | 59 +++ devplacepy/services/giphy/fake.py | 65 +++ devplacepy/services/giphy/limits.py | 76 +++ devplacepy/services/giphy/runtime.py | 16 + devplacepy/services/giphy/service.py | 38 ++ devplacepy/services/messaging/CLAUDE.md | 8 +- devplacepy/services/messaging/__init__.py | 4 + devplacepy/services/messaging/persist.py | 5 +- devplacepy/services/messaging/relay.py | 34 +- devplacepy/services/messaging/removal.py | 48 ++ devplacepy/services/nodes/geo.py | 8 +- devplacepy/services/notification_relay.py | 2 +- devplacepy/services/openai_gateway/CLAUDE.md | 11 + devplacepy/services/openai_gateway/gateway.py | 96 +++- devplacepy/services/openai_gateway/routing.py | 22 + .../services/openai_gateway/tool_emulation.py | 444 ++++++++++++++++++ devplacepy/services/registry.py | 2 + devplacepy/services/sync/exporter.py | 7 +- devplacepy/static/css/chat.css | 38 ++ devplacepy/static/css/components.css | 190 ++++++++ devplacepy/static/css/engagement.css | 1 + devplacepy/static/css/gateway.css | 25 + devplacepy/static/css/projects.css | 3 - devplacepy/static/js/AwardGiver.js | 3 +- devplacepy/static/js/CLAUDE.md | 6 +- devplacepy/static/js/ProjectActionsMenu.js | 32 -- .../static/js/components/AppActionMenu.js | 98 ++++ devplacepy/static/js/components/AppChat.js | 66 ++- devplacepy/static/js/components/AppDialog.js | 32 ++ .../static/js/components/AppGifPicker.js | 230 +++++++++ devplacepy/static/js/components/AppUpload.js | 18 + devplacepy/static/js/components/index.js | 2 + devplacepy/templates/CLAUDE.md | 4 + devplacepy/templates/_comment_form.html | 1 + devplacepy/templates/_post_composer_form.html | 7 +- devplacepy/templates/_report_button.html | 5 +- devplacepy/templates/_visibility_field.html | 10 + devplacepy/templates/admin_gateway.html | 41 +- .../templates/admin_gateway_model_form.html | 7 + .../admin_gateway_provider_form.html | 1 + devplacepy/templates/admin_news.html | 12 +- .../docs/component-dp-gif-picker.html | 47 ++ .../templates/docs/content-visibility.html | 66 +++ devplacepy/templates/docs/devii-admin.html | 2 +- devplacepy/templates/docs/devii-tools.html | 1 + devplacepy/templates/docs/feed.html | 5 + devplacepy/templates/docs/privacy.html | 1 + devplacepy/templates/gist_detail.html | 1 + devplacepy/templates/gists.html | 2 + devplacepy/templates/messages.html | 3 + devplacepy/templates/post.html | 5 +- devplacepy/templates/profile.html | 49 +- devplacepy/templates/project_detail.html | 60 ++- devplacepy/templates/projects.html | 2 + devplacepy/templating.py | 14 +- events.md | 16 +- pyproject.toml | 2 +- tests/CLAUDE.md | 2 +- tests/api/admin/gateway/models.py | 137 ++++++ tests/api/admin/news/visibility.py | 167 +++++++ tests/api/content/visibility.py | 395 ++++++++++++++++ tests/api/giphy.py | 248 ++++++++++ tests/api/messages/delete.py | 122 +++++ tests/e2e/admin/gateway.py | 163 +++++++ tests/e2e/admin/news.py | 17 + tests/e2e/giphy.py | 262 +++++++++++ tests/e2e/gists.py | 78 +++ tests/e2e/messages.py | 27 ++ tests/e2e/posts/visibility.py | 105 +++++ tests/e2e/profile/block.py | 53 ++- tests/e2e/profile/mute.py | 47 +- tests/e2e/projects/index.py | 42 +- tests/e2e/projects/visibility.py | 87 ++++ tests/e2e/projects/workspace.py | 9 +- tests/e2e/root.py | 2 +- tests/e2e/ui/components.py | 35 +- tests/unit/database/visibility.py | 247 ++++++++++ tests/unit/docs_api/visibility.py | 37 ++ tests/unit/services/backup/offload.py | 15 + tests/unit/services/dbapi/service.py | 23 + tests/unit/services/giphy/__init__.py | 1 + tests/unit/services/giphy/client.py | 190 ++++++++ tests/unit/services/giphy/limits.py | 84 ++++ tests/unit/services/giphy/service.py | 80 ++++ tests/unit/services/openai_gateway/gateway.py | 204 ++++++++ .../services/openai_gateway/tool_emulation.py | 286 +++++++++++ 154 files changed, 6394 insertions(+), 289 deletions(-) create mode 100644 devplacepy/database/visibility.py create mode 100644 devplacepy/routers/giphy.py create mode 100644 devplacepy/services/giphy/CLAUDE.md create mode 100644 devplacepy/services/giphy/__init__.py create mode 100644 devplacepy/services/giphy/client.py create mode 100644 devplacepy/services/giphy/config.py create mode 100644 devplacepy/services/giphy/fake.py create mode 100644 devplacepy/services/giphy/limits.py create mode 100644 devplacepy/services/giphy/runtime.py create mode 100644 devplacepy/services/giphy/service.py create mode 100644 devplacepy/services/messaging/removal.py create mode 100644 devplacepy/services/openai_gateway/tool_emulation.py delete mode 100644 devplacepy/static/js/ProjectActionsMenu.js create mode 100644 devplacepy/static/js/components/AppActionMenu.js create mode 100644 devplacepy/static/js/components/AppGifPicker.js create mode 100644 devplacepy/templates/_visibility_field.html create mode 100644 devplacepy/templates/docs/component-dp-gif-picker.html create mode 100644 devplacepy/templates/docs/content-visibility.html create mode 100644 tests/api/admin/news/visibility.py create mode 100644 tests/api/content/visibility.py create mode 100644 tests/api/giphy.py create mode 100644 tests/api/messages/delete.py create mode 100644 tests/e2e/giphy.py create mode 100644 tests/e2e/posts/visibility.py create mode 100644 tests/e2e/projects/visibility.py create mode 100644 tests/unit/database/visibility.py create mode 100644 tests/unit/docs_api/visibility.py create mode 100644 tests/unit/services/giphy/__init__.py create mode 100644 tests/unit/services/giphy/client.py create mode 100644 tests/unit/services/giphy/limits.py create mode 100644 tests/unit/services/giphy/service.py create mode 100644 tests/unit/services/openai_gateway/tool_emulation.py diff --git a/CLAUDE.md b/CLAUDE.md index 2945e5b1..b05906b0 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -166,6 +166,7 @@ Nested `CLAUDE.md` files (loaded automatically by Claude Code only when a file i | `devplacepy/services/game/CLAUDE.md` | Code Farm idle game (economy invariants, raids, the one-pure-function rule) | | `devplacepy/services/opinionwar/CLAUDE.md` | Opinion Wars (week-long faction battles on posts: atomic fight/resolve transitions, cooldown-before-coins compensation, event seq allocation, relay-on-lock-owner) | | `devplacepy/services/quiz/CLAUDE.md` | Quizzes (the terminal publish lock, attempt atomicity, answer-key withholding, AI free-text grading, the best-attempt scoreboard) | +| `devplacepy/services/giphy/CLAUDE.md` | GIF picker: GIPHY search through the server, admin-held API key, GIFs stored as ordinary attachments, cache and per-user caps | | `devplacepy/services/skills/CLAUDE.md` | Agent Skills: strict `SKILL.md` parser, SSRF-guarded fetcher, the bounded prompt index, per-user privacy, soft delete and account-deletion cascade, why a skill is data and never code | | `devplacepy/services/nodes/CLAUDE.md` | Community node network: the protocol specification (single source of truth), the no-open-proxy rule, the two transports and their trust levels, the decisions taken, and the gaps per phase | | `devplacepy/services/community/CLAUDE.md` | Community discovery: rsearch + feed detection, one-time structure learning, generated collectors, dry run, the failure floor, admin-only visibility | @@ -206,6 +207,7 @@ Routers in `devplacepy/routers/` are organised as a **directory tree that mirror | `/admin`, `/admin/services`, `/admin/containers` | admin/ package | | `/issues` | issues/ package - see `services/gitea/CLAUDE.md` | | `/gists`, `/news`, `/uploads`, `/media` | flat files | +| `/giphy` | giphy.py - GIF picker backend (search, trending, attach), see `services/giphy/CLAUDE.md` | | `/openai` | openai_gateway.py - see `services/openai_gateway/CLAUDE.md` | | `/devii` | devii.py - see `services/devii/CLAUDE.md` | | `/zips`, `/forks` | see `services/jobs/CLAUDE.md` | @@ -310,6 +312,7 @@ The escape hatch is deliberately two-factor and must never be self-served: after - **Admin seniority: a junior admin cannot manage a more senior admin.** Every per-user mutation in `routers/admin/users.py` and `routers/admin/moderation.py` is gated by `is_senior_admin(actor, target)` (`routers/admin/_shared.py`) - blocks (audits `result="denied"`) when the target is an Admin who registered earlier. Server-side, so it also covers Devii's admin tools. - **Never pass a `respond()` context key that collides with a Jinja global.** `respond(request, template, ctx, model=XOut)` feeds the same `ctx` to both the Pydantic model (JSON) and the template. A key like `is_admin`/`avatar_url`/`is_self` holding a non-callable value shadows the global across the whole inheritance chain, turning `{% if is_admin(user) %}` into `False(user)` -> `TypeError`, a 500 that fires only for the branch that calls the global. Name viewer/permission flags distinctly (`viewer_is_admin`, not `is_admin`) in both schema and context. - **Project visibility (`is_private`) and read-only (`read_only`)** are owner-controlled flags on `projects`. Read access is gated by the single `content.can_view_project(project, user)` predicate at EVERY read surface - never re-implement the check inline. Predicate: `not is_private OR is_owner OR (is_admin AND owner is not an admin)` - a project hidden by a member stays visible to any admin, but one hidden by an admin is visible only to that owner admin. **Containers have their own, stricter isolation predicates** (`owns_instance`, `can_view_project_containers`, `can_view_instance`, `can_manage_instance`) - the primary administrator sees/manages every container; any other admin can VIEW others' containers only on public projects and can MANAGE only instances they own. Read-only is enforced as a single data-layer guard `project_files._guard_writable(project_uid)` at the top of every mutation entrypoint - add it to any NEW file-mutating function. Devii may flip read-only/visibility only after explicit confirmation (`CONFIRM_REQUIRED`). Full UI-level detail in `devplacepy/routers/projects/CLAUDE.md`. +- **Content visibility (`published`/`hidden`/`draft`/`offline`) on posts, gists, projects and news is one specification, `devplacepy/database/visibility.py`.** Any new read of those tables applies `listed_clause`/`listed_sql` (or goes through `paginate`, which does it), and any single-item or target access uses `content.can_view_item`/`can_view_target`. NULL reads as published. Full detail in `devplacepy/database/CLAUDE.md`. - **Deletions ALWAYS require confirmation:** `confirmation_error` gates EVERY content delete tool via `CONFIRM_REQUIRED` (`delete_post`, `delete_comment`, `delete_gist`, `delete_project`, `project_delete_file`, `delete_media`, `delete_attachment`, `admin_delete_news`, container delete, and any `container_exec` matching `dispatcher.DESTRUCTIVE_COMMAND`). The first call is refused; the agent must show the exact target then pass `confirm=true`. **Load-bearing: every confirmation-gated tool MUST also declare a `confirm` boolean param in its catalog spec** - schemas set `additionalProperties: false`, so a gated tool without a declared `confirm` param can never receive it and loops forever. ## Every user-generated surface is reportable by construction (hard rule) diff --git a/README.md b/README.md index 8c7358a9..d196d80d 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ devplacepy/ | `/admin/trash` | Admin **Trash**: review, restore, and permanently purge soft-deleted content (posts, comments, gists, projects, news, project files, attachments) across the platform | | `/admin/devii-tasks` | Admin **Devii tasks**: every scheduled task across all owners with its schedule, run count, expiry and failure streak, plus per-task disable and delete | | `/notifications` | Notification list, mark read, live unread counts (`/notifications/counts`) | -| `/messages` | Real-time direct messaging over WebSocket (`/messages/ws`): live bidirectional delivery, optimistic send, typing indicators, read receipts, reconnect catch-up, and online/last-seen presence. Messages render through the shared content pipeline (emoji shortcodes, image, video and audio embeds, autolink, sanitization). AI content correction and the AI modifier apply to direct messages: the original is delivered immediately, then a second `message` frame with `ai_processed` replaces the bubble for both participants when the rewrite lands (including across workers). An opened conversation loads its 500 most recent messages and can page older rows with `?before=`; switching threads does not drop the socket. `GET /messages/conversations` returns the conversation list as JSON for a live client refresh; the `POST /messages/send` form remains as a no-JavaScript fallback and now accepts an attachment-only, empty-content message. `POST /messages/ws-ticket` exchanges a session/API-key auth to a short-lived (30s), single-use ticket a browser WebSocket can carry as `?ticket=...` on the handshake, since a native `WebSocket` cannot set an `Authorization`/`X-API-KEY` header - this is what makes off-session embedding of the chat possible. CLI: `devplace messaging prune-tickets` removes expired tickets | +| `/messages` | Real-time direct messaging over WebSocket (`/messages/ws`): live bidirectional delivery, optimistic send, typing indicators, read receipts, reconnect catch-up, and online/last-seen presence. Messages render through the shared content pipeline (emoji shortcodes, image, video and audio embeds, autolink, sanitization). AI content correction and the AI modifier apply to direct messages: the original is delivered immediately, then a second `message` frame with `ai_processed` replaces the bubble for both participants when the rewrite lands (including across workers). An opened conversation loads its 500 most recent messages and can page older rows with `?before=`; switching threads does not drop the socket. `GET /messages/conversations` returns the conversation list as JSON for a live client refresh; the `POST /messages/send` form remains as a no-JavaScript fallback and now accepts an attachment-only, empty-content message. An administrator can delete the messages they themselves sent (`POST /messages/{uid}/delete`, soft delete, confirmation in the chat UI and in Devii); the bubble disappears live for both participants. `POST /messages/ws-ticket` exchanges a session/API-key auth to a short-lived (30s), single-use ticket a browser WebSocket can carry as `?ticket=...` on the handshake, since a native `WebSocket` cannot set an `Authorization`/`X-API-KEY` header - this is what makes off-session embedding of the chat possible. CLI: `devplace messaging prune-tickets` removes expired tickets | | `/votes` | Upvote/downvote on posts, comments, projects | | `/reactions` | Emoji reactions on posts, comments, gists, projects | | `/bookmarks` | Save/unsave content; `/bookmarks/saved` personal list | @@ -229,6 +229,10 @@ The rack's **control terminal** supports every current game API route. Try `help The farm refreshes live over the pub/sub bus (a watered build appears on the owner's screen at once) and every plot countdown ticks client-side. Every endpoint also answers JSON, and Devii can play the game on the member's behalf via the `game_*` tools (`game_state`, `game_plant`, `game_harvest`, `game_plot_stop`, `game_buy_plot`, `game_upgrade_ci`, `game_fertilize`, `game_daily`, `game_claim_quest`, `game_water`, `game_steal`, `game_view_farm`, `game_leaderboard`, `game_upgrade_perk`, `game_upgrade_legacy`, `game_prestige`, `game_claim_grant`, `game_upgrade_mastery`, `game_buy_infrastructure`, `game_upgrade_defense`, `game_buy_cosmetic`, `game_equip_cosmetic`, `game_downgrade_defense`). See the API reference group **Code Farm** and the full player guide at `/docs/code-farm.html`. +## Content visibility + +Posts, gists, projects and news carry a status: **published** (default), **hidden**, **offline** or **draft**. Hidden items drop out of every list, search, topic page, profile and the sitemap but still open by direct link (marked `noindex`). Offline and draft items are visible only to their owner and administrators; everyone else gets a 404 and cannot comment, vote, react or bookmark them. The field is optional and backwards compatible: omitted on create it defaults to published, omitted on edit the current value is kept, and old rows read as published. Owners set it with the Visibility field in the create and edit forms (and the `visibility` parameter of the API and the Devii create and edit tools); administrators set a news article's status from the admin news page (`POST /admin/news/{uid}/visibility`, Devii `admin_set_news_visibility`). The specification is `devplacepy/database/visibility.py` (a NULL value reads as published, so no data migration), and the user-facing page is `/docs/content-visibility.html`. The website offers published, hidden and offline; draft is accepted by the API and reserved for a future drafting flow. + ## Engagement - **Emoji reactions** - react with **any** emoji on posts, comments, gists, and projects, separate from voting and carrying no ranking weight. A short quick-pick palette covers the common reactions, and a `+` button next to it opens the full searchable emoji picker (every standard emoji, including skin tones), so a reaction is never limited to a preset list. Emoji already used on an item are shown as counted chips beside the palette. @@ -256,6 +260,12 @@ DevPlace is open in the sense that it does not editorialise technical opinion. I - **Account deletion.** Self-service at `/profile/{username}/delete`, reauthenticated with the account password. Sessions and tokens are revoked, the profile is anonymised immediately, and all content is removed under one deletion event that stays restorable for `account_deletion_grace_hours` (default 24) before `devplace accounts prune` (and the Moderation housekeeping service) purges it permanently. The devRant `DELETE /api/users/me` routes into the same cascade. - **Legal pages**: [Terms of Service](/docs/terms.html), [Community Guidelines](/docs/community-guidelines.html), [Privacy Policy](/docs/privacy.html), [How moderation works](/docs/content-moderation.html), [Notice and takedown](/docs/intellectual-property.html) and [Contact](/docs/contact.html). All six are indexed in the sitemap and reachable from the docs index; the footer of every page links Terms, Privacy, Community Guidelines and Contact. Contact details come from the `contact_email`, `contact_phone` and `contact_address` settings, so the in-product page and any app-store trader declaration have one source of truth. +## GIF picker (GIPHY) + +A "GIF" button beside the attachment button in the post composer, every comment form and the message input opens a searchable GIPHY panel (trending first, "Powered by GIPHY" attribution). A picked GIF is downloaded by the server and stored as an ordinary attachment, so it renders in the attachment gallery, can be reported and deleted like any file, and counts against the normal attachment limits. + +An administrator enters the GIPHY API key under **Admin > Services > Giphy** (also the content rating, default `pg`, and the API base URL); the key is stored in `site_settings`, never reaches a browser, and the button stays hidden until it is set. Searches run on the server, are cached for five minutes, and each member may make 30 per minute. Routes: `GET /giphy/status`, `/giphy/search?q=`, `/giphy/trending`, `POST /giphy/attach` (documented under Uploads at `/docs/uploads.html`); Devii has `search_gifs`, `trending_gifs` and `attach_gif`. Details: `devplacepy/services/giphy/CLAUDE.md`. + ## Skills Members can install [Agent Skills](https://agentskills.io) from a URL at `/skills` so Devii can use them. A skill is a `SKILL.md` (YAML frontmatter plus markdown instructions) with optional text files under `scripts/`, `references/` and `assets/`. Skills are private to their owner, count against a per-user cap, and only the name and a one-line description of enabled skills are added to Devii's system prompt; Devii loads the full text on demand. A skill is untrusted third-party text: it can never grant Devii a tool, bypass a confirmation or run a script. Deleting a skill is a soft delete and needs confirmation when Devii does it. Documented in the API docs under *Skills* and in `devplacepy/services/skills/CLAUDE.md`. diff --git a/devplacepy/content.py b/devplacepy/content.py index e21f79f0..13073f9b 100644 --- a/devplacepy/content.py +++ b/devplacepy/content.py @@ -41,6 +41,13 @@ from devplacepy.database import ( db, get_user_recent_items, invalidate_user_recent_cache, + VISIBILITY_COLUMNS, + TARGET_TABLES, + item_visibility, + is_listed, + is_open, + listed_clause, + normalize_visibility, ) from devplacepy.utils import ( time_ago, @@ -67,7 +74,14 @@ from devplacepy.services.moderation.screening import ( screen_fields, ) -CREATE_METADATA_KEYS = ("project_type", "is_private", "language", "topic", "status") +CREATE_METADATA_KEYS = ( + "project_type", + "is_private", + "language", + "topic", + "status", + "visibility", +) BOOKMARKABLE_TYPES = {"post", "gist", "project", "news", "quiz"} REACTABLE_TYPES = {"post", "comment", "gist", "project", "quiz"} @@ -95,6 +109,26 @@ def is_owner(item: dict | None, user: dict | None) -> bool: return bool(item and user and item["user_uid"] == user["uid"]) +def can_view_item(table_name: str, item: dict | None, user: dict | None) -> bool: + if not item: + return False + if is_open(table_name, item): + return True + if not user: + return False + return bool(item.get("user_uid") == user.get("uid") or is_admin(user)) + + +def can_view_target(target_type: str, target_uid: str, user: dict | None) -> bool: + table_name = TARGET_TABLES.get(target_type) + if not table_name: + return True + item = resolve_by_slug(get_table(table_name), target_uid) + if not item: + return True + return can_view_item(table_name, item, user) + + def mature_hidden_by_default() -> bool: return get_int_setting("moderation_mature_default_hidden", 1) != 0 @@ -128,6 +162,8 @@ def _owner_is_admin(project: dict) -> bool: def can_view_project(project: dict | None, user: dict | None) -> bool: if not project: return False + if not can_view_item("projects", project, user): + return False if not project.get("is_private"): return True if is_owner(project, user): @@ -138,14 +174,17 @@ def can_view_project(project: dict | None, user: dict | None) -> bool: def get_user_sidebar_gists(user_uid: str, limit: int = 5) -> list[dict]: - return get_user_recent_items("gists", user_uid)[:limit] + rows = get_user_recent_items("gists", user_uid) + return [g for g in rows if is_listed("gists", g)][:limit] def get_user_sidebar_projects( user_uid: str, viewer: dict | None, limit: int = 5 ) -> list[dict]: rows = get_user_recent_items("projects", user_uid) - visible = [p for p in rows if can_view_project(p, viewer)] + visible = [ + p for p in rows if is_listed("projects", p) and can_view_project(p, viewer) + ] return visible[:limit] @@ -246,6 +285,13 @@ def first_image_url(item: dict, attachments: list | None) -> str | None: return None +def _with_visibility(table_name: str, fields: dict) -> dict: + column = VISIBILITY_COLUMNS.get(table_name) + if not column: + return fields + return {**fields, column: normalize_visibility(fields.get(column))} + + def create_content_item( table_name: str, target_type: str, @@ -258,6 +304,7 @@ def create_content_item( attachment_uids: list | None, request=None, ) -> tuple[str, str]: + fields = _with_visibility(table_name, fields) screening = screen_fields(table_name, fields) refuse_if_blocked(screening) uid = generate_uid() @@ -284,7 +331,10 @@ def create_content_item( award_rewards(user["uid"], xp, badge) if attachment_uids: link_attachments(attachment_uids, target_type, uid) - create_mention_notifications(mention_text, user["uid"], f"/{table_name}/{slug}") + if is_open(table_name, fields): + create_mention_notifications( + mention_text, user["uid"], f"/{table_name}/{slug}" + ) logger.info(f"{target_type} {uid} created by {user['username']}") label = fields.get("title") or slug links = [audit.target(target_type, uid, label)] @@ -667,6 +717,81 @@ def detail_context( return context +def apply_visibility( + request, + table_name: str, + target_type: str, + user: dict, + item: dict, + value: str, +) -> tuple[str, str]: + column = VISIBILITY_COLUMNS[table_name] + old_value = item_visibility(table_name, item) + new_value = normalize_visibility(value) + get_table(table_name).update({"uid": item["uid"], column: new_value}, ["uid"]) + owner_uid = item.get("user_uid") + if owner_uid: + invalidate_user_recent_cache(table_name, owner_uid) + if table_name == "posts": + clear_user_post_count(owner_uid) + if table_name == "projects": + from devplacepy.templating import clear_user_projects_cache + + clear_user_projects_cache(owner_uid) + label = item.get("title") or item["uid"] + logger.info( + f"{target_type} {item['uid']} visibility {old_value} -> {new_value} by {user['username']}" + ) + audit.record( + request, + f"{target_type}.visibility.set", + user=user, + target_type=target_type, + target_uid=item["uid"], + target_label=label, + old_value=old_value, + new_value=new_value, + summary=f"{user['username']} set {target_type} {label} to {new_value}", + links=[audit.target(target_type, item["uid"], label)], + ) + return old_value, new_value + + +def set_visibility_item( + request, + table_name: str, + target_type: str, + user: dict, + slug: str, + value: str, + redirect_fail: str, +): + from devplacepy.responses import action_result, wants_json, json_error + + item = resolve_by_slug(get_table(table_name), slug) + if not item or not is_admin(user): + audit.record( + request, + f"{target_type}.visibility.set", + user=user, + result="denied", + target_type=target_type, + target_uid=item["uid"] if item else slug, + target_label=item.get("title") if item else slug, + summary=f"{user['username']} denied changing visibility of {target_type} {slug}", + ) + if wants_json(request): + return json_error(403, "Not allowed") + return RedirectResponse(url=redirect_fail, status_code=302) + _, new_value = apply_visibility(request, table_name, target_type, user, item, value) + url = f"/{table_name}/{item.get('slug') or item['uid']}" + return action_result( + request, + url, + data={"uid": item["uid"], "visibility": new_value, "url": url}, + ) + + def edit_content_item( request, table_name: str, @@ -681,6 +806,7 @@ def edit_content_item( kind = target_type or table_name.rstrip("s") table = get_table(table_name) item = resolve_by_slug(table, slug) + visibility_value = update_fields.pop("visibility", None) if not is_owner(item, user): audit.record( request, @@ -703,6 +829,10 @@ def edit_content_item( } table.update({"uid": item["uid"], **update_fields}, ["uid"]) invalidate_user_recent_cache(table_name, item["user_uid"]) + if visibility_value is not None and normalize_visibility( + visibility_value + ) != item_visibility(table_name, item): + apply_visibility(request, table_name, kind, user, item, visibility_value) record_screening( screening, target_type=kind, @@ -817,6 +947,8 @@ def load_detail( item = resolve_by_slug(get_table(table_name), slug) if not item: return None + if not can_view_item(table_name, item, user): + return None if user and item["user_uid"] in get_blocked_uids(user["uid"]): return None author = get_users_by_uids([item["user_uid"]]).get(item["user_uid"]) @@ -898,7 +1030,10 @@ def enrich_items( def count_project_devlog(project_uid: str) -> int: - return get_table("posts").count(project_uid=project_uid, deleted_at=None) + posts = get_table("posts") + listed = listed_clause(posts) + clauses = [listed] if listed is not None else [] + return posts.count(*clauses, project_uid=project_uid, deleted_at=None) def get_project_devlog( diff --git a/devplacepy/database/CLAUDE.md b/devplacepy/database/CLAUDE.md index b34c7e57..03db3349 100644 --- a/devplacepy/database/CLAUDE.md +++ b/devplacepy/database/CLAUDE.md @@ -246,3 +246,22 @@ Operational settings - read sites and rules: **Booleans are `` for create and edit forms. Locals: `_visibility` (current value, default `published`), `_visibility_id` (unique element id). Options come from the `VISIBILITY_OPTIONS` global; `visibility_of(table, item)` and `visibility_label(value)` are the other globals. The admin news table reuses the generic `data-auto-submit` select pattern from `admin_users.html`. diff --git a/devplacepy/templates/_comment_form.html b/devplacepy/templates/_comment_form.html index ebf54a21..04530871 100644 --- a/devplacepy/templates/_comment_form.html +++ b/devplacepy/templates/_comment_form.html @@ -9,6 +9,7 @@ max-size="{{ max_upload_size_mb() }}" max-files="{{ max_attachments_per_resource() }}" allowed-types="{{ allowed_file_types() }}"> + diff --git a/devplacepy/templates/_post_composer_form.html b/devplacepy/templates/_post_composer_form.html index e4bb0338..08d6a209 100644 --- a/devplacepy/templates/_post_composer_form.html +++ b/devplacepy/templates/_post_composer_form.html @@ -25,7 +25,10 @@
- {% include "_attachment_form.html" %} +
+ {% include "_attachment_form.html" %} + +
@@ -54,6 +57,8 @@
+ {% set _visibility = "published" %}{% set _visibility_id = "composer-visibility" %}{% include "_visibility_field.html" %} + +
+ + +
+

Only needed for an upstream that rejects requests unless they look like they came from a specific client.

diff --git a/devplacepy/templates/admin_news.html b/devplacepy/templates/admin_news.html index 570eb776..9a445311 100644 --- a/devplacepy/templates/admin_news.html +++ b/devplacepy/templates/admin_news.html @@ -46,9 +46,15 @@ - - {% if item.article.get('status') == 'published' %}Published{% else %}Draft{% endif %} - +
+ +
diff --git a/devplacepy/templates/docs/component-dp-gif-picker.html b/devplacepy/templates/docs/component-dp-gif-picker.html new file mode 100644 index 00000000..98c50413 --- /dev/null +++ b/devplacepy/templates/docs/component-dp-gif-picker.html @@ -0,0 +1,47 @@ +
+# dp-gif-picker + +A "GIF" button that opens a searchable GIPHY panel. Picking a GIF stores it as an ordinary +attachment through `POST /giphy/attach` and hands it to the surrounding form's `dp-upload`, which +adds it to the hidden `attachment_uids` field. A GIF is therefore never pasted into the text: it +renders in the attachment gallery exactly like an uploaded file and is removed from the form the +same way. + +Source: `static/js/components/AppGifPicker.js`. Server: `routers/giphy.py`. + +## Availability + +The GIPHY API key is entered by an administrator in the **Giphy** service (`/admin/services`) and never reaches +the browser: every search runs on the server. The element asks `GET /giphy/status` once per page +and hides itself when no key is configured, so a site without GIPHY shows no button at all. + +## Attributes + +| Attribute | Default | Description | +|---|---|---| +| `endpoint` | `/giphy` | Base URL of the GIPHY routes. | +| `limit` | `24` | Number of GIFs requested per search (1-50). | + +## Behaviour + +- Trending GIFs load when the panel first opens; typing searches after a 350 ms pause. +- Escape or a click outside closes the panel; on phones (480px and below) it becomes a bottom sheet. +- Each member may make 30 GIF searches per minute; beyond that the panel says to wait. +- The panel always shows "Powered by GIPHY". + +## Events + +`dp-gif:attached` (bubbles, detail is the attachment record `{uid, url, original_filename, ...}`). +A `dp-upload` in `attachment` mode inside the same `
` listens for it. + +## Usage + +Place it beside the form's `dp-upload`: + +```html +<dp-upload multiple></dp-upload> +<dp-gif-picker></dp-gif-picker> +``` + +It is wired into the post composer, every comment form and the direct-message input. +
diff --git a/devplacepy/templates/docs/content-visibility.html b/devplacepy/templates/docs/content-visibility.html new file mode 100644 index 00000000..a923eae9 --- /dev/null +++ b/devplacepy/templates/docs/content-visibility.html @@ -0,0 +1,66 @@ +
+# Content visibility + +Every post, gist and project you publish has a **visibility status**. It lets you take something +down for a while, or keep it out of the public lists, without deleting it. News articles carry the +same status, set by administrators. + +## The four statuses + +| Status | Listed in feeds, searches, topics, profiles and the sitemap | Opens by direct link | Who can open it | +|---|---|---|---| +| **Published** (default) | Yes | Yes | Everyone | +| **Hidden** | No | Yes | Everyone who has the link (not indexed by search engines) | +| **Offline** | No | Owner and administrators only | You and administrators; everyone else gets a "not found" page | +| **Draft** | No | Owner and administrators only | You and administrators; reserved for a future drafting flow | + +Everything you create is **Published** unless you choose otherwise. **Draft** is part of the +specification and accepted by the API, but the website only offers Published, Hidden and Offline. + +## Changing it + +- **When you create** a post, gist or project, use the **Visibility** menu in the form. +- **When you edit** it, the same **Visibility** menu is in the edit form next to the other fields. + Leaving it out of an API edit keeps the current value. +- **Through Devii**, ask "set my post to offline"; it uses the `visibility` field of the create and + edit tools. +- **Through the API**, send `visibility` on create and edit, see + [Posts, Comments, Projects, Gists & News](/docs/content.html). +- **News** articles are set by administrators from the admin news page. + +Only the owner can change the visibility of their own content. Changing it back to **Published** +lists it again everywhere; nothing is lost while it was away. + +## Backwards compatibility + +The `visibility` field is optional everywhere, so existing API clients, scripts and bots keep +working without any change: + +- **Create** (`POST /posts/create`, `/gists/create`, `/projects/create`): leave it out and the item is + **published**. +- **Edit** (`POST /posts/edit/{slug}`, `/gists/edit/{slug}`, `/projects/edit/{slug}`): leave it out and + the current visibility is **kept**, so an old client editing the text of a hidden or offline item + never publishes it by accident. +- Items created before this feature have no stored value and read as **published**. +- Accepted values are `published`, `hidden`, `offline` and `draft` (case-insensitive); anything else is + rejected with a validation error. JSON bodies and form bodies behave the same. +- Responses now carry a `visibility` key on each post, gist and project; clients that ignore unknown + keys are unaffected. + +## What other people see + +- A hidden or offline item disappears from the feed, topics, listings, search, your profile (for + visitors), the sitemap, related and next-post links, and public counters. +- You still see all of your own items on your profile and your own listings. +- An offline or draft item cannot be commented on, voted on, reacted to, bookmarked or noted by + anyone but you and administrators, because they cannot open it. +- Mentions inside an item created as offline or draft do not notify anyone, and publishing it + later does not send them retroactively. +- Existing comments, votes and files stay attached and return with the item when you publish it. + +## Relation to private projects + +A project's **Private** switch is separate and stricter: it controls who may see the project and its +files at all. Visibility is checked first. A project that is both private and hidden stays +private; making it public does not publish it again. +
diff --git a/devplacepy/templates/docs/devii-admin.html b/devplacepy/templates/docs/devii-admin.html index 7a81bcec..8dc57a9c 100644 --- a/devplacepy/templates/docs/devii-admin.html +++ b/devplacepy/templates/docs/devii-admin.html @@ -139,7 +139,7 @@ Take the site back out of maintenance mode. ## News moderation -Curate the developer news feed: `admin_list_news`, `admin_toggle_news` (show or hide), `admin_publish_news`, `admin_landing_news` (feature on the landing page), and `admin_delete_news` (confirmation-gated). +Curate the developer news feed: `admin_list_news`, `admin_toggle_news` (show or hide), `admin_publish_news`, `admin_set_news_visibility` (published, hidden, offline or draft), `admin_landing_news` (feature on the landing page), and `admin_delete_news` (confirmation-gated). ``` Show me the latest news drafts and publish the two best ones, diff --git a/devplacepy/templates/docs/devii-tools.html b/devplacepy/templates/docs/devii-tools.html index 22d44a0b..a67e2a50 100644 --- a/devplacepy/templates/docs/devii-tools.html +++ b/devplacepy/templates/docs/devii-tools.html @@ -191,6 +191,7 @@ through them. The platform API enforces the rest: an **administrator** operating | `admin_list_news` | admin | List news for administration. | | `admin_toggle_news` | admin | Toggle a news article. | | `admin_publish_news` | admin | Publish a news article. | +| `admin_set_news_visibility` | admin | Set a news article to published, hidden, offline or draft. | | `admin_landing_news` | admin | Set a news article as landing content. | | `admin_delete_news` | admin | Delete a news article (soft delete, confirmation required). | | `admin_list_services` | admin | View managed services. | diff --git a/devplacepy/templates/docs/feed.html b/devplacepy/templates/docs/feed.html index ba9767fa..7c30fce9 100644 --- a/devplacepy/templates/docs/feed.html +++ b/devplacepy/templates/docs/feed.html @@ -28,6 +28,11 @@ The reordering applies to every view of the feed: the **All** and **Following** and the free-text search box in the left panel. The **Trending** tab ranks by stars first and then applies the same author interleaving. +## Hidden and offline posts + +Only **published** posts appear in the feed, the home page, topics, search and the author sidebars. +A post you set to hidden or offline leaves all of them; see [Content visibility](/docs/content-visibility.html). + ## Pagination Infinite scroll is unaffected. Because the interleave only rearranges the posts already chosen for a diff --git a/devplacepy/templates/docs/privacy.html b/devplacepy/templates/docs/privacy.html index 28e3cfdb..d9c5a851 100644 --- a/devplacepy/templates/docs/privacy.html +++ b/devplacepy/templates/docs/privacy.html @@ -34,6 +34,7 @@ never used to build an advertising profile. |---|---|---| | {{ ai_provider_name() }} | The text you asked an assistant feature to process | **Only after you grant `ai_third_party` consent.** Default: not granted. | | YouTube (Google) | Your browser contacts YouTube directly when a post, comment or message contains a YouTube or YouTube Music link, which DevPlace shows as an inline player. YouTube then sees your IP address and browser details, as for any embedded video. DevPlace itself sends YouTube nothing | Only when you view content that contains such a link | +| GIPHY | The search words you type in the GIF picker and the id of a GIF you pick, sent by DevPlace from the server. Your IP address and account are never sent to GIPHY by DevPlace; the GIF itself is downloaded by the server and stored as your attachment | Only when an administrator has configured GIPHY and you use the GIF picker | | Push services (Apple, browser push endpoints) | An encrypted notification payload to a device you registered | Only when you register a device and enable a push channel | | Telegram | Messages in the Telegram conversation you paired | Only when you pair Telegram yourself | | Software another member runs in a container | Your DevPlace API key and user id, injected as `DEVPLACE_API_KEY` and `DEVPLACE_USER_UID` | **Only after you grant `container_credentials` consent**, and only when the container is run by someone else. Your own containers use your own credentials with no separate consent. | diff --git a/devplacepy/templates/gist_detail.html b/devplacepy/templates/gist_detail.html index 26fd5002..cd2dbad0 100644 --- a/devplacepy/templates/gist_detail.html +++ b/devplacepy/templates/gist_detail.html @@ -102,6 +102,7 @@ {% endfor %} + {% set _visibility = visibility_of("gists", gist) %}{% set _visibility_id = "edit-gist-visibility" %}{% include "_visibility_field.html" %}
diff --git a/devplacepy/templates/gists.html b/devplacepy/templates/gists.html index 3e7e980b..12c151bb 100644 --- a/devplacepy/templates/gists.html +++ b/devplacepy/templates/gists.html @@ -127,6 +127,8 @@
+ {% set _visibility = "published" %}{% set _visibility_id = "gist-visibility" %}{% include "_visibility_field.html" %} + {% include "_attachment_form.html" %} {% endfor %} @@ -74,6 +76,7 @@ max-size="{{ max_upload_size_mb() }}" max-files="5" allowed-types="{{ allowed_file_types() }}"> + {% else %} diff --git a/devplacepy/templates/post.html b/devplacepy/templates/post.html index d6c53152..8b05e816 100644 --- a/devplacepy/templates/post.html +++ b/devplacepy/templates/post.html @@ -15,7 +15,7 @@ @@ -260,6 +261,8 @@ + {% set _visibility = visibility_of("projects", project) %}{% set _visibility_id = "edit-project-visibility" %}{% include "_visibility_field.html" %} +
@@ -339,11 +342,4 @@
{% endblock %} {% block extra_js %} - {% endblock %} diff --git a/devplacepy/templates/projects.html b/devplacepy/templates/projects.html index a67662c1..009d3b28 100644 --- a/devplacepy/templates/projects.html +++ b/devplacepy/templates/projects.html @@ -190,6 +190,8 @@
+ {% set _visibility = "published" %}{% set _visibility_id = "project-visibility" %}{% include "_visibility_field.html" %} + {% include "_attachment_form.html" %}