From 5fd29edac47de145fb7025a99137a69072dca3bb Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Fri, 27 Sep 2019 11:04:52 +0000 Subject: [PATCH 01/16] docs: add scrobble API description --- docs/api/pleroma_api.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/api/pleroma_api.md b/docs/api/pleroma_api.md index ac5489aa3..183cf8a28 100644 --- a/docs/api/pleroma_api.md +++ b/docs/api/pleroma_api.md @@ -439,3 +439,33 @@ The status posting endpoint takes an additional parameter, `in_reply_to_conversa * Params: None * Response: the archive of the pack with a 200 status code, 403 if the pack is not set as shared, 404 if the pack does not exist + +## `GET /api/v1/pleroma/accounts/:uid/now-playing` +### Requests a list of current and recent Listen activities for an account +* Method `GET` +* Authentication: not required +* Params: None +* Response: An array of media metadata entities. +* Example response: +```json +[ + { + "id": "1234", + "title": "Some Title", + "artist": "Some Artist", + "album": "Some Album", + "length": 180000 + } +] +``` + +## `POST /api/v1/pleroma/now-playing` +### Creates a new Listen activity for an account +* Method `POST` +* Authentication: required +* Params: + * `title`: the title of the media playing + * `album`: the album of the media playing [optional] + * `artist`: the artist of the media playing [optional] + * `length`: the length of the media playing [optional] +* Response: the newly created media metadata entity representing the Listen activity From c3d09921e4dd13f02ab141bba9ba8372f70bab76 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Fri, 27 Sep 2019 11:15:20 +0000 Subject: [PATCH 02/16] test: factory: implement support for generating mock audio and listen objects --- test/support/factory.ex | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/support/factory.ex b/test/support/factory.ex index 719115003..4f3244025 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -71,6 +71,47 @@ def note_factory(attrs \\ %{}) do } end + def audio_factory(attrs \\ %{}) do + text = sequence(:text, &"lain radio episode #{&1}") + + user = attrs[:user] || insert(:user) + + data = %{ + "type" => "Audio", + "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(), + "artist" => "lain", + "title" => text, + "album" => "lain radio", + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "published" => DateTime.utc_now() |> DateTime.to_iso8601(), + "actor" => user.ap_id, + "length" => 180_000 + } + + %Pleroma.Object{ + data: merge_attributes(data, Map.get(attrs, :data, %{})) + } + end + + def listen_factory do + audio = insert(:audio) + + data = %{ + "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(), + "type" => "Listen", + "actor" => audio.data["actor"], + "to" => audio.data["to"], + "object" => audio.data, + "published" => audio.data["published"] + } + + %Pleroma.Activity{ + data: data, + actor: data["actor"], + recipients: data["to"] + } + end + def direct_note_factory do user2 = insert(:user) From b7877e9b1c61e42d60bb65deef0cec7f1103dd89 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Fri, 27 Sep 2019 11:40:40 +0000 Subject: [PATCH 03/16] mastodon api: implement rendering of listen activities --- .../web/mastodon_api/views/status_view.ex | 17 +++++++++++++++++ .../web/mastodon_api/views/status_view_test.exs | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 2321d0de2..cf024a83c 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -368,6 +368,23 @@ def render("attachment.json", %{attachment: attachment}) do } end + def render("listen.json", %{activity: %Activity{data: %{"type" => "Listen"}} = activity} = opts) do + object = Object.normalize(activity) + + user = get_user(activity.data["actor"]) + created_at = Utils.to_masto_date(activity.data["published"]) + + %{ + id: activity.id, + account: AccountView.render("account.json", %{user: user, for: opts[:for]}), + created_at: created_at, + title: object.data["title"] |> HTML.strip_tags(), + artist: object.data["artist"] |> HTML.strip_tags(), + album: object.data["album"] |> HTML.strip_tags(), + length: object.data["length"] + } + end + def render("poll.json", %{object: object} = opts) do {multiple, options} = case object.data do diff --git a/test/web/mastodon_api/views/status_view_test.exs b/test/web/mastodon_api/views/status_view_test.exs index c17d0ef95..683132f8d 100644 --- a/test/web/mastodon_api/views/status_view_test.exs +++ b/test/web/mastodon_api/views/status_view_test.exs @@ -608,4 +608,13 @@ test "visibility/list" do assert status.visibility == "list" end + + test "successfully renders a Listen activity (pleroma extension)" do + listen_activity = insert(:listen) + + status = StatusView.render("listen.json", activity: listen_activity) + + assert status.length == listen_activity.data["object"]["length"] + assert status.title == listen_activity.data["object"]["title"] + end end From 1f9de2a8cdc1913b26afab1f914aea526db608d8 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Fri, 27 Sep 2019 12:22:35 +0000 Subject: [PATCH 04/16] activitypub: implement IR-level considerations for Listen activities --- lib/pleroma/web/activity_pub/activity_pub.ex | 20 +++++++++++ lib/pleroma/web/activity_pub/utils.ex | 17 ++++++++- test/web/activity_pub/activity_pub_test.exs | 36 ++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 8d0a57623..425073541 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -248,6 +248,26 @@ def create(%{to: to, actor: actor, context: context, object: object} = params, f end end + def listen(%{to: to, actor: actor, context: context, object: object} = params) do + additional = params[:additional] || %{} + # only accept false as false value + local = !(params[:local] == false) + published = params[:published] + + with listen_data <- + make_listen_data( + %{to: to, actor: actor, published: published, context: context, object: object}, + additional + ), + {:ok, activity} <- insert(listen_data, local), + :ok <- maybe_federate(activity) do + {:ok, activity} + else + {:error, message} -> + {:error, message} + end + end + def accept(%{to: to, actor: actor, object: object} = params) do # only accept false as false value local = !(params[:local] == false) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 30628a793..2ba182f4e 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -20,7 +20,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do require Logger require Pleroma.Constants - @supported_object_types ["Article", "Note", "Video", "Page", "Question", "Answer"] + @supported_object_types ["Article", "Note", "Video", "Page", "Question", "Answer", "Audio"] @supported_report_states ~w(open closed resolved) @valid_visibilities ~w(public unlisted private direct) @@ -581,6 +581,21 @@ def make_create_data(params, additional) do |> Map.merge(additional) end + #### Listen-related helpers + def make_listen_data(params, additional) do + published = params.published || make_date() + + %{ + "type" => "Listen", + "to" => params.to |> Enum.uniq(), + "actor" => params.actor.ap_id, + "object" => params.object, + "published" => published, + "context" => params.context + } + |> Map.merge(additional) + end + #### Flag-related helpers @spec make_flag_data(map(), map()) :: map() def make_flag_data(%{actor: actor, context: context, content: content} = params, additional) do diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index f28fd6871..a203d1d30 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -257,6 +257,42 @@ test "adds an id to a given object if it lacks one and is a note and inserts it end end + describe "listen activities" do + test "does not increase user note count" do + user = insert(:user) + + {:ok, activity} = + ActivityPub.listen(%{ + to: ["https://www.w3.org/ns/activitystreams#Public"], + actor: user, + context: "", + object: %{ + "actor" => user.ap_id, + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "artist" => "lain", + "title" => "lain radio episode 1", + "length" => 180_000, + "type" => "Audio" + } + }) + + assert activity.actor == user.ap_id + + user = User.get_cached_by_id(user.id) + assert user.info.note_count == 0 + end + + test "can be fetched into a timeline" do + _listen_activity_1 = insert(:listen) + _listen_activity_2 = insert(:listen) + _listen_activity_3 = insert(:listen) + + timeline = ActivityPub.fetch_activities([], %{"type" => ["Listen"]}) + + assert length(timeline) == 3 + end + end + describe "create activities" do test "removes doubled 'to' recipients" do user = insert(:user) From 172c74a77baf5b8910987e19c620158d0497d16a Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Fri, 27 Sep 2019 12:40:31 +0000 Subject: [PATCH 05/16] activitypub: transmogrifier: implement support for Listen activities --- .../web/activity_pub/transmogrifier.ex | 33 ++++++++++++++++++- test/web/activity_pub/transmogrifier_test.exs | 29 ++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index dad2fead8..63877248a 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -430,6 +430,36 @@ def handle_incoming( end end + def handle_incoming( + %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data, + options + ) do + actor = Containment.get_actor(data) + + data = + Map.put(data, "actor", actor) + |> fix_addressing + + with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do + options = Keyword.put(options, :depth, (options[:depth] || 0) + 1) + object = fix_object(object, options) + + params = %{ + to: data["to"], + object: object, + actor: user, + context: nil, + local: false, + published: data["published"], + additional: Map.take(data, ["cc", "id"]) + } + + ActivityPub.listen(params) + else + _e -> :error + end + end + def handle_incoming( %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data, _options @@ -765,7 +795,8 @@ def prepare_object(object) do # internal -> Mastodon # """ - def prepare_outgoing(%{"type" => "Create", "object" => object_id} = data) do + def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data) + when activity_type in ["Create", "Listen"] do object = object_id |> Object.normalize() diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index a35db71dc..9040c87ca 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -177,6 +177,35 @@ test "it works for incoming questions" do end) end + test "it works for incoming listens" do + data = %{ + "@context" => "https://www.w3.org/ns/activitystreams", + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "type" => "Listen", + "id" => "http://mastodon.example.org/users/admin/listens/1234/activity", + "actor" => "http://mastodon.example.org/users/admin", + "object" => %{ + "type" => "Audio", + "id" => "http://mastodon.example.org/users/admin/listens/1234", + "attributedTo" => "http://mastodon.example.org/users/admin", + "title" => "lain radio episode 1", + "artist" => "lain", + "album" => "lain radio", + "length" => 180_000 + } + } + + {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data) + + object = Object.normalize(activity) + + assert object.data["title"] == "lain radio episode 1" + assert object.data["artist"] == "lain" + assert object.data["album"] == "lain radio" + assert object.data["length"] == 180_000 + end + test "it rewrites Note votes to Answers and increments vote counters on question activities" do user = insert(:user) From 2c82d8603bb4c3f7281023752dc78aa31a814ab6 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 28 Sep 2019 00:24:32 +0000 Subject: [PATCH 06/16] common api: implement scrobbling --- lib/pleroma/web/common_api/common_api.ex | 18 +++++++++++ test/web/common_api/common_api_test.exs | 39 ++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index a00e4b0d8..a040a6ce2 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -212,6 +212,24 @@ def check_expiry_date(expiry_str) do |> check_expiry_date() end + def listen(user, %{"title" => _} = data) do + with visibility <- data["visibility"] || "public", + {to, cc} <- get_to_and_cc(user, [], nil, visibility, nil), + listen_data <- + Map.take(data, ["album", "artist", "title", "length"]) + |> Map.put("type", "Audio"), + {:ok, activity} <- + ActivityPub.listen(%{ + actor: user, + to: to, + object: listen_data, + context: Utils.generate_context_id(), + additional: %{cc: cc} + }) do + {:ok, activity} + end + end + def post(user, %{"status" => _} = data) do with {:ok, draft} <- Pleroma.Web.CommonAPI.ActivityDraft.create(user, data) do draft.changes diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index f28a66090..0f4a5eb25 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -510,4 +510,43 @@ test "does not allow to vote twice" do assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1]) end end + + describe "listen/2" do + test "returns a valid activity" do + user = insert(:user) + + {:ok, activity} = + CommonAPI.listen(user, %{ + "title" => "lain radio episode 1", + "album" => "lain radio", + "artist" => "lain", + "length" => 180_000 + }) + + object = Object.normalize(activity) + + assert object.data["title"] == "lain radio episode 1" + + assert Visibility.get_visibility(activity) == "public" + end + + test "respects visibility=private" do + user = insert(:user) + + {:ok, activity} = + CommonAPI.listen(user, %{ + "title" => "lain radio episode 1", + "album" => "lain radio", + "artist" => "lain", + "length" => 180_000, + "visibility" => "private" + }) + + object = Object.normalize(activity) + + assert object.data["title"] == "lain radio episode 1" + + assert Visibility.get_visibility(activity) == "private" + end + end end From 7cad6ea67a47df2776a15dd69b9e408c517800e6 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 28 Sep 2019 02:12:12 +0000 Subject: [PATCH 07/16] pleroma api: hook up scrobbler controller --- lib/pleroma/web/activity_pub/activity_pub.ex | 17 +++++ .../web/mastodon_api/views/status_view.ex | 4 ++ .../controllers/pleroma_api_controller.ex | 42 ++++++++++++- lib/pleroma/web/router.ex | 11 ++++ .../controllers/scrobble_controller_test.exs | 63 +++++++++++++++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 test/web/pleroma_api/controllers/scrobble_controller_test.exs diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 425073541..95f994c17 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -608,6 +608,23 @@ defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}, _) do defp restrict_thread_visibility(query, _, _), do: query + def fetch_user_abstract_activities(user, reading_user, params \\ %{}) do + params = + params + |> Map.put("user", reading_user) + |> Map.put("actor_id", user.ap_id) + |> Map.put("whole_db", true) + + recipients = + user_activities_recipients(%{ + "godmode" => params["godmode"], + "reading_user" => reading_user + }) + + fetch_activities(recipients, params) + |> Enum.reverse() + end + def fetch_user_activities(user, reading_user, params \\ %{}) do params = params diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index cf024a83c..d398f7853 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -385,6 +385,10 @@ def render("listen.json", %{activity: %Activity{data: %{"type" => "Listen"}} = a } end + def render("listens.json", opts) do + safe_render_many(opts.activities, StatusView, "listen.json", opts) + end + def render("poll.json", %{object: object} = opts) do {multiple, options} = case object.data do diff --git a/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex index d17ccf84d..1b0ed1f40 100644 --- a/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex @@ -5,11 +5,13 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do use Pleroma.Web, :controller - import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2] + import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2, fetch_integer_param: 2] alias Pleroma.Conversation.Participation alias Pleroma.Notification + alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.CommonAPI alias Pleroma.Web.MastodonAPI.ConversationView alias Pleroma.Web.MastodonAPI.NotificationView alias Pleroma.Web.MastodonAPI.StatusView @@ -86,4 +88,42 @@ def read_notification(%{assigns: %{user: user}} = conn, %{"max_id" => max_id}) d |> render("index.json", %{notifications: notifications, for: user}) end end + + def update_now_playing(%{assigns: %{user: user}} = conn, %{"title" => _} = params) do + params = + if !params["length"] do + params + else + params + |> Map.put("length", fetch_integer_param(params, "length")) + end + + with {:ok, activity} <- CommonAPI.listen(user, params) do + conn + |> put_view(StatusView) + |> render("listen.json", %{activity: activity, for: user}) + else + {:error, message} -> + conn + |> put_status(:bad_request) + |> json(%{"error" => message}) + end + end + + def user_now_playing(%{assigns: %{user: reading_user}} = conn, params) do + with %User{} = user <- User.get_cached_by_nickname_or_id(params["id"], for: reading_user) do + params = Map.put(params, "type", ["Listen"]) + + activities = ActivityPub.fetch_user_abstract_activities(user, reading_user, params) + + conn + |> add_link_headers(activities) + |> put_view(StatusView) + |> render("listens.json", %{ + activities: activities, + for: reading_user, + as: :activity + }) + end + end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 805bef16f..bd5f02af1 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -300,6 +300,17 @@ defmodule Pleroma.Web.Router do patch("/conversations/:id", PleromaAPIController, :update_conversation) post("/notifications/read", PleromaAPIController, :read_notification) end + + scope [] do + pipe_through(:oauth_write) + post("/now-playing", PleromaAPIController, :update_now_playing) + end + end + + scope "/api/v1/pleroma", Pleroma.Web.PleromaAPI do + pipe_through([:api, :oauth_read_or_public]) + + get("/accounts/:id/now-playing", PleromaAPIController, :user_now_playing) end scope "/api/v1", Pleroma.Web.MastodonAPI do diff --git a/test/web/pleroma_api/controllers/scrobble_controller_test.exs b/test/web/pleroma_api/controllers/scrobble_controller_test.exs new file mode 100644 index 000000000..8cbb5889e --- /dev/null +++ b/test/web/pleroma_api/controllers/scrobble_controller_test.exs @@ -0,0 +1,63 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.PleromaAPI.ScrobbleControllerTest do + use Pleroma.Web.ConnCase + + alias Pleroma.Web.CommonAPI + import Pleroma.Factory + + describe "POST /api/v1/pleroma/now-playing" do + test "works correctly", %{conn: conn} do + user = insert(:user) + + conn = + conn + |> assign(:user, user) + |> post("/api/v1/pleroma/now-playing", %{ + "title" => "lain radio episode 1", + "artist" => "lain", + "album" => "lain radio", + "length" => "180000" + }) + + assert %{"title" => "lain radio episode 1"} = json_response(conn, 200) + end + end + + describe "GET /api/v1/pleroma/accounts/:id/now-playing" do + test "works correctly", %{conn: conn} do + user = insert(:user) + + {:ok, _activity} = + CommonAPI.listen(user, %{ + "title" => "lain radio episode 1", + "artist" => "lain", + "album" => "lain radio" + }) + + {:ok, _activity} = + CommonAPI.listen(user, %{ + "title" => "lain radio episode 2", + "artist" => "lain", + "album" => "lain radio" + }) + + {:ok, _activity} = + CommonAPI.listen(user, %{ + "title" => "lain radio episode 3", + "artist" => "lain", + "album" => "lain radio" + }) + + conn = + conn + |> get("/api/v1/pleroma/accounts/#{user.id}/now-playing") + + result = json_response(conn, 200) + + assert length(result) == 3 + end + end +end From 53506da414f6377b6c7afdb686f3d25e55d29c05 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 28 Sep 2019 02:13:26 +0000 Subject: [PATCH 08/16] update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61323970a..80d5e1ac9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Refreshing poll results for remote polls - Admin API: Add ability to require password reset +- Mastodon API: Account entities now include `follow_requests_count` (planned Mastodon 3.x addition) +- Pleroma API: `GET /api/v1/pleroma/accounts/:id/now-playing` to get a list of recently scrobbled items +- Pleroma API: `POST /api/v1/pleroma/now-playing` to scrobble a media item ### Changed - **Breaking:** Elixir >=1.8 is now required (was >= 1.7) From e7309d3b606f4ede3282cf559b30ba23f62cbea5 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 28 Sep 2019 11:57:24 +0000 Subject: [PATCH 09/16] test: transmogrifier: add test proving that transmogrifier can handle outgoing listens --- test/web/activity_pub/transmogrifier_test.exs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 9040c87ca..f77311b3c 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -1219,6 +1219,14 @@ test "it strips BCC field" do assert is_nil(modified["bcc"]) end + + test "it can handle Listen activities" do + listen_activity = insert(:listen) + + {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data) + + assert modified["type"] == "Listen" + end end describe "user upgrade" do From 71eff09e564ae3eeaf02acecbb8d89b7d4e2e511 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 28 Sep 2019 12:12:35 +0000 Subject: [PATCH 10/16] common api: make sure the generated IR is actually federatable --- lib/pleroma/web/common_api/common_api.ex | 6 ++++-- test/web/activity_pub/transmogrifier_test.exs | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index a040a6ce2..b02c47059 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -217,14 +217,16 @@ def listen(user, %{"title" => _} = data) do {to, cc} <- get_to_and_cc(user, [], nil, visibility, nil), listen_data <- Map.take(data, ["album", "artist", "title", "length"]) - |> Map.put("type", "Audio"), + |> Map.put("type", "Audio") + |> Map.put("to", to) + |> Map.put("cc", cc), {:ok, activity} <- ActivityPub.listen(%{ actor: user, to: to, object: listen_data, context: Utils.generate_context_id(), - additional: %{cc: cc} + additional: %{"cc" => cc} }) do {:ok, activity} end diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index f77311b3c..2c6357fe6 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -1226,6 +1226,12 @@ test "it can handle Listen activities" do {:ok, modified} = Transmogrifier.prepare_outgoing(listen_activity.data) assert modified["type"] == "Listen" + + user = insert(:user) + + {:ok, activity} = CommonAPI.listen(user, %{"title" => "lain radio episode 1"}) + + {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data) end end From 84712c35f9b316b0891edfa791aeb5e358613bd2 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 28 Sep 2019 12:28:39 +0000 Subject: [PATCH 11/16] activitypub: object view: include child object for Listen activities --- lib/pleroma/web/activity_pub/views/object_view.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/views/object_view.ex b/lib/pleroma/web/activity_pub/views/object_view.ex index 0d63f0707..88c55acdd 100644 --- a/lib/pleroma/web/activity_pub/views/object_view.ex +++ b/lib/pleroma/web/activity_pub/views/object_view.ex @@ -15,7 +15,8 @@ def render("object.json", %{object: %Object{} = object}) do Map.merge(base, additional) end - def render("object.json", %{object: %Activity{data: %{"type" => "Create"}} = activity}) do + def render("object.json", %{object: %Activity{data: %{"type" => activity_type}} = activity}) + when activity_type in ["Create", "Listen"] do base = Pleroma.Web.ActivityPub.Utils.make_json_ld_header() object = Object.normalize(activity) From 8b34b221cbec366e0a605b9e64dafceb76ed3fd3 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 28 Sep 2019 12:29:00 +0000 Subject: [PATCH 12/16] common api: add some missing IR bits for listen activities' children --- lib/pleroma/web/common_api/common_api.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index b02c47059..2ec017ff8 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -219,7 +219,8 @@ def listen(user, %{"title" => _} = data) do Map.take(data, ["album", "artist", "title", "length"]) |> Map.put("type", "Audio") |> Map.put("to", to) - |> Map.put("cc", cc), + |> Map.put("cc", cc) + |> Map.put("actor", user.ap_id), {:ok, activity} <- ActivityPub.listen(%{ actor: user, From a6e1469767cd716eccf1106e3704130a4fc909b8 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sun, 29 Sep 2019 00:18:06 +0000 Subject: [PATCH 13/16] router: change scrobble timeline route from now-playing to scrobbles --- docs/api/pleroma_api.md | 6 ++++-- .../web/pleroma_api/controllers/pleroma_api_controller.ex | 2 +- lib/pleroma/web/router.ex | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/api/pleroma_api.md b/docs/api/pleroma_api.md index 183cf8a28..33116b4b9 100644 --- a/docs/api/pleroma_api.md +++ b/docs/api/pleroma_api.md @@ -440,7 +440,7 @@ The status posting endpoint takes an additional parameter, `in_reply_to_conversa * Response: the archive of the pack with a 200 status code, 403 if the pack is not set as shared, 404 if the pack does not exist -## `GET /api/v1/pleroma/accounts/:uid/now-playing` +## `GET /api/v1/pleroma/accounts/:id/scrobbles` ### Requests a list of current and recent Listen activities for an account * Method `GET` * Authentication: not required @@ -450,11 +450,13 @@ The status posting endpoint takes an additional parameter, `in_reply_to_conversa ```json [ { + "account": {...}, "id": "1234", "title": "Some Title", "artist": "Some Artist", "album": "Some Album", - "length": 180000 + "length": 180000, + "created_at": "2019-09-28T12:40:45.000Z" } ] ``` diff --git a/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex index 1b0ed1f40..6010732db 100644 --- a/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex @@ -110,7 +110,7 @@ def update_now_playing(%{assigns: %{user: user}} = conn, %{"title" => _} = param end end - def user_now_playing(%{assigns: %{user: reading_user}} = conn, params) do + def user_scrobbles(%{assigns: %{user: reading_user}} = conn, params) do with %User{} = user <- User.get_cached_by_nickname_or_id(params["id"], for: reading_user) do params = Map.put(params, "type", ["Listen"]) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index bd5f02af1..8966e8cc0 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -310,7 +310,7 @@ defmodule Pleroma.Web.Router do scope "/api/v1/pleroma", Pleroma.Web.PleromaAPI do pipe_through([:api, :oauth_read_or_public]) - get("/accounts/:id/now-playing", PleromaAPIController, :user_now_playing) + get("/accounts/:id/scrobbles", PleromaAPIController, :user_scrobbles) end scope "/api/v1", Pleroma.Web.MastodonAPI do From e653edd182338fa8f4396341cea26cd5568f0107 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sun, 29 Sep 2019 00:25:42 +0000 Subject: [PATCH 14/16] split scrobble functions into their own controller --- .../controllers/pleroma_api_controller.ex | 42 +-------------- .../controllers/scrobble_controller.ex | 52 +++++++++++++++++++ lib/pleroma/web/router.ex | 4 +- 3 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 lib/pleroma/web/pleroma_api/controllers/scrobble_controller.ex diff --git a/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex index 6010732db..d17ccf84d 100644 --- a/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex @@ -5,13 +5,11 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do use Pleroma.Web, :controller - import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2, fetch_integer_param: 2] + import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2] alias Pleroma.Conversation.Participation alias Pleroma.Notification - alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub - alias Pleroma.Web.CommonAPI alias Pleroma.Web.MastodonAPI.ConversationView alias Pleroma.Web.MastodonAPI.NotificationView alias Pleroma.Web.MastodonAPI.StatusView @@ -88,42 +86,4 @@ def read_notification(%{assigns: %{user: user}} = conn, %{"max_id" => max_id}) d |> render("index.json", %{notifications: notifications, for: user}) end end - - def update_now_playing(%{assigns: %{user: user}} = conn, %{"title" => _} = params) do - params = - if !params["length"] do - params - else - params - |> Map.put("length", fetch_integer_param(params, "length")) - end - - with {:ok, activity} <- CommonAPI.listen(user, params) do - conn - |> put_view(StatusView) - |> render("listen.json", %{activity: activity, for: user}) - else - {:error, message} -> - conn - |> put_status(:bad_request) - |> json(%{"error" => message}) - end - end - - def user_scrobbles(%{assigns: %{user: reading_user}} = conn, params) do - with %User{} = user <- User.get_cached_by_nickname_or_id(params["id"], for: reading_user) do - params = Map.put(params, "type", ["Listen"]) - - activities = ActivityPub.fetch_user_abstract_activities(user, reading_user, params) - - conn - |> add_link_headers(activities) - |> put_view(StatusView) - |> render("listens.json", %{ - activities: activities, - for: reading_user, - as: :activity - }) - end - end end diff --git a/lib/pleroma/web/pleroma_api/controllers/scrobble_controller.ex b/lib/pleroma/web/pleroma_api/controllers/scrobble_controller.ex new file mode 100644 index 000000000..ac6cd8edd --- /dev/null +++ b/lib/pleroma/web/pleroma_api/controllers/scrobble_controller.ex @@ -0,0 +1,52 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.PleromaAPI.ScrobbleController do + use Pleroma.Web, :controller + + import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2, fetch_integer_param: 2] + + alias Pleroma.User + alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.CommonAPI + alias Pleroma.Web.MastodonAPI.StatusView + + def update_now_playing(%{assigns: %{user: user}} = conn, %{"title" => _} = params) do + params = + if !params["length"] do + params + else + params + |> Map.put("length", fetch_integer_param(params, "length")) + end + + with {:ok, activity} <- CommonAPI.listen(user, params) do + conn + |> put_view(StatusView) + |> render("listen.json", %{activity: activity, for: user}) + else + {:error, message} -> + conn + |> put_status(:bad_request) + |> json(%{"error" => message}) + end + end + + def user_scrobbles(%{assigns: %{user: reading_user}} = conn, params) do + with %User{} = user <- User.get_cached_by_nickname_or_id(params["id"], for: reading_user) do + params = Map.put(params, "type", ["Listen"]) + + activities = ActivityPub.fetch_user_abstract_activities(user, reading_user, params) + + conn + |> add_link_headers(activities) + |> put_view(StatusView) + |> render("listens.json", %{ + activities: activities, + for: reading_user, + as: :activity + }) + end + end +end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8966e8cc0..8e3a72656 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -303,14 +303,14 @@ defmodule Pleroma.Web.Router do scope [] do pipe_through(:oauth_write) - post("/now-playing", PleromaAPIController, :update_now_playing) + post("/now-playing", ScrobbleController, :update_now_playing) end end scope "/api/v1/pleroma", Pleroma.Web.PleromaAPI do pipe_through([:api, :oauth_read_or_public]) - get("/accounts/:id/scrobbles", PleromaAPIController, :user_scrobbles) + get("/accounts/:id/scrobbles", ScrobbleController, :user_scrobbles) end scope "/api/v1", Pleroma.Web.MastodonAPI do From 211008ae2f1ea97490a0ac70b8c801e58af6834c Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sun, 29 Sep 2019 00:35:40 +0000 Subject: [PATCH 15/16] test: fix scrobble controller tests --- test/web/pleroma_api/controllers/scrobble_controller_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/web/pleroma_api/controllers/scrobble_controller_test.exs b/test/web/pleroma_api/controllers/scrobble_controller_test.exs index 8cbb5889e..b86bd2250 100644 --- a/test/web/pleroma_api/controllers/scrobble_controller_test.exs +++ b/test/web/pleroma_api/controllers/scrobble_controller_test.exs @@ -26,7 +26,7 @@ test "works correctly", %{conn: conn} do end end - describe "GET /api/v1/pleroma/accounts/:id/now-playing" do + describe "GET /api/v1/pleroma/accounts/:id/scrobbles" do test "works correctly", %{conn: conn} do user = insert(:user) @@ -53,7 +53,7 @@ test "works correctly", %{conn: conn} do conn = conn - |> get("/api/v1/pleroma/accounts/#{user.id}/now-playing") + |> get("/api/v1/pleroma/accounts/#{user.id}/scrobbles") result = json_response(conn, 200) From 1d7cbdaf7b2f3ff6576959ed26885d7545f31a14 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sun, 29 Sep 2019 02:18:34 +0000 Subject: [PATCH 16/16] change new scrobble endpoint --- CHANGELOG.md | 4 ++-- docs/api/pleroma_api.md | 2 +- .../web/pleroma_api/controllers/scrobble_controller.ex | 2 +- lib/pleroma/web/router.ex | 2 +- test/web/pleroma_api/controllers/scrobble_controller_test.exs | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80d5e1ac9..3d9424c8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Refreshing poll results for remote polls - Admin API: Add ability to require password reset - Mastodon API: Account entities now include `follow_requests_count` (planned Mastodon 3.x addition) -- Pleroma API: `GET /api/v1/pleroma/accounts/:id/now-playing` to get a list of recently scrobbled items -- Pleroma API: `POST /api/v1/pleroma/now-playing` to scrobble a media item +- Pleroma API: `GET /api/v1/pleroma/accounts/:id/scrobbles` to get a list of recently scrobbled items +- Pleroma API: `POST /api/v1/pleroma/scrobble` to scrobble a media item ### Changed - **Breaking:** Elixir >=1.8 is now required (was >= 1.7) diff --git a/docs/api/pleroma_api.md b/docs/api/pleroma_api.md index 33116b4b9..41889a0ef 100644 --- a/docs/api/pleroma_api.md +++ b/docs/api/pleroma_api.md @@ -461,7 +461,7 @@ The status posting endpoint takes an additional parameter, `in_reply_to_conversa ] ``` -## `POST /api/v1/pleroma/now-playing` +## `POST /api/v1/pleroma/scrobble` ### Creates a new Listen activity for an account * Method `POST` * Authentication: required diff --git a/lib/pleroma/web/pleroma_api/controllers/scrobble_controller.ex b/lib/pleroma/web/pleroma_api/controllers/scrobble_controller.ex index ac6cd8edd..0fb978c5d 100644 --- a/lib/pleroma/web/pleroma_api/controllers/scrobble_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/scrobble_controller.ex @@ -12,7 +12,7 @@ defmodule Pleroma.Web.PleromaAPI.ScrobbleController do alias Pleroma.Web.CommonAPI alias Pleroma.Web.MastodonAPI.StatusView - def update_now_playing(%{assigns: %{user: user}} = conn, %{"title" => _} = params) do + def new_scrobble(%{assigns: %{user: user}} = conn, %{"title" => _} = params) do params = if !params["length"] do params diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8e3a72656..bf32cff1e 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -303,7 +303,7 @@ defmodule Pleroma.Web.Router do scope [] do pipe_through(:oauth_write) - post("/now-playing", ScrobbleController, :update_now_playing) + post("/scrobble", ScrobbleController, :new_scrobble) end end diff --git a/test/web/pleroma_api/controllers/scrobble_controller_test.exs b/test/web/pleroma_api/controllers/scrobble_controller_test.exs index b86bd2250..881f8012c 100644 --- a/test/web/pleroma_api/controllers/scrobble_controller_test.exs +++ b/test/web/pleroma_api/controllers/scrobble_controller_test.exs @@ -8,14 +8,14 @@ defmodule Pleroma.Web.PleromaAPI.ScrobbleControllerTest do alias Pleroma.Web.CommonAPI import Pleroma.Factory - describe "POST /api/v1/pleroma/now-playing" do + describe "POST /api/v1/pleroma/scrobble" do test "works correctly", %{conn: conn} do user = insert(:user) conn = conn |> assign(:user, user) - |> post("/api/v1/pleroma/now-playing", %{ + |> post("/api/v1/pleroma/scrobble", %{ "title" => "lain radio episode 1", "artist" => "lain", "album" => "lain radio",