Skip to content

Commit

Permalink
Merge pull request #101 from epochtalk/watch-thread
Browse files Browse the repository at this point in the history
Watch thread
  • Loading branch information
unenglishable authored Aug 7, 2024
2 parents 36bb0b5 + e84987b commit 5944cda
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/epochtalk_server/models/watch_thread.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule EpochtalkServer.Models.WatchThread do

case Repo.insert(watch_thread_cs) do
{:ok, db_watch_thread} ->
db_watch_thread
{:ok, db_watch_thread}

{:error,
%Ecto.Changeset{
Expand All @@ -75,7 +75,7 @@ defmodule EpochtalkServer.Models.WatchThread do
"""
@spec delete(user :: User.t(), thread_id :: non_neg_integer) ::
{non_neg_integer(), nil | [term()]}
def delete(%User{} = user, thread_id) do
def delete(%{} = user, thread_id) do
query =
from w in WatchThread,
where: w.user_id == ^user.id and w.thread_id == ^thread_id
Expand Down
75 changes: 75 additions & 0 deletions lib/epochtalk_server_web/controllers/thread.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule EpochtalkServerWeb.Controllers.Thread do
alias EpochtalkServer.Models.UserThreadView
alias EpochtalkServer.Models.MetadataThread
alias EpochtalkServer.Models.ModerationLog
alias EpochtalkServer.Models.WatchThread
alias EpochtalkServer.Models.WatchBoard
alias EpochtalkServer.Models.AutoModeration
alias EpochtalkServer.Models.UserActivity
Expand Down Expand Up @@ -222,6 +223,80 @@ defmodule EpochtalkServerWeb.Controllers.Thread do
end
end

@doc """
Used to watch `Thread`
"""
def watch(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.watchThread"),
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)},
{:ok, watch_thread} <- WatchThread.create(user, thread_id) do
render(conn, :watch, thread: watch_thread)
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 watch thread"
)

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

_ ->
ErrorHelpers.render_json_error(conn, 400, "Error, cannot watch thread")
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
13 changes: 13 additions & 0 deletions lib/epochtalk_server_web/json/thread_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ defmodule EpochtalkServerWeb.Controllers.ThreadJSON do
def lock(%{thread: %{thread_id: thread_id, locked: locked}}),
do: %{thread_id: thread_id, locked: locked}

@doc """
Renders watched `Thread`.
iex> thread = %{
iex> thread_id: 2,
iex> user_id: 1
iex> }
iex> EpochtalkServerWeb.Controllers.ThreadJSON.watch(%{thread: thread})
thread
"""
def watch(%{thread: %{thread_id: thread_id, user_id: user_id}}),
do: %{thread_id: thread_id, user_id: user_id}

@doc """
Renders purge `Thread`.
Expand Down
12 changes: 9 additions & 3 deletions lib/epochtalk_server_web/plugs/prepare_parse.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ defmodule EpochtalkServerWeb.Plugs.PrepareParse do
end

defp try_decode(conn, body) do
case Jason.decode(body) do
{:ok, _result} -> update_in(conn.assigns[:raw_body], &[body | &1 || []])
{:error, _reason} -> raise MalformedPayload
%{method: method} = conn

if method in @methods and @env != :test do
case Jason.decode(body) do
{:ok, _result} -> update_in(conn.assigns[:raw_body], &[body | &1 || []])
{:error, _reason} -> raise MalformedPayload
end
else
conn
end
end
end
2 changes: 2 additions & 0 deletions lib/epochtalk_server_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ defmodule EpochtalkServerWeb.Router do
post "/threads/:thread_id/polls/lock", Poll, :lock
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
Loading

0 comments on commit 5944cda

Please sign in to comment.