Merge pull request #8 from derpibooru/ourconvos

Add query to enable 'our conversations' functionality
This commit is contained in:
Luna D 2019-12-21 07:59:02 +01:00 committed by GitHub
commit 6bd3e41cd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,12 +10,27 @@ defmodule PhilomenaWeb.ConversationController do
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
plug :load_and_authorize_resource, model: Conversation, id_field: "slug", only: :show, preload: [:to, :from]
def index(conn, %{"with" => partner}) do
user = conn.assigns.current_user
conversations =
Conversation
|> 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))
|> load_conversations(conn)
end
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))
|> load_conversations(conn)
end
defp load_conversations(queryable, conn) do
conversations =
queryable
|> join(:inner_lateral, [c], _ in fragment("SELECT COUNT(*) FROM messages m WHERE m.conversation_id = ?", c.id))
|> order_by(desc: :last_message_at)
|> preload([:to, :from])