common api: implement scrobbling
authorAriadne Conill <ariadne@dereferenced.org>
Sat, 28 Sep 2019 00:24:32 +0000 (00:24 +0000)
committerAriadne Conill <ariadne@dereferenced.org>
Mon, 30 Sep 2019 10:39:17 +0000 (10:39 +0000)
lib/pleroma/web/common_api/common_api.ex
test/web/common_api/common_api_test.exs

index a00e4b0d8c32540dd390e37d94c9a9a7ef6bac59..a040a6ce2bf68604d8da55a09a980b17273cce3d 100644 (file)
@@ -212,6 +212,24 @@ defmodule Pleroma.Web.CommonAPI 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
index f28a6609070c99063bf1ca3189a9f084dbb361a0..0f4a5eb25bb251d7029b8abf61dcd6bb1889c17d 100644 (file)
@@ -510,4 +510,43 @@ defmodule Pleroma.Web.CommonAPITest 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