Merge branch 'hide-muted-chats' into 'develop'

Hide chats from muted users

Closes #2230

See merge request pleroma/pleroma!3116
This commit is contained in:
lain 2020-11-04 13:48:15 +00:00
commit bc4d9c4ffc
5 changed files with 50 additions and 19 deletions

View file

@ -116,6 +116,10 @@ The modified chat message
This will return a list of chats that you have been involved in, sorted by their This will return a list of chats that you have been involved in, sorted by their
last update (so new chats will be at the top). last update (so new chats will be at the top).
Parameters:
- with_muted: Include chats from muted users (boolean).
Returned data: Returned data:
```json ```json

View file

@ -6,6 +6,7 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
alias OpenApiSpex.Operation alias OpenApiSpex.Operation
alias OpenApiSpex.Schema alias OpenApiSpex.Schema
alias Pleroma.Web.ApiSpec.Schemas.ApiError alias Pleroma.Web.ApiSpec.Schemas.ApiError
alias Pleroma.Web.ApiSpec.Schemas.BooleanLike
alias Pleroma.Web.ApiSpec.Schemas.Chat alias Pleroma.Web.ApiSpec.Schemas.Chat
alias Pleroma.Web.ApiSpec.Schemas.ChatMessage alias Pleroma.Web.ApiSpec.Schemas.ChatMessage
@ -132,7 +133,10 @@ def index_operation do
tags: ["chat"], tags: ["chat"],
summary: "Get a list of chats that you participated in", summary: "Get a list of chats that you participated in",
operationId: "ChatController.index", operationId: "ChatController.index",
parameters: pagination_params(), parameters: [
Operation.parameter(:with_muted, :query, BooleanLike, "Include chats from muted users")
| pagination_params()
],
responses: %{ responses: %{
200 => Operation.response("The chats of the user", "application/json", chats_response()) 200 => Operation.response("The chats of the user", "application/json", chats_response())
}, },

View file

@ -15,7 +15,6 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
alias Pleroma.Web.PleromaAPI.ChatView
alias Pleroma.Web.Plugs.OAuthScopesPlug alias Pleroma.Web.Plugs.OAuthScopesPlug
import Ecto.Query import Ecto.Query
@ -121,9 +120,7 @@ def mark_as_read(
) do ) do
with {:ok, chat} <- Chat.get_by_user_and_id(user, id), with {:ok, chat} <- Chat.get_by_user_and_id(user, id),
{_n, _} <- MessageReference.set_all_seen_for_chat(chat, last_read_id) do {_n, _} <- MessageReference.set_all_seen_for_chat(chat, last_read_id) do
conn render(conn, "show.json", chat: chat)
|> put_view(ChatView)
|> render("show.json", chat: chat)
end end
end end
@ -141,33 +138,30 @@ def messages(%{assigns: %{user: user}} = conn, %{id: id} = params) do
end end
end end
def index(%{assigns: %{user: %{id: user_id} = user}} = conn, _params) do def index(%{assigns: %{user: %{id: user_id} = user}} = conn, params) do
blocked_ap_ids = User.blocked_users_ap_ids(user) exclude_users =
User.blocked_users_ap_ids(user) ++
if params[:with_muted], do: [], else: User.muted_users_ap_ids(user)
chats = chats =
Chat.for_user_query(user_id) user_id
|> where([c], c.recipient not in ^blocked_ap_ids) |> Chat.for_user_query()
|> where([c], c.recipient not in ^exclude_users)
|> Repo.all() |> Repo.all()
conn render(conn, "index.json", chats: chats)
|> put_view(ChatView)
|> render("index.json", chats: chats)
end end
def create(%{assigns: %{user: user}} = conn, %{id: id}) do def create(%{assigns: %{user: user}} = conn, %{id: id}) do
with %User{ap_id: recipient} <- User.get_cached_by_id(id), with %User{ap_id: recipient} <- User.get_cached_by_id(id),
{:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
conn render(conn, "show.json", chat: chat)
|> put_view(ChatView)
|> render("show.json", chat: chat)
end end
end end
def show(%{assigns: %{user: user}} = conn, %{id: id}) do def show(%{assigns: %{user: user}} = conn, %{id: id}) do
with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do
conn render(conn, "show.json", chat: chat)
|> put_view(ChatView)
|> render("show.json", chat: chat)
end end
end end

View file

@ -343,6 +343,35 @@ test "it does not return chats with users you blocked", %{conn: conn, user: user
assert length(result) == 0 assert length(result) == 0
end end
test "it does not return chats with users you muted", %{conn: conn, user: user} do
recipient = insert(:user)
{:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
result =
conn
|> get("/api/v1/pleroma/chats")
|> json_response_and_validate_schema(200)
assert length(result) == 1
User.mute(user, recipient)
result =
conn
|> get("/api/v1/pleroma/chats")
|> json_response_and_validate_schema(200)
assert length(result) == 0
result =
conn
|> get("/api/v1/pleroma/chats?with_muted=true")
|> json_response_and_validate_schema(200)
assert length(result) == 1
end
test "it returns all chats", %{conn: conn, user: user} do test "it returns all chats", %{conn: conn, user: user} do
Enum.each(1..30, fn _ -> Enum.each(1..30, fn _ ->
recipient = insert(:user) recipient = insert(:user)