Migrate to Vue 3 and Vite
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
39
lib/service/http/generic_json_client.ex
Normal file
39
lib/service/http/generic_json_client.ex
Normal file
@@ -0,0 +1,39 @@
|
||||
defmodule Mobilizon.Service.HTTP.GenericJSONClient do
|
||||
@moduledoc """
|
||||
Tesla HTTP Client that is preconfigured to get JSON content
|
||||
"""
|
||||
|
||||
alias Mobilizon.Config
|
||||
|
||||
@default_opts [
|
||||
recv_timeout: 20_000
|
||||
]
|
||||
|
||||
@spec client(Keyword.t()) :: Tesla.Client.t()
|
||||
def client(options \\ []) do
|
||||
headers = Keyword.get(options, :headers, [])
|
||||
adapter = Application.get_env(:tesla, __MODULE__, [])[:adapter] || Tesla.Adapter.Hackney
|
||||
opts = Keyword.merge(@default_opts, Keyword.get(options, :opts, []))
|
||||
|
||||
middleware = [
|
||||
{Tesla.Middleware.Headers,
|
||||
[{"User-Agent", Config.instance_user_agent()}] ++
|
||||
headers},
|
||||
Tesla.Middleware.FollowRedirects,
|
||||
{Tesla.Middleware.Timeout, timeout: 10_000},
|
||||
Tesla.Middleware.JSON
|
||||
]
|
||||
|
||||
Tesla.client(middleware, {adapter, opts})
|
||||
end
|
||||
|
||||
@spec get(Tesla.Client.t(), String.t()) :: Tesla.Env.result()
|
||||
def get(client, url) do
|
||||
Tesla.get(client, url)
|
||||
end
|
||||
|
||||
@spec post(Tesla.Client.t(), String.t(), map() | String.t()) :: Tesla.Env.result()
|
||||
def post(client, url, data) do
|
||||
Tesla.post(client, url, data)
|
||||
end
|
||||
end
|
||||
@@ -44,7 +44,11 @@ defmodule Mobilizon.Service.Metadata.Instance do
|
||||
Tag.tag(:meta, property: "og:description", content: description),
|
||||
Tag.tag(:meta, property: "og:type", content: "website"),
|
||||
HTML.raw(instance_json_ld)
|
||||
] ++ maybe_add_instance_feeds(Config.get([:instance, :enable_instance_feeds]))
|
||||
] ++ maybe_add_instance_feeds(enable_instance_feeds())
|
||||
end
|
||||
|
||||
defp enable_instance_feeds do
|
||||
get_in(Application.get_env(:mobilizon, :instance), [:enable_instance_feeds])
|
||||
end
|
||||
|
||||
@spec maybe_add_instance_feeds(boolean()) :: list()
|
||||
|
||||
3
lib/service/pictures/information.ex
Normal file
3
lib/service/pictures/information.ex
Normal file
@@ -0,0 +1,3 @@
|
||||
defmodule Mobilizon.Service.Pictures.Information do
|
||||
defstruct [:url, :author, :source]
|
||||
end
|
||||
17
lib/service/pictures/pictures.ex
Normal file
17
lib/service/pictures/pictures.ex
Normal file
@@ -0,0 +1,17 @@
|
||||
defmodule Mobilizon.Service.Pictures do
|
||||
@moduledoc """
|
||||
Module to load the service adapter defined inside the configuration.
|
||||
|
||||
See `Mobilizon.Service.Pictures.Provider`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Returns the appropriate service adapter.
|
||||
|
||||
According to the config behind
|
||||
`config :mobilizon, Mobilizon.Service.Pictures,
|
||||
service: Mobilizon.Service.Pictures.Module`
|
||||
"""
|
||||
@spec service :: module
|
||||
def service, do: get_in(Application.get_env(:mobilizon, __MODULE__), [:service])
|
||||
end
|
||||
39
lib/service/pictures/provider.ex
Normal file
39
lib/service/pictures/provider.ex
Normal file
@@ -0,0 +1,39 @@
|
||||
defmodule Mobilizon.Service.Pictures.Provider do
|
||||
@moduledoc """
|
||||
Provider Behaviour for pictures stuff.
|
||||
|
||||
## Supported backends
|
||||
|
||||
* `Mobilizon.Service.Pictures.Unsplash` [🔗](https://unsplash.com/developers)
|
||||
* `Mobilizon.Service.Pictures.Flickr` [🔗](https://www.flickr.com/services/api/)
|
||||
|
||||
## Shared options
|
||||
|
||||
* `:lang` Lang in which to prefer results. Used as a request parameter or
|
||||
through an `Accept-Language` HTTP header. Defaults to `"en"`.
|
||||
* `:country_code` An ISO 3166 country code. String or `nil`
|
||||
* `:limit` Maximum limit for the number of results returned by the backend.
|
||||
Defaults to `10`
|
||||
* `:api_key` Allows to override the API key (if the backend requires one) set
|
||||
inside the configuration.
|
||||
* `:endpoint` Allows to override the endpoint set inside the configuration.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Service.Pictures.Information
|
||||
|
||||
@doc """
|
||||
Get a picture for a location
|
||||
|
||||
## Examples
|
||||
|
||||
iex> search("London")
|
||||
%Information{url: "https://some_url_to.a/picture.jpeg", author: %{name: "An author", url: "https://url.to/profile"}, source: %{name: "The source name", url: "The source URL" }}
|
||||
"""
|
||||
@callback search(location :: String.t(), options :: keyword) ::
|
||||
[Information.t()]
|
||||
|
||||
@spec endpoint(atom()) :: String.t()
|
||||
def endpoint(provider) do
|
||||
Application.get_env(:mobilizon, provider) |> get_in([:endpoint])
|
||||
end
|
||||
end
|
||||
65
lib/service/pictures/unsplash.ex
Normal file
65
lib/service/pictures/unsplash.ex
Normal file
@@ -0,0 +1,65 @@
|
||||
defmodule Mobilizon.Service.Pictures.Unsplash do
|
||||
@moduledoc """
|
||||
[Unsplash](https://unsplash.com) backend.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Service.Pictures.{Information, Provider}
|
||||
alias Mobilizon.Service.HTTP.GenericJSONClient
|
||||
require Logger
|
||||
|
||||
@unsplash_api "/search/photos"
|
||||
@unsplash_name "Unsplash"
|
||||
|
||||
@behaviour Provider
|
||||
|
||||
@impl Provider
|
||||
@doc """
|
||||
Unsplash implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Information.t())
|
||||
def search(location, _options \\ []) do
|
||||
url = "#{unsplash_endpoint()}#{@unsplash_api}?query=#{location}&orientation=landscape"
|
||||
|
||||
client =
|
||||
GenericJSONClient.client(headers: [{:Authorization, "Client-ID #{unsplash_access_key()}"}])
|
||||
|
||||
with {:ok, %{status: 200, body: body}} <- GenericJSONClient.get(client, url),
|
||||
selectedPicture <- Enum.random(body["results"]) do
|
||||
%Information{
|
||||
url: selectedPicture["urls"]["small"],
|
||||
author: %{
|
||||
name: selectedPicture["user"]["name"],
|
||||
url: "#{selectedPicture["user"]["links"]["html"]}#{unsplash_utm_source()}"
|
||||
},
|
||||
source: %{
|
||||
name: @unsplash_name,
|
||||
url: unsplash_url()
|
||||
}
|
||||
}
|
||||
else
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp unsplash_app_name do
|
||||
Application.get_env(:mobilizon, __MODULE__) |> get_in([:app_name])
|
||||
end
|
||||
|
||||
defp unsplash_utm_source do
|
||||
"?utm_source=#{unsplash_app_name()}&utm_medium=referral"
|
||||
end
|
||||
|
||||
defp unsplash_url do
|
||||
"https://unsplash.com/#{unsplash_utm_source()}"
|
||||
end
|
||||
|
||||
defp unsplash_endpoint do
|
||||
Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint]) ||
|
||||
"https://api.unsplash.com"
|
||||
end
|
||||
|
||||
defp unsplash_access_key do
|
||||
Application.get_env(:mobilizon, __MODULE__) |> get_in([:access_key])
|
||||
end
|
||||
end
|
||||
@@ -4,6 +4,7 @@ defmodule Mobilizon.Service.Statistics do
|
||||
"""
|
||||
|
||||
alias Mobilizon.{Actors, Discussions, Events, Users}
|
||||
alias Mobilizon.Events.Categories
|
||||
alias Mobilizon.Federation.ActivityPub.Relay
|
||||
|
||||
@spec get_cached_value(String.t()) :: any() | nil
|
||||
@@ -60,4 +61,23 @@ defmodule Mobilizon.Service.Statistics do
|
||||
relay_actor = Relay.get_actor()
|
||||
Actors.count_followings_for_actor(relay_actor)
|
||||
end
|
||||
|
||||
@spec category_statistics :: list({String.t(), non_neg_integer()})
|
||||
def category_statistics do
|
||||
case Cachex.fetch(:statistics, :categories, fn ->
|
||||
allowed_categories =
|
||||
Categories.list()
|
||||
|> Enum.map(fn %{id: category} -> category |> Atom.to_string() |> String.upcase() end)
|
||||
|
||||
statistics =
|
||||
Events.category_statistics()
|
||||
|> Enum.filter(fn {category, _} -> category in allowed_categories end)
|
||||
|> Enum.map(fn {category, number} -> %{key: category, number: number} end)
|
||||
|
||||
{:commit, statistics}
|
||||
end) do
|
||||
{status, value} when status in [:ok, :commit] -> value
|
||||
_err -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user