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 @@ -Pleroma
\ No newline at end of file +Pleroma
\ No newline at end of file diff --git a/priv/static/static/js/app.37dc091547e5c8fb0a0e.js b/priv/static/static/js/app.37dc091547e5c8fb0a0e.js new file mode 100644 index 000000000..a93a5d1d1 Binary files /dev/null and b/priv/static/static/js/app.37dc091547e5c8fb0a0e.js differ diff --git a/priv/static/static/js/app.37dc091547e5c8fb0a0e.js.map b/priv/static/static/js/app.37dc091547e5c8fb0a0e.js.map new file mode 100644 index 000000000..79ff21b3a Binary files /dev/null and b/priv/static/static/js/app.37dc091547e5c8fb0a0e.js.map differ diff --git a/priv/static/static/js/app.437f43ecaf6ade101b9a.js b/priv/static/static/js/app.437f43ecaf6ade101b9a.js deleted file mode 100644 index 242a35f88..000000000 Binary files a/priv/static/static/js/app.437f43ecaf6ade101b9a.js and /dev/null differ diff --git a/priv/static/static/js/app.437f43ecaf6ade101b9a.js.map b/priv/static/static/js/app.437f43ecaf6ade101b9a.js.map deleted file mode 100644 index 6c529b6b0..000000000 Binary files a/priv/static/static/js/app.437f43ecaf6ade101b9a.js.map and /dev/null differ diff --git a/priv/static/static/js/manifest.13905a962b52e27fb039.js b/priv/static/static/js/manifest.13905a962b52e27fb039.js deleted file mode 100644 index 4252e578d..000000000 Binary files a/priv/static/static/js/manifest.13905a962b52e27fb039.js and /dev/null differ diff --git a/priv/static/static/js/manifest.5e2e6cbffed3dea6be7c.js b/priv/static/static/js/manifest.5e2e6cbffed3dea6be7c.js new file mode 100644 index 000000000..f6e40e802 Binary files /dev/null and b/priv/static/static/js/manifest.5e2e6cbffed3dea6be7c.js differ diff --git a/priv/static/static/js/manifest.13905a962b52e27fb039.js.map b/priv/static/static/js/manifest.5e2e6cbffed3dea6be7c.js.map similarity index 92% rename from priv/static/static/js/manifest.13905a962b52e27fb039.js.map rename to priv/static/static/js/manifest.5e2e6cbffed3dea6be7c.js.map index 7a24d3c54..7df0f4d8d 100644 Binary files a/priv/static/static/js/manifest.13905a962b52e27fb039.js.map and b/priv/static/static/js/manifest.5e2e6cbffed3dea6be7c.js.map differ diff --git a/priv/static/static/js/vendor.5ae36edd0f238a1334af.js.map b/priv/static/static/js/vendor.5ae36edd0f238a1334af.js.map deleted file mode 100644 index e66467e0a..000000000 Binary files a/priv/static/static/js/vendor.5ae36edd0f238a1334af.js.map and /dev/null differ diff --git a/priv/static/static/js/vendor.5ae36edd0f238a1334af.js b/priv/static/static/js/vendor.a940853cbf3c748efda4.js similarity index 82% rename from priv/static/static/js/vendor.5ae36edd0f238a1334af.js rename to priv/static/static/js/vendor.a940853cbf3c748efda4.js index 434bcc944..a2c6937fe 100644 Binary files a/priv/static/static/js/vendor.5ae36edd0f238a1334af.js and b/priv/static/static/js/vendor.a940853cbf3c748efda4.js differ diff --git a/priv/static/static/js/vendor.a940853cbf3c748efda4.js.map b/priv/static/static/js/vendor.a940853cbf3c748efda4.js.map new file mode 100644 index 000000000..181f604cc Binary files /dev/null and b/priv/static/static/js/vendor.a940853cbf3c748efda4.js.map differ diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 56888140d..485a0d029 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -281,6 +281,14 @@ test "following / unfollowing a user", %{conn: conn} do |> post("/api/v1/accounts/#{other_user.id}/unfollow") assert %{"id" => id, "following" => false} = json_response(conn, 200) + + user = Repo.get(User, user.id) + conn = build_conn() + |> assign(:user, user) + |> post("/api/v1/follows", %{"uri" => other_user.nickname}) + + assert %{"id" => id} = json_response(conn, 200) + assert id == other_user.id end test "unimplemented block/mute endpoints" do @@ -311,6 +319,19 @@ test "unimplemented mutes, follow_requests, blocks, domain blocks" do end) end + test "account seach", %{conn: conn} do + user = insert(:user) + user_two = insert(:user, %{nickname: "shp@shitposter.club"}) + user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"}) + + conn = conn + |> assign(:user, user) + |> get("/api/v1/accounts/search", %{"q" => "2hu"}) + + assert [account] = json_response(conn, 200) + assert account["id"] == user_three.id + end + test "search", %{conn: conn} do user = insert(:user) user_two = insert(:user, %{nickname: "shp@shitposter.club"})