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.

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

package store

import (
	"context"

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

const documentExists = `-- name: DocumentExists :one
SELECT EXISTS(SELECT 1 FROM documents WHERE id=$1::text)::boolean
`

func (q *Queries) DocumentExists(ctx context.Context, id string) (bool, error) {
	row := q.db.QueryRow(ctx, documentExists, id)
	var column_1 bool
	err := row.Scan(&column_1)
	return column_1, err
}

const getAuthorizedMessageContextDocumentVersions = `-- name: GetAuthorizedMessageContextDocumentVersions :many
SELECT version.id,version.document_id,NULL::text AS source_room_id,
       version.filename,version.media_type,
       version.size_bytes,version.sha256
FROM document_versions version
JOIN documents document
  ON document.id=version.document_id AND document.tenant_id=version.tenant_id
JOIN resolve_human_document_access_v2($1::text,version.document_id) access
  ON access.tenant_id=version.tenant_id AND access.can_read
WHERE version.id=ANY($2::text[])
  AND version.tenant_id=$3::text
  AND document.status='active'
ORDER BY array_position($2::text[],version.id)
`

type GetAuthorizedMessageContextDocumentVersionsParams struct {
	AccountID string   `json:"account_id"`
	Ids       []string `json:"ids"`
	TenantID  string   `json:"tenant_id"`
}

type GetAuthorizedMessageContextDocumentVersionsRow struct {
	ID           string      `json:"id"`
	DocumentID   string      `json:"document_id"`
	SourceRoomID pgtype.Text `json:"source_room_id"`
	Filename     string      `json:"filename"`
	MediaType    string      `json:"media_type"`
	SizeBytes    int64       `json:"size_bytes"`
	Sha256       string      `json:"sha256"`
}

func (q *Queries) GetAuthorizedMessageContextDocumentVersions(ctx context.Context, arg GetAuthorizedMessageContextDocumentVersionsParams) ([]GetAuthorizedMessageContextDocumentVersionsRow, error) {
	rows, err := q.db.Query(ctx, getAuthorizedMessageContextDocumentVersions, arg.AccountID, arg.Ids, arg.TenantID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetAuthorizedMessageContextDocumentVersionsRow{}
	for rows.Next() {
		var i GetAuthorizedMessageContextDocumentVersionsRow
		if err := rows.Scan(
			&i.ID,
			&i.DocumentID,
			&i.SourceRoomID,
			&i.Filename,
			&i.MediaType,
			&i.SizeBytes,
			&i.Sha256,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getMessageAttachmentDocumentVersions = `-- name: GetMessageAttachmentDocumentVersions :many
SELECT version.id,version.document_id,binding.room_id AS source_room_id,
       version.filename,version.media_type,
       version.size_bytes,version.sha256
FROM document_versions version
JOIN documents document
  ON document.id=version.document_id AND document.tenant_id=version.tenant_id
JOIN document_context_bindings binding
  ON binding.document_id=version.document_id AND binding.tenant_id=version.tenant_id
 AND binding.binding_kind='room' AND binding.room_id=$1::text
 AND binding.status='active'
WHERE version.id=ANY($2::text[])
  AND version.tenant_id=$3::text
  AND document.status='active'
ORDER BY array_position($2::text[],version.id)
`

type GetMessageAttachmentDocumentVersionsParams struct {
	RoomID   string   `json:"room_id"`
	Ids      []string `json:"ids"`
	TenantID string   `json:"tenant_id"`
}

type GetMessageAttachmentDocumentVersionsRow struct {
	ID           string      `json:"id"`
	DocumentID   string      `json:"document_id"`
	SourceRoomID pgtype.Text `json:"source_room_id"`
	Filename     string      `json:"filename"`
	MediaType    string      `json:"media_type"`
	SizeBytes    int64       `json:"size_bytes"`
	Sha256       string      `json:"sha256"`
}

func (q *Queries) GetMessageAttachmentDocumentVersions(ctx context.Context, arg GetMessageAttachmentDocumentVersionsParams) ([]GetMessageAttachmentDocumentVersionsRow, error) {
	rows, err := q.db.Query(ctx, getMessageAttachmentDocumentVersions, arg.RoomID, arg.Ids, arg.TenantID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetMessageAttachmentDocumentVersionsRow{}
	for rows.Next() {
		var i GetMessageAttachmentDocumentVersionsRow
		if err := rows.Scan(
			&i.ID,
			&i.DocumentID,
			&i.SourceRoomID,
			&i.Filename,
			&i.MediaType,
			&i.SizeBytes,
			&i.Sha256,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listMessageAttachments = `-- name: ListMessageAttachments :many
SELECT (item.value->>'id')::text AS id,message.id AS message_id,message.tenant_id,
       (item.value->>'document_id')::text AS document_id,
       (item.value->>'document_version_id')::text AS document_version_id,
       source.room_id AS source_room_id,
       version.filename,version.media_type,
       version.size_bytes,version.sha256,
       (document.status='active')::boolean AS available
FROM messages message
CROSS JOIN LATERAL jsonb_array_elements(message.attachments) WITH ORDINALITY item(value,ordinality)
JOIN documents document
  ON document.id=item.value->>'document_id' AND document.tenant_id=message.tenant_id
JOIN document_versions version
  ON version.id=item.value->>'document_version_id'
 AND version.tenant_id=message.tenant_id
 AND version.document_id=item.value->>'document_id'
LEFT JOIN LATERAL (
    SELECT binding.room_id
    FROM document_context_bindings binding
    WHERE binding.document_id=document.id
      AND binding.tenant_id=message.tenant_id
      AND binding.status='active' AND binding.binding_kind='room'
    ORDER BY binding.created_at,binding.id
    LIMIT 1
) source ON true
WHERE message.id=ANY($1::text[])
  AND EXISTS (
      SELECT 1
      FROM jsonb_array_elements(message.segments) segment(value)
      WHERE segment.value->>'document_id'=item.value->>'document_id'
        AND (
          (segment.value->>'type'='file'
            AND segment.value->>'document_version_id'=item.value->>'document_version_id')
          OR (segment.value->>'type'='document'
            AND segment.value->>'presentation'='card')
        )
  )
ORDER BY array_position($1::text[],message.id),item.ordinality
`

type ListMessageAttachmentsRow struct {
	ID                string      `json:"id"`
	MessageID         string      `json:"message_id"`
	TenantID          string      `json:"tenant_id"`
	DocumentID        string      `json:"document_id"`
	DocumentVersionID string      `json:"document_version_id"`
	SourceRoomID      pgtype.Text `json:"source_room_id"`
	Filename          string      `json:"filename"`
	MediaType         string      `json:"media_type"`
	SizeBytes         int64       `json:"size_bytes"`
	Sha256            string      `json:"sha256"`
	Available         bool        `json:"available"`
}

// Only attachments shown by a file or Document card segment; HTML fragment
// attachments stay hidden.
func (q *Queries) ListMessageAttachments(ctx context.Context, messageIds []string) ([]ListMessageAttachmentsRow, error) {
	rows, err := q.db.Query(ctx, listMessageAttachments, messageIds)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListMessageAttachmentsRow{}
	for rows.Next() {
		var i ListMessageAttachmentsRow
		if err := rows.Scan(
			&i.ID,
			&i.MessageID,
			&i.TenantID,
			&i.DocumentID,
			&i.DocumentVersionID,
			&i.SourceRoomID,
			&i.Filename,
			&i.MediaType,
			&i.SizeBytes,
			&i.Sha256,
			&i.Available,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const lockCurrentMessageFileDocumentVersions = `-- name: LockCurrentMessageFileDocumentVersions :many
SELECT document.id AS document_id,version.id AS document_version_id,
       version.filename,version.media_type
FROM documents document
JOIN document_context_bindings binding
  ON binding.document_id=document.id AND binding.tenant_id=document.tenant_id
 AND binding.binding_kind='room'
 AND binding.room_id=collaboration_membership_room_id($1::text)
 AND binding.status='active'
JOIN rooms room
  ON room.id=binding.room_id AND room.tenant_id=binding.tenant_id
JOIN document_versions version
  ON version.id=document.current_version_id
 AND version.tenant_id=document.tenant_id AND version.document_id=document.id
WHERE document.id=ANY($2::text[])
  AND document.tenant_id=$3::text
  AND document.status='active' AND room.deletion_started_at IS NULL
ORDER BY array_position($2::text[],document.id)
FOR SHARE OF document,binding,room,version
`

type LockCurrentMessageFileDocumentVersionsParams struct {
	RoomID      string   `json:"room_id"`
	DocumentIds []string `json:"document_ids"`
	TenantID    string   `json:"tenant_id"`
}

type LockCurrentMessageFileDocumentVersionsRow struct {
	DocumentID        string `json:"document_id"`
	DocumentVersionID string `json:"document_version_id"`
	Filename          string `json:"filename"`
	MediaType         string `json:"media_type"`
}

func (q *Queries) LockCurrentMessageFileDocumentVersions(ctx context.Context, arg LockCurrentMessageFileDocumentVersionsParams) ([]LockCurrentMessageFileDocumentVersionsRow, error) {
	rows, err := q.db.Query(ctx, lockCurrentMessageFileDocumentVersions, arg.RoomID, arg.DocumentIds, arg.TenantID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []LockCurrentMessageFileDocumentVersionsRow{}
	for rows.Next() {
		var i LockCurrentMessageFileDocumentVersionsRow
		if err := rows.Scan(
			&i.DocumentID,
			&i.DocumentVersionID,
			&i.Filename,
			&i.MediaType,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const lockMessageAttachmentUpload = `-- name: LockMessageAttachmentUpload :exec
SELECT pg_advisory_xact_lock(hashtextextended('message-attachment-upload|'||$1::text, 0))
`

// Orders a send, a cancellation and the expiry job for one upload.
func (q *Queries) LockMessageAttachmentUpload(ctx context.Context, objectKey string) error {
	_, err := q.db.Exec(ctx, lockMessageAttachmentUpload, objectKey)
	return err
}