Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

12.5 KB
Raw Blame
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: thread_room.sql

package store

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const getMessageInRoom = `-- name: GetMessageInRoom :one
SELECT id, tenant_id, room_id, author_actor_id, body, segments, message_kind,
       created_at
FROM messages
WHERE id = $1::text AND room_id = $2::text
`

type GetMessageInRoomParams struct {
	ID     string `json:"id"`
	RoomID string `json:"room_id"`
}

type GetMessageInRoomRow struct {
	ID            string             `json:"id"`
	TenantID      string             `json:"tenant_id"`
	RoomID        string             `json:"room_id"`
	AuthorActorID string             `json:"author_actor_id"`
	Body          string             `json:"body"`
	Segments      []byte             `json:"segments"`
	MessageKind   string             `json:"message_kind"`
	CreatedAt     pgtype.Timestamptz `json:"created_at"`
}

func (q *Queries) GetMessageInRoom(ctx context.Context, arg GetMessageInRoomParams) (GetMessageInRoomRow, error) {
	row := q.db.QueryRow(ctx, getMessageInRoom, arg.ID, arg.RoomID)
	var i GetMessageInRoomRow
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.RoomID,
		&i.AuthorActorID,
		&i.Body,
		&i.Segments,
		&i.MessageKind,
		&i.CreatedAt,
	)
	return i, err
}

const getRoomByID = `-- name: GetRoomByID :one
SELECT id, tenant_id, name, description, created_by_actor_id,
       next_room_seq, created_at, updated_at,
       deletion_started_at, deletion_started_by_actor_id,
       deleted_at, deleted_by_actor_id,
       direct_human_actor_id, direct_agent_actor_id, parent_room_id, root_message_id,
       team_md_raw_markdown, team_md_collaborate_pattern, team_md_document,
       team_md_revision, team_md_updated_at, team_md_updated_by_actor_id
FROM rooms
WHERE id = $1::text AND deleted_at IS NULL
`

func (q *Queries) GetRoomByID(ctx context.Context, id string) (Room, error) {
	row := q.db.QueryRow(ctx, getRoomByID, id)
	var i Room
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.Name,
		&i.Description,
		&i.CreatedByActorID,
		&i.NextRoomSeq,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletionStartedAt,
		&i.DeletionStartedByActorID,
		&i.DeletedAt,
		&i.DeletedByActorID,
		&i.DirectHumanActorID,
		&i.DirectAgentActorID,
		&i.ParentRoomID,
		&i.RootMessageID,
		&i.TeamMdRawMarkdown,
		&i.TeamMdCollaboratePattern,
		&i.TeamMdDocument,
		&i.TeamMdRevision,
		&i.TeamMdUpdatedAt,
		&i.TeamMdUpdatedByActorID,
	)
	return i, err
}

const getThreadReplyStats = `-- name: GetThreadReplyStats :one
SELECT count(*)::bigint AS reply_count, max(message.created_at) AS latest_reply_at
FROM messages message
JOIN rooms thread ON thread.id=message.room_id AND thread.parent_room_id IS NOT NULL
WHERE message.room_id = $1::text
  AND message.message_kind='conversation'
`

type GetThreadReplyStatsRow struct {
	ReplyCount    int64       `json:"reply_count"`
	LatestReplyAt interface{} `json:"latest_reply_at"`
}

func (q *Queries) GetThreadReplyStats(ctx context.Context, roomID string) (GetThreadReplyStatsRow, error) {
	row := q.db.QueryRow(ctx, getThreadReplyStats, roomID)
	var i GetThreadReplyStatsRow
	err := row.Scan(&i.ReplyCount, &i.LatestReplyAt)
	return i, err
}

const getThreadRoomByRoomID = `-- name: GetThreadRoomByRoomID :one
SELECT id AS room_id, tenant_id, parent_room_id::text AS parent_room_id,
       root_message_id::text AS root_message_id, created_by_actor_id, created_at
FROM rooms
WHERE id = $1::text AND parent_room_id IS NOT NULL
`

type GetThreadRoomByRoomIDRow struct {
	RoomID           string             `json:"room_id"`
	TenantID         string             `json:"tenant_id"`
	ParentRoomID     string             `json:"parent_room_id"`
	RootMessageID    string             `json:"root_message_id"`
	CreatedByActorID string             `json:"created_by_actor_id"`
	CreatedAt        pgtype.Timestamptz `json:"created_at"`
}

func (q *Queries) GetThreadRoomByRoomID(ctx context.Context, roomID string) (GetThreadRoomByRoomIDRow, error) {
	row := q.db.QueryRow(ctx, getThreadRoomByRoomID, roomID)
	var i GetThreadRoomByRoomIDRow
	err := row.Scan(
		&i.RoomID,
		&i.TenantID,
		&i.ParentRoomID,
		&i.RootMessageID,
		&i.CreatedByActorID,
		&i.CreatedAt,
	)
	return i, err
}

