2026-04-06 09:12:59 +02:00
|
|
|
// Code generated by sqlc. DO NOT EDIT.
|
|
|
|
|
// versions:
|
|
|
|
|
// sqlc v1.30.0
|
|
|
|
|
// source: messages.sql
|
|
|
|
|
|
|
|
|
|
package dbgen
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-31 15:41:57 +02:00
|
|
|
const createMessage = `-- name: CreateMessage :one
|
2026-04-06 09:12:59 +02:00
|
|
|
INSERT INTO messages (channel_id, user_id, content, reply_to) VALUES (?, ?, ?, ?)
|
2026-08-01 22:06:14 +02:00
|
|
|
RETURNING id, channel_id, user_id, content, reply_to, edited_at, deleted, pinned, timestamp,
|
|
|
|
|
mentions_everyone
|
2026-04-06 09:12:59 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type CreateMessageParams struct {
|
|
|
|
|
ChannelID int64 `json:"channelId"`
|
|
|
|
|
UserID int64 `json:"userId"`
|
|
|
|
|
Content string `json:"content"`
|
|
|
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-31 15:41:57 +02:00
|
|
|
func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error) {
|
|
|
|
|
row := q.db.QueryRowContext(ctx, createMessage,
|
2026-04-06 09:12:59 +02:00
|
|
|
arg.ChannelID,
|
|
|
|
|
arg.UserID,
|
|
|
|
|
arg.Content,
|
|
|
|
|
arg.ReplyTo,
|
|
|
|
|
)
|
2026-07-31 15:41:57 +02:00
|
|
|
var i Message
|
|
|
|
|
err := row.Scan(
|
|
|
|
|
&i.ID,
|
|
|
|
|
&i.ChannelID,
|
|
|
|
|
&i.UserID,
|
|
|
|
|
&i.Content,
|
|
|
|
|
&i.ReplyTo,
|
|
|
|
|
&i.EditedAt,
|
|
|
|
|
&i.Deleted,
|
|
|
|
|
&i.Pinned,
|
|
|
|
|
&i.Timestamp,
|
2026-08-01 22:06:14 +02:00
|
|
|
&i.MentionsEveryone,
|
2026-07-31 15:41:57 +02:00
|
|
|
)
|
|
|
|
|
return i, err
|
2026-04-06 09:12:59 +02:00
|
|
|
}
|
|
|
|
|
|
2026-07-31 15:41:57 +02:00
|
|
|
const editMessageContent = `-- name: EditMessageContent :one
|
2026-04-06 09:12:59 +02:00
|
|
|
UPDATE messages SET content = ?, edited_at = datetime('now') WHERE id = ?
|
2026-08-01 22:06:14 +02:00
|
|
|
RETURNING id, channel_id, user_id, content, reply_to, edited_at, deleted, pinned, timestamp,
|
|
|
|
|
mentions_everyone
|
2026-04-06 09:12:59 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type EditMessageContentParams struct {
|
|
|
|
|
Content string `json:"content"`
|
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-31 15:41:57 +02:00
|
|
|
func (q *Queries) EditMessageContent(ctx context.Context, arg EditMessageContentParams) (Message, error) {
|
|
|
|
|
row := q.db.QueryRowContext(ctx, editMessageContent, arg.Content, arg.ID)
|
|
|
|
|
var i Message
|
|
|
|
|
err := row.Scan(
|
|
|
|
|
&i.ID,
|
|
|
|
|
&i.ChannelID,
|
|
|
|
|
&i.UserID,
|
|
|
|
|
&i.Content,
|
|
|
|
|
&i.ReplyTo,
|
|
|
|
|
&i.EditedAt,
|
|
|
|
|
&i.Deleted,
|
|
|
|
|
&i.Pinned,
|
|
|
|
|
&i.Timestamp,
|
2026-08-01 22:06:14 +02:00
|
|
|
&i.MentionsEveryone,
|
2026-07-31 15:41:57 +02:00
|
|
|
)
|
|
|
|
|
return i, err
|
2026-04-06 09:12:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getChannelUnreadCounts = `-- name: GetChannelUnreadCounts :many
|
|
|
|
|
SELECT c.id,
|
2026-07-31 15:41:57 +02:00
|
|
|
(SELECT COALESCE(MAX(m.id), 0) FROM messages m
|
|
|
|
|
WHERE m.channel_id = c.id AND m.deleted = 0) AS last_msg_id,
|
|
|
|
|
(SELECT COUNT(*) FROM messages m
|
|
|
|
|
WHERE m.channel_id = c.id AND m.deleted = 0
|
|
|
|
|
AND m.id > COALESCE((SELECT rs.last_message_id FROM read_states rs
|
2026-08-01 22:06:14 +02:00
|
|
|
WHERE rs.channel_id = c.id AND rs.user_id = ?), 0)) AS unread,
|
|
|
|
|
COALESCE((SELECT rs.mention_count FROM read_states rs
|
|
|
|
|
WHERE rs.channel_id = c.id AND rs.user_id = ?), 0) AS mentions
|
2026-04-06 09:12:59 +02:00
|
|
|
FROM channels c
|
2026-07-31 15:41:57 +02:00
|
|
|
WHERE c.type IN ('text', 'announcement')
|
2026-08-01 22:06:14 +02:00
|
|
|
OR (c.type = 'dm' AND EXISTS (SELECT 1 FROM dm_participants dp
|
|
|
|
|
WHERE dp.channel_id = c.id AND dp.user_id = ?))
|
2026-04-06 09:12:59 +02:00
|
|
|
`
|
|
|
|
|
|
2026-08-01 22:06:14 +02:00
|
|
|
type GetChannelUnreadCountsParams struct {
|
|
|
|
|
UserID int64 `json:"userId"`
|
|
|
|
|
UserID_2 int64 `json:"userId2"`
|
|
|
|
|
UserID_3 int64 `json:"userId3"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 09:12:59 +02:00
|
|
|
type GetChannelUnreadCountsRow struct {
|
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
LastMsgID interface{} `json:"lastMsgId"`
|
|
|
|
|
Unread int64 `json:"unread"`
|
2026-08-01 22:06:14 +02:00
|
|
|
Mentions interface{} `json:"mentions"`
|
2026-04-06 09:12:59 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-01 22:06:14 +02:00
|
|
|
func (q *Queries) GetChannelUnreadCounts(ctx context.Context, arg GetChannelUnreadCountsParams) ([]GetChannelUnreadCountsRow, error) {
|
|
|
|
|
rows, err := q.db.QueryContext(ctx, getChannelUnreadCounts, arg.UserID, arg.UserID_2, arg.UserID_3)
|
2026-04-06 09:12:59 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
items := []GetChannelUnreadCountsRow{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var i GetChannelUnreadCountsRow
|
2026-08-01 22:06:14 +02:00
|
|
|
if err := rows.Scan(
|
|
|
|
|
&i.ID,
|
|
|
|
|
&i.LastMsgID,
|
|
|
|
|
&i.Unread,
|
|
|
|
|
&i.Mentions,
|
|
|
|
|
); err != nil {
|
2026-04-06 09:12:59 +02:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
items = append(items, i)
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return items, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getLatestMessageID = `-- name: GetLatestMessageID :one
|
|
|
|
|
SELECT COALESCE(MAX(id), 0) FROM messages WHERE channel_id = ? AND deleted = 0
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) GetLatestMessageID(ctx context.Context, channelID int64) (interface{}, error) {
|
|
|
|
|
row := q.db.QueryRowContext(ctx, getLatestMessageID, channelID)
|
|
|
|
|
var coalesce interface{}
|
|
|
|
|
err := row.Scan(&coalesce)
|
|
|
|
|
return coalesce, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getMessage = `-- name: GetMessage :one
|
2026-08-01 22:06:14 +02:00
|
|
|
SELECT id, channel_id, user_id, content, reply_to, edited_at, deleted, pinned, timestamp,
|
|
|
|
|
mentions_everyone
|
2026-04-06 09:12:59 +02:00
|
|
|
FROM messages WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) GetMessage(ctx context.Context, id int64) (Message, error) {
|
|
|
|
|
row := q.db.QueryRowContext(ctx, getMessage, id)
|
|
|
|
|
var i Message
|
|
|
|
|
err := row.Scan(
|
|
|
|
|
&i.ID,
|
|
|
|
|
&i.ChannelID,
|
|
|
|
|
&i.UserID,
|
|
|
|
|
&i.Content,
|
|
|
|
|
&i.ReplyTo,
|
|
|
|
|
&i.EditedAt,
|
|
|
|
|
&i.Deleted,
|
|
|
|
|
&i.Pinned,
|
|
|
|
|
&i.Timestamp,
|
2026-08-01 22:06:14 +02:00
|
|
|
&i.MentionsEveryone,
|
2026-04-06 09:12:59 +02:00
|
|
|
)
|
|
|
|
|
return i, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getMessagesForAPI = `-- name: GetMessagesForAPI :many
|
|
|
|
|
SELECT m.id, m.channel_id, m.user_id, u.username, u.avatar,
|
|
|
|
|
m.content, m.reply_to, m.edited_at, m.deleted, m.pinned, m.timestamp
|
|
|
|
|
FROM messages m JOIN users u ON m.user_id = u.id
|
|
|
|
|
WHERE m.channel_id = ? AND m.deleted = 0
|
|
|
|
|
ORDER BY m.id DESC LIMIT ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type GetMessagesForAPIParams struct {
|
|
|
|
|
ChannelID int64 `json:"channelId"`
|
|
|
|
|
Limit int64 `json:"limit"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GetMessagesForAPIRow struct {
|
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
ChannelID int64 `json:"channelId"`
|
|
|
|
|
UserID int64 `json:"userId"`
|
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
Avatar *string `json:"avatar"`
|
|
|
|
|
Content string `json:"content"`
|
|
|
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
|
|
|
EditedAt *string `json:"editedAt"`
|
|
|
|
|
Deleted int64 `json:"deleted"`
|
|
|
|
|
Pinned int64 `json:"pinned"`
|
|
|
|
|
Timestamp string `json:"timestamp"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) GetMessagesForAPI(ctx context.Context, arg GetMessagesForAPIParams) ([]GetMessagesForAPIRow, error) {
|
|
|
|
|
rows, err := q.db.QueryContext(ctx, getMessagesForAPI, arg.ChannelID, arg.Limit)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
items := []GetMessagesForAPIRow{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var i GetMessagesForAPIRow
|
|
|
|
|
if err := rows.Scan(
|
|
|
|
|
&i.ID,
|
|
|
|
|
&i.ChannelID,
|
|
|
|
|
&i.UserID,
|
|
|
|
|
&i.Username,
|
|
|
|
|
&i.Avatar,
|
|
|
|
|
&i.Content,
|
|
|
|
|
&i.ReplyTo,
|
|
|
|
|
&i.EditedAt,
|
|
|
|
|
&i.Deleted,
|
|
|
|
|
&i.Pinned,
|
|
|
|
|
&i.Timestamp,
|
|
|
|
|
); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
items = append(items, i)
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return items, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 20:50:47 +02:00
|
|
|
const getReadState = `-- name: GetReadState :one
|
|
|
|
|
SELECT last_message_id, mention_count FROM read_states
|
|
|
|
|
WHERE user_id = ? AND channel_id = ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type GetReadStateParams struct {
|
|
|
|
|
UserID int64 `json:"userId"`
|
|
|
|
|
ChannelID int64 `json:"channelId"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GetReadStateRow struct {
|
|
|
|
|
LastMessageID int64 `json:"lastMessageId"`
|
|
|
|
|
MentionCount int64 `json:"mentionCount"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reader-pool lookup that lets the channel-focus path skip the UpdateReadState
|
|
|
|
|
// UPSERT when the row is already correct, keeping no-op focus events off the
|
|
|
|
|
// single writer connection.
|
|
|
|
|
func (q *Queries) GetReadState(ctx context.Context, arg GetReadStateParams) (GetReadStateRow, error) {
|
|
|
|
|
row := q.db.QueryRowContext(ctx, getReadState, arg.UserID, arg.ChannelID)
|
|
|
|
|
var i GetReadStateRow
|
|
|
|
|
err := row.Scan(&i.LastMessageID, &i.MentionCount)
|
|
|
|
|
return i, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 09:12:59 +02:00
|
|
|
const setMessagePinned = `-- name: SetMessagePinned :execresult
|
|
|
|
|
UPDATE messages SET pinned = ? WHERE id = ? AND deleted = 0
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type SetMessagePinnedParams struct {
|
|
|
|
|
Pinned int64 `json:"pinned"`
|
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SetMessagePinned(ctx context.Context, arg SetMessagePinnedParams) (sql.Result, error) {
|
|
|
|
|
return q.db.ExecContext(ctx, setMessagePinned, arg.Pinned, arg.ID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const softDeleteMessage = `-- name: SoftDeleteMessage :exec
|
|
|
|
|
UPDATE messages SET deleted = 1 WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SoftDeleteMessage(ctx context.Context, id int64) error {
|
|
|
|
|
_, err := q.db.ExecContext(ctx, softDeleteMessage, id)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const updateReadState = `-- name: UpdateReadState :exec
|
2026-08-01 22:06:14 +02:00
|
|
|
INSERT INTO read_states (user_id, channel_id, last_message_id, mention_count)
|
|
|
|
|
VALUES (?, ?, ?, 0)
|
|
|
|
|
ON CONFLICT(user_id, channel_id) DO UPDATE SET
|
|
|
|
|
last_message_id = excluded.last_message_id,
|
|
|
|
|
mention_count = 0
|
2026-04-06 09:12:59 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type UpdateReadStateParams struct {
|
|
|
|
|
UserID int64 `json:"userId"`
|
|
|
|
|
ChannelID int64 `json:"channelId"`
|
|
|
|
|
LastMessageID int64 `json:"lastMessageId"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-01 22:06:14 +02:00
|
|
|
// Marking a channel read also clears its mention badge: channel_focus is the
|
|
|
|
|
// only caller, and a focused channel has no outstanding mentions by definition.
|
2026-04-06 09:12:59 +02:00
|
|
|
func (q *Queries) UpdateReadState(ctx context.Context, arg UpdateReadStateParams) error {
|
|
|
|
|
_, err := q.db.ExecContext(ctx, updateReadState, arg.UserID, arg.ChannelID, arg.LastMessageID)
|
|
|
|
|
return err
|
|
|
|
|
}
|