defmodule Mastomation do @moduledoc """ CLI helpers to fetch and remove Mastodon statuses. """ alias Mastomation.Delete alias Mastomation.Notes alias Mastomation.Parser alias Mastomation.Threads @doc """ Parses delete-related CLI options from raw arguments. """ @spec parse([String.t()]) :: map() def parse(args \\ []) do Delete.parse_options(args) end @doc """ Executes the CLI command router. """ @spec run([String.t()]) :: :ok | :error def run(args \\ []) do parse_args = if args == [], do: ["--help"], else: args dispatch(Parser.parse(parse_args), parse_args) end @spec dispatch(term(), [String.t()]) :: :ok | :error defp dispatch({:ok, [:thread, :see], result}, _parse_args), do: Threads.run_thread_see(result) defp dispatch({:ok, [:thread, :export], result}, _parse_args), do: Threads.run_thread_export(result) defp dispatch({:ok, [:thread], result}, _parse_args), do: Threads.run_thread_see(result) defp dispatch({:ok, [:delete, :all], result}, _parse_args), do: Delete.run_delete_all(result) defp dispatch({:ok, [:delete, :thread], result}, _parse_args), do: Delete.run_delete_thread(result) defp dispatch({:ok, [:delete], result}, _parse_args), do: Delete.run_delete(result) defp dispatch({:ok, [:notes, :download], result}, _parse_args), do: run_notes_download(result) defp dispatch({:ok, [:notes, :search], result}, _parse_args), do: run_notes_search(result) defp dispatch(:help, parse_args), do: print_parser_help(parse_args, :ok) defp dispatch({:help, _subcommand_path}, parse_args), do: print_parser_help(parse_args, :ok) defp dispatch(:version, parse_args), do: print_parser_help(parse_args, :ok) defp dispatch({:error, _errors}, parse_args), do: print_parser_help(parse_args, :error) defp dispatch(_other, _parse_args), do: :ok @spec print_parser_help([String.t()], :ok | :error) :: :ok | :error defp print_parser_help(parse_args, status) do _ = Parser.print_and_parse!(parse_args) status end @spec run_notes_download(map()) :: :ok defp run_notes_download(result) do options = result.options instance = options.override_instance_url || options.instance_url || System.get_env("MASTODON_INSTANCE_URL") token = options.override_access_token || options.access_token || System.get_env("MASTODON_ACCESS_TOKEN") path_opt = if options.path in [nil, ""], do: [], else: [path: options.path] {:ok, path, entries} = Notes.download(instance, token, path_opt) IO.puts("saved #{Enum.count(entries)} notes to #{path}") :ok end @spec run_notes_search(map()) :: :ok | :error defp run_notes_search(result) do options = result.options args = result.args path_opt = if options.path in [nil, ""], do: [], else: [path: options.path] terms = [args.query | options.term] |> Enum.reject(&(&1 in [nil, ""])) case Notes.search(terms, path_opt) do {:ok, entries} -> instance_url = System.get_env("MASTODON_INSTANCE_URL") Enum.each(entries, fn entry -> IO.puts("#{entry.username} (@#{entry.handle})") IO.puts("profile: #{profile_url(instance_url, entry.handle)}") IO.puts("note: #{entry.note}") IO.puts("") end) IO.puts("found #{Enum.count(entries)} matches") :ok {:error, :notes_file_not_found} -> IO.puts("notes file not found. run `mastomation notes download` first.") :error end end @spec profile_url(String.t() | nil, String.t()) :: String.t() defp profile_url(nil, handle), do: "@#{handle}" defp profile_url(instance_url, handle) do normalized = String.trim_trailing(instance_url, "/") case System.get_env("MASTODON_UI") do "DECK" -> "#{normalized}/deck/@#{handle}" "deck" -> "#{normalized}/deck/@#{handle}" _ -> "#{normalized}/@#{handle}" end end @doc """ Decodes HTTP response bodies into maps/lists when needed. """ @spec decode!(binary() | map() | list()) :: map() | list() def decode!(result) do case result do binary when is_binary(binary) -> JSON.decode!(binary) %{} = map -> map list when is_list(list) -> list other -> raise "Unexpected body type: #{inspect(other)}" end end @doc """ Builds authorization/content headers for Mastodon API requests. """ @spec build_header(String.t()) :: keyword(String.t()) def build_header(token) do [ authorization: "Bearer #{token}", "content-type": "application/json" ] end end