2019-11-16 05:38:42 +01:00
|
|
|
defmodule PhilomenaWeb.ConversationController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-11-18 17:00:08 +01:00
|
|
|
alias Philomena.{Conversations, Conversations.Conversation, Conversations.Message}
|
2019-11-16 05:38:42 +01:00
|
|
|
alias Philomena.Textile.Renderer
|
|
|
|
alias Philomena.Repo
|
|
|
|
import Ecto.Query
|
|
|
|
|
2019-11-17 19:18:21 +01:00
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
|
2019-11-16 05:42:36 +01:00
|
|
|
plug :load_and_authorize_resource, model: Conversation, id_field: "slug", only: :show, preload: [:to, :from]
|
2019-11-16 05:38:42 +01:00
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
conversations =
|
|
|
|
Conversation
|
|
|
|
|> where([c], (c.from_id == ^user.id and not c.from_hidden) or (c.to_id == ^user.id and not c.to_hidden))
|
|
|
|
|> order_by(desc: :last_message_at)
|
|
|
|
|> preload([:to, :from])
|
|
|
|
|> Repo.paginate(conn.assigns.scrivener)
|
|
|
|
|
|
|
|
render(conn, "index.html", conversations: conversations)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, _params) do
|
|
|
|
conversation = conn.assigns.conversation
|
2019-11-18 17:00:08 +01:00
|
|
|
user = conn.assigns.current_user
|
2019-11-16 05:38:42 +01:00
|
|
|
|
|
|
|
messages =
|
|
|
|
Message
|
|
|
|
|> where(conversation_id: ^conversation.id)
|
|
|
|
|> order_by(asc: :created_at)
|
|
|
|
|> preload([:from])
|
|
|
|
|> Repo.paginate(conn.assigns.scrivener)
|
|
|
|
|
|
|
|
rendered =
|
|
|
|
messages.entries
|
2019-12-01 18:11:00 +01:00
|
|
|
|> Renderer.render_collection(conn)
|
2019-11-16 05:38:42 +01:00
|
|
|
|
|
|
|
messages =
|
|
|
|
%{messages | entries: Enum.zip(messages.entries, rendered)}
|
|
|
|
|
2019-11-18 17:00:08 +01:00
|
|
|
changeset =
|
|
|
|
%Message{}
|
|
|
|
|> Conversations.change_message()
|
|
|
|
|
|
|
|
conversation
|
|
|
|
|> Conversations.mark_conversation_read(user)
|
|
|
|
|
|
|
|
render(conn, "show.html", conversation: conversation, messages: messages, changeset: changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
def new(conn, params) do
|
|
|
|
changeset =
|
|
|
|
%Conversation{recipient: params["recipient"], messages: [%Message{}]}
|
|
|
|
|> Conversations.change_conversation()
|
|
|
|
|
|
|
|
render(conn, "new.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Somewhat annoying, cast_assoc has no "limit" validation so we force it
|
|
|
|
# here to require exactly 1
|
2019-11-18 17:25:27 +01:00
|
|
|
def create(conn, %{"conversation" => %{"messages" => %{"0" => message_params} = messages_params} = conversation_params})
|
|
|
|
when %{"0" => message_params} == messages_params
|
|
|
|
do
|
2019-11-18 17:00:08 +01:00
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
case Conversations.create_conversation(user, conversation_params) do
|
|
|
|
{:ok, conversation} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Conversation successfully created.")
|
|
|
|
|> redirect(to: Routes.conversation_path(conn, :show, conversation))
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
conn
|
|
|
|
|> render("new.html", changeset: changeset)
|
|
|
|
end
|
2019-11-16 05:38:42 +01:00
|
|
|
end
|
|
|
|
end
|