Commit ed9c5f73 authored by Bat's avatar Bat
Browse files

Blog creation

parent 557e4038
......@@ -30,10 +30,15 @@ Amber::Server.configure do |app|
end
routes :web do
get "/", HomeController, :index
get "/configure", HomeController, :show_configure
post "/configure", HomeController, :configure
get "/blog", ActivityPub::BlogController, :show
get "/~:name", BlogController, :show
get "/blogs/new", BlogController, :new
post "/blogs", BlogController, :create
end
end
-- +micrate Up
ALTER TABLE instances ADD COLUMN hostname VARCHAR;
-- +micrate Down
ALTER TABLE instances DROP COLUMN hostname;
require "./spec_helper"
describe BlogController do
end
require "../spec_helper"
require "garnet_spec"
require "./utils"
require "./blog_object"
\ No newline at end of file
require "./blog_object"
class ActivityPub::BlogObject
include Utils
include ActivityPub::Utils
def initialize (blog : Blog)
@blog = blog
......@@ -20,4 +20,4 @@ class ActivityPub::BlogObject
end
end
end
end
\ No newline at end of file
end
module Utils
module ActivityPub::Utils
def url(path)
"https://#{Amber.settings.host}:#{Amber.settings.port}/#{path}/"
end
end
\ No newline at end of file
def make_actor_id(display_name)
display_name.downcase.split(/[^\w]/).join("-")
end
end
class BlogController < ApplicationController
include ActivityPub::Utils
def show
blog = Blog.from_name(make_actor_id(params["name"]))
title = blog.title
render("index.ecr")
end
def new
title = "New Blog"
render("new.ecr")
end
def create
blog = Blog.new
blog.title = params["title"]
blog.actor_id = make_actor_id(params["title"])
blog.title = params["title"]
blog.subtitle = params["subtitle"]
blog.description = params["description"]
blog.default_license = params["license"]
Repo.insert(blog)
redirect_to(controller: :blog, action: :show, status: 302, params: { "name" => blog.title.not_nil! })
end
end
......@@ -8,7 +8,12 @@ class Blog < Crecto::Model
belongs_to :instance, Instance
end
def self.get
Repo.all(Blog, Query.limit(1))[0]
def self.from_name (name : String)
parsed = name.split("@")
instance_id = Instance.get.id
if parsed.size > 1
instance_id = Repo.all(Instance, Query.where(hostname: parsed[1]))[0].id
end
Repo.all(Blog, Query.where(actor_id: parsed[0]).limit(1))[0]
end
end
class Instance < Crecto::Model
schema "instances" do
field :name, String
field :hostname, String
has_many :blogs, Blog, dependent: :destroy
has_many :accounts, Account, dependent: :destroy
end
......
<h1><%= blog.title %></h1>
<h2><%= blog.subtitle %></h2>
\ No newline at end of file
<main>
<h1>New Blog</h1>
<form method="POST" action="/blogs/">
<label for="title">Blog title</label>
<input name="title">
<label for="subtitle">Blog subtitle</label>
<input name="subtitle">
<label for="description">Blog description</label>
<input name="description">
<label for="license">Default articles license</label>
<input name="license" value="CC-0">
<%= csrf_tag %>
<input type="submit" value="Create">
</form>
</main>
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