const getThreadRoomByRoot = `-- name: GetThreadRoomByRoot :one
SELECT id AS room_id, tenant_id, parent_room_id::text AS parent_room_id,
       root_message_id::text AS root_message_id, created_by_actor_id, created_at
FROM rooms
WHERE parent_room_id = $1::text
  AND root_message_id = $2::text
`

type GetThreadRoomByRootParams struct {
	ParentRoomID  string `json:"parent_room_id"`
	RootMessageID string `json:"root_message_id"`
}

type GetThreadRoomByRootRow struct {
	RoomID           string             `json:"room_id"`
	TenantID         string             `json:"tenant_id"`
	ParentRoomID     string             `json:"parent_room_id"`
	RootMessageID    string             `json:"root_message_id"`
	CreatedByActorID string             `json:"created_by_actor_id"`
	CreatedAt        pgtype.Timestamptz `json:"created_at"`
}

func (q *Queries) GetThreadRoomByRoot(ctx context.Context, arg GetThreadRoomByRootParams) (GetThreadRoomByRootRow, error) {
	row := q.db.QueryRow(ctx, getThreadRoomByRoot, arg.ParentRoomID, arg.RootMessageID)
	var i GetThreadRoomByRootRow
	err := row.Scan(
		&i.RoomID,
		&i.TenantID,
		&i.ParentRoomID,
		&i.RootMessageID,
		&i.CreatedByActorID,
		&i.CreatedAt,
	)
	return i, err
}

const inSameCollaborationRoom = `-- name: InSameCollaborationRoom :one
SELECT (collaboration_membership_room_id($1::text)
    =collaboration_membership_room_id($2::text))::boolean AS same
`

type InSameCollaborationRoomParams struct {
	RoomID      string `json:"room_id"`
	OtherRoomID string `json:"other_room_id"`
}

// A Thread shares its parent Room's collaboration.
func (q *Queries) InSameCollaborationRoom(ctx context.Context, arg InSameCollaborationRoomParams) (bool, error) {
	row := q.db.QueryRow(ctx, inSameCollaborationRoom, arg.RoomID, arg.OtherRoomID)
	var same bool
	err := row.Scan(&same)
	return same, err
}

const isThreadRoom = `-- name: IsThreadRoom :one
SELECT EXISTS(SELECT 1 FROM rooms WHERE id = $1::text AND parent_room_id IS NOT NULL)
`

