philomena/lib/philomena_web/controllers/conversation_controller.ex

129 lines
3.5 KiB
Elixir
Raw Normal View History

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}
alias PhilomenaWeb.MarkdownRenderer
2019-11-16 05:38:42 +01:00
alias Philomena.Repo
import Ecto.Query
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
def index(conn, %{"with" => partner}) do
user = conn.assigns.current_user
2019-12-21 15:46:58 +01:00
Conversation
2020-01-11 05:20:19 +01:00
|> where(
[c],
(c.from_id == ^user.id and c.to_id == ^partner and not c.from_hidden) or
(c.to_id == ^user.id and c.from_id == ^partner and not c.to_hidden)
)
2019-12-21 15:46:58 +01:00
|> load_conversations(conn)
end
2019-11-16 05:38:42 +01:00
def index(conn, _params) do
user = conn.assigns.current_user
2019-12-21 15:46:58 +01:00
Conversation
2020-01-11 05:20:19 +01:00
|> where(
[c],
(c.from_id == ^user.id and not c.from_hidden) or (c.to_id == ^user.id and not c.to_hidden)
)
2019-12-21 15:46:58 +01:00
|> load_conversations(conn)
end
defp load_conversations(queryable, conn) do
conversations =
queryable
2020-01-11 05:20:19 +01:00
|> join(
:inner_lateral,
[c],
2023-05-18 16:23:17 +02:00
_ in fragment("SELECT COUNT(*) FROM messages m WHERE m.conversation_id = ?", c.id),
on: true
2020-01-11 05:20:19 +01:00
)
2019-11-16 05:38:42 +01:00
|> order_by(desc: :last_message_at)
|> preload([:to, :from])
2019-12-08 00:24:37 +01:00
|> select([c, cnt], {c, cnt.count})
2019-11-16 05:38:42 +01:00
|> Repo.paginate(conn.assigns.scrivener)
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
pref = load_direction(user)
2019-11-16 05:38:42 +01:00
messages =
Message
|> where(conversation_id: ^conversation.id)
|> order_by([{^pref, :created_at}])
2019-11-16 05:38:42 +01:00
|> preload([:from])
|> Repo.paginate(conn.assigns.scrivener)
rendered =
messages.entries
|> MarkdownRenderer.render_collection(conn)
2019-11-16 05:38:42 +01:00
2020-01-11 05:20:19 +01:00
messages = %{messages | entries: Enum.zip(messages.entries, rendered)}
2019-11-16 05:38:42 +01:00
2019-11-18 17:00:08 +01:00
changeset =
%Message{}
|> Conversations.change_message()
conversation
|> Conversations.mark_conversation_read(user)
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
changeset =
%Conversation{recipient: params["recipient"], messages: [%Message{}]}
|> Conversations.change_conversation()
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} ->
2022-03-24 17:31:57 +01:00
if not hd(conversation.messages).approved do
Conversations.report_non_approved(conversation.id)
Conversations.set_as_read(conversation)
end
2019-11-18 17:00:08 +01:00
conn
|> put_flash(:info, "Conversation successfully created.")
2024-06-06 22:28:35 +02:00
|> redirect(to: ~p"/conversations/#{conversation}")
2019-11-18 17:00:08 +01:00
{:error, changeset} ->
conn
|> render("new.html", changeset: changeset)
end
2019-11-16 05:38:42 +01:00
end
defp load_direction(%{messages_newest_first: false}), do: :asc
defp load_direction(_user), do: :desc
2019-12-16 20:24:38 +01:00
end