diff --git a/apps/cf/lib/accounts/username_generator.ex b/apps/cf/lib/accounts/username_generator.ex index fa542358..2194b652 100644 --- a/apps/cf/lib/accounts/username_generator.ex +++ b/apps/cf/lib/accounts/username_generator.ex @@ -6,7 +6,17 @@ defmodule CF.Accounts.UsernameGenerator do @name __MODULE__ @username_prefix "NewUser-" - def start_link do + def child_spec(opts) do + %{ + id: __MODULE__, + start: {__MODULE__, :start_link, [opts]}, + type: :worker, + restart: :permanent, + shutdown: 500 + } + end + + def start_link(_opts \\ []) do Agent.start_link( fn -> Hashids.new( diff --git a/apps/cf/lib/application.ex b/apps/cf/lib/application.ex index 93d1a21c..063ac4bb 100644 --- a/apps/cf/lib/application.ex +++ b/apps/cf/lib/application.ex @@ -9,11 +9,11 @@ defmodule CF.Application do # Define workers and child supervisors to be supervised children = [ # Other custom supervisors - supervisor(CF.Sources.Fetcher, []), + {CF.Sources.Fetcher, []}, # Misc workers - worker(CF.Accounts.UsernameGenerator, []), + {CF.Accounts.UsernameGenerator, []}, # Sweep tokens from db - worker(Guardian.DB.Token.SweeperServer, []) + {Guardian.DB.Token.SweeperServer, []} ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html diff --git a/apps/cf/lib/errors/errors.ex b/apps/cf/lib/errors/errors.ex index b693fc34..183bda66 100644 --- a/apps/cf/lib/errors/errors.ex +++ b/apps/cf/lib/errors/errors.ex @@ -36,30 +36,9 @@ defmodule CF.Errors do end @spec do_report(:error | :exit | :throw, any(), [any()], cf_error_params()) :: :ok - def do_report(type, value, stacktrace, params) do + def do_report(type, value, stacktrace, _params) do # Any call to Sentry, Rollbar, etc. should be done here Logger.error("[ERROR][#{type}] #{inspect(value)} - #{inspect(stacktrace)}") :ok end - - defp build_occurence_data(params) do - default_occurrence_data() - |> add_user(params[:user]) - |> Map.merge(params[:data] || %{}) - end - - defp default_occurrence_data() do - %{ - "code_version" => CF.Application.version() - } - end - - defp add_user(base, nil), - do: base - - defp add_user(base, %{id: id, username: username}), - do: Map.merge(base, %{"person" => %{"id" => Integer.to_string(id), "username" => username}}) - - defp add_user(base, %{id: id}), - do: Map.merge(base, %{"person" => %{"id" => Integer.to_string(id)}}) end diff --git a/apps/cf/lib/llms/statements_creator.ex b/apps/cf/lib/llms/statements_creator.ex index 2e918397..6daa9d0f 100644 --- a/apps/cf/lib/llms/statements_creator.ex +++ b/apps/cf/lib/llms/statements_creator.ex @@ -56,9 +56,7 @@ defmodule CF.LLMs.StatementsCreator do end end - @doc """ - Chunk captions everytime we reach the max caption length - """ + # Chunk captions each time we reach the max caption length defp chunk_captions(captions) do # TODO: Add last captions from previous batch to preserve context Enum.chunk_every(captions, @captions_chunk_size) diff --git a/apps/cf/lib/llms/templates/statements_extractor_user_prompt.eex b/apps/cf/lib/llms/templates/statements_extractor_user_prompt.eex index 466c9848..4efc3c69 100644 --- a/apps/cf/lib/llms/templates/statements_extractor_user_prompt.eex +++ b/apps/cf/lib/llms/templates/statements_extractor_user_prompt.eex @@ -4,8 +4,8 @@ "title": "<%= video.id %>" }, "captions": <%= captions |> Enum.map(fn caption -> %{ - "start": floor(caption["start"]), - "text": String.trim(caption["text"]) + start: floor(caption["start"]), + text: String.trim(caption["text"]) } end) |> Jason.encode! %> } ``` diff --git a/apps/cf/lib/sources/fetcher.ex b/apps/cf/lib/sources/fetcher.ex index 47b53b47..49f5c451 100644 --- a/apps/cf/lib/sources/fetcher.ex +++ b/apps/cf/lib/sources/fetcher.ex @@ -10,7 +10,17 @@ defmodule CF.Sources.Fetcher do # ---- Public API ---- - def start_link() do + def child_spec(opts) do + %{ + id: __MODULE__, + start: {__MODULE__, :start_link, [opts]}, + type: :worker, + restart: :permanent, + shutdown: 500 + } + end + + def start_link(_opts \\ []) do import Supervisor.Spec Supervisor.start_link( diff --git a/apps/cf_atom_feed/lib/application.ex b/apps/cf_atom_feed/lib/application.ex index 087c5585..df23f988 100644 --- a/apps/cf_atom_feed/lib/application.ex +++ b/apps/cf_atom_feed/lib/application.ex @@ -4,14 +4,8 @@ defmodule CF.AtomFeed.Application do # See https://hexdocs.pm/elixir/Application.html # for more information on OTP Applications def start(_type, _args) do - import Supervisor.Spec - - children = [] config = Application.get_env(:cf_atom_feed, CF.AtomFeed.Router) - - if config[:cowboy] do - children = [supervisor(CF.AtomFeed.Router, []) | children] - end + children = if config[:cowboy], do: [{CF.AtomFeed.Router, []}], else: [] # See https://hexdocs.pm/elixir/Supervisor.html # for other strategies and supported options diff --git a/apps/cf_atom_feed/lib/router.ex b/apps/cf_atom_feed/lib/router.ex index 009adae0..bc0a7ebc 100644 --- a/apps/cf_atom_feed/lib/router.ex +++ b/apps/cf_atom_feed/lib/router.ex @@ -6,7 +6,17 @@ defmodule CF.AtomFeed.Router do plug(:match) plug(:dispatch) - def start_link do + def child_spec(opts) do + %{ + id: __MODULE__, + start: {__MODULE__, :start_link, [opts]}, + type: :worker, + restart: :permanent, + shutdown: 500 + } + end + + def start_link(_opts \\ []) do config = Application.get_env(:cf_atom_feed, CF.AtomFeed.Router) Logger.info("Running CF.AtomFeed.Router with cowboy on port #{config[:cowboy][:port]}") Plug.Cowboy.http(CF.AtomFeed.Router, [], config[:cowboy]) diff --git a/apps/cf_graphql/lib/application.ex b/apps/cf_graphql/lib/application.ex index 64ca65a6..a9ecb110 100644 --- a/apps/cf_graphql/lib/application.ex +++ b/apps/cf_graphql/lib/application.ex @@ -11,7 +11,7 @@ defmodule CF.Graphql.Application do # Start the PubSub system {Phoenix.PubSub, name: CF.Graphql.PubSub}, # Start the endpoint when the application starts - supervisor(CF.GraphQLWeb.Endpoint, []) + {CF.GraphQLWeb.Endpoint, []} ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/apps/cf_rest_api/lib/application.ex b/apps/cf_rest_api/lib/application.ex index 33368ed0..c341b91a 100644 --- a/apps/cf_rest_api/lib/application.ex +++ b/apps/cf_rest_api/lib/application.ex @@ -9,9 +9,9 @@ defmodule CF.RestApi.Application do # Start the PubSub system {Phoenix.PubSub, name: CF.RestApi.PubSub}, # Start the endpoint when the application starts - supervisor(CF.RestApi.Endpoint, []), + {CF.RestApi.Endpoint, []}, # Presence to track number of connected users to a channel - supervisor(CF.RestApi.Presence, []) + {CF.RestApi.Presence, []} ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html diff --git a/apps/cf_reverse_proxy/lib/plug.ex b/apps/cf_reverse_proxy/lib/plug.ex index 64447300..4bb82412 100644 --- a/apps/cf_reverse_proxy/lib/plug.ex +++ b/apps/cf_reverse_proxy/lib/plug.ex @@ -11,12 +11,6 @@ defmodule CF.ReverseProxy.Plug do ) @default_host CF.RestApi.Endpoint - @base_host_regex ~r/^(?rest|graphql|feed)\./ - @subdomains %{ - "graphql" => CF.GraphQLWeb.Endpoint, - "rest" => CF.RestApi.Endpoint, - "feed" => CF.AtomFeed.Router - } def init(opts), do: opts @@ -26,6 +20,13 @@ defmodule CF.ReverseProxy.Plug do # https://github.com/jesseshieh/master_proxy if Application.get_env(:cf, :env) == :dev do + @base_host_regex ~r/^(?rest|graphql|feed)\./ + @subdomains %{ + "graphql" => CF.GraphQLWeb.Endpoint, + "rest" => CF.RestApi.Endpoint, + "feed" => CF.AtomFeed.Router + } + # Dev requests are routed through here def call(conn, _) do if conn.request_path == "/status" do diff --git a/apps/db/lib/db/application.ex b/apps/db/lib/db/application.ex index f9ba5589..f97ee3e8 100644 --- a/apps/db/lib/db/application.ex +++ b/apps/db/lib/db/application.ex @@ -9,9 +9,7 @@ defmodule DB.Application do # Define workers and child supervisors to be supervised children = [ - # Starts a worker by calling: DB.Worker.start_link(arg1, arg2, arg3) - # worker(DB.Worker, [arg1, arg2, arg3]), - supervisor(DB.Repo, []) + {DB.Repo, []} ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html diff --git a/apps/db/lib/db_schema/source.ex b/apps/db/lib/db_schema/source.ex index 52b1b013..4f1b835f 100644 --- a/apps/db/lib/db_schema/source.ex +++ b/apps/db/lib/db_schema/source.ex @@ -65,7 +65,7 @@ defmodule DB.Schema.Source do end defp validate_file_mime_type(:file_mime_type, mime_type) do - if MIME.valid?(mime_type) do + if MIME.extensions(mime_type) != [] do [] else [file_mime_type: "Invalid MIME type"] diff --git a/apps/db/lib/db_schema/video.ex b/apps/db/lib/db_schema/video.ex index 68964fe1..b39b6e33 100644 --- a/apps/db/lib/db_schema/video.ex +++ b/apps/db/lib/db_schema/video.ex @@ -297,13 +297,4 @@ defmodule DB.Schema.Video do ) end) end - - # Return IDs of videos with at least 3 statements - defp popular_videos_subquery do - Video - |> join(:inner, [v], s in assoc(v, :statements)) - |> select([:id]) - |> group_by([v], v.id) - |> having([v, s], count(s.id) >= 3) - end end diff --git a/apps/db/lib/db_type/flag_reason.ex b/apps/db/lib/db_type/flag_reason.ex index 5a45bac6..c34da214 100644 --- a/apps/db/lib/db_type/flag_reason.ex +++ b/apps/db/lib/db_type/flag_reason.ex @@ -66,4 +66,7 @@ defmodule DB.Type.FlagReason do |> Enum.find(fn {_, id} -> id == reason_id end) |> elem(0) end + + # Implement the embed_as/1 function required by the Ecto.Type behaviour + def embed_as(_), do: :dump end diff --git a/apps/db/mix.exs b/apps/db/mix.exs index 314ec309..45ef65f3 100644 --- a/apps/db/mix.exs +++ b/apps/db/mix.exs @@ -52,9 +52,10 @@ defmodule DB.Mixfile do {:burnex, "~> 3.1"}, {:hashids, "~> 2.0"}, {:kaur, "~> 1.1"}, - {:mime, "~> 1.2"}, + {:mime, "~> 2.0.6"}, {:scrivener_ecto, "~> 2.0"}, {:algoliax, "~> 0.7.1"}, + {:httpoison, "~> 2.2"}, # Dev only {:exsync, "~> 0.2", only: :dev}, diff --git a/apps/db/test/db_schema/speaker_test.exs b/apps/db/test/db_schema/speaker_test.exs index 13be6ec3..5d7aac01 100644 --- a/apps/db/test/db_schema/speaker_test.exs +++ b/apps/db/test/db_schema/speaker_test.exs @@ -4,7 +4,7 @@ defmodule DB.Schema.SpeakerTest do alias DB.Schema.Speaker @valid_attrs %{ - full_name: "#{Faker.Name.first_name()} #{Faker.Name.last_name()}", + full_name: "#{Faker.Person.first_name()} #{Faker.Person.last_name()}", wikidata_item_id: nil } @invalid_attrs %{} diff --git a/apps/db/test/support/factory.ex b/apps/db/test/support/factory.ex index 050d1c81..25019ffd 100644 --- a/apps/db/test/support/factory.ex +++ b/apps/db/test/support/factory.ex @@ -25,7 +25,7 @@ defmodule DB.Factory do def user_factory do %User{ - name: Faker.Name.first_name(), + name: Faker.Person.first_name(), username: "User-#{random_string(10)}", email: Faker.Internet.email(), encrypted_password: "$2b$12$fe55IfCdqNzKp1wMIJDwVeG3f7guOduEE5HS2C9IJyfkuk3avbjQG", @@ -65,8 +65,8 @@ defmodule DB.Factory do def speaker_factory do %Speaker{ - full_name: Faker.Name.name(), - title: Faker.Name.title(), + full_name: Faker.Person.name(), + title: Faker.Person.title(), country: Faker.Address.country_code() } end