From b559bd9dc37a49b73e0788ea8881ff21094ba10a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 30 Aug 2019 19:44:51 -0400 Subject: [PATCH] Add OPML export --- lib/frenzy/opml/exporter.ex | 32 +++++++++++++++++++ .../controllers/account_controller.ex | 6 ++++ lib/frenzy_web/router.ex | 1 + .../templates/account/show.html.eex | 3 ++ mix.exs | 2 +- 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 lib/frenzy/opml/exporter.ex diff --git a/lib/frenzy/opml/exporter.ex b/lib/frenzy/opml/exporter.ex new file mode 100644 index 0000000..da8684d --- /dev/null +++ b/lib/frenzy/opml/exporter.ex @@ -0,0 +1,32 @@ +defmodule Frenzy.OPML.Exporter do + import XmlBuilder + + @spec export([Frenzy.Group.t()]) :: String.t() + def export(groups) do + element(:opml, %{version: "1.0"}, [ + element(:head, %{}, [ + element(:title, "Frenzy export") + ]), + element( + :body, + %{}, + Enum.map(groups, fn group -> + element( + :outline, + %{text: group.title, title: group.title}, + Enum.map(group.feeds, fn feed -> + element(:outline, %{ + type: "rss", + text: feed.title, + title: feed.title, + xmlUrl: feed.feed_url, + htmlUrl: feed.site_url + }) + end) + ) + end) + ) + ]) + |> generate() + end +end diff --git a/lib/frenzy_web/controllers/account_controller.ex b/lib/frenzy_web/controllers/account_controller.ex index 34e1d93..2343d39 100644 --- a/lib/frenzy_web/controllers/account_controller.ex +++ b/lib/frenzy_web/controllers/account_controller.ex @@ -146,4 +146,10 @@ defmodule FrenzyWeb.AccountController do ) |> redirect(to: Routes.group_path(Endpoint, :index)) end + + def export(conn, _params) do + user = conn.assigns[:user] |> Repo.preload(:groups) + opml = Frenzy.OPML.Exporter.export(user.groups) + send_download(conn, {:binary, opml}, filename: "frenzy_export.opml") + end end diff --git a/lib/frenzy_web/router.ex b/lib/frenzy_web/router.ex index e5dc4d8..a17d98f 100644 --- a/lib/frenzy_web/router.ex +++ b/lib/frenzy_web/router.ex @@ -44,6 +44,7 @@ defmodule FrenzyWeb.Router do post "/account/change_fever_password", AccountController, :do_change_fever_password post "/account/remove_client", AccountController, :remove_client post "/account/import", AccountController, :import + post "/account/export", AccountController, :export get "/", GroupController, :index resources "/groups", GroupController, except: [:edit, :update] diff --git a/lib/frenzy_web/templates/account/show.html.eex b/lib/frenzy_web/templates/account/show.html.eex index ecca72e..e7f48f3 100644 --- a/lib/frenzy_web/templates/account/show.html.eex +++ b/lib/frenzy_web/templates/account/show.html.eex @@ -11,6 +11,9 @@ <%= file_input f, :file %> <%= submit "Import OPML", class: "btn btn-primary" %> <% end %> + <%= form_for @conn, Routes.account_path(@conn, :export), [method: :post, class: "mt-2"], fn f -> %> + <%= submit "Export OPML", class: "btn btn-primary" %> + <% end %>
diff --git a/mix.exs b/mix.exs index fbc60a9..1e5ecb5 100644 --- a/mix.exs +++ b/mix.exs @@ -48,7 +48,7 @@ defmodule Frenzy.MixProject do {:timex, "~> 3.0"}, {:readability, git: "https://github.com/shadowfacts/readability.git", branch: "master"}, {:bcrypt_elixir, "~> 2.0"}, - {:dialyxir, "~> 1.0.0-rc.6"}, + {:dialyxir, "~> 1.0.0-rc.6", only: :dev, runtime: false}, {:xml_builder, "~> 2.1.1"} ] end