Basic status creation and retrieval.
authorRoger Braun <roger@rogerbraun.net>
Tue, 21 Mar 2017 16:53:20 +0000 (17:53 +0100)
committerRoger Braun <roger@rogerbraun.net>
Tue, 21 Mar 2017 16:53:20 +0000 (17:53 +0100)
14 files changed:
lib/pleroma/user.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/twitter_api/representers/activity_representer.ex [new file with mode: 0644]
lib/pleroma/web/twitter_api/twitter_api.ex [new file with mode: 0644]
priv/repo/migrations/20170321133335_add_following_list_to_users.exs [new file with mode: 0644]
priv/repo/migrations/20170321143152_add_ap_id_to_users.exs [new file with mode: 0644]
test/builders/user_builder.ex [new file with mode: 0644]
test/support/builders/activity_builder.ex [new file with mode: 0644]
test/support/builders/user_builder.ex [new file with mode: 0644]
test/user_test.exs [new file with mode: 0644]
test/web/activity_pub/activity_pub_test.exs
test/web/twitter_api/representers/activity_representer_test.exs [new file with mode: 0644]
test/web/twitter_api/twitter_api_controller_test.exs
test/web/twitter_api/twitter_api_test.exs [new file with mode: 0644]

index 3fb298d0550ae6ed2e2fa6973e0fe639068338dc..6d9fe623d5781505ce5a8d9746f30a2a71f4da7a 100644 (file)
@@ -1,5 +1,6 @@
 defmodule Pleroma.User do
   use Ecto.Schema
+  alias Pleroma.User
 
   schema "users" do
     field :bio, :string
@@ -7,7 +8,22 @@ defmodule Pleroma.User do
     field :name, :string
     field :nickname, :string
     field :password_hash, :string
+    field :following, { :array, :string }, default: []
+    field :ap_id, :string
 
     timestamps()
   end
+
+  def ap_id(%User{nickname: nickname}) do
+    host =
+      Application.get_env(:pleroma, Pleroma.Web.Endpoint)
+      |> Keyword.fetch!(:url)
+      |> Keyword.fetch!(:host)
+
+    "https://#{host}/users/#{nickname}"
+  end
+
+  def ap_followers(%User{} = user) do
+    "#{ap_id(user)}/followers"
+  end
 end
index b70f4dbb1b97536f6d370868a5a1d214825a7778..0317b4cd5000e2d1db826901255c5def340a77c3 100644 (file)
@@ -1,8 +1,16 @@
 defmodule Pleroma.Web.ActivityPub.ActivityPub do
   alias Pleroma.Repo
   alias Pleroma.Activity
+  import Ecto.Query
 
   def insert(map) when is_map(map) do
     Repo.insert(%Activity{data: map})
   end
+
+  def fetch_public_activities do
+    query = from activity in Activity,
+    where: fragment(~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}'), activity.data)
+
+    Repo.all(query)
+  end
 end
diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex
new file mode 100644 (file)
index 0000000..32871c0
--- /dev/null
@@ -0,0 +1,17 @@
+defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
+  use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
+  alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
+
+  def to_map(activity, %{user: user}) do
+    content = get_in(activity.data, ["object", "content"])
+    %{
+      "id" => activity.id,
+      "user" => UserRepresenter.to_map(user),
+      "attentions" => [],
+      "statusnet_html" => content,
+      "text" => content,
+      "is_local" => true,
+      "is_post_verb" => true
+    }
+  end
+end
diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex
new file mode 100644 (file)
index 0000000..16e05de
--- /dev/null
@@ -0,0 +1,33 @@
+defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
+  alias Pleroma.User
+  alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Repo
+  alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
+
+  def create_status(user = %User{}, data = %{}) do
+    activity = %{
+      type: "Create",
+      to: [
+        User.ap_followers(user),
+        "https://www.w3.org/ns/activitystreams#Public"
+      ],
+      actor: User.ap_id(user),
+      object: %{
+        type: "Note",
+        content: data.status
+      }
+    }
+
+    ActivityPub.insert(activity)
+  end
+
+  def fetch_public_statuses do
+    activities = ActivityPub.fetch_public_activities
+
+    Enum.map(activities, fn(activity) ->
+      actor = get_in(activity.data, ["actor"])
+      user = Repo.get_by!(User, ap_id: actor)
+      ActivityRepresenter.to_map(activity, %{user: user})
+    end)
+  end
+end
diff --git a/priv/repo/migrations/20170321133335_add_following_list_to_users.exs b/priv/repo/migrations/20170321133335_add_following_list_to_users.exs
new file mode 100644 (file)
index 0000000..b02c127
--- /dev/null
@@ -0,0 +1,9 @@
+defmodule Pleroma.Repo.Migrations.AddFollowingListToUsers do
+  use Ecto.Migration
+
+  def change do
+    alter table(:users) do
+      add :following, :map
+    end
+  end
+end
diff --git a/priv/repo/migrations/20170321143152_add_ap_id_to_users.exs b/priv/repo/migrations/20170321143152_add_ap_id_to_users.exs
new file mode 100644 (file)
index 0000000..e6f0366
--- /dev/null
@@ -0,0 +1,9 @@
+defmodule Pleroma.Repo.Migrations.AddApIdToUsers do
+  use Ecto.Migration
+
+  def change do
+    alter table(:users) do
+      add :ap_id, :string
+    end
+  end
+end
diff --git a/test/builders/user_builder.ex b/test/builders/user_builder.ex
new file mode 100644 (file)
index 0000000..6a9495e
--- /dev/null
@@ -0,0 +1,17 @@
+defmodule Pleroma.Builders.UserBuilder do
+  alias Pleroma.{User, Repo}
+
+  def build do
+   %User{
+      email: "test@example.org",
+      name: "Test Name",
+      nickname: "testname",
+      password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
+      bio: "A tester.",
+    }
+  end
+
+  def insert do
+    Repo.insert(build())
+  end
+end
diff --git a/test/support/builders/activity_builder.ex b/test/support/builders/activity_builder.ex
new file mode 100644 (file)
index 0000000..0b7f79c
--- /dev/null
@@ -0,0 +1,37 @@
+defmodule Pleroma.Builders.ActivityBuilder do
+  alias Pleroma.Builders.UserBuilder
+  alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.User
+
+  def public_and_non_public do
+    {:ok, user} = UserBuilder.insert
+    public = %{
+      "id" => 1,
+      "actor" => user.ap_id,
+      "to" => ["https://www.w3.org/ns/activitystreams#Public"],
+      "object" => %{
+        "type" => "Note",
+        "content" => "test"
+      }
+    }
+
+    non_public = %{
+      "id" => 2,
+      "actor" => user.ap_id,
+      "to" => [],
+      "object" => %{
+        "type" => "Note",
+        "content" => "test"
+      }
+    }
+
+    {:ok, public} = ActivityPub.insert(public)
+    {:ok, non_public} = ActivityPub.insert(non_public)
+
+    %{
+      public: public,
+      non_public: non_public,
+      user: user
+    }
+  end
+end
diff --git a/test/support/builders/user_builder.ex b/test/support/builders/user_builder.ex
new file mode 100644 (file)
index 0000000..0028d42
--- /dev/null
@@ -0,0 +1,18 @@
+defmodule Pleroma.Builders.UserBuilder do
+  alias Pleroma.{User, Repo}
+
+  def build do
+    %User{
+      email: "test@example.org",
+      name: "Test Name",
+      nickname: "testname",
+      password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
+      bio: "A tester.",
+      ap_id: "some id"
+    }
+  end
+
+  def insert do
+    Repo.insert(build())
+  end
+end
diff --git a/test/user_test.exs b/test/user_test.exs
new file mode 100644 (file)
index 0000000..30e4144
--- /dev/null
@@ -0,0 +1,26 @@
+defmodule Pleroma.UserTest do
+  alias Pleroma.Builders.UserBuilder
+  alias Pleroma.User
+  use Pleroma.DataCase
+
+  test "ap_id returns the activity pub id for the user" do
+    host =
+      Application.get_env(:pleroma, Pleroma.Web.Endpoint)
+      |> Keyword.fetch!(:url)
+      |> Keyword.fetch!(:host)
+
+    user = UserBuilder.build
+
+    expected_ap_id = "https://#{host}/users/#{user.nickname}"
+
+    assert expected_ap_id == User.ap_id(user)
+  end
+
+  test "ap_followers returns the followers collection for the user" do
+    user = UserBuilder.build
+
+    expected_followers_collection = "#{User.ap_id(user)}/followers"
+
+    assert expected_followers_collection == User.ap_followers(user)
+  end
+end
index 1b4b121b3569274d052c3a6e2717d62bac648073..5ea2d40f11c527184869f74ae00e9bd5b03191c3 100644 (file)
@@ -2,6 +2,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
   use Pleroma.DataCase
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Activity
+  alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
 
   describe "insertion" do
     test "inserts a given map into the activity database" do
