Commit 2f52786d authored by Bat's avatar Bat
Browse files

Add webfinger

parent 630bc05d
require "json"
require "http/client"
require "./initializers/**"
......
......@@ -50,6 +50,8 @@ Amber::Server.configure do |app|
get "/~:name/:post", BlogController, :show_post
get "/blogs/new", BlogController, :new
post "/blogs", BlogController, :create
get "/.well-known/webfinger", WellknownController, :webfinger
end
routes :admin do
......
class HomeController < ApplicationController
def index
instance = Instance.get
instance = Instance.local
title = "Home"
recent_posts = Post.recent_posts
recent_posts.pretty_print(PrettyPrint.new(STDOUT))
......@@ -17,12 +17,12 @@ class HomeController < ApplicationController
end
def configure
puts "Configuring !"
instance = Instance.new
instance.name = params["name"]
instance.hostname = Amber.settings.host
Repo.insert(instance)
puts "INSTANCE :"
puts Instance.get
puts Instance.local
redirect_to(controller: :account, action: :new, status: 302, params: {
"title" => "Create the first user"
})
......
class WellknownController < ApplicationController
RES_TYPES = ["acct"]
def webfinger
res = params["ressource"].split(":")
res_type = res[0]
res_path = res[1].split("@")
if !RES_TYPES.includes?(res_type)
puts "not acct"
halt! 400, "Invalid ressource type"
else
res_name = res_path[0]
res_host = res_path[1]
if res_host != Instance.local.hostname
puts "not local, which is "
puts Instance.local.hostname
halt! 400, "Incorrect hostname"
else
acct = Repo.all(Account, Query.where(actor_id: res_name).limit(1))
blog = Repo.all(Blog, Query.where(actor_id: res_name).limit(1))
context.response.content_type = "application/jrd+json"
if acct.size > 0
serialize(acct[0])
elsif blog.size > 0
serialize(blog[0])
end
end
end
end
def serialize(obj)
JSON.build do |json|
json.object do
json.field "subject", params["ressource"]
json.field "links" do
json.array do
json.object do
json.field "rel", "self"
json.field "type", "application/activity+json"
json.field "href", "https://" + Instance.local.hostname.not_nil! + "/@" + obj.actor_id.not_nil!
end
end
end
end
end
end
end
......@@ -14,7 +14,7 @@ class Blog < Crecto::Model
def self.from_name (name : String)
parsed = name.split("@")
instance_id = Instance.get.id
instance_id = Instance.local.id
if parsed.size > 1
instance_id = Repo.all(Instance, Query.where(hostname: parsed[1]))[0].id
end
......
......@@ -6,7 +6,7 @@ class Instance < Crecto::Model
has_many :accounts, Account, dependent: :destroy
end
def self.get
def self.local
puts Repo.all(Instance).size
Repo.all(Instance, Query.limit(1))[0]
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment