Fix specs, add some user info.
authorRoger Braun <roger@rogerbraun.net>
Thu, 20 Apr 2017 22:51:09 +0000 (00:51 +0200)
committerRoger Braun <roger@rogerbraun.net>
Thu, 20 Apr 2017 22:51:09 +0000 (00:51 +0200)
lib/pleroma/user.ex
lib/pleroma/web/twitter_api/representers/user_representer.ex
test/web/twitter_api/representers/user_representer_test.exs
test/web/twitter_api/twitter_api_controller_test.exs

index 86b4b8b5eab301d1428c091472ec28f63de7aca6..3a4dd5d0877b526f4ddd0dacf4bc3819e8df2c04 100644 (file)
@@ -1,7 +1,8 @@
 defmodule Pleroma.User do
   use Ecto.Schema
   import Ecto.Changeset
-  alias Pleroma.{Repo, User}
+  import Ecto.Query
+  alias Pleroma.{Repo, User, Activity, Object}
 
   schema "users" do
     field :bio, :string
@@ -32,6 +33,22 @@ defmodule Pleroma.User do
     |> validate_required([:following])
   end
 
+  def user_info(%User{} = user) do
+    note_count_query = from a in Object,
+      where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
+      select: count(a.id)
+
+    follower_count_query = from u in User,
+      where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
+      select: count(u.id)
+
+    %{
+      following_count: length(user.following),
+      note_count: Repo.one(note_count_query),
+      follower_count: Repo.one(follower_count_query)
+    }
+  end
+
   def register_changeset(struct, params \\ %{}) do
     changeset = struct
     |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
index 2ee4ee25443a00bc2a9d9dd16b8c5d20f5f777e7..f358baf3c6d38b8d119ab370641db3416b12bb4b 100644 (file)
@@ -15,6 +15,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do
       false
     end
 
+    user_info = User.user_info(user)
+
     map = %{
       "id" => user.id,
       "name" => user.name,
@@ -23,9 +25,9 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do
       "following" => following,
       # Fake fields
       "favourites_count" => 0,
-      "statuses_count" => 0,
-      "friends_count" => 0,
-      "followers_count" => 0,
+      "statuses_count" => user_info[:note_count],
+      "friends_count" => user_info[:following_count],
+      "followers_count" => user_info[:follower_count],
       "profile_image_url" => image,
       "profile_image_url_https" => image,
       "profile_image_url_profile_size" => image,
index 913d1322c0b20518e3e64f136937009510623517..1e92c519046391e6dffbaed50e9187a90e1f3a99 100644 (file)
@@ -19,7 +19,18 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
     assert represented["profile_image_url"] == image
   end
 
-  test "A user", %{user: user} do
+  test "A user" do
+    note_activity = insert(:note_activity)
+    user = User.get_cached_by_ap_id(note_activity.data["actor"])
+    follower = insert(:user)
+    second_follower = insert(:user)
+
+    User.follow(follower, user)
+    User.follow(second_follower, user)
+    User.follow(user, follower)
+
+    user = Repo.get!(User, user.id)
+
     image = "https://placehold.it/48x48"
 
     represented = %{
@@ -29,9 +40,9 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
       "description" => user.bio,
       # Fake fields
       "favourites_count" => 0,
-      "statuses_count" => 0,
-      "friends_count" => 0,
-      "followers_count" => 0,
+      "statuses_count" => 1,
+      "friends_count" => 1,
+      "followers_count" => 2,
       "profile_image_url" => image,
       "profile_image_url_https" => image,
       "profile_image_url_profile_size" => image,
@@ -55,7 +66,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
       "favourites_count" => 0,
       "statuses_count" => 0,
       "friends_count" => 0,
-      "followers_count" => 0,
+      "followers_count" => 1,
       "profile_image_url" => image,
       "profile_image_url_https" => image,
       "profile_image_url_profile_size" => image,
index e4ed6cd543c14577ec0be847e300ff207fea5ecb..0761d05669b15a50c38b6f21b812b271a8ac7efc 100644 (file)
@@ -367,7 +367,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
   end
 
   defp valid_user(_context) do
-    { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
+    user = insert(:user)
     [user: user]
   end