Commit 561aa5d6 authored by Bat's avatar Bat
Browse files

List posts

parent 75c493d4
......@@ -3,8 +3,8 @@ require "json"
require "./initializers/**"
require "amber"
require "../src/models/**"
require "../src/activity_pub"
require "../src/models/**"
require "../src/controllers/application_controller"
require "../src/controllers/**"
......
......@@ -4,12 +4,12 @@ class AccountController < ApplicationController
def show
account = Account.from_name(make_actor_id(params["name"]))
title = "@" + account.actor_id.not_nil!
render("index.ecr")
template("index.ecr")
end
def new
title = params["title"] || "Register"
render("new.ecr")
template("new.ecr")
end
def create
......
......@@ -4,6 +4,19 @@ class ApplicationController < Amber::Controller::Base
include JasperHelpers
LAYOUT = "application.ecr"
def get_blog?
begin
Blog.from_name(params["name"])
rescue exception
nil
end
end
macro template(template = nil, layout = true, partial = nil, path = "src/views", folder = __FILE__)
blog = get_blog?
render({{template}}, layout: {{layout}}, partial: {{partial}}, path: {{path}}, folder: {{folder}})
end
def get_account
begin
acct
......
class BlogController < ApplicationController
include ActivityPub::Utils
def get_blog
begin
Blog.from_name(params["name"])
rescue exception
halt!(404, "Not found")
Blog.new
end
end
def get_blog : Blog
get_blog?.not_nil!
end
def show
blog = get_blog
title = blog.title
render("index.ecr")
if get_blog?
blog = get_blog
recent_posts = Post.recent_posts(blog: blog)
title = blog.title
template("index.ecr")
else
halt!(404, "Blog not found")
end
end
def new
title = "New Blog"
render("new.ecr")
template("new.ecr")
end
def create
......@@ -33,14 +33,15 @@ class BlogController < ApplicationController
end
def new_post
title = "New Post in " + get_blog.title.not_nil!
render("new_post.ecr")
title = "New Post in " + get_blog.title.to_s
template("new_post.ecr")
end
def create_post
post = Post.new
post.title = params["title"]
post.content = params["content"]
post.blog = get_blog
id = Repo.insert(post).instance.id.to_s
respond_with {
json ({
......@@ -57,6 +58,6 @@ class BlogController < ApplicationController
.limit(1)
)[0]
title = post.title
render("show_post.ecr")
template("show_post.ecr")
end
end
......@@ -2,15 +2,17 @@ class HomeController < ApplicationController
def index
instance = Instance.get
title = "Home"
render("index.ecr")
recent_posts = Post.recent_posts
recent_posts.pretty_print(PrettyPrint.new(STDOUT))
template("index.ecr")
end
def show_configure
title = "Configuration"
if Instance.initialized?
render("config_success.ecr")
template("config_success.ecr")
else
render("initial_config.ecr")
template("initial_config.ecr")
end
end
......
......@@ -2,7 +2,7 @@ class SessionController < ApplicationController
def new
account = Account.new
title = "Sign In"
render("new.ecr")
template("new.ecr")
end
def create
......@@ -15,7 +15,7 @@ class SessionController < ApplicationController
flash[:danger] = "Invalid email or password"
title = "Sign In"
account = Account.new
render("new.ecr")
template("new.ecr")
end
end
......
class Blog < Crecto::Model
include ActivityPub::Utils
schema "blogs" do
field :actor_id, String
field :title, String
......@@ -16,6 +18,11 @@ class Blog < Crecto::Model
if parsed.size > 1
instance_id = Repo.all(Instance, Query.where(hostname: parsed[1]))[0].id
end
# TODO: use instance_id
Repo.all(Blog, Query.where(actor_id: parsed[0]).limit(1))[0]
end
def url
"/~" + actor_id.to_s
end
end
class Post < Crecto::Model
include ActivityPub::Utils
schema "posts" do
field :title, String
field :illustration, String
......@@ -7,4 +8,21 @@ class Post < Crecto::Model
field :content, String
field :published, Bool
end
def get_blog
Repo.get_association(self, :blog).as(Blog)
end
def url
get_blog.url + "/" + id.to_s
end
def self.recent_posts(limit = 10, blog : Blog | Nil = nil)
query = Query.order_by("posts.created_at DESC")
if blog
Repo.all(Post, query.where(blog_id: blog.id).limit(limit))
else
Repo.all(Post, query.limit(limit))
end
end
end
<h1><%= blog.title %></h1>
<h2><%= blog.subtitle %></h2>
\ No newline at end of file
<h1><%= blog.not_nil!.title.to_s %></h1>
<h2><%= blog.not_nil!.subtitle.to_s %></h2>
<h2>Latest posts</h2>
<% recent_posts.each do |p| %>
<%= link_to p.title.not_nil!, p.url %>, in <%= link_to p.get_blog.title.to_s, p.get_blog.url %>
<% end %>
......@@ -2,3 +2,9 @@
<% if get_account != nil %>
<p>Welcome <%= acct.display_name %>!</p>
<% end %>
<h2>Latest posts</h2>
<% recent_posts.each do |p| %>
<%= link_to p.title.not_nil!, p.url %>, in <%= link_to p.get_blog.title.to_s, p.get_blog.url %>
<% end %>
......@@ -8,6 +8,18 @@
<link rel="stylesheet" href="/dist/main.bundle.css">
<head>
<body>
<header>
<%= link_to "Home", "/" %>
<% if blog %>
<%= link_to blog.title.to_s, blog.url %>
<% end %>
<% if get_account != nil %>
<%= link_to "Log Out", "/logout" %>
<% else %>
<%= link_to "Sign In", "/signin" %>
<%= link_to "Sign Up", "/signup" %>
<% end %>
</header>
<%= content %>
<script src="/dist/main.bundle.js"></script>
</body>
......
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