Compare commits
5 commits
f3c35e8e55
...
b63eaa34be
Author | SHA1 | Date | |
---|---|---|---|
b63eaa34be | |||
82c641035a | |||
76ec1d144f | |||
7372e0fdc1 | |||
385afb9308 |
12 changed files with 134 additions and 52 deletions
|
@ -89,7 +89,9 @@ defmodule Nulla.ActivityPub do
|
||||||
def note(domain, note) do
|
def note(domain, note) do
|
||||||
attachment =
|
attachment =
|
||||||
case note.media_attachments do
|
case note.media_attachments do
|
||||||
[] -> []
|
[] ->
|
||||||
|
[]
|
||||||
|
|
||||||
attachments ->
|
attachments ->
|
||||||
[
|
[
|
||||||
attachment:
|
attachment:
|
||||||
|
@ -151,8 +153,16 @@ defmodule Nulla.ActivityPub do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec following(String.t(), Nulla.Models.User.t(), Integer.t(), List.t(), Integer.t(), Integer.t()) :: Jason.OrderedObject.t()
|
@spec following(
|
||||||
def following(domain, user, total, following_list, page, offset) when is_integer(page) and page > 0 do
|
String.t(),
|
||||||
|
Nulla.Models.User.t(),
|
||||||
|
Integer.t(),
|
||||||
|
List.t(),
|
||||||
|
Integer.t(),
|
||||||
|
Integer.t()
|
||||||
|
) :: Jason.OrderedObject.t()
|
||||||
|
def following(domain, user, total, following_list, page, offset)
|
||||||
|
when is_integer(page) and page > 0 do
|
||||||
data = [
|
data = [
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
id: "https://#{domain}/@#{user.username}/following?page=#{page}",
|
id: "https://#{domain}/@#{user.username}/following?page=#{page}",
|
||||||
|
@ -194,8 +204,16 @@ defmodule Nulla.ActivityPub do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec followers(String.t(), Nulla.Models.User.t(), Integer.t(), List.t(), Integer.t(), Integer.t()) :: Jason.OrderedObject.t()
|
@spec followers(
|
||||||
def followers(domain, user, total, followers_list, page, offset) when is_integer(page) and page > 0 do
|
String.t(),
|
||||||
|
Nulla.Models.User.t(),
|
||||||
|
Integer.t(),
|
||||||
|
List.t(),
|
||||||
|
Integer.t(),
|
||||||
|
Integer.t()
|
||||||
|
) :: Jason.OrderedObject.t()
|
||||||
|
def followers(domain, user, total, followers_list, page, offset)
|
||||||
|
when is_integer(page) and page > 0 do
|
||||||
data = [
|
data = [
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
id: "https://#{domain}/@#{user.username}/followers?page=#{page}",
|
id: "https://#{domain}/@#{user.username}/followers?page=#{page}",
|
||||||
|
@ -225,4 +243,17 @@ defmodule Nulla.ActivityPub do
|
||||||
|
|
||||||
Jason.OrderedObject.new(data)
|
Jason.OrderedObject.new(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def webfinger(domain, username, resource) do
|
||||||
|
Jason.OrderedObject.new(
|
||||||
|
subject: resource,
|
||||||
|
links: [
|
||||||
|
Jason.OrderedObject.new(
|
||||||
|
rel: "self",
|
||||||
|
type: "application/activity+json",
|
||||||
|
href: "https://#{domain}/@#{username}"
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,8 +5,11 @@ defmodule Nulla.KeyGen do
|
||||||
{:RSAPrivateKey, :"two-prime", n, e, _d, _p, _q, _dp, _dq, _qi, _other} = rsa_key
|
{:RSAPrivateKey, :"two-prime", n, e, _d, _p, _q, _dp, _dq, _qi, _other} = rsa_key
|
||||||
public_key = {:RSAPublicKey, n, e}
|
public_key = {:RSAPublicKey, n, e}
|
||||||
|
|
||||||
private_entry = {:PrivateKeyInfo, :public_key.der_encode(:RSAPrivateKey, rsa_key), :not_encrypted}
|
private_entry =
|
||||||
public_entry = {:SubjectPublicKeyInfo, :public_key.der_encode(:RSAPublicKey, public_key), :not_encrypted}
|
{:PrivateKeyInfo, :public_key.der_encode(:RSAPrivateKey, rsa_key), :not_encrypted}
|
||||||
|
|
||||||
|
public_entry =
|
||||||
|
{:SubjectPublicKeyInfo, :public_key.der_encode(:RSAPublicKey, public_key), :not_encrypted}
|
||||||
|
|
||||||
private_pem = :public_key.pem_encode([private_entry])
|
private_pem = :public_key.pem_encode([private_entry])
|
||||||
public_pem = :public_key.pem_encode([public_entry])
|
public_pem = :public_key.pem_encode([public_entry])
|
||||||
|
|
|
@ -77,5 +77,7 @@ defmodule Nulla.Models.User do
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_user_by_username(username), do: Repo.get_by(User, username: username)
|
||||||
|
|
||||||
def get_user_by_username!(username), do: Repo.get_by!(User, username: username)
|
def get_user_by_username!(username), do: Repo.get_by!(User, username: username)
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,15 +32,17 @@ defmodule Nulla.Utils do
|
||||||
offset = (page - 1) * per_page
|
offset = (page - 1) * per_page
|
||||||
|
|
||||||
query =
|
query =
|
||||||
from [f, u] in
|
from(
|
||||||
from(f in Follow,
|
[f, u] in from(f in Follow,
|
||||||
join: u in User, on: u.id == f.target_id,
|
join: u in User,
|
||||||
|
on: u.id == f.target_id,
|
||||||
where: f.user_id == ^user_id,
|
where: f.user_id == ^user_id,
|
||||||
order_by: [asc: u.inserted_at],
|
order_by: [asc: u.inserted_at],
|
||||||
offset: ^offset,
|
offset: ^offset,
|
||||||
limit: ^per_page,
|
limit: ^per_page,
|
||||||
select: u
|
select: u
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
users = Repo.all(query)
|
users = Repo.all(query)
|
||||||
|
|
||||||
|
@ -73,7 +75,8 @@ defmodule Nulla.Utils do
|
||||||
query =
|
query =
|
||||||
from f in Follow,
|
from f in Follow,
|
||||||
where: f.target_id == ^user_id,
|
where: f.target_id == ^user_id,
|
||||||
join: u in User, on: u.id == f.user_id,
|
join: u in User,
|
||||||
|
on: u.id == f.user_id,
|
||||||
order_by: [asc: u.inserted_at],
|
order_by: [asc: u.inserted_at],
|
||||||
offset: ^offset,
|
offset: ^offset,
|
||||||
limit: ^per_page,
|
limit: ^per_page,
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
defmodule NullaWeb.FollowController do
|
defmodule NullaWeb.FollowController do
|
||||||
use NullaWeb, :controller
|
use NullaWeb, :controller
|
||||||
alias Nulla.Models.User
|
|
||||||
alias Nulla.Models.InstanceSettings
|
|
||||||
alias Nulla.ActivityPub
|
alias Nulla.ActivityPub
|
||||||
alias Nulla.Utils
|
alias Nulla.Utils
|
||||||
|
alias Nulla.Models.User
|
||||||
|
alias Nulla.Models.InstanceSettings
|
||||||
|
|
||||||
def following(conn, %{"username" => username, "page" => page_param}) do
|
def following(conn, %{"username" => username, "page" => page_param}) do
|
||||||
instance_settings = InstanceSettings.get_instance_settings!()
|
instance_settings = InstanceSettings.get_instance_settings!()
|
||||||
|
@ -11,11 +11,13 @@ defmodule NullaWeb.FollowController do
|
||||||
offset = instance_settings.offset
|
offset = instance_settings.offset
|
||||||
user = User.get_user_by_username!(username)
|
user = User.get_user_by_username!(username)
|
||||||
total = Utils.count_following_by_username!(user.username)
|
total = Utils.count_following_by_username!(user.username)
|
||||||
|
|
||||||
page =
|
page =
|
||||||
case Integer.parse(page_param) do
|
case Integer.parse(page_param) do
|
||||||
{int, _} when int > 0 -> int
|
{int, _} when int > 0 -> int
|
||||||
_ -> 1
|
_ -> 1
|
||||||
end
|
end
|
||||||
|
|
||||||
following_list = Utils.get_following_users_by_username!(user.username, page)
|
following_list = Utils.get_following_users_by_username!(user.username, page)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|
@ -40,11 +42,13 @@ defmodule NullaWeb.FollowController do
|
||||||
offset = instance_settings.offset
|
offset = instance_settings.offset
|
||||||
user = User.get_user_by_username!(username)
|
user = User.get_user_by_username!(username)
|
||||||
total = Utils.count_followers_by_username!(user.username)
|
total = Utils.count_followers_by_username!(user.username)
|
||||||
|
|
||||||
page =
|
page =
|
||||||
case Integer.parse(page_param) do
|
case Integer.parse(page_param) do
|
||||||
{int, _} when int > 0 -> int
|
{int, _} when int > 0 -> int
|
||||||
_ -> 1
|
_ -> 1
|
||||||
end
|
end
|
||||||
|
|
||||||
followers_list = Utils.get_followers_by_username!(user.username, page)
|
followers_list = Utils.get_followers_by_username!(user.username, page)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
defmodule NullaWeb.NoteController do
|
defmodule NullaWeb.NoteController do
|
||||||
use NullaWeb, :controller
|
use NullaWeb, :controller
|
||||||
alias Nulla.Repo
|
alias Nulla.Repo
|
||||||
|
alias Nulla.ActivityPub
|
||||||
alias Nulla.Models.Note
|
alias Nulla.Models.Note
|
||||||
alias Nulla.Models.InstanceSettings
|
alias Nulla.Models.InstanceSettings
|
||||||
alias Nulla.ActivityPub
|
|
||||||
|
|
||||||
def index(conn, _params) do
|
def index(conn, _params) do
|
||||||
notes = Notes.list_notes()
|
notes = Notes.list_notes()
|
||||||
|
@ -49,10 +49,10 @@ defmodule NullaWeb.NoteController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#def show(conn, %{"id" => id}) do
|
# def show(conn, %{"id" => id}) do
|
||||||
# note = Notes.get_note!(id)
|
# note = Notes.get_note!(id)
|
||||||
# render(conn, :show, note: note)
|
# render(conn, :show, note: note)
|
||||||
#end
|
# end
|
||||||
|
|
||||||
def edit(conn, %{"id" => id}) do
|
def edit(conn, %{"id" => id}) do
|
||||||
note = Notes.get_note!(id)
|
note = Notes.get_note!(id)
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
defmodule NullaWeb.UserController do
|
defmodule NullaWeb.UserController do
|
||||||
use NullaWeb, :controller
|
use NullaWeb, :controller
|
||||||
|
alias Nulla.ActivityPub
|
||||||
|
alias Nulla.Utils
|
||||||
alias Nulla.Models.User
|
alias Nulla.Models.User
|
||||||
alias Nulla.Models.Note
|
alias Nulla.Models.Note
|
||||||
alias Nulla.Models.InstanceSettings
|
alias Nulla.Models.InstanceSettings
|
||||||
alias Nulla.ActivityPub
|
|
||||||
alias Nulla.Utils
|
|
||||||
|
|
||||||
def show(conn, %{"username" => username}) do
|
def show(conn, %{"username" => username}) do
|
||||||
accept = List.first(get_req_header(conn, "accept"))
|
accept = List.first(get_req_header(conn, "accept"))
|
||||||
|
|
34
lib/nulla_web/controllers/webfinger_controller.ex
Normal file
34
lib/nulla_web/controllers/webfinger_controller.ex
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
defmodule NullaWeb.WebfingerController do
|
||||||
|
use NullaWeb, :controller
|
||||||
|
alias Nulla.Repo
|
||||||
|
alias Nulla.ActivityPub
|
||||||
|
alias Nulla.Models.User
|
||||||
|
alias Nulla.Models.InstanceSettings
|
||||||
|
|
||||||
|
def show(conn, %{"resource" => resource}) do
|
||||||
|
case Regex.run(~r/^acct:([^@]+)@(.+)$/, resource) do
|
||||||
|
[_, username, domain] ->
|
||||||
|
case User.get_user_by_username(username) do
|
||||||
|
nil ->
|
||||||
|
conn
|
||||||
|
|> put_status(:not_found)
|
||||||
|
|> json(%{error: "Not Found"})
|
||||||
|
|
||||||
|
user ->
|
||||||
|
instance_settings = InstanceSettings.get_instance_settings!()
|
||||||
|
|
||||||
|
if domain == instance_settings.domain do
|
||||||
|
json(conn, ActivityPub.webfinger(domain, username, resource))
|
||||||
|
else
|
||||||
|
conn
|
||||||
|
|> put_status(:not_found)
|
||||||
|
|> json(%{error: "Not Found"})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
_ ->
|
||||||
|
conn
|
||||||
|
|> put_status(:bad_request)
|
||||||
|
|> json(%{error: "Bad Request"})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -21,6 +21,8 @@ defmodule NullaWeb.Router do
|
||||||
scope "/", NullaWeb do
|
scope "/", NullaWeb do
|
||||||
pipe_through :browser
|
pipe_through :browser
|
||||||
|
|
||||||
|
get "/.well-known/webfinger", WebfingerController, :show
|
||||||
|
|
||||||
get "/@:username", UserController, :show
|
get "/@:username", UserController, :show
|
||||||
get "/@:username/following", FollowController, :following
|
get "/@:username/following", FollowController, :following
|
||||||
get "/@:username/followers", FollowController, :followers
|
get "/@:username/followers", FollowController, :followers
|
||||||
|
|
|
@ -21,11 +21,14 @@ defmodule Nulla.Repo.Migrations.CreateInstanceSettings do
|
||||||
execute(fn ->
|
execute(fn ->
|
||||||
{public_key, private_key} = Nulla.KeyGen.generate_keys()
|
{public_key, private_key} = Nulla.KeyGen.generate_keys()
|
||||||
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
||||||
|
|
||||||
domain =
|
domain =
|
||||||
Application.get_env(:nulla, NullaWeb.Endpoint, [])
|
Application.get_env(:nulla, NullaWeb.Endpoint, [])
|
||||||
|> Keyword.get(:url, [])
|
|> Keyword.get(:url, [])
|
||||||
|> Keyword.get(:host, "localhost")
|
|> Keyword.get(:host, "localhost")
|
||||||
|
|
||||||
esc = fn str -> "'#{String.replace(str, "'", "''")}'" end
|
esc = fn str -> "'#{String.replace(str, "'", "''")}'" end
|
||||||
|
|
||||||
sql = """
|
sql = """
|
||||||
INSERT INTO instance_settings (
|
INSERT INTO instance_settings (
|
||||||
name, description, domain, registration,
|
name, description, domain, registration,
|
||||||
|
|
|
@ -8,7 +8,7 @@ defmodule Nulla.Repo.Migrations.CreateUsers do
|
||||||
add :password, :string
|
add :password, :string
|
||||||
add :is_moderator, :boolean, default: false, null: false
|
add :is_moderator, :boolean, default: false, null: false
|
||||||
add :realname, :string
|
add :realname, :string
|
||||||
add :bio, :string
|
add :bio, :text
|
||||||
add :location, :string
|
add :location, :string
|
||||||
add :birthday, :date
|
add :birthday, :date
|
||||||
add :fields, :map
|
add :fields, :map
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 152 B After Width: | Height: | Size: 161 KiB |
Loading…
Add table
Add a link
Reference in a new issue