Files
OwnCord/Server/db/queries/sqlite/dm.sql
T
J3vb db0275a290 fix: batch of 29 correctness fixes across server and client (#1369)
* fix(ws): 2 defect(s) (OC-0013, OC-0140)

* fix(voice): 1 defect(s) (OC-0044)

* fix(ws): 1 defect(s) (OC-0024)

* fix(server): 1 defect(s) (OC-0027)

* fix(ws): 1 defect(s) (OC-0028)

* fix(server): 7 defect(s) (OC-0033, OC-0066, OC-0067, OC-0068, OC-0074, OC-0077, OC-0106)

* fix(ws): 1 defect(s) (OC-0051)

* fix(client): 1 defect(s) (OC-0053)

* fix(client): 1 defect(s) (OC-0055)

* fix(service): 1 defect(s) (OC-0069)

* fix(voice): 1 defect(s) (OC-0072)

* fix(service): 1 defect(s) (OC-0082)

* fix(client): 1 defect(s) (OC-0083)

* fix(plugin): 1 defect(s) (OC-0088)

* fix(plugin): 4 defect(s) (OC-0104, OC-0126, OC-0127, OC-0133)

* fix(admin): 1 defect(s) (OC-0110)

* fix(client): 1 defect(s) (OC-0114)

* fix(api): 1 defect(s) (OC-0139)

* fix(client): 1 defect(s) (OC-0149)

* test(server): adapt existing tests to updated OpenDM and IncrementMentionCounts signatures

* style(plugin): modernize loops and goroutine spawns in race test

* fix(ws): mirror the focus admission gate in the post-subscribe revalidation

* fix(service): detach DM post-commit side effects from the request ctx, fail delete closed, add empty-fan-out fallback

* fix(plugin): preserve enabled intent when upgrade reactivation hits a runtime-less build

* chore(skills): harden bughunt-fix workflow and fold review lessons into bughunt-run/db-change

* Add comprehensive documentation for task-observer skill

- Introduced environments.md to outline activation setup, compaction behavior, and handoff-doc mode.
- Created skill-authoring.md detailing taxonomy, licensing, confidentiality, and editing rules for skill creation.
- Added weekly-review.md for a structured review process of OPEN observations, including scheduled and in-session fallback modes.

* chore(go): pin toolchain go1.26.6 (stdlib CVE fixes flagged by govulncheck)
2026-08-14 10:05:40 +02:00

81 lines
3.3 KiB
SQL

-- name: OpenDM :execrows
INSERT OR IGNORE INTO dm_open_state (user_id, channel_id) VALUES (?, ?);
-- name: CloseDM :exec
DELETE FROM dm_open_state WHERE user_id = ? AND channel_id = ?;
-- name: IsDMParticipant :one
SELECT user_id FROM dm_participants WHERE user_id = ? AND channel_id = ?;
-- name: GetDMParticipantIDs :many
SELECT user_id FROM dm_participants WHERE channel_id = ?;
-- name: CountDMParticipants :one
SELECT COUNT(*) FROM dm_participants WHERE channel_id = ?;
-- name: IsGroupDM :one
SELECT is_group FROM channels WHERE id = ? AND type = 'dm';
-- name: RemoveDMParticipant :exec
DELETE FROM dm_participants WHERE channel_id = ? AND user_id = ?;
-- name: SetDMChannelName :exec
UPDATE channels SET name = ? WHERE id = ? AND type = 'dm';
-- name: GetDMParticipants :many
SELECT
u.id AS id,
u.username AS username,
COALESCE(u.display_name, '') AS display_name,
COALESCE(u.avatar, '') AS avatar,
u.status AS status
FROM dm_participants dp
JOIN users u ON u.id = dp.user_id
WHERE dp.channel_id = ?
ORDER BY u.id ASC;
-- name: GetUserDMChannelIDs :many
SELECT channel_id FROM dm_open_state WHERE user_id = ?;
-- A DM row carries no recipient any more: dm_participants holds N users, so
-- "the other one" is only well defined for a two-person DM. The participant
-- set comes from GetDMParticipantsForUser below, one extra query for the whole
-- list rather than one per channel, and the Go layer stitches them together.
-- name: GetUserDMChannels :many
SELECT
c.id AS channel_id,
c.name AS name,
c.is_group AS is_group,
lm.id AS last_message_id,
COALESCE(lm.content, '') AS last_message,
COALESCE(lm.timestamp, '') AS last_message_at,
(SELECT COUNT(*) FROM messages mu
WHERE mu.channel_id = c.id AND mu.deleted = 0
AND mu.id > COALESCE((SELECT rs.last_message_id FROM read_states rs
WHERE rs.channel_id = c.id AND rs.user_id = dos.user_id), 0)
) AS unread_count
FROM dm_open_state dos
JOIN channels c ON c.id = dos.channel_id AND c.type = 'dm'
LEFT JOIN messages lm ON lm.id = (
SELECT MAX(id) FROM messages WHERE channel_id = c.id AND deleted = 0
)
WHERE dos.user_id = ?
ORDER BY COALESCE(lm.timestamp, dos.opened_at) DESC;
-- Every participant of every DM the user has open, in one pass. Includes the
-- user themselves so a caller can tell "group of three" from "group of three
-- others"; the Go layer filters when it needs the others.
-- name: GetDMParticipantsForUser :many
SELECT
dp.channel_id AS channel_id,
u.id AS id,
u.username AS username,
COALESCE(u.display_name, '') AS display_name,
COALESCE(u.avatar, '') AS avatar,
u.status AS status
FROM dm_open_state dos
JOIN dm_participants dp ON dp.channel_id = dos.channel_id
JOIN users u ON u.id = dp.user_id
WHERE dos.user_id = ?
ORDER BY dp.channel_id ASC, u.id ASC;