TwitterAPI: Add Qvitter notification endpoint.
authoreal <eal@waifu.club>
Thu, 19 Apr 2018 18:46:59 +0000 (21:46 +0300)
committereal <eal@waifu.club>
Thu, 19 Apr 2018 18:46:59 +0000 (21:46 +0300)
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
lib/pleroma/web/twitter_api/views/notification_view.ex [new file with mode: 0644]
test/web/twitter_api/twitter_api_controller_test.exs
test/web/twitter_api/views/notification_view_test.exs [new file with mode: 0644]

index 8ee27e63c08b77c59fc9742b901d56ec08834a96..8f63fdc7072a163c5693b41108974add56b416c9 100644 (file)
@@ -212,6 +212,7 @@ defmodule Pleroma.Web.Router do
     get("/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline)
     get("/statuses/mentions", TwitterAPI.Controller, :mentions_timeline)
     get("/statuses/mentions_timeline", TwitterAPI.Controller, :mentions_timeline)
+    get("/qvitter/statuses/notifications", TwitterAPI.Controller, :notifications)
 
     post("/statuses/update", TwitterAPI.Controller, :status_update)
     post("/statuses/retweet/:id", TwitterAPI.Controller, :retweet)
index 5f44e46c058b097c55230717fc97fa145e3a7fd3..6cf8682b882033147c34d844e4c3d69c3f23b2a4 100644 (file)
@@ -1,8 +1,8 @@
 defmodule Pleroma.Web.TwitterAPI.Controller do
   use Pleroma.Web, :controller
-  alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView}
+  alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView, NotificationView}
   alias Pleroma.Web.CommonAPI
-  alias Pleroma.{Repo, Activity, User}
+  alias Pleroma.{Repo, Activity, User, Notification}
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Ecto.Changeset
 
@@ -119,6 +119,13 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     |> render(ActivityView, "index.json", %{activities: activities, for: user})
   end
 
+  def notifications(%{assigns: %{user: user}} = conn, params) do
+    notifications = Notification.for_user(user, params)
+
+    conn
+    |> render(NotificationView, "notification.json", %{notifications: notifications, for: user})
+  end
+
   def follow(%{assigns: %{user: user}} = conn, params) do
     case TwitterAPI.follow(user, params) do
       {:ok, user, followed, _activity} ->
diff --git a/lib/pleroma/web/twitter_api/views/notification_view.ex b/lib/pleroma/web/twitter_api/views/notification_view.ex
new file mode 100644 (file)
index 0000000..4e1ba0b
--- /dev/null
@@ -0,0 +1,47 @@
+defmodule Pleroma.Web.TwitterAPI.NotificationView do
+  use Pleroma.Web, :view
+  alias Pleroma.{Notification, User}
+  alias Pleroma.Web.CommonAPI.Utils
+  alias Pleroma.Web.MediaProxy
+  alias Pleroma.Web.TwitterAPI.UserView
+  alias Pleroma.Web.TwitterAPI.ActivityView
+
+  defp get_user(ap_id, opts) do
+    cond do
+      user = opts[:users][ap_id] ->
+        user
+
+      String.ends_with?(ap_id, "/followers") ->
+        nil
+
+      ap_id == "https://www.w3.org/ns/activitystreams#Public" ->
+        nil
+
+      true ->
+        User.get_cached_by_ap_id(ap_id)
+    end
+  end
+
+  def render("notification.json", %{notifications: notifications, for: user}) do
+    render_many(notifications, Pleroma.Web.TwitterAPI.NotificationView, "notification.json", for: user)
+  end
+
+  def render("notification.json", %{notification: %Notification{id: id, seen: seen, activity: activity, inserted_at: created_at}, for: user} = opts) do
+    ntype = case activity.data["type"] do
+              "Create" -> "mention"
+              "Like" -> "like"
+              "Announce" -> "repeat"
+              "Follow" -> "follow"
+            end
+    from = get_user(activity.data["actor"], opts)
+
+    %{
+      "id" => id,
+      "ntype" => ntype,
+      "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}),
+      "from_profile" => UserView.render("show.json", %{user: from, for: user}),
+      "is_seen" => (if seen, do: 1, else: 0),
+      "created_at" => created_at |> Utils.format_naive_asctime()
+    }
+  end
+end
index 6084381cb099e4f9cf14731ec3be18e8bb365161..18a3243f5ff0584caca42caf42b8f02334e4f174 100644 (file)
@@ -2,9 +2,10 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
   use Pleroma.Web.ConnCase
   alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
   alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
-  alias Pleroma.{Repo, Activity, User, Object}
+  alias Pleroma.{Repo, Activity, User, Object, Notification}
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.TwitterAPI.UserView
+  alias Pleroma.Web.TwitterAPI.NotificationView
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.TwitterAPI.TwitterAPI
 
@@ -247,6 +248,35 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     end
   end
 
+  describe "GET /api/qvitter/statuses/notifications.json" do
+    setup [:valid_user]
+
+    test "without valid credentials", %{conn: conn} do
+      conn = get(conn, "/api/qvitter/statuses/notifications.json")
+      assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+    end
+
+    test "with credentials", %{conn: conn, user: current_user} do
+      {:ok, activity} =
+        ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
+
+      conn =
+        conn
+        |> with_credentials(current_user.nickname, "test")
+        |> get("/api/qvitter/statuses/notifications.json")
+
+      response = json_response(conn, 200)
+
+      assert length(response) == 1
+
+      assert response ==
+        NotificationView.render(
+          "notification.json",
+          %{notifications: Notification.for_user(current_user), for: current_user}
+        )
+    end
+  end
+
   describe "GET /statuses/user_timeline.json" do
     setup [:valid_user]
 
diff --git a/test/web/twitter_api/views/notification_view_test.exs b/test/web/twitter_api/views/notification_view_test.exs
new file mode 100644 (file)
index 0000000..33aaa89
--- /dev/null
@@ -0,0 +1,100 @@
+defmodule Pleroma.Web.TwitterAPI.NotificationViewTest do
+  use Pleroma.DataCase
+
+  alias Pleroma.{User, Notification}
+  alias Pleroma.Web.TwitterAPI.TwitterAPI
+  alias Pleroma.Web.TwitterAPI.NotificationView
+  alias Pleroma.Web.TwitterAPI.UserView
+  alias Pleroma.Web.TwitterAPI.ActivityView
+  alias Pleroma.Web.CommonAPI.Utils
+  alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Builders.UserBuilder
+
+  import Pleroma.Factory
+
+  setup do
+    user = insert(:user, bio: "<span>Here's some html</span>")
+    [user: user]
+  end
+
+  test "A follow notification" do
+    note_activity = insert(:note_activity)
+    user = User.get_cached_by_ap_id(note_activity.data["actor"])
+    follower = insert(:user)
+
+    {:ok, follower} = User.follow(follower, user)
+    {:ok, activity} = ActivityPub.follow(follower, user)
+    Cachex.set(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
+    [follow_notif] = Notification.for_user(user)
+
+    represented = %{
+      "created_at" => follow_notif.inserted_at |> Utils.format_naive_asctime(),
+      "from_profile" => UserView.render("show.json", %{user: follower, for: user}),
+      "id" => follow_notif.id,
+      "is_seen" => 0,
+      "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}),
+      "ntype" => "follow"
+    }
+
+    assert represented == NotificationView.render("notification.json", %{notification: follow_notif, for: user})
+  end
+
+  test "A mention notification" do
+    user = insert(:user)
+    other_user = insert(:user)
+
+    {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "Päivää, @#{user.nickname}"})
+    [notification] = Notification.for_user(user)
+
+    represented = %{
+      "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
+      "from_profile" => UserView.render("show.json", %{user: other_user, for: user}),
+      "id" => notification.id,
+      "is_seen" => 0,
+      "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}),
+      "ntype" => "mention"
+    }
+
+    assert represented == NotificationView.render("notification.json", %{notification: notification, for: user})
+  end
+
+  test "A retweet notification" do
+    note_activity = insert(:note_activity)
+    user = User.get_cached_by_ap_id(note_activity.data["actor"])
+    repeater = insert(:user)
+
+    {:ok, activity} = TwitterAPI.repeat(repeater, note_activity.id)
+    [notification] = Notification.for_user(user)
+
+    represented = %{
+      "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
+      "from_profile" => UserView.render("show.json", %{user: repeater, for: user}),
+      "id" => notification.id,
+      "is_seen" => 0,
+      "notice" => ActivityView.render("activity.json", %{activity: notification.activity, for: user}),
+      "ntype" => "repeat"
+    }
+
+    assert represented == NotificationView.render("notification.json", %{notification: notification, for: user})
+  end
+
+  test "A like notification" do
+    note_activity = insert(:note_activity)
+    user = User.get_cached_by_ap_id(note_activity.data["actor"])
+    liker = insert(:user)
+
+    {:ok, activity} = TwitterAPI.fav(liker, note_activity.id)
+    [notification] = Notification.for_user(user)
+
+    represented = %{
+      "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
+      "from_profile" => UserView.render("show.json", %{user: liker, for: user}),
+      "id" => notification.id,
+      "is_seen" => 0,
+      "notice" => ActivityView.render("activity.json", %{activity: notification.activity, for: user}),
+      "ntype" => "like"
+    }
+
+    assert represented == NotificationView.render("notification.json", %{notification: notification, for: user})
+  end
+end