added unread_notifications_count
for /api/v1/accounts/verify_credentials
This commit is contained in:
parent
8b97b6f5ba
commit
bd261309cc
5 changed files with 55 additions and 4 deletions
|
@ -61,6 +61,7 @@ Has these additional fields under the `pleroma` object:
|
||||||
- `deactivated`: boolean, true when the user is deactivated
|
- `deactivated`: boolean, true when the user is deactivated
|
||||||
- `allow_following_move`: boolean, true when the user allows automatically follow moved following accounts
|
- `allow_following_move`: boolean, true when the user allows automatically follow moved following accounts
|
||||||
- `unread_conversation_count`: The count of unread conversations. Only returned to the account owner.
|
- `unread_conversation_count`: The count of unread conversations. Only returned to the account owner.
|
||||||
|
- `unread_notifications_count`: The count of unread notifications. Only returned to the account owner.
|
||||||
|
|
||||||
### Source
|
### Source
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,14 @@ defmodule Pleroma.Notification do
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec unread_notifications_count(User.t()) :: integer()
|
||||||
|
def unread_notifications_count(%User{id: user_id}) do
|
||||||
|
from(q in __MODULE__,
|
||||||
|
where: q.user_id == ^user_id and q.seen == false
|
||||||
|
)
|
||||||
|
|> Repo.aggregate(:count, :id)
|
||||||
|
end
|
||||||
|
|
||||||
def changeset(%Notification{} = notification, attrs) do
|
def changeset(%Notification{} = notification, attrs) do
|
||||||
notification
|
notification
|
||||||
|> cast(attrs, [:seen])
|
|> cast(attrs, [:seen])
|
||||||
|
|
|
@ -36,9 +36,11 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
end
|
end
|
||||||
|
|
||||||
def render("show.json", %{user: user} = opts) do
|
def render("show.json", %{user: user} = opts) do
|
||||||
if User.visible_for?(user, opts[:for]),
|
if User.visible_for?(user, opts[:for]) do
|
||||||
do: do_render("show.json", opts),
|
do_render("show.json", opts)
|
||||||
else: %{}
|
else
|
||||||
|
%{}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render("mention.json", %{user: user}) do
|
def render("mention.json", %{user: user}) do
|
||||||
|
@ -221,7 +223,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
fields: user.fields,
|
fields: user.fields,
|
||||||
bot: bot,
|
bot: bot,
|
||||||
source: %{
|
source: %{
|
||||||
note: (user.bio || "") |> String.replace(~r(<br */?>), "\n") |> Pleroma.HTML.strip_tags(),
|
note: prepare_user_bio(user),
|
||||||
sensitive: false,
|
sensitive: false,
|
||||||
fields: user.raw_fields,
|
fields: user.raw_fields,
|
||||||
pleroma: %{
|
pleroma: %{
|
||||||
|
@ -253,8 +255,17 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
|> maybe_put_follow_requests_count(user, opts[:for])
|
|> maybe_put_follow_requests_count(user, opts[:for])
|
||||||
|> maybe_put_allow_following_move(user, opts[:for])
|
|> maybe_put_allow_following_move(user, opts[:for])
|
||||||
|> maybe_put_unread_conversation_count(user, opts[:for])
|
|> maybe_put_unread_conversation_count(user, opts[:for])
|
||||||
|
|> maybe_put_unread_notification_count(user, opts[:for])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp prepare_user_bio(%User{bio: ""}), do: ""
|
||||||
|
|
||||||
|
defp prepare_user_bio(%User{bio: bio}) when is_binary(bio) do
|
||||||
|
bio |> String.replace(~r(<br */?>), "\n") |> Pleroma.HTML.strip_tags()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp prepare_user_bio(_), do: ""
|
||||||
|
|
||||||
defp username_from_nickname(string) when is_binary(string) do
|
defp username_from_nickname(string) when is_binary(string) do
|
||||||
hd(String.split(string, "@"))
|
hd(String.split(string, "@"))
|
||||||
end
|
end
|
||||||
|
@ -350,6 +361,16 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
|
|
||||||
defp maybe_put_unread_conversation_count(data, _, _), do: data
|
defp maybe_put_unread_conversation_count(data, _, _), do: data
|
||||||
|
|
||||||
|
defp maybe_put_unread_notification_count(data, %User{id: user_id}, %User{id: user_id} = user) do
|
||||||
|
Kernel.put_in(
|
||||||
|
data,
|
||||||
|
[:pleroma, :unread_notifications_count],
|
||||||
|
Pleroma.Notification.unread_notifications_count(user)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_put_unread_notification_count(data, _, _), do: data
|
||||||
|
|
||||||
defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
|
defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
|
||||||
defp image_url(_), do: nil
|
defp image_url(_), do: nil
|
||||||
end
|
end
|
||||||
|
|
|
@ -1196,12 +1196,15 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|
||||||
describe "verify_credentials" do
|
describe "verify_credentials" do
|
||||||
test "verify_credentials" do
|
test "verify_credentials" do
|
||||||
%{user: user, conn: conn} = oauth_access(["read:accounts"])
|
%{user: user, conn: conn} = oauth_access(["read:accounts"])
|
||||||
|
[notification | _] = insert_list(7, :notification, user: user)
|
||||||
|
Pleroma.Notification.set_read_up_to(user, notification.id)
|
||||||
conn = get(conn, "/api/v1/accounts/verify_credentials")
|
conn = get(conn, "/api/v1/accounts/verify_credentials")
|
||||||
|
|
||||||
response = json_response_and_validate_schema(conn, 200)
|
response = json_response_and_validate_schema(conn, 200)
|
||||||
|
|
||||||
assert %{"id" => id, "source" => %{"privacy" => "public"}} = response
|
assert %{"id" => id, "source" => %{"privacy" => "public"}} = response
|
||||||
assert response["pleroma"]["chat_token"]
|
assert response["pleroma"]["chat_token"]
|
||||||
|
assert response["pleroma"]["unread_notifications_count"] == 6
|
||||||
assert id == to_string(user.id)
|
assert id == to_string(user.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -466,6 +466,24 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
|
||||||
:unread_conversation_count
|
:unread_conversation_count
|
||||||
] == 1
|
] == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "shows unread_count only to the account owner" do
|
||||||
|
user = insert(:user)
|
||||||
|
insert_list(7, :notification, user: user)
|
||||||
|
other_user = insert(:user)
|
||||||
|
|
||||||
|
user = User.get_cached_by_ap_id(user.ap_id)
|
||||||
|
|
||||||
|
assert AccountView.render(
|
||||||
|
"show.json",
|
||||||
|
%{user: user, for: other_user}
|
||||||
|
)[:pleroma][:unread_notifications_count] == nil
|
||||||
|
|
||||||
|
assert AccountView.render(
|
||||||
|
"show.json",
|
||||||
|
%{user: user, for: user}
|
||||||
|
)[:pleroma][:unread_notifications_count] == 7
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "follow requests counter" do
|
describe "follow requests counter" do
|
||||||
|
|
Loading…
Reference in a new issue