-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1299 from code-corps/1291-conversation-part-contr…
…oller Add conversation part controller
- Loading branch information
Showing
15 changed files
with
673 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule CodeCorps.Messages.ConversationParts do | ||
@moduledoc ~S""" | ||
An individual part of a conversation in a `CodeCorps.Conversation` thread, | ||
i.e. a reply to the `CodeCorps.Conversation` by any participant. | ||
""" | ||
|
||
import Ecto.Changeset, only: [assoc_constraint: 2, cast: 3, validate_required: 2] | ||
|
||
alias CodeCorps.{ | ||
ConversationPart, | ||
Repo | ||
} | ||
|
||
@spec create(map) :: ConversationPart.t | Ecto.Changeset.t | ||
def create(attrs) do | ||
%ConversationPart{} |> create_changeset(attrs) |> Repo.insert() | ||
end | ||
|
||
@doc false | ||
@spec create_changeset(ConversationPart.t, map) :: Ecto.Changeset.t | ||
def create_changeset(%ConversationPart{} = conversation_part, attrs) do | ||
conversation_part | ||
|> cast(attrs, [:author_id, :body, :conversation_id, :read_at]) | ||
|> validate_required([:author_id, :body, :conversation_id]) | ||
|> assoc_constraint(:author) | ||
|> assoc_constraint(:conversation) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
defmodule CodeCorps.Policy.ConversationPart do | ||
@moduledoc ~S""" | ||
Handles `CodeCorps.User` authorization of actions on `CodeCorps.Conversation` | ||
records. | ||
""" | ||
|
||
import CodeCorps.Policy.Helpers, | ||
only: [ | ||
administered_by?: 2, get_conversation: 1, get_message: 1, get_project: 1 | ||
] | ||
import Ecto.Query | ||
|
||
alias CodeCorps.{Conversation, ConversationPart, Policy, Repo, User} | ||
|
||
@spec scope(Ecto.Queryable.t, User.t) :: Ecto.Queryable.t | ||
def scope(queryable, %User{admin: true}), do: queryable | ||
def scope(queryable, %User{id: id} = current_user) do | ||
scoped_conversation_ids = | ||
Conversation | ||
|> Policy.Conversation.scope(current_user) | ||
|> select([c], c.id) | ||
|> Repo.all() | ||
|
||
queryable | ||
|> where(author_id: ^id) | ||
|> or_where([cp], cp.conversation_id in ^scoped_conversation_ids) | ||
end | ||
|
||
def create?(%User{} = user, %{"conversation_id" => _} = params) do | ||
authorize(user, params) | ||
end | ||
def create?(_, _), do: false | ||
|
||
def show?(%User{} = user, %ConversationPart{conversation_id: _} = part) do | ||
authorize(user, part) | ||
end | ||
def show?(_, _), do: false | ||
|
||
@spec authorize(User.t, ConversationPart.t | map) :: boolean | ||
defp authorize(%User{} = user, attrs) do | ||
%Conversation{} = conversation = attrs |> get_conversation() | ||
is_target? = conversation |> conversation_target?(user) | ||
|
||
is_admin? = | ||
conversation | ||
|> get_message() | ||
|> get_project() | ||
|> administered_by?(user) | ||
|
||
is_target? or is_admin? | ||
end | ||
|
||
defp conversation_target?(%Conversation{user_id: target_id}, %User{id: user_id}) do | ||
target_id == user_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
lib/code_corps_web/controllers/conversation_part_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule CodeCorpsWeb.ConversationPartController do | ||
@moduledoc false | ||
use CodeCorpsWeb, :controller | ||
|
||
alias CodeCorps.{ | ||
ConversationPart, | ||
Messages, | ||
User | ||
} | ||
|
||
action_fallback CodeCorpsWeb.FallbackController | ||
plug CodeCorpsWeb.Plug.DataToAttributes | ||
plug CodeCorpsWeb.Plug.IdsToIntegers | ||
|
||
@spec index(Conn.t, map) :: Conn.t | ||
def index(%Conn{} = conn, %{} = params) do | ||
with %User{} = current_user <- conn |> CodeCorps.Guardian.Plug.current_resource, | ||
conversation_parts <- ConversationPart |> Policy.scope(current_user) |> Messages.list_parts(params) do | ||
conn |> render("index.json-api", data: conversation_parts) | ||
end | ||
end | ||
|
||
@spec create(Plug.Conn.t, map) :: Conn.t | ||
def create(%Conn{} = conn, %{} = params) do | ||
with %User{} = current_user <- conn |> CodeCorps.Guardian.Plug.current_resource, | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:create, %ConversationPart{}, params), | ||
{:ok, %ConversationPart{} = message} <- Messages.add_part(params), | ||
message <- preload(message) | ||
do | ||
conn |> put_status(:created) |> render("show.json-api", data: message) | ||
end | ||
end | ||
|
||
@spec show(Conn.t, map) :: Conn.t | ||
def show(%Conn{} = conn, %{"id" => id}) do | ||
with %User{} = current_user <- conn |> CodeCorps.Guardian.Plug.current_resource, | ||
%ConversationPart{} = conversation_part <- Messages.get_part(id), | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:show, conversation_part, %{}) do | ||
conn |> render("show.json-api", data: conversation_part) | ||
end | ||
end | ||
|
||
@preloads [:author, :conversation] | ||
|
||
def preload(data) do | ||
Repo.preload(data, @preloads) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule CodeCorpsWeb.ConversationPartView do | ||
@moduledoc false | ||
use CodeCorpsWeb, :view | ||
use JaSerializer.PhoenixView | ||
|
||
attributes [:body, :inserted_at, :read_at, :updated_at] | ||
|
||
has_one :author, type: "user", field: :author_id | ||
has_one :conversation, type: "conversation", field: :conversation_id | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule CodeCorps.Messages.ConversationPartsTest do | ||
use CodeCorps.ModelCase | ||
|
||
alias CodeCorps.{ | ||
ConversationPart, | ||
Messages.ConversationParts, | ||
Repo | ||
} | ||
|
||
@valid_attrs %{ | ||
body: "Test body." | ||
} | ||
|
||
describe "create_changeset/2" do | ||
test "with valid attributes" do | ||
attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1}) | ||
changeset = ConversationParts.create_changeset(%ConversationPart{}, attrs) | ||
assert changeset.valid? | ||
end | ||
|
||
test "requires author_id" do | ||
conversation_id = insert(:conversation).id | ||
|
||
changeset = ConversationParts.create_changeset(%ConversationPart{}, %{conversation_id: conversation_id}) | ||
|
||
refute changeset.valid? | ||
assert_error_message(changeset, :author_id, "can't be blank") | ||
end | ||
|
||
test "requires conversation_id" do | ||
author_id = insert(:user).id | ||
|
||
changeset = ConversationParts.create_changeset(%ConversationPart{}, %{author_id: author_id}) | ||
|
||
refute changeset.valid? | ||
assert_error_message(changeset, :conversation_id, "can't be blank") | ||
end | ||
|
||
test "requires id of actual author" do | ||
author_id = -1 | ||
conversation_id = insert(:conversation).id | ||
attrs = @valid_attrs |> Map.merge(%{author_id: author_id, conversation_id: conversation_id}) | ||
|
||
{result, changeset} = | ||
ConversationParts.create_changeset(%ConversationPart{}, attrs) | ||
|> Repo.insert() | ||
|
||
assert result == :error | ||
refute changeset.valid? | ||
assert_error_message(changeset, :author, "does not exist") | ||
end | ||
|
||
test "requires id of actual conversation" do | ||
author_id = insert(:user).id | ||
conversation_id = -1 | ||
attrs = @valid_attrs |> Map.merge(%{author_id: author_id, conversation_id: conversation_id}) | ||
|
||
{result, changeset} = | ||
ConversationParts.create_changeset(%ConversationPart{}, attrs) | ||
|> Repo.insert() | ||
|
||
assert result == :error | ||
refute changeset.valid? | ||
assert_error_message(changeset, :conversation, "does not exist") | ||
end | ||
end | ||
end |
Oops, something went wrong.