Merge branch 'feature/qvitter-notification-endpoint' into 'develop'
TwitterAPI: Add Qvitter notification endpoint. Closes #142 See merge request pleroma/pleroma!123
This commit is contained in:
commit
3f7b02e7c4
@ -212,6 +212,7 @@ def user_fetcher(username) 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)
|
||||
|
@ -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 @@ def mentions_timeline(%{assigns: %{user: user}} = conn, params) 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} ->
|
||||
|
47
lib/pleroma/web/twitter_api/views/notification_view.ex
Normal file
47
lib/pleroma/web/twitter_api/views/notification_view.ex
Normal file
@ -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
|
@ -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 @@ test "with credentials", %{conn: conn, user: current_user} 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]
|
||||
|
||||
|
100
test/web/twitter_api/views/notification_view_test.exs
Normal file
100
test/web/twitter_api/views/notification_view_test.exs
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user