@@ -14,4 +15,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
   end
 
+  describe "fetch activities" do
+    test "retrieves all public activities" do
+      %{user: user, public: public} = ActivityBuilder.public_and_non_public
+
+      activities = ActivityPub.fetch_public_activities
+      assert length(activities) == 1
+      assert Enum.at(activities, 0) == public
+    end
+  end
 end
diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs
new file mode 100644 (file)
index 0000000..b34d3b7
--- /dev/null
@@ -0,0 +1,39 @@
+defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
+  use Pleroma.DataCase
+  alias Pleroma.{User, Activity}
+  alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
+  alias Pleroma.Builders.UserBuilder
+
+  test "an activity" do
+    {:ok, user} = UserBuilder.insert
+    content = "Some content"
+    activity = %Activity{
+      id: 1,
+      data: %{
+        "type" => "Create",
+        "to" => [
+          User.ap_followers(user),
+          "https://www.w3.org/ns/activitystreams#Public"
+        ],
+        "actor" => User.ap_id(user),
+        "object" => %{
+          "type" => "Note",
+          "content" => content
+        }
+      }
+    }
+
+
+    expected_status = %{
+      "id" => activity.id,
+      "user" => UserRepresenter.to_map(user),
+      "is_local" => true,
+      "attentions" => [],
+      "statusnet_html" => content,
+      "text" => content,
+      "is_post_verb" => true
+    }
+
+    assert ActivityRepresenter.to_map(activity, %{user: user}) == expected_status
+  end
+end
index 118ef932d632d1d8e4436f4c81e0ba4ebca9b16c..81f1e893e08c7d91c45ad0cf4655a7d12beedeeb 100644 (file)
@@ -1,7 +1,7 @@
 defmodule Pleroma.Web.TwitterAPI.ControllerTest do
   use Pleroma.Web.ConnCase
-  alias Pleroma.{User, Repo}
   alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
+  alias Pleroma.Builders.UserBuilder
 
   describe "POST /api/account/verify_credentials" do
     setup [:valid_user]
@@ -20,14 +20,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
   end
 
   defp valid_user(_context) do
-    user = %User{
-      email: "test@example.org",
-      name: "Test Name",
-      nickname: "testname",
-      password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
-      bio: "A tester."
-    }
-    user = Repo.insert!(user)
+    { :ok, user } = UserBuilder.insert
     [user: user]
   end
 
diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs
new file mode 100644 (file)
index 0000000..aecf099
--- /dev/null
@@ -0,0 +1,31 @@
+defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
+  use Pleroma.DataCase
+  alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
+  alias Pleroma.Web.TwitterAPI.TwitterAPI
+  alias Pleroma.{Activity, User}
+  alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
+  alias Pleroma.Web.ActivityPub.ActivityPub
+
+  test "create a status" do
+    user = UserBuilder.build
+    input = %{
+      status: "Hello again."
+    }
+
+    { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
+
+    assert get_in(activity.data, [:object, :content]) == "Hello again."
+    assert get_in(activity.data, [:object, :type]) == "Note"
+    assert get_in(activity.data, [:actor]) == User.ap_id(user)
+    assert Enum.member?(get_in(activity.data, [:to]), User.ap_followers(user))
+    assert Enum.member?(get_in(activity.data, [:to]), "https://www.w3.org/ns/activitystreams#Public")
+  end
+
+  test "fetch public activities" do
+    %{ public: activity, user: user } = ActivityBuilder.public_and_non_public
+    statuses = TwitterAPI.fetch_public_statuses()
+
+    assert length(statuses) == 1
+    assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user})
+  end
+end