diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pleroma/activity.ex | 16 | ||||
| -rw-r--r-- | lib/pleroma/notification.ex | 7 | ||||
| -rw-r--r-- | lib/pleroma/user.ex | 56 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 1 | ||||
| -rw-r--r-- | lib/pleroma/web/router.ex | 1 | ||||
| -rw-r--r-- | lib/pleroma/web/twitter_api/controllers/util_controller.ex | 11 | ||||
| -rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api.ex | 9 | 
7 files changed, 85 insertions, 16 deletions
diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index 4a2ded518..9c1c804e0 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -106,7 +106,10 @@ defmodule Pleroma.Activity do    end    def get_by_id(id) do -    Repo.get(Activity, id) +    Activity +    |> where([a], a.id == ^id) +    |> restrict_deactivated_users() +    |> Repo.one()    end    def get_by_id_with_object(id) do @@ -174,6 +177,7 @@ defmodule Pleroma.Activity do    def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do      create_by_object_ap_id(ap_id) +    |> restrict_deactivated_users()      |> Repo.one()    end @@ -260,4 +264,14 @@ defmodule Pleroma.Activity do      |> where([s], s.actor == ^actor)      |> Repo.all()    end + +  def restrict_deactivated_users(query) do +    from(activity in query, +      where: +        fragment( +          "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')", +          activity.actor +        ) +    ) +  end  end diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index dd274cf6b..844264307 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -33,6 +33,13 @@ defmodule Pleroma.Notification do    def for_user_query(user) do      Notification      |> where(user_id: ^user.id) +    |> where( +      [n, a], +      fragment( +        "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')", +        a.actor +      ) +    )      |> join(:inner, [n], activity in assoc(n, :activity))      |> join(:left, [n, a], object in Object,        on: diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index f1feab279..d103cd809 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -107,10 +107,8 @@ defmodule Pleroma.User do    def ap_followers(%User{} = user), do: "#{ap_id(user)}/followers"    def user_info(%User{} = user) do -    oneself = if user.local, do: 1, else: 0 -      %{ -      following_count: length(user.following) - oneself, +      following_count: following_count(user),        note_count: user.info.note_count,        follower_count: user.info.follower_count,        locked: user.info.locked, @@ -119,6 +117,23 @@ defmodule Pleroma.User do      }    end +  defp restrict_deactivated(query) do +    from(u in query, +      where: not fragment("? \\? 'deactivated' AND ?->'deactivated' @> 'true'", u.info, u.info) +    ) +  end + +  def following_count(%User{following: []}), do: 0 + +  def following_count(%User{following: following, id: id}) do +    from(u in User, +      where: u.follower_address in ^following, +      where: u.id != ^id +    ) +    |> restrict_deactivated() +    |> Repo.aggregate(:count, :id) +  end +    def remote_user_creation(params) do      params =        params @@ -576,6 +591,7 @@ defmodule Pleroma.User do        where: fragment("? <@ ?", ^[follower_address], u.following),        where: u.id != ^id      ) +    |> restrict_deactivated()    end    def get_followers_query(user, page) do @@ -603,6 +619,7 @@ defmodule Pleroma.User do        where: u.follower_address in ^following,        where: u.id != ^id      ) +    |> restrict_deactivated()    end    def get_friends_query(user, page) do @@ -714,11 +731,10 @@ defmodule Pleroma.User do      info_cng = User.Info.set_note_count(user.info, note_count) -    cng = -      change(user) -      |> put_embed(:info, info_cng) - -    update_and_set_cache(cng) +    user +    |> change() +    |> put_embed(:info, info_cng) +    |> update_and_set_cache()    end    def update_follower_count(%User{} = user) do @@ -727,6 +743,7 @@ defmodule Pleroma.User do        |> where([u], ^user.follower_address in u.following)        |> where([u], u.id != ^user.id)        |> select([u], %{count: count(u.id)}) +      |> restrict_deactivated()      User      |> where(id: ^user.id) @@ -877,6 +894,7 @@ defmodule Pleroma.User do            ^processed_query          )      ) +    |> restrict_deactivated()    end    defp trigram_search_subquery(term) do @@ -895,6 +913,7 @@ defmodule Pleroma.User do        },        where: fragment("trim(? || ' ' || coalesce(?, '')) % ?", u.nickname, u.name, ^term)      ) +    |> restrict_deactivated()    end    def blocks_import(%User{} = blocker, blocked_identifiers) when is_list(blocked_identifiers) do @@ -1138,14 +1157,27 @@ defmodule Pleroma.User do      )    end +  def deactivate_async(user, status \\ true) do +    PleromaJobQueue.enqueue(:user, __MODULE__, [:deactivate_async, user, status]) +  end + +  def perform(:deactivate_async, user, status), do: deactivate(user, status) +    def deactivate(%User{} = user, status \\ true) do      info_cng = User.Info.set_activation_status(user.info, status) -    cng = -      change(user) -      |> put_embed(:info, info_cng) +    with {:ok, friends} <- User.get_friends(user), +         {:ok, followers} <- User.get_followers(user), +         {:ok, user} <- +           user +           |> change() +           |> put_embed(:info, info_cng) +           |> update_and_set_cache() do +      Enum.each(followers, &invalidate_cache(&1)) +      Enum.each(friends, &update_follower_count(&1)) -    update_and_set_cache(cng) +      {:ok, user} +    end    end    def update_notification_settings(%User{} = user, settings \\ %{}) do diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 604ffae7b..6bf54d1cc 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -805,6 +805,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do      |> restrict_reblogs(opts)      |> restrict_pinned(opts)      |> restrict_muted_reblogs(opts) +    |> Activity.restrict_deactivated_users()    end    def fetch_activities(recipients, opts \\ %{}) do diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index ff4f08af5..5f7617ece 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -197,6 +197,7 @@ defmodule Pleroma.Web.Router do        post("/change_password", UtilController, :change_password)        post("/delete_account", UtilController, :delete_account)        put("/notification_settings", UtilController, :update_notificaton_settings) +      post("/disable_account", UtilController, :disable_account)      end      scope [] do diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 1122e6c5d..6c8c2fe24 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -360,6 +360,17 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do      end    end +  def disable_account(%{assigns: %{user: user}} = conn, params) do +    case CommonAPI.Utils.confirm_current_password(user, params["password"]) do +      {:ok, user} -> +        User.deactivate_async(user) +        json(conn, %{status: "success"}) + +      {:error, msg} -> +        json(conn, %{error: msg}) +    end +  end +    def captcha(conn, _params) do      json(conn, Pleroma.Captcha.new())    end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index adeac6f3c..2353a95a8 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -231,12 +231,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do    def get_user(user \\ nil, params) do      case params do        %{"user_id" => user_id} -> -        case target = User.get_cached_by_nickname_or_id(user_id) do +        case User.get_cached_by_nickname_or_id(user_id) do            nil ->              {:error, "No user with such user_id"} -          _ -> -            {:ok, target} +          %User{info: %{deactivated: true}} -> +            {:error, "User has been disabled"} + +          user -> +            {:ok, user}          end        %{"screen_name" => nickname} ->  | 
