Chat Moderation: use explicit `sender` and `recipient` fields

This commit is contained in:
Alex Gleason 2020-09-10 01:44:32 -05:00
parent 67726453f8
commit e229536e5c
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 48 additions and 8 deletions

View File

@ -1346,7 +1346,12 @@ Loads json generated from `config/descriptions.exs`.
```json ```json
[ [
{ {
"account": { "sender": {
"id": "someflakeid",
"username": "somenick",
...
},
"receiver": {
"id": "someflakeid", "id": "someflakeid",
"username": "somenick", "username": "somenick",
... ...
@ -1369,7 +1374,12 @@ Loads json generated from `config/descriptions.exs`.
```json ```json
{ {
"account": { "sender": {
"id": "someflakeid",
"username": "somenick",
...
},
"receiver": {
"id": "someflakeid", "id": "someflakeid",
"username": "somenick", "username": "somenick",
... ...

View File

@ -21,11 +21,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
alias Pleroma.Web.AdminAPI.ModerationLogView alias Pleroma.Web.AdminAPI.ModerationLogView
alias Pleroma.Web.AdminAPI.Search alias Pleroma.Web.AdminAPI.Search
alias Pleroma.Web.Endpoint alias Pleroma.Web.Endpoint
alias Pleroma.Web.PleromaAPI
alias Pleroma.Web.Router alias Pleroma.Web.Router
require Logger
@users_page_size 50 @users_page_size 50
plug( plug(
@ -270,7 +267,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
|> Pleroma.Repo.all() |> Pleroma.Repo.all()
conn conn
|> put_view(PleromaAPI.ChatView) |> put_view(AdminAPI.ChatView)
|> render("index.json", chats: chats) |> render("index.json", chats: chats)
else else
_ -> {:error, :not_found} _ -> {:error, :not_found}

View File

@ -11,9 +11,9 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
alias Pleroma.ModerationLog alias Pleroma.ModerationLog
alias Pleroma.Pagination alias Pleroma.Pagination
alias Pleroma.Plugs.OAuthScopesPlug alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.Web.AdminAPI
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
require Logger require Logger
@ -78,7 +78,7 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
def show(conn, %{id: id}) do def show(conn, %{id: id}) do
with %Chat{} = chat <- Chat.get_by_id(id) do with %Chat{} = chat <- Chat.get_by_id(id) do
conn conn
|> put_view(ChatView) |> put_view(AdminAPI.ChatView)
|> render("show.json", chat: chat) |> render("show.json", chat: chat)
end end
end end

View File

@ -0,0 +1,30 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.ChatView do
use Pleroma.Web, :view
alias Pleroma.Chat
alias Pleroma.User
alias Pleroma.Web.MastodonAPI
alias Pleroma.Web.PleromaAPI
def render("index.json", %{chats: chats} = opts) do
render_many(chats, __MODULE__, "show.json", Map.delete(opts, :chats))
end
def render("show.json", %{chat: %Chat{user_id: user_id}} = opts) do
user = User.get_by_id(user_id)
sender = MastodonAPI.AccountView.render("show.json", user: user, skip_visibility_check: true)
serialized_chat = PleromaAPI.ChatView.render("show.json", opts)
serialized_chat
|> Map.put(:sender, sender)
|> Map.put(:receiver, serialized_chat[:account])
|> Map.delete(:account)
end
def render(view, opts), do: PleromaAPI.ChatView.render(view, opts)
end

View File

@ -123,6 +123,9 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
assert result["id"] == to_string(chat.id) assert result["id"] == to_string(chat.id)
assert %{} = result["sender"]
assert %{} = result["receiver"]
refute result["account"]
end end
end end
end end