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.

fix(replay): stop faking success when adding to a built-in collection (#91813)

posthog[bot]a20ef89241ed
diff --git a/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts b/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts
index b00f03bf2c28..c7653d5af0f2 100644
--- a/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts
+++ b/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts
@@ -1045,6 +1045,8 @@ export const sessionRecordingsPlaylistLogic = kea<sessionRecordingsPlaylistLogic
limit: 30,
order: '-last_modified_at',
type: 'collection',
+ // Built-in collections can't be added to, so keep them out of the list.
+ collection_type: 'custom',
search: values.addToCollectionSearch || undefined,
})
)
@@ -1551,25 +1553,32 @@ export const sessionRecordingsPlaylistLogic = kea<sessionRecordingsPlaylistLogic
actions.loadSessionRecordings()
},
handleBulkAddToPlaylist: async ({ short_id }: { short_id: string }) => {
+ const requestedCount = values.selectedRecordingsIds.length
+ let addedCount = 0
await lemonToast.promise(
(async () => {
- try {
- await api.recordings.bulkAddRecordingsToPlaylist(short_id, values.selectedRecordingsIds)
- actions.setSelectedRecordingsIds([])
-
- // Reload the playlist to show the new recordings
- handleLoadCollectionRecordings(short_id)
- } catch (e) {
- posthog.captureException(e)
+ const result = await api.recordings
+ .bulkAddRecordingsToPlaylist(short_id, values.selectedRecordingsIds)
+ .catch((e) => {
+ // Report real API or network failures; rethrow so the toast still shows its error state.
+ posthog.captureException(e)
+ throw e
+ })
+ // The endpoint answers 200 even when it saved nothing, so trust added_count.
+ if (result.added_count === 0) {
+ throw new Error('No recordings were added to the collection')
}
+ addedCount = result.added_count
+ actions.setSelectedRecordingsIds([])
+
+ // Reload the playlist to show the new recordings
+ handleLoadCollectionRecordings(short_id)
})(),
{
- success: `${values.selectedRecordingsIds.length} recording${
- values.selectedRecordingsIds.length > 1 ? 's' : ''
- } added to collection!`,
+ success: () => `${addedCount} recording${addedCount > 1 ? 's' : ''} added to collection!`,
error: 'Failed to add to collection!',
- pending: `Adding ${values.selectedRecordingsIds.length} recording${
- values.selectedRecordingsIds.length > 1 ? 's' : ''
+ pending: `Adding ${requestedCount} recording${
+ requestedCount > 1 ? 's' : ''
} to the collection...`,
},
{
diff --git a/posthog/session_recordings/session_recording_playlist_api.py b/posthog/session_recordings/session_recording_playlist_api.py
index 1f28b42d5a9e..32e7746a693c 100644
--- a/posthog/session_recordings/session_recording_playlist_api.py
+++ b/posthog/session_recordings/session_recording_playlist_api.py
@@ -93,6 +93,12 @@ class SessionRecordingPlaylistPagination(LimitOffsetPagination):
max_limit = PLAYLIST_LIST_MAX_LIMIT
+# Built-in (synthetic) collections have no DB row and derive their contents per request,
+# so recordings cannot be pinned to them. Guard the write paths with a clear message.
+SYNTHETIC_PLAYLIST_ADD_ERROR = (
+ "This is a built-in collection, so you can't add recordings to it. Create your own collection to save recordings."
+)
+
DEFAULT_PLAYLIST_ORDER = "-last_modified_at"
# Orders the list endpoint supports. An unrecognised `order` normalises to the
# default so the DB slice and the synthetic-rank maths can't disagree on an unknown
@@ -1014,6 +1020,9 @@ class SessionRecordingPlaylistViewSet(
) -> response.Response:
playlist = self.get_object()
+ if getattr(playlist, "_is_synthetic", False):
+ raise ValidationError(SYNTHETIC_PLAYLIST_ADD_ERROR)
+
# TODO: Maybe we need to save the created_at date here properly to help with filtering
if request.method == "POST":
if playlist.type == SessionRecordingPlaylist.PlaylistType.FILTERS:
@@ -1055,6 +1064,9 @@ class SessionRecordingPlaylistViewSet(
) -> response.Response:
playlist = self.get_object()
+ if getattr(playlist, "_is_synthetic", False):
+ raise ValidationError(SYNTHETIC_PLAYLIST_ADD_ERROR)
+
# Get session_recording_ids from request body
session_recording_ids = request.data.get("session_recording_ids", [])
diff --git a/posthog/session_recordings/test/test_synthetic_playlists.py b/posthog/session_recordings/test/test_synthetic_playlists.py
index 6e2d30476bd6..a8cd731795f4 100644
--- a/posthog/session_recordings/test/test_synthetic_playlists.py
+++ b/posthog/session_recordings/test/test_synthetic_playlists.py
@@ -220,6 +220,17 @@ class TestSyntheticPlaylists(APIBaseTest):
assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED
+ def test_cannot_add_recordings_to_synthetic_playlist(self) -> None:
+ base = f"/api/projects/{self.team.id}/session_recording_playlists/synthetic-watch-history/recordings"
+
+ single = self.client.post(f"{base}/session-1")
+ assert single.status_code == status.HTTP_400_BAD_REQUEST
+
+ bulk = self.client.post(f"{base}/bulk_add", {"session_recording_ids": ["session-1"]})
+ assert bulk.status_code == status.HTTP_400_BAD_REQUEST
+
+ assert SessionRecordingPlaylist.objects.count() == 0
+
def test_pagination_includes_synthetics_only_on_first_page(self) -> None:
for i in range(25):
SessionRecordingPlaylist.objects.create(