2019-11-18 17:00:08 +01:00
|
|
|
defmodule PhilomenaWeb.Conversation.MessageController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2024-07-15 01:18:25 +02:00
|
|
|
alias Philomena.Conversations.Conversation
|
2019-11-18 17:00:08 +01:00
|
|
|
alias Philomena.Conversations
|
|
|
|
|
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug
|
2019-11-18 19:24:02 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :show
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: Conversation,
|
|
|
|
id_name: "conversation_id",
|
|
|
|
id_field: "slug",
|
|
|
|
persisted: true
|
2019-11-18 17:00:08 +01:00
|
|
|
|
2024-07-15 01:18:25 +02:00
|
|
|
@page_size 25
|
|
|
|
|
2019-11-18 17:00:08 +01:00
|
|
|
def create(conn, %{"message" => message_params}) do
|
|
|
|
conversation = conn.assigns.conversation
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
case Conversations.create_message(conversation, user, message_params) do
|
2024-07-15 00:56:23 +02:00
|
|
|
{:ok, _message} ->
|
2024-07-15 01:18:25 +02:00
|
|
|
count = Conversations.count_messages(conversation)
|
|
|
|
page = div(count + @page_size - 1, @page_size)
|
2019-11-18 17:00:08 +01:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Message successfully sent.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/conversations/#{conversation}?#{[page: page]}")
|
2019-11-18 17:00:08 +01:00
|
|
|
|
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "There was an error posting your message")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/conversations/#{conversation}")
|
2019-11-18 17:00:08 +01:00
|
|
|
end
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
end
|