aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authoreugenijm <eugenijm@protonmail.com>2019-03-28 14:52:09 +0300
committereugenijm <eugenijm@protonmail.com>2019-03-28 18:55:16 +0300
commitcd90695a349f33b84f287794bae6070e9eec446a (patch)
tree92fc170ff80b7c066f7f795b1a3e91f81279edf2 /lib
parent55d086b52077a220aef60c8d9071aea990431d74 (diff)
downloadpleroma-cd90695a349f33b84f287794bae6070e9eec446a.tar.gz
Add PUT /api/pleroma/notification_settings endpoint
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/notification.ex12
-rw-r--r--lib/pleroma/user.ex8
-rw-r--r--lib/pleroma/user/info.ex13
-rw-r--r--lib/pleroma/web/mastodon_api/views/account_view.ex22
-rw-r--r--lib/pleroma/web/router.ex1
-rw-r--r--lib/pleroma/web/twitter_api/controllers/util_controller.ex6
6 files changed, 48 insertions, 14 deletions
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index caa6b755e..14de1a097 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -163,13 +163,11 @@ defmodule Pleroma.Notification do
User.blocks?(user, %{ap_id: actor})
end
- def skip?(:local, %{local: true}, user) do
- user.info.notification_settings["local"] == false
- end
+ def skip?(:local, %{local: true}, %{info: %{notification_settings: %{"local" => false}}}),
+ do: true
- def skip?(:local, %{local: false}, user) do
- user.info.notification_settings["remote"] == false
- end
+ def skip?(:local, %{local: false}, %{info: %{notification_settings: %{"remote" => false}}}),
+ do: true
def skip?(:muted, activity, user) do
actor = activity.data["actor"]
@@ -194,7 +192,7 @@ defmodule Pleroma.Notification do
User.following?(user, followed)
end
- def skip?(:recently_followed, activity, user) do
+ def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
actor = activity.data["actor"]
Notification.for_user(user)
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index 728b00a56..73c2a82a7 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -1082,6 +1082,14 @@ defmodule Pleroma.User do
update_and_set_cache(cng)
end
+ def update_notification_settings(%User{} = user, settings \\ %{}) do
+ info_changeset = User.Info.update_notification_settings(user.info, settings)
+
+ change(user)
+ |> put_embed(:info, info_changeset)
+ |> update_and_set_cache()
+ end
+
def delete(%User{} = user) do
{:ok, user} = User.deactivate(user)
diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex
index c36efa126..33fd77b02 100644
--- a/lib/pleroma/user/info.ex
+++ b/lib/pleroma/user/info.ex
@@ -61,6 +61,19 @@ defmodule Pleroma.User.Info do
|> validate_required([:deactivated])
end
+ def update_notification_settings(info, settings) do
+ notification_settings =
+ info.notification_settings
+ |> Map.merge(settings)
+ |> Map.take(["remote", "local", "followers", "follows"])
+
+ params = %{notification_settings: notification_settings}
+
+ info
+ |> cast(params, [:notification_settings])
+ |> validate_required([:notification_settings])
+ end
+
def add_to_note_count(info, number) do
set_note_count(info, info.note_count + number)
end
diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex
index b5f3bbb9d..25899e491 100644
--- a/lib/pleroma/web/mastodon_api/views/account_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/account_view.ex
@@ -117,13 +117,15 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
},
# Pleroma extension
- pleroma: %{
- confirmation_pending: user_info.confirmation_pending,
- tags: user.tags,
- is_moderator: user.info.is_moderator,
- is_admin: user.info.is_admin,
- relationship: relationship
- }
+ pleroma:
+ %{
+ confirmation_pending: user_info.confirmation_pending,
+ tags: user.tags,
+ is_moderator: user.info.is_moderator,
+ is_admin: user.info.is_admin,
+ relationship: relationship
+ }
+ |> with_notification_settings(user, opts[:for])
}
end
@@ -132,4 +134,10 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
end
defp username_from_nickname(_), do: nil
+
+ defp with_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
+ Map.put(data, :notification_settings, user.info.notification_settings)
+ end
+
+ defp with_notification_settings(data, _, _), do: data
end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 32e5f7644..36cbf0f57 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -182,6 +182,7 @@ defmodule Pleroma.Web.Router do
post("/change_password", UtilController, :change_password)
post("/delete_account", UtilController, :delete_account)
+ put("/notification_settings", UtilController, :update_notificaton_settings)
end
scope [] do
diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex
index faa733fec..2708299cb 100644
--- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex
+++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex
@@ -269,6 +269,12 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
json(conn, Enum.into(Emoji.get_all(), %{}))
end
+ def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
+ with {:ok, _} <- User.update_notification_settings(user, params) do
+ json(conn, %{status: "success"})
+ end
+ end
+
def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
follow_import(conn, %{"list" => File.read!(listfile.path)})
end