-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for creation of associated conversations with message via params #1301
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule CodeCorps.Messages.Conversations do | ||
@moduledoc ~S""" | ||
Subcontext aimed at managing `CodeCorps.Conversation` records aimed at a | ||
specific user belonging to a `CodeCorps.Message`. | ||
""" | ||
|
||
alias Ecto.Changeset | ||
|
||
alias CodeCorps.{Conversation} | ||
|
||
@doc false | ||
@spec create_changeset(Conversation.t, map) :: Ecto.Changeset.t | ||
def create_changeset(%Conversation{} = conversation, %{} = attrs) do | ||
conversation | ||
|> Changeset.cast(attrs, [:user_id]) | ||
|> Changeset.validate_required([:user_id]) | ||
|> Changeset.assoc_constraint(:user) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changeset is needed to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need a test. |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,11 @@ defmodule CodeCorps.Messages do | |
def create(%{} = params) do | ||
%Message{} | ||
|> Message.changeset(params) | ||
|> Changeset.cast(params, [:author_id, :project_id]) | ||
|> Changeset.validate_required([:author_id, :project_id]) | ||
|> Changeset.assoc_constraint(:author) | ||
|> Changeset.assoc_constraint(:project) | ||
|> Changeset.cast_assoc(:conversations, with: &Messages.Conversations.create_changeset/2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an argument to be made here that this belongs in its own There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @begedin this is the nested POST, right? I remember doing this once and I had to create a changeset. I think what I did was take the nested ids, pass it through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But that required the nested ids to already exist in the DB. Not sure what the case is here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @snewcomer The ids are not in the database in this case. The child record is created alongside the parent. |
||
|> Repo.insert() | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule CodeCorpsWeb.ConversationChannel do | ||
use Phoenix.Channel | ||
|
||
alias CodeCorps.{Conversation, Policy, Repo, User} | ||
alias Phoenix.Socket | ||
|
||
@spec join(String.t, map, Socket.t) :: {:ok, Socket.t} | {:error, map} | ||
def join("conversation:" <> id, %{}, %Socket{} = socket) do | ||
with %Conversation{} = conversation <- Conversation |> Repo.get(id), | ||
%User{} = current_user <- socket.assigns[:current_user], | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:show, conversation, %{}) do | ||
|
||
{:ok, socket} | ||
else | ||
nil -> {:error, %{reason: "unauthenticated"}} | ||
{:error, :not_authorized} -> {:error, %{reason: "unauthorized"}} | ||
end | ||
end | ||
|
||
def event("new:conversation-part", socket, message) do | ||
broadcast socket, "new:conversation-part", message | ||
{:ok, socket} | ||
end | ||
|
||
def broadcast_new_conversation_part(conversation_part) do | ||
channel = "conversation:#{conversation_part.conversation_id}" | ||
event = "new:conversation-part" | ||
payload = %{ | ||
id: conversation_part.id | ||
} | ||
|
||
CodeCorpsWeb.Endpoint.broadcast(channel, event, payload) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs removed.