-
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.
Add support for creating associated conversations with message
- Fix DataToAttributes plug's parse_included fn - Add conversation parts to view and controller - Add conversation channel
- Loading branch information
Showing
20 changed files
with
514 additions
and
31 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
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,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 | ||
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,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 |
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
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
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
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
Oops, something went wrong.