diff --git a/lib/post.ex b/lib/post.ex index eaddc2e..d4cbe30 100644 --- a/lib/post.ex +++ b/lib/post.ex @@ -4,6 +4,7 @@ defmodule PlugIndie.Post do :content, :like_of, :title, - :type + :type, + :published_at ] end diff --git a/lib/properties.ex b/lib/properties.ex index cb39b92..dbdf665 100644 --- a/lib/properties.ex +++ b/lib/properties.ex @@ -3,12 +3,23 @@ defmodule PlugIndie.Properties do {:ok, type} = get_post_type(properties) content = get_content(properties) title = get_title(properties) + published = is_published?(properties) + published_at = if published, do: DateTime.utc_now(), else: nil case type do :note -> {:ok, %PlugIndie.Post{ type: type, + published_at: published_at, + content: content + }} + + :article -> + {:ok, + %PlugIndie.Post{ + type: type, + published_at: published_at, title: title, content: content }} @@ -17,6 +28,7 @@ defmodule PlugIndie.Properties do {:ok, %PlugIndie.Post{ type: type, + published_at: published_at, title: title, content: content, bookmark_of: get_bookmarked_url(properties) @@ -26,6 +38,7 @@ defmodule PlugIndie.Properties do {:ok, %PlugIndie.Post{ type: type, + published_at: published_at, like_of: get_liked_url(properties) }} @@ -42,9 +55,12 @@ defmodule PlugIndie.Properties do Map.has_key?(properties, "bookmark-of") -> {:ok, :bookmark} - Map.has_key?(properties, "content") -> + Map.has_key?(properties, "content") and not Map.has_key?(properties, "name") -> {:ok, :note} + Map.has_key?(properties, "content") and Map.has_key?(properties, "name") -> + {:ok, :article} + true -> {:ok, :unknown} end diff --git a/lib/token.ex b/lib/token.ex index 4403325..b51b031 100644 --- a/lib/token.ex +++ b/lib/token.ex @@ -60,15 +60,6 @@ defmodule PlugIndie.Token do supported_scopes, own_hostname ) do - # {%{ - # "client_id" => "https://indiepass.app/", - # "issued_at" => 1_733_382_601, - # "issued_by" => "https://tokens.indieauth.com/token", - # "me" => "https://blog.inhji.de/", - # "nonce" => 358_618_865, - # "scope" => "create update delete media read follow channels mute block" - # }, "create", ["create", "media"], "inhji.de"} - Logger.info("Host-URI: '#{host_uri}'") Logger.info("ClientId: '#{client_id}'") Logger.info("Scopes: '#{scope}'") diff --git a/test/properties_test.exs b/test/properties_test.exs index 54dfb27..d3dc805 100644 --- a/test/properties_test.exs +++ b/test/properties_test.exs @@ -12,21 +12,35 @@ defmodule PropertiesTest do %PlugIndie.Post{ type: :note, content: "Hello World!" - }} == parse(props) + }} = parse(props) + end + + test "parse/1 identifies an article" do + props = %{ + "name" => ["My Title"], + "content" => ["Hello World!"] + } + + assert {:ok, + %PlugIndie.Post{ + type: :article, + content: "Hello World!", + title: "My Title" + }} = parse(props) end test "parse/1 identifies a bookmark" do props = %{ - "content" => ["Hello World!"], + "content" => ["100 Rabbits"], "bookmark-of" => ["https://100r.co"] } assert {:ok, %PlugIndie.Post{ type: :bookmark, - content: "Hello World!", + content: "100 Rabbits", bookmark_of: "https://100r.co" - }} == parse(props) + }} = parse(props) end test "parse/1 identifies a like" do @@ -38,6 +52,6 @@ defmodule PropertiesTest do %PlugIndie.Post{ type: :like, like_of: "https://100r.co" - }} == parse(props) + }} = parse(props) end end diff --git a/test/token_test.exs b/test/token_test.exs index 82dfe6c..9634749 100644 --- a/test/token_test.exs +++ b/test/token_test.exs @@ -39,13 +39,36 @@ defmodule TokenTest do supported_scopes = ["create", "media"] hostname = "blog.inhji.de" - assert PlugIndie.Token.verify_token_response( - body, - required_scope, - supported_scopes, - hostname - ) == - :ok + assert :ok == + PlugIndie.Token.verify_token_response( + body, + required_scope, + supported_scopes, + hostname + ) + end + + test "with mismatching hostname" do + body = %{ + "client_id" => "https://indiepass.app/", + "issued_at" => 1_733_382_601, + "issued_by" => "https://tokens.indieauth.com/token", + "me" => "https://inhji.wtf/", + "nonce" => 358_618_865, + "scope" => "create update delete media read follow channels mute block" + } + + required_scope = "create" + supported_scopes = ["create", "media"] + hostname = "blog.inhji.de" + + assert {:error, _, _} = + PlugIndie.Token.verify_token_response( + body, + required_scope, + supported_scopes, + hostname + ) end end end