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.follower_address),
- select: count(u.id)
-
%{
following_count: length(user.following),
- note_count: Repo.one(note_count_query),
- follower_count: Repo.one(follower_count_query)
+ note_count: user.info["note_count"] || 0,
+ follower_count: user.info["follower_count"] || 0
}
end
following = [ap_followers | follower.following]
|> Enum.uniq
- follower
+ follower = follower
|> follow_changeset(%{following: following})
|> Repo.update
+
+ {:ok, followed} = update_follower_count(followed)
+
+ follower
end
end
{ :ok, follower } = follower
|> follow_changeset(%{following: following})
|> Repo.update
- { :ok, follower, Utils.fetch_latest_follow(follower, followed)}
+
+ {:ok, followed} = update_follower_count(followed)
+
+ {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
else
{:error, "Not subscribed!"}
end
{:ok, Repo.all(q)}
end
+
+ def update_note_count(%User{} = user) do
+ note_count_query = from a in Object,
+ where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
+ select: count(a.id)
+
+ note_count = Repo.one(note_count_query)
+
+ new_info = Map.put(user.info, "note_count", note_count)
+
+ cs = info_changeset(user, %{info: new_info})
+
+ Repo.update(cs)
+ end
+
+ def update_follower_count(%User{} = user) do
+ follower_count_query = from u in User,
+ where: fragment("? @> ?", u.following, ^user.follower_address),
+ select: count(u.id)
+
+ follower_count = Repo.one(follower_count_query)
+
+ new_info = Map.put(user.info, "follower_count", follower_count)
+
+ cs = info_changeset(user, %{info: new_info})
+
+ Repo.update(cs)
+ end
end
# TODO: Handle this case in make_note_data
note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note)
do
- ActivityPub.create(to, actor, context, note, %{}, date, false)
+ res = ActivityPub.create(to, actor, context, note, %{}, date, false)
+ User.update_note_count(actor)
+ res
else
%Activity{} = activity -> {:ok, activity}
e -> {:error, e}
context <- make_context(inReplyTo),
tags <- Formatter.parse_tags(status),
object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags) do
- ActivityPub.create(to, user, context, object)
+ res = ActivityPub.create(to, user, context, object)
+ User.update_note_count(user)
+ res
end
end
email: sequence(:email, &"user#{&1}@example.com"),
nickname: sequence(:nickname, &"nick#{&1}"),
password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
- bio: sequence(:bio, &"Tester Number #{&1}"),
+ bio: sequence(:bio, &"Tester Number #{&1}")
}
%{ user | ap_id: Pleroma.User.ap_id(user), follower_address: Pleroma.User.ap_followers(user) }
end
user = Repo.get(User, user.id)
+ followed = User.get_by_ap_id(followed.ap_id)
+ assert followed.info["follower_count"] == 1
+
assert user.following == [User.ap_followers(followed)]
end
{:ok, res} = User.get_friends(user)
+ followed_one = User.get_by_ap_id(followed_one.ap_id)
+ followed_two = User.get_by_ap_id(followed_two.ap_id)
assert res == [followed_one, followed_two]
end
end
+
+ describe "updating note and follower count" do
+ test "it sets the info->note_count property" do
+ note = insert(:note)
+
+ user = User.get_by_ap_id(note.data["actor"])
+
+ assert user.info["note_count"] == nil
+
+ {:ok, user} = User.update_note_count(user)
+
+ assert user.info["note_count"] == 1
+ end
+
+ test "it sets the info->follower_count property" do
+ user = insert(:user)
+ follower = insert(:user)
+
+ User.follow(follower, user)
+
+ assert user.info["follower_count"] == nil
+
+ {:ok, user} = User.update_follower_count(user)
+
+ assert user.info["follower_count"] == 1
+ end
+ end
end
incoming = File.read!("test/fixtures/incoming_note_activity.xml")
{:ok, [activity]} = OStatus.handle_incoming(incoming)
+ user = User.get_by_ap_id(activity.data["actor"])
+ assert user.info["note_count"] == 1
assert activity.data["type"] == "Create"
assert activity.data["object"]["type"] == "Note"
assert activity.data["object"]["id"] == "tag:gs.example.org:4040,2017-04-23:noticeId=29:objectType=note"
import Pleroma.Factory
test "create a status" do
- # user = UserBuilder.build(%{ap_id: "142344"})
- user = insert(:user, %{ap_id: "142344"})
+ user = insert(:user)
_mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
object_data = %{
assert is_list(activity.data["object"]["attachment"])
assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
+
+ user = User.get_by_ap_id(user.ap_id)
+
+ assert user.info["note_count"] == 1
end
test "create a status that is a reply" do
- user = UserBuilder.build(%{ap_id: "some_cool_id"})
+ user = insert(:user)
input = %{
"status" => "Hello again."
}
assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"])
assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
- assert Enum.member?(get_in(reply.data, ["to"]), "some_cool_id")
+ assert Enum.member?(get_in(reply.data, ["to"]), user.ap_id)
end
test "fetch public statuses, excluding remote ones." do
{:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
assert user.following == [User.ap_followers(followed)]
+ followed = User.get_by_ap_id(followed.ap_id)
+ assert followed.info["follower_count"] == 1
+
{ :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
assert msg == "Could not follow user: #{followed.nickname} is already on your list."
end
test "A user" do
note_activity = insert(:note_activity)
user = User.get_cached_by_ap_id(note_activity.data["actor"])
+ {:ok, user} = User.update_note_count(user)
follower = insert(:user)
second_follower = insert(:user)
test "A user for a given other follower", %{user: user} do
{:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
+ {:ok, user} = User.update_follower_count(user)
image = "https://placehold.it/48x48"
represented = %{
"id" => user.id,