2019-11-16 05:38:42 +01:00
|
|
|
defmodule PhilomenaWeb.ConversationController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-02 15:55:48 +01:00
|
|
|
alias PhilomenaWeb.NotificationCountPlug
|
2019-11-18 17:00:08 +01:00
|
|
|
alias Philomena.{Conversations, Conversations.Conversation, Conversations.Message}
|
2021-09-29 22:24:38 +02:00
|
|
|
alias PhilomenaWeb.MarkdownRenderer
|
2019-11-16 05:38:42 +01:00
|
|
|
|
2019-11-17 19:18:21 +01:00
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2020-07-21 16:50:33 +02:00
|
|
|
plug PhilomenaWeb.LimitPlug,
|
|
|
|
[time: 60, error: "You may only create a conversation once every minute."]
|
|
|
|
when action in [:create]
|
|
|
|
|
2020-01-11 05:20:19 +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
|
|
|
|
2024-07-15 01:18:25 +02:00
|
|
|
def index(conn, params) do
|
2019-12-21 05:41:21 +01:00
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
conversations =
|
2024-07-15 01:18:25 +02:00
|
|
|
case params do
|
|
|
|
%{"with" => partner_id} ->
|
|
|
|
Conversations.list_conversations_with(partner_id, user, conn.assigns.scrivener)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Conversations.list_conversations(user, conn.assigns.scrivener)
|
|
|
|
end
|
2019-11-16 05:38:42 +01:00
|
|
|
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "index.html", title: "Conversations", conversations: conversations)
|
2019-11-16 05:38:42 +01:00
|
|
|
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 =
|
2024-07-15 01:18:25 +02:00
|
|
|
Conversations.list_messages(
|
|
|
|
conversation,
|
|
|
|
user,
|
|
|
|
&MarkdownRenderer.render_collection(&1, conn),
|
|
|
|
conn.assigns.scrivener
|
|
|
|
)
|
2019-11-18 17:00:08 +01:00
|
|
|
|
2024-07-15 01:18:25 +02:00
|
|
|
changeset = Conversations.change_message(%Message{})
|
|
|
|
Conversations.mark_conversation_read(conversation, user)
|
2019-11-18 17:00:08 +01:00
|
|
|
|
2019-12-02 15:55:48 +01:00
|
|
|
# Update the conversation ticker in the header
|
|
|
|
conn = NotificationCountPlug.call(conn)
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "show.html",
|
|
|
|
title: "Showing Conversation",
|
|
|
|
conversation: conversation,
|
|
|
|
messages: messages,
|
|
|
|
changeset: changeset
|
|
|
|
)
|
2019-11-18 17:00:08 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def new(conn, params) do
|
2024-07-15 01:18:25 +02:00
|
|
|
conversation =
|
2019-11-18 17:00:08 +01:00
|
|
|
%Conversation{recipient: params["recipient"], messages: [%Message{}]}
|
2024-07-15 01:18:25 +02:00
|
|
|
|
|
|
|
changeset = Conversations.change_conversation(conversation)
|
2019-11-18 17:00:08 +01:00
|
|
|
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "new.html", title: "New Conversation", changeset: changeset)
|
2019-11-18 17:00:08 +01:00
|
|
|
end
|
|
|
|
|
2019-12-04 14:27:35 +01:00
|
|
|
def create(conn, %{"conversation" => conversation_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.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/conversations/#{conversation}")
|
2019-11-18 17:00:08 +01:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
2024-07-15 00:56:23 +02:00
|
|
|
render(conn, "new.html", changeset: changeset)
|
2019-11-18 17:00:08 +01:00
|
|
|
end
|
2019-11-16 05:38:42 +01:00
|
|
|
end
|
2019-12-16 20:24:38 +01:00
|
|
|
end
|