adding following_address field to user
authorAlex S <alex.strizhakov@gmail.com>
Wed, 10 Jul 2019 13:01:32 +0000 (16:01 +0300)
committerAlex S <alex.strizhakov@gmail.com>
Wed, 10 Jul 2019 14:42:18 +0000 (17:42 +0300)
lib/pleroma/user.ex
lib/pleroma/web/activity_pub/activity_pub.ex
priv/repo/migrations/20190710115833_add_following_address_to_user.exs [new file with mode: 0644]
priv/repo/migrations/20190710125051_add_following_address_index_to_user.exs [new file with mode: 0644]
test/web/activity_pub/transmogrifier_test.exs

index 034c414bf66aa8da24dca176c9a7b368b56d55b0..81efb4f13d4abb1845f93bec1e8aa66b9be49d6a 100644 (file)
@@ -52,6 +52,7 @@ defmodule Pleroma.User do
     field(:avatar, :map)
     field(:local, :boolean, default: true)
     field(:follower_address, :string)
+    field(:following_address, :string)
     field(:search_rank, :float, virtual: true)
     field(:search_type, :integer, virtual: true)
     field(:tags, {:array, :string}, default: [])
@@ -162,9 +163,10 @@ defmodule Pleroma.User do
 
     if changes.valid? do
       case info_cng.changes[:source_data] do
-        %{"followers" => followers} ->
+        %{"followers" => followers, "following" => following} ->
           changes
           |> put_change(:follower_address, followers)
+          |> put_change(:following_address, following)
 
         _ ->
           followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
@@ -196,7 +198,14 @@ defmodule Pleroma.User do
       |> User.Info.user_upgrade(params[:info])
 
     struct
-    |> cast(params, [:bio, :name, :follower_address, :avatar, :last_refreshed_at])
+    |> cast(params, [
+      :bio,
+      :name,
+      :follower_address,
+      :following_address,
+      :avatar,
+      :last_refreshed_at
+    ])
     |> unique_constraint(:nickname)
     |> validate_format(:nickname, local_nickname_regex())
     |> validate_length(:bio, max: 5000)
@@ -1039,15 +1048,20 @@ defmodule Pleroma.User do
     end
   end
 
+  @spec external_users_query() :: Ecto.Query.t()
+  def external_users_query do
+    User.Query.build(%{
+      external: true,
+      active: true,
+      order_by: :id
+    })
+  end
+
   @spec external_users(keyword()) :: [User.t()]
   def external_users(opts \\ []) do
     query =
-      User.Query.build(%{
-        external: true,
-        active: true,
-        order_by: :id,
-        select: [:id, :ap_id, :info]
-      })
+      external_users_query()
+      |> select([u], struct(u, [:id, :ap_id, :info]))
 
     query =
       if opts[:max_id],
index 41b55bbabcf1ddfd24674a49d13b6b97aaa3939b..a3174a7871fc2f4d8188b26125ccf9ee8a832330 100644 (file)
@@ -994,6 +994,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
       avatar: avatar,
       name: data["name"],
       follower_address: data["followers"],
+      following_address: data["following"],
       bio: data["summary"]
     }
 
diff --git a/priv/repo/migrations/20190710115833_add_following_address_to_user.exs b/priv/repo/migrations/20190710115833_add_following_address_to_user.exs
new file mode 100644 (file)
index 0000000..fe30472
--- /dev/null
@@ -0,0 +1,9 @@
+defmodule Pleroma.Repo.Migrations.AddFollowingAddressToUser do
+  use Ecto.Migration
+
+  def change do
+    alter table(:users) do
+      add(:following_address, :string, unique: true)
+    end
+  end
+end
diff --git a/priv/repo/migrations/20190710125051_add_following_address_index_to_user.exs b/priv/repo/migrations/20190710125051_add_following_address_index_to_user.exs
new file mode 100644 (file)
index 0000000..0cbfb71
--- /dev/null
@@ -0,0 +1,8 @@
+defmodule Pleroma.Repo.Migrations.AddFollowingAddressIndexToUser do
+  use Ecto.Migration
+
+  @disable_ddl_transaction true
+  def change do
+    create(index(:users, [:following_address], concurrently: true))
+  end
+end
index 825e998793cf6170ea99f37d5ea1c8806c3591b8..6d05138fbf3a81667444a1b38f879e66ab3e3fab 100644 (file)
@@ -1121,6 +1121,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
       assert user.info.ap_enabled
       assert user.info.note_count == 1
       assert user.follower_address == "https://niu.moe/users/rye/followers"
+      assert user.following_address == "https://niu.moe/users/rye/following"
 
       user = User.get_cached_by_id(user.id)
       assert user.info.note_count == 1