2019-11-18 17:00:08 +01:00
|
|
|
defmodule PhilomenaWeb.Conversation.MessageController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Conversations.{Conversation, Message}
|
|
|
|
alias Philomena.Conversations
|
|
|
|
alias Philomena.Repo
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
2022-03-24 17:31:57 +01:00
|
|
|
{:ok, %{message: message}} ->
|
|
|
|
if not message.approved do
|
|
|
|
Conversations.report_non_approved(message.conversation_id)
|
|
|
|
end
|
|
|
|
|
2019-11-18 17:00:08 +01:00
|
|
|
count =
|
|
|
|
Message
|
|
|
|
|> where(conversation_id: ^conversation.id)
|
|
|
|
|> Repo.aggregate(:count, :id)
|
|
|
|
|
|
|
|
page =
|
|
|
|
Float.ceil(count / 25)
|
|
|
|
|> round()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Message successfully sent.")
|
|
|
|
|> redirect(to: Routes.conversation_path(conn, :show, conversation, page: page))
|
|
|
|
|
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "There was an error posting your message")
|
2020-09-07 06:28:24 +02:00
|
|
|
|> redirect(to: Routes.conversation_path(conn, :show, conversation))
|
2019-11-18 17:00:08 +01:00
|
|
|
end
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
end
|