~afontaine/home

9d096b67fa37ff3f73ea78c67edcce8fd85842b9 — Andrew Fontaine 8 months ago 936773f
Generate Mail Context

This is where all the mail parsing will happen for posts and comments.

Right now it just creates a Post struct by parsing what the lists.sr.ht
API sends us via a web hook.
3 files changed, 105 insertions(+), 0 deletions(-)

A apps/blog/lib/blog/mail.ex
A apps/blog/lib/blog/mail/post.ex
A apps/blog/test/blog/mail_test.exs
A apps/blog/lib/blog/mail.ex => apps/blog/lib/blog/mail.ex +26 -0
@@ 0,0 1,26 @@
defmodule Blog.Mail do
  @moduledoc """
  The Mail context.
  """

  import Ecto.Query, warn: false
  alias Blog.Repo

  alias Blog.Mail.Post

  @doc """
  Creates a post.

  ## Examples

      iex> create_post(%{field: value})
      {:ok, %Post{}}

      iex> create_post(%{field: bad_value})
      {:error, ...}

  """
  def create_post(attrs \\ %{}) do
    raise "TODO"
  end
end

A apps/blog/lib/blog/mail/post.ex => apps/blog/lib/blog/mail/post.ex +17 -0
@@ 0,0 1,17 @@
defmodule Blog.Mail.Post do
  use Ecto.Schema
  import Ecto.Changeset

  embedded_schema do
    field :message_id, :string
    field :subject, :string
    field :envelope, :string
  end

  @doc false
  def changeset(post, attrs) do
    post
    |> cast(attrs, [:message_id, :subject, :envelope])
    |> validate_required([:message_id, :subject, :envelope])
  end
end

A apps/blog/test/blog/mail_test.exs => apps/blog/test/blog/mail_test.exs +62 -0
@@ 0,0 1,62 @@
defmodule Blog.MailTest do
  use Blog.DataCase

  alias Blog.Mail

  describe "posts" do
    alias Blog.Mail.Post

    @valid_attrs %{}
    @update_attrs %{}
    @invalid_attrs %{}

    def post_fixture(attrs \\ %{}) do
      {:ok, post} =
        attrs
        |> Enum.into(@valid_attrs)
        |> Mail.create_post()

      post
    end

    test "list_posts/0 returns all posts" do
      post = post_fixture()
      assert Mail.list_posts() == [post]
    end

    test "get_post!/1 returns the post with given id" do
      post = post_fixture()
      assert Mail.get_post!(post.id) == post
    end

    test "create_post/1 with valid data creates a post" do
      assert {:ok, %Post{} = post} = Mail.create_post(@valid_attrs)
    end

    test "create_post/1 with invalid data returns error changeset" do
      assert {:error, %Ecto.Changeset{}} = Mail.create_post(@invalid_attrs)
    end

    test "update_post/2 with valid data updates the post" do
      post = post_fixture()
      assert {:ok, %Post{} = post} = Mail.update_post(post, @update_attrs)
    end

    test "update_post/2 with invalid data returns error changeset" do
      post = post_fixture()
      assert {:error, %Ecto.Changeset{}} = Mail.update_post(post, @invalid_attrs)
      assert post == Mail.get_post!(post.id)
    end

    test "delete_post/1 deletes the post" do
      post = post_fixture()
      assert {:ok, %Post{}} = Mail.delete_post(post)
      assert_raise Ecto.NoResultsError, fn -> Mail.get_post!(post.id) end
    end

    test "change_post/1 returns a post changeset" do
      post = post_fixture()
      assert %Ecto.Changeset{} = Mail.change_post(post)
    end
  end
end