philomena/lib/philomena_web/controllers/conversation_controller.ex

44 lines
1.3 KiB
Elixir
Raw Normal View History

2019-11-16 05:38:42 +01:00
defmodule PhilomenaWeb.ConversationController do
use PhilomenaWeb, :controller
alias Philomena.Conversations.{Conversation, Message}
alias Philomena.Textile.Renderer
alias Philomena.Repo
import Ecto.Query
2019-11-17 19:18:21 +01:00
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
2019-11-16 05:42:36 +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, _params) do
user = conn.assigns.current_user
conversations =
Conversation
|> where([c], (c.from_id == ^user.id and not c.from_hidden) or (c.to_id == ^user.id and not c.to_hidden))
|> order_by(desc: :last_message_at)
|> preload([:to, :from])
|> Repo.paginate(conn.assigns.scrivener)
render(conn, "index.html", conversations: conversations)
end
def show(conn, _params) do
conversation = conn.assigns.conversation
messages =
Message
|> where(conversation_id: ^conversation.id)
|> order_by(asc: :created_at)
|> preload([:from])
|> Repo.paginate(conn.assigns.scrivener)
rendered =
messages.entries
|> Renderer.render_collection()
messages =
%{messages | entries: Enum.zip(messages.entries, rendered)}
2019-11-16 05:41:10 +01:00
render(conn, "show.html", conversation: conversation, messages: messages)
2019-11-16 05:38:42 +01:00
end
end