From 4fcf5489da05dc5bdb9f356f109464a68027af4a Mon Sep 17 00:00:00 2001 From: Amogh Lele Date: Sat, 28 May 2022 10:58:17 +0530 Subject: [PATCH] fix(pastes): attempt getting a non-split version of the paste id Signed-off-by: Amogh Lele --- lib/ketbin_web/controllers/page_controller.ex | 8 +++++++- lib/ketbin_web/controllers/user_auth.ex | 10 +++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/ketbin_web/controllers/page_controller.ex b/lib/ketbin_web/controllers/page_controller.ex index a042622..315cce6 100644 --- a/lib/ketbin_web/controllers/page_controller.ex +++ b/lib/ketbin_web/controllers/page_controller.ex @@ -16,7 +16,13 @@ defmodule KetbinWeb.PageController do [head | tail] = String.split(id, ".") # fetch paste from db - paste = Pastes.get_paste!(head) + paste = + try do + Pastes.get_paste!(head) + rescue + Ecto.NoResultsError -> + Pastes.get_paste!(id) + end # paste is a url, redirect # regular paste, show content diff --git a/lib/ketbin_web/controllers/user_auth.ex b/lib/ketbin_web/controllers/user_auth.ex index 748265e..dae64e4 100644 --- a/lib/ketbin_web/controllers/user_auth.ex +++ b/lib/ketbin_web/controllers/user_auth.ex @@ -98,7 +98,15 @@ defmodule KetbinWeb.UserAuth do def owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do [head | _tail] = String.split(id, ".") - paste = Pastes.get_paste!(head) + + paste = + try do + Pastes.get_paste!(head) + rescue + Ecto.NoResultsError -> + Pastes.get_paste!(id) + end + assign(conn, :show_edit, (user && user.id == paste.belongs_to) || false) end