Skip to content

Commit

Permalink
feat(watch-thread): implement unwatch thread route
Browse files Browse the repository at this point in the history
  • Loading branch information
akinsey committed Jul 31, 2024
1 parent e4eadb3 commit d9939e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/epochtalk_server_web/controllers/thread.ex
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,43 @@ defmodule EpochtalkServerWeb.Controllers.Thread do
end
end

@doc """
Used to unwatch `Thread`
"""
def unwatch(conn, attrs) do
with user <- Guardian.Plug.current_resource(conn),
thread_id <- Validate.cast(attrs, "thread_id", :integer, required: true),
:ok <- ACL.allow!(conn, "watchlist.unwatchThread"),
user_priority <- ACL.get_user_priority(conn),
{:can_read, {:ok, true}} <-
{:can_read, Board.get_read_access_by_thread_id(thread_id, user_priority)},
{:is_active, true} <-
{:is_active, User.is_active?(user.id)},
{1, nil} <- WatchThread.delete(user, thread_id) do
render(conn, :watch, thread: %{thread_id: thread_id, user_id: user.id})
else
{:can_read, {:ok, false}} ->
ErrorHelpers.render_json_error(
conn,
403,
"Unauthorized, you do not have permission to read"
)

{:is_active, false} ->
ErrorHelpers.render_json_error(
conn,
400,
"Account must be active to unwatch thread"
)

{:error, data} ->
ErrorHelpers.render_json_error(conn, 400, data)

_ ->
ErrorHelpers.render_json_error(conn, 400, "Error, cannot unwatch thread")
end
end

@doc """
Used to lock `Thread`
"""
Expand Down
1 change: 1 addition & 0 deletions lib/epochtalk_server_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ defmodule EpochtalkServerWeb.Router do
put "/threads/:thread_id/polls", Poll, :update
post "/threads/:thread_id/polls", Poll, :create
post "/watchlist/threads/:thread_id", Thread, :watch
delete "/watchlist/threads/:thread_id", Thread, :unwatch
get "/posts/draft", PostDraft, :by_user_id
put "/posts/draft", PostDraft, :upsert
post "/posts", Post, :create
Expand Down

0 comments on commit d9939e7

Please sign in to comment.