mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 21:47:59 +01:00
Merge pull request #8 from derpibooru/ourconvos
Add query to enable 'our conversations' functionality
This commit is contained in:
commit
6bd3e41cd0
1 changed files with 15 additions and 0 deletions
|
@ -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])
|
||||
|
|
Loading…
Reference in a new issue