diff --git a/lib/clacks/inbox.ex b/lib/clacks/inbox.ex new file mode 100644 index 0000000..154f690 --- /dev/null +++ b/lib/clacks/inbox.ex @@ -0,0 +1,24 @@ +defmodule Clacks.Inbox do + alias Clacks.{Repo, Activity, Object, Actor} + + def handle_create(%{"type" => "Create", "object" => object, "actor" => actor} = activity) do + changeset = Object.changeset(%Object{}, %{data: object}) + {:ok, _object} = Repo.insert(changeset) + + changeset = + Activity.changeset(%Activity{}, %{ + data: activity, + local: false, + actor: actor + }) + + {:ok, _activity} = Repo.insert(changeset) + end + + def handle_follow(%{"type" => "Follow", "object" => followed_id, "actor" => follower}) + when is_binary(followed_id) do + followed = Actor.get_by_ap_id(followed_id) + changeset = Actor.changeset(followed, %{followers: [follower | followed.followers]}) + {:ok, _followed} = Repo.update(changeset) + end +end diff --git a/lib/clacks_web/controllers/inbox_controller.ex b/lib/clacks_web/controllers/inbox_controller.ex index 7383293..30f2589 100644 --- a/lib/clacks_web/controllers/inbox_controller.ex +++ b/lib/clacks_web/controllers/inbox_controller.ex @@ -1,14 +1,33 @@ defmodule ClacksWeb.InboxController do use ClacksWeb, :controller + alias Clacks.Inbox plug Plug.Parsers, parsers: [:urlencoded, :json], json_decoder: Jason plug ClacksWeb.Plug.HTTPSignature - def shared(conn, params) do - IO.inspect(params) + def shared(conn, _params) do + handle(conn, conn.body_params) end - def user_specific(conn, params) do - IO.inspect(params) + def user_specific(conn, _params) do + handle(conn, conn.body_params) end + + def handle(conn, %{"type" => "Create"} = activity) do + Inbox.handle_create(activity) + + conn + |> put_status(200) + end + + def handle(conn, %{"type" => "Follow", "object" => object} = activity) when is_binary(object) do + Inbox.handle_follow(activity) + + conn + |> put_status(200) + end + + # def handle(conn, _) do + # # todo: figure out how handle unhandled activity types + # end end