diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index 3c42c7cb5..a5a1d6a76 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -24,7 +24,7 @@ def change(struct, params \\ %{}) do def get_by_ap_id(nil), do: nil def get_by_ap_id(ap_id) do Repo.one(from object in Object, - where: fragment("? @> ?", object.data, ^%{id: ap_id})) + where: fragment("(?)->>'id' = ?", object.data, ^ap_id)) end def get_cached_by_ap_id(ap_id) do diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 938b57d90..bf63a22b3 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -241,7 +241,7 @@ def get_friends(%User{id: id, following: following}) do def update_note_count(%User{} = user) do note_count_query = from a in Object, - where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}), + where: fragment("?->>'actor' = ? and ?->>'type' = 'Note'", a.data, ^user.ap_id, a.data), select: count(a.id) note_count = Repo.one(note_count_query) @@ -274,4 +274,14 @@ def get_notified_from_activity(%Activity{data: %{"to" => to}} = activity) do Repo.all(query) end + + def search(query, resolve) do + if resolve do + User.get_or_fetch_by_nickname(query) + end + q = from u in User, + where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query), + limit: 20 + Repo.all(q) + end end diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 4b8e6b690..4e3a7e2bd 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -90,7 +90,11 @@ def update_object_in_activities(%{data: %{"id" => id}} = object) do """ def get_existing_like(actor, %{data: %{"id" => id}} = object) do query = from activity in Activity, - where: fragment("? @> ?", activity.data, ^%{actor: actor, object: id, type: "Like"}) + where: fragment("(?)->>'actor' = ?", activity.data, ^actor), + # this is to use the index + where: fragment("coalesce((?)->'object'->>'id', (?)->>'object') = ?", activity.data, activity.data, ^id), + where: fragment("(?)->>'type' = 'Like'", activity.data) + Repo.one(query) end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index a01a199fb..9399dee86 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -271,9 +271,27 @@ def following(conn, %{"id" => id}) do def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do with %User{} = followed <- Repo.get(User, id), - {:ok, follower} <- User.follow(follower, followed), - {:ok, activity} <- ActivityPub.follow(follower, followed) do + {:ok, follower} <- User.follow(follower, followed), + {:ok, activity} <- ActivityPub.follow(follower, followed) do render conn, AccountView, "relationship.json", %{user: follower, target: followed} + else + {:error, message} = err -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => message})) + end + end + + def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do + with %User{} = followed <- Repo.get_by(User, nickname: uri), + {:ok, follower} <- User.follow(follower, followed), + {:ok, activity} <- ActivityPub.follow(follower, followed) do + render conn, AccountView, "account.json", %{user: followed} + else + {:error, message} = err -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => message})) end end @@ -291,14 +309,7 @@ def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do end def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do - if params["resolve"] == "true" do - User.get_or_fetch_by_nickname(query) - end - - q = from u in User, - where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query), - limit: 20 - accounts = Repo.all(q) + accounts = User.search(query, params["resolve"] == "true") q = from a in Activity, where: fragment("?->>'type' = 'Create'", a.data), @@ -315,6 +326,14 @@ def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do json(conn, res) end + def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do + accounts = User.search(query, params["resolve"] == "true") + + res = AccountView.render("accounts.json", users: accounts, for: user, as: :user) + + json(conn, res) + end + def favourites(%{assigns: %{user: user}} = conn, params) do params = conn |> Map.put("type", "Create") diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index cb818b3cc..1fb5eadf6 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -55,6 +55,7 @@ def user_fetcher(username) do get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials get "/accounts/relationships", MastodonAPIController, :relationships + get "/accounts/search", MastodonAPIController, :account_search post "/accounts/:id/follow", MastodonAPIController, :follow post "/accounts/:id/unfollow", MastodonAPIController, :unfollow post "/accounts/:id/block", MastodonAPIController, :relationship_noop @@ -62,6 +63,8 @@ def user_fetcher(username) do post "/accounts/:id/mute", MastodonAPIController, :relationship_noop post "/accounts/:id/unmute", MastodonAPIController, :relationship_noop + post "/follows", MastodonAPIController, :follow + get "/blocks", MastodonAPIController, :empty_array get "/domain_blocks", MastodonAPIController, :empty_array get "/follow_requests", MastodonAPIController, :empty_array diff --git a/priv/repo/migrations/20171024090137_drop_object_index.exs b/priv/repo/migrations/20171024090137_drop_object_index.exs new file mode 100644 index 000000000..29b4c9333 --- /dev/null +++ b/priv/repo/migrations/20171024090137_drop_object_index.exs @@ -0,0 +1,7 @@ +defmodule Pleroma.Repo.Migrations.DropObjectIndex do + use Ecto.Migration + + def change do + drop_if_exists index(:objects, [:data], using: :gin) + end +end diff --git a/priv/repo/migrations/20171024121413_add_object_actor_index.exs b/priv/repo/migrations/20171024121413_add_object_actor_index.exs new file mode 100644 index 000000000..344c9c825 --- /dev/null +++ b/priv/repo/migrations/20171024121413_add_object_actor_index.exs @@ -0,0 +1,9 @@ +defmodule Pleroma.Repo.Migrations.AddObjectActorIndex do + use Ecto.Migration + + @disable_ddl_transaction true + + def change do + create index(:objects, ["(data->>'actor')", "(data->>'type')"], concurrently: true, name: :objects_actor_type) + end +end diff --git a/priv/static/index.html b/priv/static/index.html index ee1d42efb..b4df60919 100644 --- a/priv/static/index.html +++ b/priv/static/index.html @@ -1 +1 @@ -