diff options
author | Roman Chvanikov <chvanikoff@pm.me> | 2020-06-19 23:13:56 +0300 |
---|---|---|
committer | Roman Chvanikov <chvanikoff@pm.me> | 2020-06-19 23:13:56 +0300 |
commit | ae48af590507aff8b58429ee7bae70da26d2c4d8 (patch) | |
tree | 744ec417101d972744e2d2e8cebfd567af6c6b48 /lib | |
parent | 0103df2d0a2932eb55d737d17ea16a7452728232 (diff) | |
parent | 436bb0cd12825b3202a46dbc04cd063935e40344 (diff) | |
download | pleroma-ae48af590507aff8b58429ee7bae70da26d2c4d8.tar.gz |
Merge branch 'develop' into refactor/fe-bundles
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/pagination.ex | 6 | ||||
-rw-r--r-- | lib/pleroma/user.ex | 32 | ||||
-rw-r--r-- | lib/pleroma/web/api_spec/helpers.ex | 6 | ||||
-rw-r--r-- | lib/pleroma/web/mastodon_api/controllers/search_controller.ex | 31 |
4 files changed, 41 insertions, 34 deletions
diff --git a/lib/pleroma/pagination.ex b/lib/pleroma/pagination.ex index 1b99e44f9..9a3795769 100644 --- a/lib/pleroma/pagination.ex +++ b/lib/pleroma/pagination.ex @@ -64,6 +64,12 @@ defmodule Pleroma.Pagination do @spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()] def paginate(query, options, method \\ :keyset, table_binding \\ nil) + def paginate(list, options, _method, _table_binding) when is_list(list) do + offset = options[:offset] || 0 + limit = options[:limit] || 0 + Enum.slice(list, offset, limit) + end + def paginate(query, options, :keyset, table_binding) do query |> restrict(:min_id, options, table_binding) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 19ce9fb56..f0ccc7c79 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -758,7 +758,6 @@ defmodule Pleroma.User do follower |> update_following_count() - |> set_cache() end end @@ -787,7 +786,6 @@ defmodule Pleroma.User do {:ok, follower} = follower |> update_following_count() - |> set_cache() {:ok, follower, followed} @@ -1139,35 +1137,25 @@ defmodule Pleroma.User do ]) end + @spec update_follower_count(User.t()) :: {:ok, User.t()} def update_follower_count(%User{} = user) do if user.local or !Pleroma.Config.get([:instance, :external_user_synchronization]) do - follower_count_query = - User.Query.build(%{followers: user, deactivated: false}) - |> select([u], %{count: count(u.id)}) - - User - |> where(id: ^user.id) - |> join(:inner, [u], s in subquery(follower_count_query)) - |> update([u, s], - set: [follower_count: s.count] - ) - |> select([u], u) - |> Repo.update_all([]) - |> case do - {1, [user]} -> set_cache(user) - _ -> {:error, user} - end + follower_count = FollowingRelationship.follower_count(user) + + user + |> follow_information_changeset(%{follower_count: follower_count}) + |> update_and_set_cache else {:ok, maybe_fetch_follow_information(user)} end end - @spec update_following_count(User.t()) :: User.t() + @spec update_following_count(User.t()) :: {:ok, User.t()} def update_following_count(%User{local: false} = user) do if Pleroma.Config.get([:instance, :external_user_synchronization]) do - maybe_fetch_follow_information(user) + {:ok, maybe_fetch_follow_information(user)} else - user + {:ok, user} end end @@ -1176,7 +1164,7 @@ defmodule Pleroma.User do user |> follow_information_changeset(%{following_count: following_count}) - |> Repo.update!() + |> update_and_set_cache() end def set_unread_conversation_count(%User{local: true} = user) do diff --git a/lib/pleroma/web/api_spec/helpers.ex b/lib/pleroma/web/api_spec/helpers.ex index a9cfe0fed..a258e8421 100644 --- a/lib/pleroma/web/api_spec/helpers.ex +++ b/lib/pleroma/web/api_spec/helpers.ex @@ -40,6 +40,12 @@ defmodule Pleroma.Web.ApiSpec.Helpers do "Return the newest items newer than this ID" ), Operation.parameter( + :offset, + :query, + %Schema{type: :integer, default: 0}, + "Return items past this number of items" + ), + Operation.parameter( :limit, :query, %Schema{type: :integer, default: 20}, diff --git a/lib/pleroma/web/mastodon_api/controllers/search_controller.ex b/lib/pleroma/web/mastodon_api/controllers/search_controller.ex index 3be0ca095..e50980122 100644 --- a/lib/pleroma/web/mastodon_api/controllers/search_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/search_controller.ex @@ -107,21 +107,21 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do ) end - defp resource_search(:v2, "hashtags", query, _options) do + defp resource_search(:v2, "hashtags", query, options) do tags_path = Web.base_url() <> "/tag/" query - |> prepare_tags() + |> prepare_tags(options) |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end) end - defp resource_search(:v1, "hashtags", query, _options) do - prepare_tags(query) + defp resource_search(:v1, "hashtags", query, options) do + prepare_tags(query, options) end - defp prepare_tags(query, add_joined_tag \\ true) do + defp prepare_tags(query, options) do tags = query |> preprocess_uri_query() @@ -139,13 +139,20 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do tags = Enum.map(tags, fn tag -> String.trim_leading(tag, "#") end) - if Enum.empty?(explicit_tags) && add_joined_tag do - tags - |> Kernel.++([joined_tag(tags)]) - |> Enum.uniq_by(&String.downcase/1) - else - tags - end + tags = + if Enum.empty?(explicit_tags) && !options[:skip_joined_tag] do + add_joined_tag(tags) + else + tags + end + + Pleroma.Pagination.paginate(tags, options) + end + + defp add_joined_tag(tags) do + tags + |> Kernel.++([joined_tag(tags)]) + |> Enum.uniq_by(&String.downcase/1) end # If `query` is a URI, returns last component of its path, otherwise returns `query` |