Skip to content

Conversation

@hila-f-qodo
Copy link

@qodo-code-review
Copy link

qodo-code-review bot commented Dec 23, 2025

Code Review by Qodo (Alpha)

Grey Divider

Analysis Completed

Published 1 inline comment(s)

Grey Divider

Qodo Logo

Comment on lines +98 to +116
def unsubscribe
@topic_view = TopicView.new(params[:topic_id], current_user)

if slugs_do_not_match || (!request.format.json? && params[:slug].blank?)
return redirect_to @topic_view.topic.unsubscribe_url, status: 301
end

tu = TopicUser.find_by(user_id: current_user.id, topic_id: params[:topic_id])

if tu.notification_level > TopicUser.notification_levels[:regular]
tu.notification_level = TopicUser.notification_levels[:regular]
else
tu.notification_level = TopicUser.notification_levels[:muted]
end

tu.save!

perform_show_response
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[action_required] [Null Reference in Unsubscribe Action]
• The TopicUser.find_by(...) call returns nil when no record exists for the user/topic combination
• The code immediately accesses tu.notification_level without checking if tu is nil
• This will cause a NoMethodError crash when users click unsubscribe links for topics they've never visited
• Users can receive notifications without having a TopicUser record (e.g., via @mentions)

Agent Prompt
Copy this prompt and use it to remediate the issue with your preferred AI generation tools
## Issue description
The `unsubscribe` action in `topics_controller.rb` calls `TopicUser.find_by(...)` which can return `nil` when no record exists. The code then immediately accesses `tu.notification_level` without null checking, causing a NoMethodError crash.

## Issue Context
- `TopicUser` records are created when users interact with topics (visit, read, etc.)
- Users can receive notifications without having interacted with a topic (e.g., @mentions, category watching)
- Clicking unsubscribe link for such topics crashes the application

## Fix Focus Areas
- app/controllers/topics_controller.rb[105-114]

Add a nil check for `tu` before accessing its properties. If `tu` is nil, use `TopicUser.change` to create a new record with muted notification level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants