From fa37acfcc75d069aaaea0b00e619f716e5d6a19a Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 19 Apr 2018 21:46:59 +0300 Subject: [PATCH] TwitterAPI: Add Qvitter notification endpoint. --- lib/pleroma/web/router.ex | 1 + .../web/twitter_api/twitter_api_controller.ex | 11 +- .../twitter_api/views/notification_view.ex | 47 ++++++++ .../twitter_api_controller_test.exs | 32 +++++- .../views/notification_view_test.exs | 100 ++++++++++++++++++ 5 files changed, 188 insertions(+), 3 deletions(-) create mode 100644 lib/pleroma/web/twitter_api/views/notification_view.ex create mode 100644 test/web/twitter_api/views/notification_view_test.exs diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8ee27e63c..8f63fdc70 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -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) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 5f44e46c0..6cf8682b8 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -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 index 000000000..4e1ba0b54 --- /dev/null +++ b/lib/pleroma/web/twitter_api/views/notification_view.ex @@ -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 diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 6084381cb..18a3243f5 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -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 index 000000000..33aaa89e1 --- /dev/null +++ b/test/web/twitter_api/views/notification_view_test.exs @@ -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: "Here's some html") + [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