mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
- Generate Server/db/dbgen/{events,plugins}.sql.go and full Server/db/pgdbgen/ (//go:build postgres gated)
- Implement PostgresStore EventStore and PluginStore methods in store/postgres.go
- Wire plugin host_events.go EventSink into hub broadcast path (SetPluginEventSink)
- Wire host_commands.go slash-command dispatcher: chat_command V1 handler + hub.SetPluginRegistry
- Add handlers_command.go + handlers_command_test.go for plugin slash-command dispatch
- Add reconnect_db_test.go: TestReconnect_BufferMiss_FallsBackToDBTier (cold-tier DB replay)
- Add otel-up/otel-down Makefile targets; docker-compose.otel.yml + prometheus.dev.yml
- Update PHASE_BC_LOCAL_TODO.md: mark in-session items complete; document remaining network-blocked steps
- Minor fixes: channel_handler access-control, router plugin handler wiring, service span instrumentation
480 lines
14 KiB
Go
480 lines
14 KiB
Go
//go:build postgres
|
|
|
|
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: messages.sql
|
|
|
|
package pgdbgen
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const createMessage = `-- name: CreateMessage :one
|
|
|
|
INSERT INTO messages (channel_id, user_id, content, reply_to)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING id
|
|
`
|
|
|
|
type CreateMessageParams struct {
|
|
ChannelID int64 `json:"channelId"`
|
|
UserID int64 `json:"userId"`
|
|
Content string `json:"content"`
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
}
|
|
|
|
// PostgreSQL variants of the sqlite messages queries.
|
|
// `deleted = 0/1` and `pinned = 0/1` become FALSE/TRUE (columns are BOOLEAN).
|
|
// The FTS search queries are NOT included here: on postgres, messages.fts is
|
|
// a tsvector column with a GIN index (see migrations/postgres/001_initial_schema.sql)
|
|
// and FTS queries are hand-written in the postgres-specific store dispatch,
|
|
// mirroring how sqlite's FTS5 queries live in message_queries.go.
|
|
func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (int64, error) {
|
|
row := q.db.QueryRow(ctx, createMessage,
|
|
arg.ChannelID,
|
|
arg.UserID,
|
|
arg.Content,
|
|
arg.ReplyTo,
|
|
)
|
|
var id int64
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const editMessageContent = `-- name: EditMessageContent :exec
|
|
UPDATE messages SET content = $1, edited_at = NOW() WHERE id = $2
|
|
`
|
|
|
|
type EditMessageContentParams struct {
|
|
Content string `json:"content"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) EditMessageContent(ctx context.Context, arg EditMessageContentParams) error {
|
|
_, err := q.db.Exec(ctx, editMessageContent, arg.Content, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const getChannelUnreadCounts = `-- name: GetChannelUnreadCounts :many
|
|
SELECT c.id,
|
|
COALESCE(MAX(m.id), 0)::BIGINT AS last_msg_id,
|
|
COUNT(CASE WHEN m.id > COALESCE(rs.last_message_id, 0) AND m.deleted = FALSE THEN 1 END) AS unread
|
|
FROM channels c
|
|
LEFT JOIN messages m ON m.channel_id = c.id AND m.deleted = FALSE
|
|
LEFT JOIN read_states rs ON rs.channel_id = c.id AND rs.user_id = $1
|
|
WHERE c.type = 'text'
|
|
GROUP BY c.id
|
|
`
|
|
|
|
type GetChannelUnreadCountsRow struct {
|
|
ID int64 `json:"id"`
|
|
LastMsgID int64 `json:"lastMsgId"`
|
|
Unread int64 `json:"unread"`
|
|
}
|
|
|
|
func (q *Queries) GetChannelUnreadCounts(ctx context.Context, userID int64) ([]GetChannelUnreadCountsRow, error) {
|
|
rows, err := q.db.Query(ctx, getChannelUnreadCounts, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetChannelUnreadCountsRow{}
|
|
for rows.Next() {
|
|
var i GetChannelUnreadCountsRow
|
|
if err := rows.Scan(&i.ID, &i.LastMsgID, &i.Unread); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getLatestMessageID = `-- name: GetLatestMessageID :one
|
|
SELECT COALESCE(MAX(id), 0)::BIGINT FROM messages WHERE channel_id = $1 AND deleted = FALSE
|
|
`
|
|
|
|
func (q *Queries) GetLatestMessageID(ctx context.Context, channelID int64) (int64, error) {
|
|
row := q.db.QueryRow(ctx, getLatestMessageID, channelID)
|
|
var column_1 int64
|
|
err := row.Scan(&column_1)
|
|
return column_1, err
|
|
}
|
|
|
|
const getMessage = `-- name: GetMessage :one
|
|
SELECT id, channel_id, user_id, content, reply_to, edited_at, deleted, pinned, timestamp
|
|
FROM messages WHERE id = $1
|
|
`
|
|
|
|
type GetMessageRow struct {
|
|
ID int64 `json:"id"`
|
|
ChannelID int64 `json:"channelId"`
|
|
UserID int64 `json:"userId"`
|
|
Content string `json:"content"`
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
EditedAt pgtype.Timestamptz `json:"editedAt"`
|
|
Deleted bool `json:"deleted"`
|
|
Pinned bool `json:"pinned"`
|
|
Timestamp pgtype.Timestamptz `json:"timestamp"`
|
|
}
|
|
|
|
func (q *Queries) GetMessage(ctx context.Context, id int64) (GetMessageRow, error) {
|
|
row := q.db.QueryRow(ctx, getMessage, id)
|
|
var i GetMessageRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ChannelID,
|
|
&i.UserID,
|
|
&i.Content,
|
|
&i.ReplyTo,
|
|
&i.EditedAt,
|
|
&i.Deleted,
|
|
&i.Pinned,
|
|
&i.Timestamp,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getMessagesByChannel = `-- name: GetMessagesByChannel :many
|
|
SELECT m.id, m.channel_id, m.user_id, m.content, m.reply_to,
|
|
m.edited_at, m.deleted, m.pinned, m.timestamp,
|
|
u.username, u.avatar
|
|
FROM messages m JOIN users u ON m.user_id = u.id
|
|
WHERE m.channel_id = $1 AND m.deleted = FALSE
|
|
ORDER BY m.id DESC LIMIT $2
|
|
`
|
|
|
|
type GetMessagesByChannelParams struct {
|
|
ChannelID int64 `json:"channelId"`
|
|
Limit int32 `json:"limit"`
|
|
}
|
|
|
|
type GetMessagesByChannelRow struct {
|
|
ID int64 `json:"id"`
|
|
ChannelID int64 `json:"channelId"`
|
|
UserID int64 `json:"userId"`
|
|
Content string `json:"content"`
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
EditedAt pgtype.Timestamptz `json:"editedAt"`
|
|
Deleted bool `json:"deleted"`
|
|
Pinned bool `json:"pinned"`
|
|
Timestamp pgtype.Timestamptz `json:"timestamp"`
|
|
Username string `json:"username"`
|
|
Avatar *string `json:"avatar"`
|
|
}
|
|
|
|
func (q *Queries) GetMessagesByChannel(ctx context.Context, arg GetMessagesByChannelParams) ([]GetMessagesByChannelRow, error) {
|
|
rows, err := q.db.Query(ctx, getMessagesByChannel, arg.ChannelID, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetMessagesByChannelRow{}
|
|
for rows.Next() {
|
|
var i GetMessagesByChannelRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ChannelID,
|
|
&i.UserID,
|
|
&i.Content,
|
|
&i.ReplyTo,
|
|
&i.EditedAt,
|
|
&i.Deleted,
|
|
&i.Pinned,
|
|
&i.Timestamp,
|
|
&i.Username,
|
|
&i.Avatar,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getMessagesByChannelBeforeCursor = `-- name: GetMessagesByChannelBeforeCursor :many
|
|
SELECT m.id, m.channel_id, m.user_id, m.content, m.reply_to,
|
|
m.edited_at, m.deleted, m.pinned, m.timestamp,
|
|
u.username, u.avatar
|
|
FROM messages m JOIN users u ON m.user_id = u.id
|
|
WHERE m.channel_id = $1 AND m.id < $2 AND m.deleted = FALSE
|
|
ORDER BY m.id DESC LIMIT $3
|
|
`
|
|
|
|
type GetMessagesByChannelBeforeCursorParams struct {
|
|
ChannelID int64 `json:"channelId"`
|
|
ID int64 `json:"id"`
|
|
Limit int32 `json:"limit"`
|
|
}
|
|
|
|
type GetMessagesByChannelBeforeCursorRow struct {
|
|
ID int64 `json:"id"`
|
|
ChannelID int64 `json:"channelId"`
|
|
UserID int64 `json:"userId"`
|
|
Content string `json:"content"`
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
EditedAt pgtype.Timestamptz `json:"editedAt"`
|
|
Deleted bool `json:"deleted"`
|
|
Pinned bool `json:"pinned"`
|
|
Timestamp pgtype.Timestamptz `json:"timestamp"`
|
|
Username string `json:"username"`
|
|
Avatar *string `json:"avatar"`
|
|
}
|
|
|
|
func (q *Queries) GetMessagesByChannelBeforeCursor(ctx context.Context, arg GetMessagesByChannelBeforeCursorParams) ([]GetMessagesByChannelBeforeCursorRow, error) {
|
|
rows, err := q.db.Query(ctx, getMessagesByChannelBeforeCursor, arg.ChannelID, arg.ID, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetMessagesByChannelBeforeCursorRow{}
|
|
for rows.Next() {
|
|
var i GetMessagesByChannelBeforeCursorRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ChannelID,
|
|
&i.UserID,
|
|
&i.Content,
|
|
&i.ReplyTo,
|
|
&i.EditedAt,
|
|
&i.Deleted,
|
|
&i.Pinned,
|
|
&i.Timestamp,
|
|
&i.Username,
|
|
&i.Avatar,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
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 = $1 AND m.deleted = FALSE
|
|
ORDER BY m.id DESC LIMIT $2
|
|
`
|
|
|
|
type GetMessagesForAPIParams struct {
|
|
ChannelID int64 `json:"channelId"`
|
|
Limit int32 `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 pgtype.Timestamptz `json:"editedAt"`
|
|
Deleted bool `json:"deleted"`
|
|
Pinned bool `json:"pinned"`
|
|
Timestamp pgtype.Timestamptz `json:"timestamp"`
|
|
}
|
|
|
|
func (q *Queries) GetMessagesForAPI(ctx context.Context, arg GetMessagesForAPIParams) ([]GetMessagesForAPIRow, error) {
|
|
rows, err := q.db.Query(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.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getMessagesForAPIBeforeCursor = `-- name: GetMessagesForAPIBeforeCursor :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 = $1 AND m.id < $2 AND m.deleted = FALSE
|
|
ORDER BY m.id DESC LIMIT $3
|
|
`
|
|
|
|
type GetMessagesForAPIBeforeCursorParams struct {
|
|
ChannelID int64 `json:"channelId"`
|
|
ID int64 `json:"id"`
|
|
Limit int32 `json:"limit"`
|
|
}
|
|
|
|
type GetMessagesForAPIBeforeCursorRow 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 pgtype.Timestamptz `json:"editedAt"`
|
|
Deleted bool `json:"deleted"`
|
|
Pinned bool `json:"pinned"`
|
|
Timestamp pgtype.Timestamptz `json:"timestamp"`
|
|
}
|
|
|
|
func (q *Queries) GetMessagesForAPIBeforeCursor(ctx context.Context, arg GetMessagesForAPIBeforeCursorParams) ([]GetMessagesForAPIBeforeCursorRow, error) {
|
|
rows, err := q.db.Query(ctx, getMessagesForAPIBeforeCursor, arg.ChannelID, arg.ID, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetMessagesForAPIBeforeCursorRow{}
|
|
for rows.Next() {
|
|
var i GetMessagesForAPIBeforeCursorRow
|
|
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.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getPinnedMessageRows = `-- name: GetPinnedMessageRows :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 = $1 AND m.pinned = TRUE AND m.deleted = FALSE
|
|
ORDER BY m.id DESC
|
|
`
|
|
|
|
type GetPinnedMessageRowsRow 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 pgtype.Timestamptz `json:"editedAt"`
|
|
Deleted bool `json:"deleted"`
|
|
Pinned bool `json:"pinned"`
|
|
Timestamp pgtype.Timestamptz `json:"timestamp"`
|
|
}
|
|
|
|
func (q *Queries) GetPinnedMessageRows(ctx context.Context, channelID int64) ([]GetPinnedMessageRowsRow, error) {
|
|
rows, err := q.db.Query(ctx, getPinnedMessageRows, channelID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetPinnedMessageRowsRow{}
|
|
for rows.Next() {
|
|
var i GetPinnedMessageRowsRow
|
|
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.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const setMessagePinned = `-- name: SetMessagePinned :execrows
|
|
UPDATE messages SET pinned = $1 WHERE id = $2 AND deleted = FALSE
|
|
`
|
|
|
|
type SetMessagePinnedParams struct {
|
|
Pinned bool `json:"pinned"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) SetMessagePinned(ctx context.Context, arg SetMessagePinnedParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, setMessagePinned, arg.Pinned, arg.ID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const softDeleteMessage = `-- name: SoftDeleteMessage :exec
|
|
UPDATE messages SET deleted = TRUE WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) SoftDeleteMessage(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, softDeleteMessage, id)
|
|
return err
|
|
}
|
|
|
|
const updateReadState = `-- name: UpdateReadState :exec
|
|
INSERT INTO read_states (user_id, channel_id, last_message_id)
|
|
VALUES ($1, $2, $3)
|
|
ON CONFLICT (user_id, channel_id) DO UPDATE SET last_message_id = EXCLUDED.last_message_id
|
|
`
|
|
|
|
type UpdateReadStateParams struct {
|
|
UserID int64 `json:"userId"`
|
|
ChannelID int64 `json:"channelId"`
|
|
LastMessageID int64 `json:"lastMessageId"`
|
|
}
|
|
|
|
func (q *Queries) UpdateReadState(ctx context.Context, arg UpdateReadStateParams) error {
|
|
_, err := q.db.Exec(ctx, updateReadState, arg.UserID, arg.ChannelID, arg.LastMessageID)
|
|
return err
|
|
}
|