From c9f6eb9a419b4c693cdb95c9f9e0fae724d4e415 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Wed, 19 Sep 2018 06:13:18 +0000 Subject: [PATCH 1/3] user: implement dynamic refresh of profiles (gets rid of need for fix_ap_users task) --- lib/pleroma/user.ex | 21 +++++++++++++++++-- ...0919060348_users_add_last_refreshed_at.exs | 9 ++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 priv/repo/migrations/20180919060348_users_add_last_refreshed_at.exs diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 1dad30e87..e3e6aa0d8 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -22,6 +22,7 @@ defmodule Pleroma.User do field(:info, :map, default: %{}) field(:follower_address, :string) field(:search_distance, :float, virtual: true) + field(:last_refreshed_at, :naive_datetime) has_many(:notifications, Notification) timestamps() @@ -112,8 +113,12 @@ def update_changeset(struct, params \\ %{}) do end def upgrade_changeset(struct, params \\ %{}) do + params = + params + |> Map.put(:last_refreshed_at, NaiveDateTime.utc_now()) + struct - |> cast(params, [:bio, :name, :info, :follower_address, :avatar]) + |> cast(params, [:bio, :name, :info, :follower_address, :avatar, :last_refreshed_at]) |> unique_constraint(:nickname) |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/) |> validate_length(:bio, max: 5000) @@ -169,6 +174,16 @@ def register_changeset(struct, params \\ %{}) do end end + def needs_update?(%User{local: true}), do: false + + def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true + + def needs_update?(%User{local: false} = user) do + NaiveDateTime.diff(NaiveDateTime.utc_now(), user.last_refreshed_at) >= 86400 + end + + def needs_update?(_), do: true + def maybe_direct_follow(%User{} = follower, %User{info: info} = followed) do user_config = Application.get_env(:pleroma, :user) deny_follow_blocked = Keyword.get(user_config, :deny_follow_blocked) @@ -655,7 +670,9 @@ def delete(%User{} = user) do end def get_or_fetch_by_ap_id(ap_id) do - if user = get_by_ap_id(ap_id) do + user = get_by_ap_id(ap_id) + + if !is_nil(user) and !User.needs_update?(user) do user else ap_try = ActivityPub.make_user_from_ap_id(ap_id) diff --git a/priv/repo/migrations/20180919060348_users_add_last_refreshed_at.exs b/priv/repo/migrations/20180919060348_users_add_last_refreshed_at.exs new file mode 100644 index 000000000..1942e4e9c --- /dev/null +++ b/priv/repo/migrations/20180919060348_users_add_last_refreshed_at.exs @@ -0,0 +1,9 @@ +defmodule Pleroma.Repo.Migrations.UsersAddLastRefreshedAt do + use Ecto.Migration + + def change do + alter table(:users) do + add :last_refreshed_at, :naive_datetime + end + end +end From 200ab27e7a279555dcd73f59d5242adcc83accc8 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 20 Sep 2018 23:49:08 +0000 Subject: [PATCH 2/3] test: add test proving that users are refreshed when stale --- test/user_test.exs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/user_test.exs b/test/user_test.exs index 5c61b0930..58fe6eeda 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -166,6 +166,25 @@ test "returns nil for nonexistant local user" do fetched_user = User.get_or_fetch_by_nickname("nonexistant") assert fetched_user == nil end + + test "updates an existing user, if stale" do + a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800) + + orig_user = + insert( + :user, + local: false, + nickname: "admin@mastodon.example.org", + ap_id: "http://mastodon.example.org/users/admin", + last_refreshed_at: a_week_ago + ) + + assert orig_user.last_refreshed_at == a_week_ago + + user = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin") + + refute user.last_refreshed_at == orig_user.last_refreshed_at + end end test "returns an ap_id for a user" do From 8e28e8a18f330696d48070a138a6fa83ef7edfb2 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 20 Sep 2018 23:51:06 +0000 Subject: [PATCH 3/3] mix: remove fix_ap_users task, now obsolete --- lib/mix/tasks/fix_ap_users.ex | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 lib/mix/tasks/fix_ap_users.ex diff --git a/lib/mix/tasks/fix_ap_users.ex b/lib/mix/tasks/fix_ap_users.ex deleted file mode 100644 index 7e970850e..000000000 --- a/lib/mix/tasks/fix_ap_users.ex +++ /dev/null @@ -1,28 +0,0 @@ -defmodule Mix.Tasks.FixApUsers do - use Mix.Task - import Ecto.Query - alias Pleroma.{Repo, User} - - @shortdoc "Grab all ap users again" - def run([]) do - Mix.Task.run("app.start") - - q = - from( - u in User, - where: fragment("? @> ?", u.info, ^%{"ap_enabled" => true}), - where: u.local == false - ) - - users = Repo.all(q) - - Enum.each(users, fn user -> - try do - IO.puts("Fetching #{user.nickname}") - Pleroma.Web.ActivityPub.Transmogrifier.upgrade_user_from_ap_id(user.ap_id, false) - rescue - e -> IO.inspect(e) - end - end) - end -end