Add PUT /api/pleroma/notification_settings endpoint
authoreugenijm <eugenijm@protonmail.com>
Thu, 28 Mar 2019 11:52:09 +0000 (14:52 +0300)
committereugenijm <eugenijm@protonmail.com>
Thu, 28 Mar 2019 15:55:16 +0000 (18:55 +0300)
docs/Pleroma-API.md
lib/pleroma/notification.ex
lib/pleroma/user.ex
lib/pleroma/user/info.ex
lib/pleroma/web/mastodon_api/views/account_view.ex
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/controllers/util_controller.ex
test/web/mastodon_api/account_view_test.exs
test/web/twitter_api/util_controller_test.exs

index 478c9d874e371bc5286ce9ad1a1a500e37979933..bb7ed3744d946afaa9b299727594856c7f9d47ab 100644 (file)
@@ -27,14 +27,14 @@ Request parameters can be passed via [query strings](https://en.wikipedia.org/wi
 * Method: `GET`
 * Authentication: not required
 * Params: none
-* Response: Provider specific JSON, the only guaranteed parameter is `type` 
+* Response: Provider specific JSON, the only guaranteed parameter is `type`
 * Example response: `{"type": "kocaptcha", "token": "whatever", "url": "https://captcha.kotobank.ch/endpoint"}`
 
 ## `/api/pleroma/delete_account`
 ### Delete an account
 * Method `POST`
 * Authentication: required
-* Params: 
+* Params:
     * `password`: user's password
 * Response: JSON. Returns `{"status": "success"}` if the deletion was successful, `{"error": "[error message]"}` otherwise
 * Example response: `{"error": "Invalid password."}`
@@ -116,3 +116,13 @@ See [Admin-API](Admin-API.md)
 * Params:
     * `id`: notifications's id
 * Response: JSON. Returns `{"status": "success"}` if the reading was successful, otherwise returns `{"error": "error_msg"}`
+## `/api/pleroma/notification_settings`
+### Updates user notification settings
+* Method `PUT`
+* Authentication: required
+* Params:
+    * `followers`: BOOLEAN field, receives notifications from followers
+    * `follows`: BOOLEAN field, receives notifications from people the user follows
+    * `remote`: BOOLEAN field, receives notifications from people on remote instances
+    * `local`: BOOLEAN field, receives notifications from people on the local instance
+* Response: JSON. Returns `{"status": "success"}` if the update was successful, otherwise returns `{"error": "error_msg"}`
index caa6b755e40027a0737d10e146babc0d6a7738c2..14de1a0970de3d30f7f092675e321c141a73be2c 100644 (file)
@@ -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)
index 728b00a56eaa5c767a91401ce5d2f011b38ea89c..73c2a82a7b64acd12bbf5c4d59d76dd08c6286a2 100644 (file)
@@ -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)
 
index c36efa126e7fd8564f635e8944b069787e362e44..33fd77b02d714c18ed5362930a7304627bd7bbcf 100644 (file)
@@ -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
index b5f3bbb9d0c4a291d67a306430232139faed9f13..25899e491552a2d0220269358d901f7b5c3ef47c 100644 (file)
@@ -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
index 32e5f7644c4023a989857cae030af39bc8bfa3da..36cbf0f572b8eaa7902a6d0bfe4a8125e306396e 100644 (file)
@@ -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
index faa733fec5741f9f873716c8ec52e92ea7db4089..2708299cb3ada01f5189ef9ee3455030efaade13 100644 (file)
@@ -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
index 6dc60afe96280dcc9fa992950eff1e9a42d4efa6..aa6a1e960b75992a9a4c4cdb66f7179e737eece1 100644 (file)
@@ -71,6 +71,20 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
     assert expected == AccountView.render("account.json", %{user: user})
   end
 
+  test "Represent the user account for the account owner" do
+    user = insert(:user)
+
+    notification_settings = %{
+      "remote" => true,
+      "local" => true,
+      "followers" => true,
+      "follows" => true
+    }
+
+    assert %{pleroma: %{notification_settings: ^notification_settings}} =
+             AccountView.render("account.json", %{user: user, for: user})
+  end
+
   test "Represent a Service(bot) account" do
     user =
       insert(:user, %{
index 832fdc09692b118fe71a1e3d94a6e682ae254efb..42650835334930181ad4a060c5fdb2c8449f3cb4 100644 (file)
@@ -3,6 +3,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
 
   alias Pleroma.Notification
   alias Pleroma.Repo
+  alias Pleroma.User
   alias Pleroma.Web.CommonAPI
   import Pleroma.Factory
 
@@ -74,6 +75,26 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
     end
   end
 
+  describe "PUT /api/pleroma/notification_settings" do
+    test "it updates notification settings", %{conn: conn} do
+      user = insert(:user)
+
+      conn
+      |> assign(:user, user)
+      |> put("/api/pleroma/notification_settings", %{
+        "remote" => false,
+        "followers" => false,
+        "bar" => 1
+      })
+      |> json_response(:ok)
+
+      user = Repo.get(User, user.id)
+
+      assert %{"remote" => false, "local" => true, "followers" => false, "follows" => true} ==
+               user.info.notification_settings
+    end
+  end
+
   describe "GET /api/statusnet/config.json" do
     test "returns the state of safe_dm_mentions flag", %{conn: conn} do
       option = Pleroma.Config.get([:instance, :safe_dm_mentions])