Skip to content

Commit 11052a4

Browse files
committed
reference the chat in the podcast
1 parent 3eb9e1b commit 11052a4

4 files changed

Lines changed: 45 additions & 3 deletions

File tree

surfsense_backend/alembic/versions/32_add_podcast_staleness_detection.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
"""Add podcast staleness detection columns
1+
"""Add podcast staleness detection columns to chats and podcasts tables
2+
3+
This feature allows the system to detect when a podcast is outdated compared to the current state of the chat it was generated from, enabling users to regenerate podcasts when needed.
24
35
Revision ID: 32
46
Revises: 31
@@ -18,7 +20,7 @@
1820

1921

2022
def upgrade() -> None:
21-
"""Add state_version to chats table and chat_state_version to podcasts table."""
23+
"""Add state_version, chat_state_version, and chat_id to chats and podcasts tables."""
2224

2325
# Add state_version column to chats table with default value of 1
2426
op.add_column(
@@ -31,12 +33,18 @@ def upgrade() -> None:
3133
"podcasts", sa.Column("chat_state_version", sa.BigInteger(), nullable=True)
3234
)
3335

36+
# Add chat_id column to podcasts table (nullable, set when podcast is generated from a chat)
37+
op.add_column("podcasts", sa.Column("chat_id", sa.Integer(), nullable=True))
38+
3439

3540
def downgrade() -> None:
36-
"""Remove state_version and chat_state_version columns."""
41+
"""Remove state_version, chat_state_version, and chat_id columns."""
3742

3843
# Remove chat_state_version from podcasts table
3944
op.drop_column("podcasts", "chat_state_version")
4045

46+
# Remove chat_id from podcasts table
47+
op.drop_column("podcasts", "chat_id")
48+
4149
# Remove state_version from chats table
4250
op.drop_column("chats", "state_version")

surfsense_backend/app/db.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ class Podcast(BaseModel, TimestampMixin):
209209
title = Column(String, nullable=False, index=True)
210210
podcast_transcript = Column(JSON, nullable=False, default={})
211211
file_location = Column(String(500), nullable=False, default="")
212+
chat_id = Column(
213+
Integer, ForeignKey("chats.id", ondelete="CASCADE"), nullable=True
214+
) # If generated from a chat, this will be the chat id, else null ( can be from a document or a chat )
212215
chat_state_version = Column(BigInteger, nullable=True)
213216

214217
search_space_id = Column(

surfsense_backend/app/routes/podcasts_routes.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,33 @@ def iterfile():
288288
raise HTTPException(
289289
status_code=500, detail=f"Error streaming podcast: {e!s}"
290290
) from e
291+
292+
293+
@router.get("/podcasts/by-chat/{chat_id}", response_model=PodcastRead)
294+
async def get_podcast_by_chat_id(
295+
chat_id: int,
296+
session: AsyncSession = Depends(get_async_session),
297+
user: User = Depends(current_active_user),
298+
):
299+
try:
300+
# Get the podcast and check if user has access
301+
result = await session.execute(
302+
select(Podcast)
303+
.join(SearchSpace)
304+
.filter(Podcast.chat_id == chat_id, SearchSpace.user_id == user.id)
305+
)
306+
podcast = result.scalars().first()
307+
308+
if not podcast:
309+
raise HTTPException(
310+
status_code=404,
311+
detail="Podcast not found or you don't have permission to access it",
312+
)
313+
314+
return podcast
315+
except HTTPException as he:
316+
raise he
317+
except Exception as e:
318+
raise HTTPException(
319+
status_code=500, detail=f"Error fetching podcast: {e!s}"
320+
) from e

surfsense_backend/app/tasks/podcast_tasks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ async def generate_chat_podcast(
145145
file_location=result["final_podcast_file_path"],
146146
search_space_id=search_space_id,
147147
chat_state_version=chat.state_version,
148+
chat_id=chat.id,
148149
)
149150

150151
# Add to session and commit

0 commit comments

Comments
 (0)