Basic inbox handling

This commit is contained in:
Shadowfacts 2019-10-01 21:59:43 -04:00
parent 1b8f3e212c
commit d652b71426
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 47 additions and 4 deletions

24
lib/clacks/inbox.ex Normal file
View File

@ -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

View File

@ -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