Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.5 KB
Raw Blame
from django.db import models

from posthog.models.activity_logging.model_activity import ModelActivityMixin
from posthog.models.scoping.root_mixin import TeamScopedRootMixin
from posthog.models.utils import CreatedMetaFields, UpdatedMetaFields, UUIDModel


class TicketMacro(ModelActivityMixin, TeamScopedRootMixin, UUIDModel, CreatedMetaFields, UpdatedMetaFields):
    """A saved reply an agent can insert into the ticket composer."""

    class Visibility(models.TextChoices):
        SHARED = "shared", "Everyone in the project"
        PERSONAL = "personal", "Only the creator"

    activity_logging_on_delete = True

    # db_constraint=False: a real FK constraint takes SHARE ROW EXCLUSIVE on the parent while
    # migrating, stalling writes under traffic.
    team = models.ForeignKey(
        "posthog.Team",
        on_delete=models.CASCADE,
        db_constraint=False,
        db_index=False,
        related_name="+",
    )
    created_by = models.ForeignKey(
        "posthog.User",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        db_constraint=False,
        related_name="+",
    )

    name = models.CharField(max_length=200)
    shortcut = models.CharField(max_length=50, null=True, blank=True)
    body = models.TextField()
    visibility = models.CharField(
        max_length=16,
        choices=Visibility.choices,
        default=Visibility.SHARED,
        db_default=Visibility.SHARED,
    )
    usage_count = models.PositiveIntegerField(default=0, db_default=0)
    last_used_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        app_label = "conversations"
        db_table = "posthog_conversations_ticket_macro"
        constraints = [
            # Null shortcuts are not unique: several macros can have no slash command.
            # A personal shortcut only has to be unique for its creator, so it cannot block teammates.
            models.UniqueConstraint(
                fields=["team", "shortcut"],
                condition=models.Q(shortcut__isnull=False, visibility="shared"),
                name="conv_macro_shared_shortcut_uniq",
            ),
            models.UniqueConstraint(
                fields=["team", "created_by", "shortcut"],
                condition=models.Q(shortcut__isnull=False, visibility="personal"),
                name="conv_macro_personal_shortcut_uniq",
            ),
        ]
        indexes = [
            models.Index(fields=["team", "-usage_count"], name="conv_macro_team_usage_idx"),
        ]

    def __str__(self) -> str:
        return self.name