Merge branch 'refactor/subscription' into 'develop'
Refactor subscription functionality Closes #1130 See merge request pleroma/pleroma!1664
This commit is contained in:
commit
eb9aa7aa10
14 changed files with 708 additions and 9 deletions
|
|
@ -13,6 +13,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
|||
alias Pleroma.Object.Fetcher
|
||||
alias Pleroma.Pagination
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.SubscriptionNotification
|
||||
alias Pleroma.Upload
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ActivityPub.MRF
|
||||
|
|
@ -151,6 +152,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
|||
BackgroundWorker.enqueue("fetch_data_for_activity", %{"activity_id" => activity.id})
|
||||
|
||||
Notification.create_notifications(activity)
|
||||
SubscriptionNotification.create_notifications(activity)
|
||||
|
||||
participations =
|
||||
activity
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationController do
|
||||
use Pleroma.Web, :controller
|
||||
|
||||
import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
|
||||
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.SubscriptionNotification
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.PleromaAPI.PleromaAPI
|
||||
|
||||
def index(%{assigns: %{user: user}} = conn, params) do
|
||||
notifications =
|
||||
user
|
||||
|> PleromaAPI.get_subscription_notifications(params)
|
||||
|> Enum.map(&build_notification_data/1)
|
||||
|
||||
conn
|
||||
|> add_link_headers(notifications)
|
||||
|> render("index.json", %{notifications: notifications, for: user})
|
||||
end
|
||||
|
||||
def show(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
|
||||
with {:ok, notification} <- SubscriptionNotification.get(user, id) do
|
||||
render(conn, "show.json", %{
|
||||
subscription_notification: build_notification_data(notification),
|
||||
for: user
|
||||
})
|
||||
else
|
||||
{:error, reason} ->
|
||||
conn
|
||||
|> put_status(:forbidden)
|
||||
|> json(%{"error" => reason})
|
||||
end
|
||||
end
|
||||
|
||||
def clear(%{assigns: %{user: user}} = conn, _params) do
|
||||
SubscriptionNotification.clear(user)
|
||||
json(conn, %{})
|
||||
end
|
||||
|
||||
def dismiss(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
|
||||
with {:ok, _notif} <- SubscriptionNotification.dismiss(user, id) do
|
||||
json(conn, %{})
|
||||
else
|
||||
{:error, reason} ->
|
||||
conn
|
||||
|> put_status(:forbidden)
|
||||
|> json(%{"error" => reason})
|
||||
end
|
||||
end
|
||||
|
||||
def destroy_multiple(
|
||||
%{assigns: %{user: user}} = conn,
|
||||
%{"ids" => ids} = _params
|
||||
) do
|
||||
SubscriptionNotification.destroy_multiple(user, ids)
|
||||
json(conn, %{})
|
||||
end
|
||||
|
||||
defp build_notification_data(%{activity: %{data: data}} = notification) do
|
||||
%{
|
||||
notification: notification,
|
||||
actor: User.get_cached_by_ap_id(data["actor"]),
|
||||
parent_activity: Activity.get_create_by_object_ap_id(data["object"])
|
||||
}
|
||||
end
|
||||
end
|
||||
40
lib/pleroma/web/pleroma_api/pleroma_api.ex
Normal file
40
lib/pleroma/web/pleroma_api/pleroma_api.ex
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
defmodule Pleroma.Web.PleromaAPI.PleromaAPI do
|
||||
import Ecto.Query
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.Pagination
|
||||
alias Pleroma.SubscriptionNotification
|
||||
|
||||
def get_subscription_notifications(user, params \\ %{}) do
|
||||
options = cast_params(params)
|
||||
|
||||
user
|
||||
|> SubscriptionNotification.for_user_query(options)
|
||||
|> restrict(:exclude_types, options)
|
||||
|> Pagination.fetch_paginated(params)
|
||||
end
|
||||
|
||||
defp cast_params(params) do
|
||||
param_types = %{
|
||||
exclude_types: {:array, :string},
|
||||
reblogs: :boolean,
|
||||
with_muted: :boolean
|
||||
}
|
||||
|
||||
changeset = cast({%{}, param_types}, params, Map.keys(param_types))
|
||||
changeset.changes
|
||||
end
|
||||
|
||||
defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do
|
||||
ap_types =
|
||||
mastodon_types
|
||||
|> Enum.map(&Activity.from_mastodon_notification_type/1)
|
||||
|> Enum.filter(& &1)
|
||||
|
||||
query
|
||||
|> where([q, a], not fragment("? @> ARRAY[?->>'type']::varchar[]", ^ap_types, a.data))
|
||||
end
|
||||
|
||||
defp restrict(query, _, _), do: query
|
||||
end
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationView do
|
||||
use Pleroma.Web, :view
|
||||
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.Web.CommonAPI
|
||||
alias Pleroma.Web.MastodonAPI.AccountView
|
||||
alias Pleroma.Web.MastodonAPI.StatusView
|
||||
alias Pleroma.Web.PleromaAPI.SubscriptionNotificationView
|
||||
|
||||
def render("index.json", %{notifications: notifications, for: user}) do
|
||||
safe_render_many(notifications, SubscriptionNotificationView, "show.json", %{for: user})
|
||||
end
|
||||
|
||||
def render("show.json", %{
|
||||
subscription_notification: %{
|
||||
notification: %{activity: activity} = notification,
|
||||
actor: actor,
|
||||
parent_activity: parent_activity
|
||||
},
|
||||
for: user
|
||||
}) do
|
||||
mastodon_type = Activity.mastodon_notification_type(activity)
|
||||
|
||||
response = %{
|
||||
id: to_string(notification.id),
|
||||
type: mastodon_type,
|
||||
created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
|
||||
account: AccountView.render("account.json", %{user: actor, for: user})
|
||||
}
|
||||
|
||||
case mastodon_type do
|
||||
"mention" ->
|
||||
response
|
||||
|> Map.merge(%{
|
||||
status: StatusView.render("status.json", %{activity: activity, for: user})
|
||||
})
|
||||
|
||||
"favourite" ->
|
||||
response
|
||||
|> Map.merge(%{
|
||||
status: StatusView.render("status.json", %{activity: parent_activity, for: user})
|
||||
})
|
||||
|
||||
"reblog" ->
|
||||
response
|
||||
|> Map.merge(%{
|
||||
status: StatusView.render("status.json", %{activity: parent_activity, for: user})
|
||||
})
|
||||
|
||||
"follow" ->
|
||||
response
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -9,6 +9,7 @@ defmodule Pleroma.Web.Push.Impl do
|
|||
alias Pleroma.Notification
|
||||
alias Pleroma.Object
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.SubscriptionNotification
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.Metadata.Utils
|
||||
alias Pleroma.Web.Push.Subscription
|
||||
|
|
@ -19,7 +20,7 @@ defmodule Pleroma.Web.Push.Impl do
|
|||
@types ["Create", "Follow", "Announce", "Like"]
|
||||
|
||||
@doc "Performs sending notifications for user subscriptions"
|
||||
@spec perform(Notification.t()) :: list(any) | :error
|
||||
@spec perform(Notification.t() | SubscriptionNotification.t()) :: list(any) | :error
|
||||
def perform(
|
||||
%{
|
||||
activity: %{data: %{"type" => activity_type}, id: activity_id} = activity,
|
||||
|
|
|
|||
|
|
@ -293,6 +293,14 @@ defmodule Pleroma.Web.Router do
|
|||
pipe_through(:oauth_read)
|
||||
get("/conversations/:id/statuses", PleromaAPIController, :conversation_statuses)
|
||||
get("/conversations/:id", PleromaAPIController, :conversation)
|
||||
|
||||
scope "/subscription_notifications" do
|
||||
post("/clear", SubscriptionNotificationController, :clear)
|
||||
post("/dismiss", SubscriptionNotificationController, :dismiss)
|
||||
delete("/destroy_multiple", SubscriptionNotificationController, :destroy_multiple)
|
||||
get("/", SubscriptionNotificationController, :index)
|
||||
get("/:id", SubscriptionNotificationController, :show)
|
||||
end
|
||||
end
|
||||
|
||||
scope [] do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue