: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(tasks): star a space for whoever creates it (#80306)
Adam Leith426b5425aaa5
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex 5ea6e2210053..b4a0845335c8 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -5771,6 +5771,17 @@ def normalize_channel_name(name: str) -> str:return re.sub(r"\s+", "-", name.strip().lower())[:128]+def _set_channel_star(channel_id: UUID, team_id: int, user_id: int, *, starred: bool) -> None:+ if starred:+ ChannelStar.objects.get_or_create(channel_id=channel_id, user_id=user_id, defaults={"team_id": team_id})+ else:+ ChannelStar.objects.filter(channel_id=channel_id, user_id=user_id).delete()+++def _is_channel_starred(channel_id: UUID, user_id: int) -> bool:+ return ChannelStar.objects.filter(channel_id=channel_id, user_id=user_id).exists()++def _channel_to_dto(channel: Channel, *, starred: bool = False) -> contracts.ChannelDTO:return contracts.ChannelDTO(id=channel.id,@@ -5854,9 +5865,11 @@ def _emit_channel_created(channel: Channel, user_id: int | None) -> None:logger.exception("Failed to emit channel_created feed message", extra={"channel_id": str(channel.id)})-def resolve_channel(team_id: int, user_id: int | None, *, name: str) -> contracts.ChannelDTO | None:+def resolve_channel(team_id: int, user_id: int | None, *, name: str, star: bool) -> contracts.ChannelDTO | None:"""Resolve-or-create a public channel by (normalized) name. ``None`` for empty names.- Emits a ``channel_created`` feed message the first time a channel is created."""+ Emits a ``channel_created`` feed message the first time a channel is created, and (unless+ ``star`` is false) stars the channel for whoever created it. Resolving a channel that+ already exists leaves the requester's star alone — only creation stars."""normalized = normalize_channel_name(name)if not normalized:return None@@ -5875,7 +5888,15 @@ def resolve_channel(team_id: int, user_id: int | None, *, name: str) -> contract)if created:_emit_channel_created(channel, user_id)- return _channel_to_dto(channel)+ if user_id is None:+ starred = False+ elif not created:+ starred = _is_channel_starred(channel.id, user_id)+ else:+ starred = star+ if star:+ _set_channel_star(channel.id, team_id, user_id, starred=True)+ return _channel_to_dto(channel, starred=starred)def update_channel(@@ -6031,7 +6052,7 @@ def get_channel(channel_id: str | UUID, team_id: int, user_id: int | None) -> cochannel = _visible_channel(channel_id, team_id, user_id)if channel is None:return None- starred = user_id is not None and ChannelStar.objects.filter(channel_id=channel.id, user_id=user_id).exists()+ starred = user_id is not None and _is_channel_starred(channel.id, user_id)return _channel_to_dto(channel, starred=starred)@@ -6230,10 +6251,7 @@ def star_channel(channel_id: str | UUID, team_id: int, user_id: int, *, starred:channel = _visible_channel(channel_id, team_id, user_id)if channel is None:return False- if starred:- ChannelStar.objects.get_or_create(channel_id=channel.id, user_id=user_id, defaults={"team_id": team_id})- else:- ChannelStar.objects.filter(channel_id=channel.id, user_id=user_id).delete()+ _set_channel_star(channel.id, team_id, user_id, starred=starred)return Truediff --git a/products/tasks/backend/presentation/serializers.py b/products/tasks/backend/presentation/serializers.pyindex 9c14672e8107..58ff6204b325 100644--- a/products/tasks/backend/presentation/serializers.py+++ b/products/tasks/backend/presentation/serializers.py@@ -1552,6 +1552,14 @@ class ChannelWriteSerializer(serializers.Serializer):name = serializers.CharField(max_length=128, help_text="Channel name, rendered as #<name>. Normalized to lowercase-dashed.")+ star = serializers.BooleanField(+ required=False,+ default=True,+ help_text=(+ "Star the channel for the requester when this call creates it. "+ "Ignored when the channel already exists, which leaves existing stars untouched."+ ),+ )class ChannelUpdateSerializer(serializers.Serializer):diff --git a/products/tasks/backend/presentation/views/channels_api.py b/products/tasks/backend/presentation/views/channels_api.pyindex 6f73d9a0c56a..90d350ed3769 100644--- a/products/tasks/backend/presentation/views/channels_api.py+++ b/products/tasks/backend/presentation/views/channels_api.py@@ -104,12 +104,20 @@ class ChannelViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):request=ChannelWriteSerializer,responses={200: ChannelSerializer},summary="Resolve or create a public channel",- description="Returns the existing public channel with the (normalized) name, creating it if needed.",+ description=(+ "Returns the existing public channel with the (normalized) name, creating it if needed. "+ "A channel created here is starred for the requester unless star is false."+ ),)def create(self, request, **kwargs):serializer = ChannelWriteSerializer(data=request.data)serializer.is_valid(raise_exception=True)- channel = tasks_facade.resolve_channel(self.team_id, self._user_id(), name=serializer.validated_data["name"])+ channel = tasks_facade.resolve_channel(+ self.team_id,+ self._user_id(),+ name=serializer.validated_data["name"],+ star=serializer.validated_data["star"],+ )if channel is None:return Response({"detail": "Invalid channel name"}, status=status.HTTP_400_BAD_REQUEST)return Response(ChannelSerializer(channel).data)diff --git a/products/tasks/backend/tests/test_channel_extras.py b/products/tasks/backend/tests/test_channel_extras.pyindex d7c21745ef8d..8618cfdff3c9 100644--- a/products/tasks/backend/tests/test_channel_extras.py+++ b/products/tasks/backend/tests/test_channel_extras.py@@ -5,6 +5,7 @@ from posthog.test.base import APIBaseTestfrom django.utils import timezone as django_timezone+from parameterized import parameterizedfrom rest_framework import statusfrom rest_framework.test import APIClient@@ -186,3 +187,28 @@ class TestChannelStars(ChannelExtrasBaseTest):response = self.client.post(f"{self.base}/star/", {"starred": False}, format="json")assert response.status_code == status.HTTP_204_NO_CONTENTassert not ChannelStar.objects.unscoped().filter(channel=self.channel, user=self.user).exists()++ @parameterized.expand(+ [+ ("star_omitted", {}, True),+ ("star_on", {"star": True}, True),+ ("star_off", {"star": False}, False),+ ]+ )+ def test_creating_a_channel_stars_it_for_its_creator(self, _name, body, expected_starred):+ response = self.client.post(+ f"/api/projects/{self.team.id}/task_channels/", {"name": "growth", **body}, format="json"+ )+ assert response.status_code == status.HTTP_200_OK+ assert response.json()["starred"] is expected_starred+ assert (+ ChannelStar.objects.unscoped().filter(channel_id=response.json()["id"], user=self.user).exists()+ is expected_starred+ )++ def test_resolving_an_existing_channel_leaves_stars_alone(self):+ response = self.client.post(f"/api/projects/{self.team.id}/task_channels/", {"name": "general"}, format="json")+ assert response.status_code == status.HTTP_200_OK+ assert response.json()["id"] == str(self.channel.id)+ assert response.json()["starred"] is False+ assert not ChannelStar.objects.unscoped().filter(channel=self.channel, user=self.user).exists()diff --git a/products/tasks/frontend/generated/api.schemas.ts b/products/tasks/frontend/generated/api.schemas.tsindex abc98461a575..10858d9138e9 100644--- a/products/tasks/frontend/generated/api.schemas.ts+++ b/products/tasks/frontend/generated/api.schemas.ts@@ -1171,6 +1171,8 @@ export interface ChannelWriteApi {* @maxLength 128*/name: string+ /** Star the channel for the requester when this call creates it. Ignored when the channel already exists, which leaves existing stars untouched. */+ star?: boolean}export type ChannelFeedMessageDTOApiPayload = { [key: string]: unknown }diff --git a/products/tasks/frontend/generated/api.ts b/products/tasks/frontend/generated/api.tsindex d20c5ff4cbf3..d34f2dbc6798 100644--- a/products/tasks/frontend/generated/api.ts+++ b/products/tasks/frontend/generated/api.ts@@ -858,7 +858,7 @@ export const getTaskChannelsCreateUrl = (projectId: string) => {}/**- * Returns the existing public channel with the (normalized) name, creating it if needed.+ * Returns the existing public channel with the (normalized) name, creating it if needed. A channel created here is starred for the requester unless star is false.* @summary Resolve or create a public channel*/export const taskChannelsCreate = async (diff --git a/products/tasks/frontend/generated/api.zod.ts b/products/tasks/frontend/generated/api.zod.tsindex 51fb81536a23..fbb886104741 100644--- a/products/tasks/frontend/generated/api.zod.ts+++ b/products/tasks/frontend/generated/api.zod.ts@@ -1002,17 +1002,25 @@ export const TaskAutomationsPartialUpdateBody = /* @__PURE__ */ zod.describe('Request body for creating or updating a task automation.')/**- * Returns the existing public channel with the (normalized) name, creating it if needed.+ * Returns the existing public channel with the (normalized) name, creating it if needed. A channel created here is starred for the requester unless star is false.* @summary Resolve or create a public channel*/export const taskChannelsCreateBodyNameMax = 128+export const taskChannelsCreateBodyStarDefault = true+export const TaskChannelsCreateBody = /* @__PURE__ */ zod.object({name: zod.string().max(taskChannelsCreateBodyNameMax).describe('Channel name, rendered as #<name>. Normalized to lowercase-dashed.'),+ star: zod+ .boolean()+ .default(taskChannelsCreateBodyStarDefault)+ .describe(+ 'Star the channel for the requester when this call creates it. Ignored when the channel already exists, which leaves existing stars untouched.'+ ),}).describe('Request body for creating (resolve-or-create) or renaming a public channel.')diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex af0f50e9bda4..116105edf94c 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -14979,6 +14979,8 @@ export namespace Schemas {* @maxLength 128*/name: string;+ /** Star the channel for the requester when this call creates it. Ignored when the channel already exists, which leaves existing stars untouched. */+ star?: boolean;}export interface CheckDatabaseNameResponse {diff --git a/services/mcp/src/generated/tasks/api.ts b/services/mcp/src/generated/tasks/api.tsindex d5ebbe468362..e75a3c2f420f 100644--- a/services/mcp/src/generated/tasks/api.ts+++ b/services/mcp/src/generated/tasks/api.ts@@ -752,7 +752,7 @@ export const TaskChannelsListQueryParams = /* @__PURE__ */ zod.object({})/**- * Returns the existing public channel with the (normalized) name, creating it if needed.+ * Returns the existing public channel with the (normalized) name, creating it if needed. A channel created here is starred for the requester unless star is false.* @summary Resolve or create a public channel*/export const TaskChannelsCreateParams = /* @__PURE__ */ zod.object({@@ -765,12 +765,20 @@ export const TaskChannelsCreateParams = /* @__PURE__ */ zod.object({export const taskChannelsCreateBodyNameMax = 128+export const taskChannelsCreateBodyStarDefault = true+export const TaskChannelsCreateBody = /* @__PURE__ */ zod.object({name: zod.string().max(taskChannelsCreateBodyNameMax).describe('Channel name, rendered as #<name>. Normalized to lowercase-dashed.'),+ star: zod+ .boolean()+ .default(taskChannelsCreateBodyStarDefault)+ .describe(+ 'Star the channel for the requester when this call creates it. Ignored when the channel already exists, which leaves existing stars untouched.'+ ),}).describe('Request body for creating (resolve-or-create) or renaming a public channel.')diff --git a/services/mcp/src/tools/generated/tasks.ts b/services/mcp/src/tools/generated/tasks.tsindex 7a787b71135d..e7160f9f09af 100644--- a/services/mcp/src/tools/generated/tasks.ts+++ b/services/mcp/src/tools/generated/tasks.ts@@ -50,6 +50,9 @@ const channelCreate = (): ToolBase<typeof ChannelCreateSchema, Schemas.ChannelDTif (params.name !== undefined) {body['name'] = params.name}+ if (params.star !== undefined) {+ body['star'] = params.star+ }const result = await context.api.request<Schemas.ChannelDTO>({method: 'POST',path: `/api/projects/${encodeURIComponent(String(projectId))}/task_channels/`,diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/channel-create.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/channel-create.jsonindex 4091bbd52b5b..e3e5f789af50 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/channel-create.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/channel-create.json@@ -6,6 +6,11 @@"description": "Channel name, rendered as #<name>. Normalized to lowercase-dashed.","maxLength": 128,"type": "string"+ },+ "star": {+ "default": true,+ "description": "Star the channel for the requester when this call creates it. Ignored when the channel already exists, which leaves existing stars untouched.",+ "type": "boolean"}},"required": ["name"],