diff --git a/README.md b/README.md index fbedbf7..f4b0eda 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ - [ ] Unfollowing - [x] Being followed - [x] Accepting follows -- Approving / declining follows and authorized instance list +- [ ] Approving / declining follows and authorized instance list - [ ] Liking - [ ] Unliking @@ -16,7 +16,7 @@ - [ ] Posting - [ ] Timeline - [ ] Deleting posts -- [ ] Following +- [~] Following - [ ] Unfollowing - [ ] Being followed - [ ] Accepting follows diff --git a/lib/postland/activities.ex b/lib/postland/activities.ex index e97b4b3..8589617 100644 --- a/lib/postland/activities.ex +++ b/lib/postland/activities.ex @@ -7,13 +7,47 @@ defmodule Postland.Activities do alias Postland.Repo alias Postland.Follows + def record_markdown_post(markdown) do + id = Ecto.UUID.autogenerate() + html = Earmark.as_html!(markdown) + + %{ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => url(~p"/activities/#{id}"), + "type" => "Create", + "actor" => Postland.my_actor_id(), + "object" => [ + %{ + "id" => url(~p"/activities/#{id}"), + "type" => "Note", + "published" => DateTime.utc_now() |> DateTime.to_iso8601(), + "attributedTo" => Postland.my_actor_id(), + "mediaType" => "text/html", + "content" => html, + "to" => "https://www.w3.org/ns/activitystreams#Public" + }, + %{ + "id" => url(~p"/activities/#{id}"), + "type" => "Note", + "published" => DateTime.utc_now() |> DateTime.to_iso8601(), + "attributedTo" => Postland.my_actor_id(), + "mediaType" => "text/markdown", + "content" => markdown, + "to" => "https://www.w3.org/ns/activitystreams#Public" + } + ] + } + |> Postland.Activity.changeset() + |> Repo.insert() + end + def process_activity(params) do case record_activity(params) do {:ok, activity} -> cause_effects(activity) - other -> - other + other -> + other end end @@ -23,16 +57,20 @@ defmodule Postland.Activities do |> Repo.insert() end - def cause_effects(%Activity{type: "Accept", data: %{"object" => %{"type" => "Follow", "id" => follow_id}}} = activity) do + def cause_effects( + %Activity{type: "Accept", data: %{"object" => %{"type" => "Follow", "id" => follow_id}}} = + activity + ) do pattern = ~r|/follows/([^/]+)| - actor_id = case Regex.run(pattern, follow_id) do - [_, encoded_actor_id] -> - Base.url_decode64!(encoded_actor_id, padding: false) + actor_id = + case Regex.run(pattern, follow_id) do + [_, encoded_actor_id] -> + Base.url_decode64!(encoded_actor_id, padding: false) - _other -> - nil - end + _other -> + nil + end case Follows.get(Postland.my_actor_id(), actor_id) do nil -> @@ -44,14 +82,17 @@ defmodule Postland.Activities do Follows.confirm_request(request) end - {:ok, activity} + {:ok, activity} end - def cause_effects(%Activity{actor_id: actor_id, type: "Follow", data: %{"object" => object}} = activity) do + def cause_effects( + %Activity{actor_id: actor_id, type: "Follow", data: %{"object" => object}} = activity + ) do if object == Postland.my_actor_id() do case Postland.Follows.record_inbound_request(actor_id) do {:ok, _follow} -> {:ok, activity} + other -> other end diff --git a/lib/postland/post.ex b/lib/postland/post.ex new file mode 100644 index 0000000..a00ea48 --- /dev/null +++ b/lib/postland/post.ex @@ -0,0 +1,16 @@ +defmodule Postland.Post do + def html_content(activity) do + case Map.get(activity.data, "object") do + map when is_map(map) -> + Map.get(map, "content") + + list when is_list(list) -> + Enum.find_value(list, fn map -> + Map.get(map, "mediaType") == "text/html" && Map.get(map, "content") + end) || "" + + nil -> + "" + end + end +end diff --git a/lib/postland_web/components/core_components.ex b/lib/postland_web/components/core_components.ex index 1b58fae..4915bca 100644 --- a/lib/postland_web/components/core_components.ex +++ b/lib/postland_web/components/core_components.ex @@ -19,6 +19,64 @@ defmodule PostlandWeb.CoreComponents do alias Phoenix.LiveView.JS use Gettext, backend: PostlandWeb.Gettext + def post_form(assigns) do + ~H""" +