2018-04-24 11:21:39 +02:00
|
|
|
use heck::KebabCase;
|
2018-04-23 16:25:39 +02:00
|
|
|
use rocket::request::Form;
|
2018-04-24 11:21:39 +02:00
|
|
|
use rocket::response::Redirect;
|
2018-04-23 16:25:39 +02:00
|
|
|
use rocket_contrib::Template;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2018-05-04 13:09:08 +02:00
|
|
|
use activity_pub::{context, activity_pub, ActivityPub};
|
2018-05-02 22:44:03 +02:00
|
|
|
use activity_pub::activity::Create;
|
2018-05-04 13:09:08 +02:00
|
|
|
use activity_pub::object::Object;
|
2018-05-01 17:51:49 +02:00
|
|
|
use activity_pub::outbox::broadcast;
|
2018-04-23 16:25:39 +02:00
|
|
|
use db_conn::DbConn;
|
|
|
|
use models::blogs::*;
|
2018-04-23 16:39:06 +02:00
|
|
|
use models::post_authors::*;
|
2018-04-24 11:21:39 +02:00
|
|
|
use models::posts::*;
|
2018-04-23 17:19:28 +02:00
|
|
|
use models::users::User;
|
2018-04-24 11:21:39 +02:00
|
|
|
use utils;
|
2018-04-23 16:25:39 +02:00
|
|
|
|
2018-05-04 13:09:08 +02:00
|
|
|
#[get("/~/<blog>/<slug>", rank = 4)]
|
2018-04-23 16:25:39 +02:00
|
|
|
fn details(blog: String, slug: String, conn: DbConn) -> String {
|
|
|
|
let blog = Blog::find_by_actor_id(&*conn, blog).unwrap();
|
|
|
|
let post = Post::find_by_slug(&*conn, slug).unwrap();
|
|
|
|
format!("{} in {}", post.title, blog.title)
|
|
|
|
}
|
|
|
|
|
2018-05-04 13:09:08 +02:00
|
|
|
#[get("/~/<_blog>/<slug>", rank = 3, format = "application/activity+json")]
|
|
|
|
fn activity_details(_blog: String, slug: String, conn: DbConn) -> ActivityPub {
|
|
|
|
// TODO: posts in different blogs may have the same slug
|
|
|
|
let post = Post::find_by_slug(&*conn, slug).unwrap();
|
|
|
|
|
|
|
|
let mut act = post.serialize(&*conn);
|
|
|
|
act["@context"] = context();
|
|
|
|
activity_pub(act)
|
2018-04-23 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 11:21:39 +02:00
|
|
|
#[get("/~/<_blog>/new", rank = 2)]
|
|
|
|
fn new_auth(_blog: String) -> Redirect {
|
2018-04-23 16:25:39 +02:00
|
|
|
utils::requires_login()
|
|
|
|
}
|
|
|
|
|
2018-05-04 13:09:08 +02:00
|
|
|
#[get("/~/<_blog>/new", rank = 1)]
|
|
|
|
fn new(_blog: String, _user: User) -> Template {
|
|
|
|
Template::render("posts/new", HashMap::<String, String>::new())
|
|
|
|
}
|
|
|
|
|
2018-04-23 16:25:39 +02:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct NewPostForm {
|
|
|
|
pub title: String,
|
|
|
|
pub content: String,
|
|
|
|
pub license: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/~/<blog_name>/new", data = "<data>")]
|
2018-04-23 16:39:06 +02:00
|
|
|
fn create(blog_name: String, data: Form<NewPostForm>, user: User, conn: DbConn) -> Redirect {
|
2018-04-23 16:25:39 +02:00
|
|
|
let blog = Blog::find_by_actor_id(&*conn, blog_name.to_string()).unwrap();
|
|
|
|
let form = data.get();
|
|
|
|
let slug = form.title.to_string().to_kebab_case();
|
2018-04-23 16:39:06 +02:00
|
|
|
let post = Post::insert(&*conn, NewPost {
|
2018-04-23 16:25:39 +02:00
|
|
|
blog_id: blog.id,
|
|
|
|
slug: slug.to_string(),
|
|
|
|
title: form.title.to_string(),
|
|
|
|
content: form.content.to_string(),
|
|
|
|
published: true,
|
|
|
|
license: form.license.to_string()
|
|
|
|
});
|
2018-04-23 16:39:06 +02:00
|
|
|
PostAuthor::insert(&*conn, NewPostAuthor {
|
|
|
|
post_id: post.id,
|
|
|
|
author_id: user.id
|
|
|
|
});
|
2018-05-01 17:51:49 +02:00
|
|
|
|
2018-05-02 22:44:03 +02:00
|
|
|
let act = Create::new(&user, &post, &*conn);
|
2018-05-03 21:11:04 +02:00
|
|
|
broadcast(&*conn, &user, act, user.get_followers(&*conn));
|
2018-05-01 17:51:49 +02:00
|
|
|
|
2018-04-23 16:25:39 +02:00
|
|
|
Redirect::to(format!("/~/{}/{}", blog_name, slug).as_str())
|
|
|
|
}
|