Migrate to Vue 3 and Vite
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
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
|
||||
Reference in New Issue
Block a user