mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-05 00:57:59 +01:00
43 lines
1.2 KiB
Elixir
43 lines
1.2 KiB
Elixir
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
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :show
|
|
|
|
plug :load_and_authorize_resource,
|
|
model: Conversation,
|
|
id_name: "conversation_id",
|
|
id_field: "slug",
|
|
persisted: true
|
|
|
|
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
|
|
{:ok, _result} ->
|
|
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")
|
|
|> redirect(external: conn.assigns.referrer)
|
|
end
|
|
end
|
|
end
|