Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
6.2 KB
Raw Blame
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: issue_unread.sql

package db

import (
	"context"

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

const listIssueUnread = `-- name: ListIssueUnread :many
SELECT u.issue_id, u.outcome, u.agent_id, u.task_id, u.created_at, run_comment.id AS comment_id
FROM issue_unread u
LEFT JOIN LATERAL (
  SELECT c.id
  FROM comment c
  WHERE c.issue_id = u.issue_id
    AND c.source_task_id = u.task_id
    AND c.deleted_at IS NULL
  ORDER BY c.created_at DESC, c.id DESC
  LIMIT 1
) run_comment ON true
WHERE u.workspace_id = $1
  AND u.user_id = $2
  -- MUL-8472: private projects the caller cannot see.
  AND ($3::uuid[] IS NULL OR NOT EXISTS (
    SELECT 1 FROM issue hidden_issue
    WHERE hidden_issue.id = u.issue_id
      AND COALESCE(hidden_issue.project_id, '00000000-0000-0000-0000-000000000000'::uuid) = ANY($3::uuid[])
  ))
  AND (
    $4::timestamptz IS NULL
    OR (u.created_at, u.issue_id) < ($4::timestamptz, $5::uuid)
  )
ORDER BY u.created_at DESC, u.issue_id DESC
LIMIT $6
`

type ListIssueUnreadParams struct {
	WorkspaceID      pgtype.UUID        `json:"workspace_id"`
	UserID           pgtype.UUID        `json:"user_id"`
	HiddenProjectIds []pgtype.UUID      `json:"hidden_project_ids"`
	BeforeCreatedAt  pgtype.Timestamptz `json:"before_created_at"`
	BeforeIssueID    pgtype.UUID        `json:"before_issue_id"`
	PageLimit        int32              `json:"page_limit"`
}

type ListIssueUnreadRow struct {
	IssueID   pgtype.UUID        `json:"issue_id"`
	Outcome   string             `json:"outcome"`
	AgentID   pgtype.UUID        `json:"agent_id"`
	TaskID    pgtype.UUID        `json:"task_id"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
	CommentID pgtype.UUID        `json:"comment_id"`
}

// One page of the caller's unread issues in one workspace, newest first. Pages
// continue strictly after the (created_at, issue_id) of the previous page's
// last row, so every row is reachable however many there are.
// comment_id is the last live comment the unread run posted on this issue: where
// opening the issue from the sidebar lands. The run just ended, so its comment
// is among the issue's newest and the newest-first timeline index finds it fast.
func (q *Queries) ListIssueUnread(ctx context.Context, arg ListIssueUnreadParams) ([]ListIssueUnreadRow, error) {
	rows, err := q.db.Query(ctx, listIssueUnread,
		arg.WorkspaceID,
		arg.UserID,
		arg.HiddenProjectIds,
		arg.BeforeCreatedAt,
		arg.BeforeIssueID,
		arg.PageLimit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListIssueUnreadRow{}
	for rows.Next() {
		var i ListIssueUnreadRow
		if err := rows.Scan(
			&i.IssueID,
			&i.Outcome,
			&i.AgentID,
			&i.TaskID,
			&i.CreatedAt,
			&i.CommentID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markIssueRead = `-- name: MarkIssueRead :execrows
DELETE FROM issue_unread
WHERE workspace_id = $1 AND user_id = $2 AND issue_id = $3
`

type MarkIssueReadParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	UserID      pgtype.UUID `json:"user_id"`
	IssueID     pgtype.UUID `json:"issue_id"`
}

func (q *Queries) MarkIssueRead(ctx context.Context, arg MarkIssueReadParams) (int64, error) {
	result, err := q.db.Exec(ctx, markIssueRead, arg.WorkspaceID, arg.UserID, arg.IssueID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const markIssueUnreadForSubscribers = `-- name: MarkIssueUnreadForSubscribers :many
INSERT INTO issue_unread (workspace_id, user_id, issue_id, outcome, agent_id, task_id, created_at)
SELECT i.workspace_id, s.user_id, i.id, $1::text, $2::uuid, $3::uuid, now()
FROM issue i
JOIN issue_subscriber s
  ON s.issue_id = i.id
 AND s.user_type = 'member'
 AND s.unsubscribed_at IS NULL
 AND s.reason <> 'delegated'
JOIN member m ON m.workspace_id = i.workspace_id AND m.user_id = s.user_id
WHERE i.id = $4
ON CONFLICT (workspace_id, user_id, issue_id) DO UPDATE
SET outcome = EXCLUDED.outcome,
    agent_id = EXCLUDED.agent_id,
    task_id = EXCLUDED.task_id,
    created_at = EXCLUDED.created_at
RETURNING workspace_id, user_id
`

type MarkIssueUnreadForSubscribersParams struct {
	Outcome string      `json:"outcome"`
	AgentID pgtype.UUID `json:"agent_id"`
	TaskID  pgtype.UUID `json:"task_id"`
	IssueID pgtype.UUID `json:"issue_id"`
}

type MarkIssueUnreadForSubscribersRow struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	UserID      pgtype.UUID `json:"user_id"`
}

// An agent run on the issue ended: mark it unread for every active member
// subscriber who is still in the workspace. Delegated subscriptions (sub-issues
// an agent filed under the member's issue) are the reduced notification tier
// and do not light the sidebar. A newer run replaces the row's run details.
func (q *Queries) MarkIssueUnreadForSubscribers(ctx context.Context, arg MarkIssueUnreadForSubscribersParams) ([]MarkIssueUnreadForSubscribersRow, error) {
	rows, err := q.db.Query(ctx, markIssueUnreadForSubscribers,
		arg.Outcome,
		arg.AgentID,
		arg.TaskID,
		arg.IssueID,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []MarkIssueUnreadForSubscribersRow{}
	for rows.Next() {
		var i MarkIssueUnreadForSubscribersRow
		if err := rows.Scan(&i.WorkspaceID, &i.UserID); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markIssueUnreadManually = `-- name: MarkIssueUnreadManually :execrows
INSERT INTO issue_unread (workspace_id, user_id, issue_id, outcome)
VALUES ($1, $2, $3, 'manual')
ON CONFLICT (workspace_id, user_id, issue_id) DO NOTHING
`

type MarkIssueUnreadManuallyParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	UserID      pgtype.UUID `json:"user_id"`
	IssueID     pgtype.UUID `json:"issue_id"`
}

// Keeps an existing row: an unread run result already says more than "manual".
func (q *Queries) MarkIssueUnreadManually(ctx context.Context, arg MarkIssueUnreadManuallyParams) (int64, error) {
	result, err := q.db.Exec(ctx, markIssueUnreadManually, arg.WorkspaceID, arg.UserID, arg.IssueID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}