Merge branch 'feature/suggestions-api-with-third-party-recommendation-engine' into 'develop'
Feature / Suggestions API with third party recommendation engine See merge request pleroma/pleroma!254
This commit is contained in:
commit
9908cf8fda
|
@ -111,6 +111,13 @@
|
|||
ip: {0, 0, 0, 0},
|
||||
port: 9999
|
||||
|
||||
config :pleroma, :suggestions,
|
||||
enabled: false,
|
||||
third_party_engine:
|
||||
"http://vinayaka.distsn.org/cgi-bin/vinayaka-user-match-suggestions-api.cgi?{{host}}+{{user}}",
|
||||
timeout: 300_000,
|
||||
web: "https://vinayaka.distsn.org/?{{host}}+{{user}}"
|
||||
|
||||
# Import environment specific config. This must remain at the bottom
|
||||
# of this file so it overrides the configuration defined above.
|
||||
import_config "#{Mix.env()}.exs"
|
||||
|
|
|
@ -11,6 +11,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
|||
import Ecto.Query
|
||||
require Logger
|
||||
|
||||
@httpoison Application.get_env(:pleroma, :httpoison)
|
||||
|
||||
action_fallback(:errors)
|
||||
|
||||
def create_app(conn, params) do
|
||||
|
@ -1097,4 +1099,38 @@ def errors(conn, _) do
|
|||
|> put_status(500)
|
||||
|> json("Something went wrong")
|
||||
end
|
||||
|
||||
@suggestions Application.get_env(:pleroma, :suggestions)
|
||||
|
||||
def suggestions(%{assigns: %{user: user}} = conn, _) do
|
||||
if Keyword.get(@suggestions, :enabled, false) do
|
||||
api = Keyword.get(@suggestions, :third_party_engine, "")
|
||||
timeout = Keyword.get(@suggestions, :timeout, 5000)
|
||||
|
||||
host =
|
||||
Application.get_env(:pleroma, Pleroma.Web.Endpoint)
|
||||
|> Keyword.get(:url)
|
||||
|> Keyword.get(:host)
|
||||
|
||||
user = user.nickname
|
||||
url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
|
||||
|
||||
with {:ok, %{status_code: 200, body: body}} <-
|
||||
@httpoison.get(url, [], timeout: timeout, recv_timeout: timeout),
|
||||
{:ok, data} <- Jason.decode(body) do
|
||||
data2 =
|
||||
Enum.slice(data, 0, 40)
|
||||
|> Enum.map(fn x ->
|
||||
Map.put(x, "id", User.get_or_fetch(x["acct"]).id)
|
||||
end)
|
||||
|
||||
conn
|
||||
|> json(data2)
|
||||
else
|
||||
e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
|
||||
end
|
||||
else
|
||||
json(conn, [])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -21,6 +21,7 @@ def schemas(conn, _params) do
|
|||
def nodeinfo(conn, %{"version" => "2.0"}) do
|
||||
instance = Application.get_env(:pleroma, :instance)
|
||||
media_proxy = Application.get_env(:pleroma, :media_proxy)
|
||||
suggestions = Application.get_env(:pleroma, :suggestions)
|
||||
stats = Stats.get_stats()
|
||||
|
||||
response = %{
|
||||
|
@ -45,7 +46,13 @@ def nodeinfo(conn, %{"version" => "2.0"}) do
|
|||
nodeName: Keyword.get(instance, :name),
|
||||
nodeDescription: Keyword.get(instance, :description),
|
||||
mediaProxy: Keyword.get(media_proxy, :enabled),
|
||||
private: !Keyword.get(instance, :public, true)
|
||||
private: !Keyword.get(instance, :public, true),
|
||||
suggestions: %{
|
||||
enabled: Keyword.get(suggestions, :enabled, false),
|
||||
thirdPartyEngine: Keyword.get(suggestions, :third_party_engine, ""),
|
||||
timeout: Keyword.get(suggestions, :timeout, 5000),
|
||||
web: Keyword.get(suggestions, :web, "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -141,6 +141,8 @@ def user_fetcher(username) do
|
|||
get("/domain_blocks", MastodonAPIController, :domain_blocks)
|
||||
post("/domain_blocks", MastodonAPIController, :block_domain)
|
||||
delete("/domain_blocks", MastodonAPIController, :unblock_domain)
|
||||
|
||||
get("/suggestions", MastodonAPIController, :suggestions)
|
||||
end
|
||||
|
||||
scope "/api/web", Pleroma.Web.MastodonAPI do
|
||||
|
|
Loading…
Reference in New Issue