func (q *Queries) IsThreadRoom(ctx context.Context, roomID string) (bool, error) {
	row := q.db.QueryRow(ctx, isThreadRoom, roomID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const listLatestThreadReplyAuthors = `-- name: ListLatestThreadReplyAuthors :many
WITH latest AS (
    SELECT DISTINCT ON (reply.author_actor_id)
           reply.author_actor_id, reply.created_at, reply.id
    FROM messages reply
    JOIN rooms thread ON thread.id=reply.room_id AND thread.parent_room_id IS NOT NULL
    WHERE reply.room_id = $1::text
      AND reply.message_kind='conversation'
    ORDER BY reply.author_actor_id, reply.created_at DESC, reply.id DESC
)
SELECT author.id, author.tenant_id, author.kind, author.display_name,
       author.account_id, author.handle, author.created_at
FROM latest
JOIN actors author ON author.id = latest.author_actor_id
ORDER BY latest.created_at DESC, latest.id DESC
LIMIT 3
`

func (q *Queries) ListLatestThreadReplyAuthors(ctx context.Context, roomID string) ([]Actor, error) {
	rows, err := q.db.Query(ctx, listLatestThreadReplyAuthors, roomID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Actor{}
	for rows.Next() {
		var i Actor
		if err := rows.Scan(
			&i.ID,
			&i.TenantID,
			&i.Kind,
			&i.DisplayName,
			&i.AccountID,
			&i.Handle,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listLatestThreadReplyAuthorsBatch = `-- name: ListLatestThreadReplyAuthorsBatch :many
SELECT thread.id AS room_id, author.id, author.tenant_id, author.kind, author.display_name, author.account_id, author.handle, author.created_at
FROM rooms thread
CROSS JOIN LATERAL (
    SELECT latest.author_actor_id, latest.created_at, latest.id
    FROM (
        SELECT DISTINCT ON (reply.author_actor_id)
               reply.author_actor_id, reply.created_at, reply.id
        FROM messages reply
        WHERE reply.room_id = thread.id
          AND reply.message_kind = 'conversation'
        ORDER BY reply.author_actor_id, reply.created_at DESC, reply.id DESC
    ) latest
    ORDER BY latest.created_at DESC, latest.id DESC
    LIMIT 3
) latest
JOIN actors author ON author.id = latest.author_actor_id
WHERE thread.id = ANY($1::text[])
  AND thread.parent_room_id IS NOT NULL
ORDER BY thread.id, latest.created_at DESC, latest.id DESC
`

type ListLatestThreadReplyAuthorsBatchRow struct {
	RoomID string `json:"room_id"`
	Actor  Actor  `json:"actor"`
}

// Keep the per-Thread distinct-author ordering and limit used by the single
// Thread query, but fetch all requested Threads in one database round trip.
func (q *Queries) ListLatestThreadReplyAuthorsBatch(ctx context.Context, roomIds []string) ([]ListLatestThreadReplyAuthorsBatchRow, error) {
	rows, err := q.db.Query(ctx, listLatestThreadReplyAuthorsBatch, roomIds)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListLatestThreadReplyAuthorsBatchRow{}
	for rows.Next() {
		var i ListLatestThreadReplyAuthorsBatchRow
		if err := rows.Scan(
			&i.RoomID,
			&i.Actor.ID,
			&i.Actor.TenantID,
			&i.Actor.Kind,
			&i.Actor.DisplayName,
			&i.Actor.AccountID,
			&i.Actor.Handle,
			&i.Actor.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listMessageThreadSummaries = `-- name: ListMessageThreadSummaries :many
SELECT thread.root_message_id::text AS root_message_id, thread.id AS room_id,
       coalesce(stats.reply_count, 0)::bigint AS reply_count,
       stats.latest_reply_at
FROM rooms thread
LEFT JOIN LATERAL (
    SELECT count(*)::bigint AS reply_count, max(reply.created_at) AS latest_reply_at
    FROM messages reply
    WHERE reply.room_id = thread.id
      AND reply.message_kind='conversation'
) stats ON true
WHERE thread.parent_room_id = $1::text
  AND thread.root_message_id = ANY($2::text[])
`

type ListMessageThreadSummariesParams struct {
	ParentRoomID string   `json:"parent_room_id"`
	MessageIds   []string `json:"message_ids"`
}

type ListMessageThreadSummariesRow struct {
	RootMessageID string      `json:"root_message_id"`
	RoomID        string      `json:"room_id"`
	ReplyCount    int64       `json:"reply_count"`
	LatestReplyAt interface{} `json:"latest_reply_at"`
}

func (q *Queries) ListMessageThreadSummaries(ctx context.Context, arg ListMessageThreadSummariesParams) ([]ListMessageThreadSummariesRow, error) {
	rows, err := q.db.Query(ctx, listMessageThreadSummaries, arg.ParentRoomID, arg.MessageIds)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListMessageThreadSummariesRow{}
	for rows.Next() {
		var i ListMessageThreadSummariesRow
		if err := rows.Scan(
			&i.RootMessageID,
			&i.RoomID,
			&i.ReplyCount,
			&i.LatestReplyAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markThreadOpened = `-- name: MarkThreadOpened :exec
INSERT INTO room_member_states(room_id, actor_id, thread_opened_at)
VALUES ($1::text, $2::text, now())
ON CONFLICT (room_id, actor_id) DO UPDATE
SET thread_opened_at=COALESCE(room_member_states.thread_opened_at, excluded.thread_opened_at)
`

type MarkThreadOpenedParams struct {
	RoomID  string `json:"room_id"`
	ActorID string `json:"actor_id"`
}

// Opening is the one Thread list reason that leaves no Message or mention
// behind. A new row starts unread, as a missing read state does.
func (q *Queries) MarkThreadOpened(ctx context.Context, arg MarkThreadOpenedParams) error {
	_, err := q.db.Exec(ctx, markThreadOpened, arg.RoomID, arg.ActorID)
	return err
}

const setThreadRoomParent = `-- name: SetThreadRoomParent :one
UPDATE rooms
SET parent_room_id=$1::text,
    root_message_id=$2::text
WHERE id=$3::text
  AND tenant_id=$4::text
  AND parent_room_id IS NULL
RETURNING id
`

type SetThreadRoomParentParams struct {
	ParentRoomID  string `json:"parent_room_id"`
	RootMessageID string `json:"root_message_id"`
	RoomID        string `json:"room_id"`
	TenantID      string `json:"tenant_id"`
}

// A new Thread Room is linked to its parent only after its room.created
// event, so that event reaches no Account.
func (q *Queries) SetThreadRoomParent(ctx context.Context, arg SetThreadRoomParentParams) (string, error) {
	row := q.db.QueryRow(ctx, setThreadRoomParent,
		arg.ParentRoomID,
		arg.RootMessageID,
		arg.RoomID,
		arg.TenantID,
	)
	var id string
	err := row.Scan(&id)
	return id, err
}