Skip to content

Instantly share code, notes, and snippets.

@Nezteb
Created February 22, 2024 18:22
Show Gist options
  • Save Nezteb/c924f76f71891d5c5f0013cba2cb4983 to your computer and use it in GitHub Desktop.
Save Nezteb/c924f76f71891d5c5f0013cba2cb4983 to your computer and use it in GitHub Desktop.
See discussion here: https://elixirforum.com/t/official-discourse-follow-plugin-for-elixirforum/61603 (requires Discourse auth because it's in a "members only" section)

ElixirForum Discourse RSS OPML Generator

Section

defmodule Discourse.OPML do
  defmodule Behaviour do
    @callback outline(list(String.t())) :: String.t()
  end

  def generate(usernames, forum_specific_generator) do
    outline = forum_specific_generator.outline(usernames)

    """
    <?xml version="1.0" encoding="UTF-8"?>
    <opml version="1.0">
      <head>
        <title>RSS OPML Subscriptions</title>
      </head>
      <body>
        #{outline}
      </body>
    </opml>
    """
  end

  defmodule ElixirForum do
    @behaviour Discourse.OPML.Behaviour

    @impl true
    def outline(usernames) do
      inner_outlines = Enum.map(usernames, &outlines_for_user/1)

      """
      <outline title="ElixirForum" text="ElixirForum">
        #{inner_outlines}
      </outline>
      """
    end

    defp outlines_for_user(username) do
      """
      <outline
        text="Latest posts by @#{username}"
        title="Latest posts by @#{username}"
        type="rss" 
        xmlUrl="https://elixirforum.com/users/#{username}/activity.rss"
        htmlUrl="https://elixirforum.com/u/#{username}/activity"
      />
      <outline
        text="Latest topics by @#{username}"
        title="Latest topics by @#{username}"
        type="rss" 
        xmlUrl="https://elixirforum.com/users/#{username}/activity/topics.rss"
        htmlUrl="https://elixirforum.com/u/#{username}/activity/topics"
      />
      """
    end
  end
end
[
  "josevalim",
  "mhanberg",
  "wojtekmach",
  "nicd",
  "dorgan",
  "axelson",
  "benwilson512",
  "LostKobrakai",
  "Fl4m3Ph03n1x",
  "NobbZ",
  "hauleth",
  "astonj",
  "michalmuskala",
  "mbuhot",
  "ConnorRigby",
  "derpycoder"
]
|> Enum.map(&String.downcase/1)
|> Enum.sort()
|> Discourse.OPML.generate(Discourse.OPML.ElixirForum)
|> IO.puts()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment