Browse Source

Basic inbox handling

master
Shadowfacts 2 years ago
parent
commit
d652b71426
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
2 changed files with 47 additions and 4 deletions
  1. +24
    -0
      lib/clacks/inbox.ex
  2. +23
    -4
      lib/clacks_web/controllers/inbox_controller.ex

+ 24
- 0
lib/clacks/inbox.ex 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

+ 23
- 4
lib/clacks_web/controllers/inbox_controller.ex 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

Loading…
Cancel
Save