+ defp parse_int(_, default), do: default
+
+ def search(_user, %{"q" => query} = params) do
+ limit = parse_int(params["rpp"], 20)
+ page = parse_int(params["page"], 1)
+ offset = (page - 1) * limit
+
+ q =
+ from(
+ a in Activity,
+ where: fragment("?->>'type' = 'Create'", a.data),
+ where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
+ where:
+ fragment(
+ "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
+ a.data,
+ ^query
+ ),
+ limit: ^limit,
+ offset: ^offset,
+ # this one isn't indexed so psql won't take the wrong index.
+ order_by: [desc: :inserted_at]
+ )
+
+ _activities = Repo.all(q)
+ end
+
+ defp make_date do
+ DateTime.utc_now() |> DateTime.to_iso8601()
+ end
+
+ # DEPRECATED mostly, context objects are now created at insertion time.
+ def context_to_conversation_id(context) do
+ with %Object{id: id} <- Object.get_cached_by_ap_id(context) do
+ id
+ else
+ _e ->
+ changeset = Object.context_mapping(context)
+
+ case Repo.insert(changeset) do
+ {:ok, %{id: id}} ->
+ id
+
+ # This should be solved by an upsert, but it seems ecto
+ # has problems accessing the constraint inside the jsonb.
+ {:error, _} ->
+ Object.get_cached_by_ap_id(context).id
+ end
+ end
+ end
+
+ def conversation_id_to_context(id) do
+ with %Object{data: %{"id" => context}} <- Repo.get(Object, id) do
+ context
+ else
+ _e ->
+ {:error, "No such conversation"}
+ end
+ end
+
+ def get_external_profile(for_user, uri) do
+ with %User{} = user <- User.get_or_fetch(uri) do
+ spawn(fn ->
+ with url <- user.info["topic"],
+ {:ok, %{body: body}} <-
+ @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do
+ OStatus.handle_incoming(body)
+ end
+ end)
+
+ {:ok, UserView.render("show.json", %{user: user, for: for_user})}
+ else
+ _e ->
+ {:error, "Couldn't find user"}
+ end