Skip to content

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

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

package db

import (
	"context"

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

const addProjectSubscriber = `-- name: AddProjectSubscriber :exec
INSERT INTO project_subscriber (workspace_id, project_id, user_id)
SELECT m.workspace_id, $1::uuid, m.user_id
FROM member m
WHERE m.workspace_id = $2 AND m.user_id = $3
ON CONFLICT (project_id, user_id) DO NOTHING
`

type AddProjectSubscriberParams struct {
	ProjectID   pgtype.UUID `json:"project_id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	UserID      pgtype.UUID `json:"user_id"`
}

// Only a current workspace member can watch a project.
func (q *Queries) AddProjectSubscriber(ctx context.Context, arg AddProjectSubscriberParams) error {
	_, err := q.db.Exec(ctx, addProjectSubscriber, arg.ProjectID, arg.WorkspaceID, arg.UserID)
	return err
}

const deleteProjectSubscribersByProject = `-- name: DeleteProjectSubscribersByProject :exec
DELETE FROM project_subscriber
WHERE project_id = $1 AND workspace_id = $2
`

type DeleteProjectSubscribersByProjectParams struct {
	ProjectID   pgtype.UUID `json:"project_id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

func (q *Queries) DeleteProjectSubscribersByProject(ctx context.Context, arg DeleteProjectSubscribersByProjectParams) error {
	_, err := q.db.Exec(ctx, deleteProjectSubscribersByProject, arg.ProjectID, arg.WorkspaceID)
	return err
}

const deleteProjectSubscribersByUser = `-- name: DeleteProjectSubscribersByUser :exec
DELETE FROM project_subscriber
WHERE workspace_id = $1 AND user_id = $2
`

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

// A member leaving the workspace stops watching every project in it.
func (q *Queries) DeleteProjectSubscribersByUser(ctx context.Context, arg DeleteProjectSubscribersByUserParams) error {
	_, err := q.db.Exec(ctx, deleteProjectSubscribersByUser, arg.WorkspaceID, arg.UserID)
	return err
}

const isProjectSubscriber = `-- name: IsProjectSubscriber :one
SELECT EXISTS(
    SELECT 1 FROM project_subscriber
    WHERE project_id = $1
      AND workspace_id = $2
      AND user_id = $3
) AS subscribed
`

type IsProjectSubscriberParams struct {
	ProjectID   pgtype.UUID `json:"project_id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	UserID      pgtype.UUID `json:"user_id"`
}

func (q *Queries) IsProjectSubscriber(ctx context.Context, arg IsProjectSubscriberParams) (bool, error) {
	row := q.db.QueryRow(ctx, isProjectSubscriber, arg.ProjectID, arg.WorkspaceID, arg.UserID)
	var subscribed bool
	err := row.Scan(&subscribed)
	return subscribed, err
}

const listProjectSubscribersForIssue = `-- name: ListProjectSubscribersForIssue :many
WITH RECURSIVE ancestors(node_id, parent_id, is_root) AS (
    SELECT root.id, root.parent_issue_id, true FROM issue root WHERE root.id = $1
    UNION
    SELECT i.id, i.parent_issue_id, false FROM issue i JOIN ancestors a ON i.id = a.parent_id
)
SELECT ps.user_id, ps.created_at
FROM issue target
JOIN project p ON p.id = target.project_id AND p.workspace_id = target.workspace_id
JOIN project_subscriber ps
  ON ps.project_id = p.id AND ps.workspace_id = p.workspace_id
JOIN member m ON m.workspace_id = ps.workspace_id AND m.user_id = ps.user_id
WHERE target.id = $1
  AND (
      p.visibility <> 'private'
      OR EXISTS (
          SELECT 1 FROM project_member pm
          WHERE pm.project_id = p.id AND pm.user_id = ps.user_id
      )
  )
  AND NOT EXISTS (
      SELECT 1
      FROM issue_subscriber s
      JOIN ancestors a ON a.node_id = s.issue_id
      WHERE s.user_type = 'member' AND s.user_id = ps.user_id
        AND s.unsubscribed_at IS NOT NULL
        AND (a.is_root OR s.opt_out_scope = 'subtree')
  )
ORDER BY ps.created_at, ps.user_id
`

type ListProjectSubscribersForIssueRow struct {
	UserID    pgtype.UUID        `json:"user_id"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
}

// Members whose project subscription currently covers this issue: still in
// the workspace, able to see the project, and not opted out of this issue or
// of a tree containing it (the same rule as HasAncestorOptOut). Leaving an
// issue is the narrower, more recent intent, so the project never brings the
// member back. Both notification fanout and the issue's effective subscriber
// list read this, so the two cannot disagree.
// UNION drops repeated rows, so a parent loop an older server stored still
// ends the walk.
func (q *Queries) ListProjectSubscribersForIssue(ctx context.Context, issueID pgtype.UUID) ([]ListProjectSubscribersForIssueRow, error) {
	rows, err := q.db.Query(ctx, listProjectSubscribersForIssue, issueID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListProjectSubscribersForIssueRow{}
	for rows.Next() {
		var i ListProjectSubscribersForIssueRow
		if err := rows.Scan(&i.UserID, &i.CreatedAt); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const removeProjectSubscriber = `-- name: RemoveProjectSubscriber :exec
DELETE FROM project_subscriber
WHERE project_id = $1
  AND workspace_id = $2
  AND user_id = $3
`

type RemoveProjectSubscriberParams struct {
	ProjectID   pgtype.UUID `json:"project_id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	UserID      pgtype.UUID `json:"user_id"`
}

func (q *Queries) RemoveProjectSubscriber(ctx context.Context, arg RemoveProjectSubscriberParams) error {
	_, err := q.db.Exec(ctx, removeProjectSubscriber, arg.ProjectID, arg.WorkspaceID, arg.UserID)
	return err
}