Commit 5c8f07f0 authored by lain's avatar lain
Browse files

Fix note counting.

parent 47d883d3
...@@ -411,22 +411,19 @@ def get_follow_requests(%User{} = user) do ...@@ -411,22 +411,19 @@ def get_follow_requests(%User{} = user) do
end end
def increase_note_count(%User{} = user) do def increase_note_count(%User{} = user) do
note_count = (user.info["note_count"] || 0) + 1 info_cng = User.Info.add_to_note_count(user.info, 1)
new_info = Map.put(user.info, "note_count", note_count) cng = change(user)
|> put_embed(:info, info_cng)
cs = info_changeset(user, %{info: new_info})
update_and_set_cache(cs) update_and_set_cache(cng)
end end
def decrease_note_count(%User{} = user) do def decrease_note_count(%User{} = user) do
note_count = user.info["note_count"] || 0 info_cng = User.Info.add_to_note_count(user.info, -1)
note_count = if note_count <= 0, do: 0, else: note_count - 1 cng = change(user)
new_info = Map.put(user.info, "note_count", note_count) |> put_embed(:info, info_cng)
cs = info_changeset(user, %{info: new_info})
update_and_set_cache(cs) update_and_set_cache(cng)
end end
def update_note_count(%User{} = user) do def update_note_count(%User{} = user) do
......
...@@ -24,4 +24,12 @@ def set_activation_status(info, deactivated) do ...@@ -24,4 +24,12 @@ def set_activation_status(info, deactivated) do
|> cast(params, [:deactivated]) |> cast(params, [:deactivated])
|> validate_required([:deactivated]) |> validate_required([:deactivated])
end end
def add_to_note_count(info, number) do
params = %{note_count: Enum.max([0, info.note_count + number])}
info
|> cast(params, [:note_count])
|> validate_required([:note_count])
end
end end
...@@ -322,34 +322,34 @@ test "it increases the info->note_count property" do ...@@ -322,34 +322,34 @@ test "it increases the info->note_count property" do
note = insert(:note) note = insert(:note)
user = User.get_by_ap_id(note.data["actor"]) user = User.get_by_ap_id(note.data["actor"])
assert user.info["note_count"] == nil assert user.info.note_count == 0
{:ok, user} = User.increase_note_count(user) {:ok, user} = User.increase_note_count(user)
assert user.info["note_count"] == 1 assert user.info.note_count == 1
{:ok, user} = User.increase_note_count(user) {:ok, user} = User.increase_note_count(user)
assert user.info["note_count"] == 2 assert user.info.note_count == 2
end end
test "it decreases the info->note_count property" do test "it decreases the info->note_count property" do
note = insert(:note) note = insert(:note)
user = User.get_by_ap_id(note.data["actor"]) user = User.get_by_ap_id(note.data["actor"])
assert user.info["note_count"] == nil assert user.info.note_count == 0
{:ok, user} = User.increase_note_count(user) {:ok, user} = User.increase_note_count(user)
assert user.info["note_count"] == 1 assert user.info.note_count == 1
{:ok, user} = User.decrease_note_count(user) {:ok, user} = User.decrease_note_count(user)
assert user.info["note_count"] == 0 assert user.info.note_count == 0
{:ok, user} = User.decrease_note_count(user) {:ok, user} = User.decrease_note_count(user)
assert user.info["note_count"] == 0 assert user.info.note_count == 0
end end
test "it sets the info->follower_count property" do test "it sets the info->follower_count property" do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment