FIX: raise `Discourse::NotFound` unless the topic is present

FIX: raise Discourse::NotFound unless the topic is present

From 539f1c62528afed30e9ba5c6d792771798c556e5 Mon Sep 17 00:00:00 2001
From: Arpit Jalan <arpit@techapj.com>
Date: Wed, 21 Nov 2018 09:48:38 +0530
Subject: [PATCH] FIX: raise `Discourse::NotFound` unless the topic is present


diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 12c1e99..ae1ecff 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -501,9 +501,11 @@ class TopicsController < ApplicationController
     params.require(:username)
     topic = Topic.find_by(id: params[:topic_id])
     user = User.find_by(username: params[:username])
+
+    raise Discourse::NotFound unless topic
     guardian.ensure_can_remove_allowed_users!(topic, user)
 
-    if topic&.remove_allowed_user(current_user, user)
+    if topic.remove_allowed_user(current_user, user)
       render json: success_json
     else
       render json: failed_json, status: 422

GitHub

2 Likes

Minor optimization here but if the topic isn’t found, we can raise the error immediately without having to find the user which is an extra DB call. Also, it looks like we need to raise an error if user is nil as well?

3 Likes

Thanks for the tips and review. Done in:

3 Likes