use activitystreams_types::{ activity::Follow, collection::OrderedCollection }; use rocket::{request::Form, response::Redirect}; use rocket_contrib::Template; use serde_json; use activity_pub::{ activity_pub, ActivityPub, ActivityStream, context, broadcast, Id, IntoId, actor::Actor, object::Object }; use db_conn::DbConn; use models::{ follows, instance::Instance, posts::Post, users::* }; #[get("/me")] fn me(user: User) -> Redirect { Redirect::to(format!("/@/{}", user.username).as_ref()) } #[get("/@/", rank = 2)] fn details(name: String, conn: DbConn, account: Option) -> Template { let user = User::find_by_fqn(&*conn, name).unwrap(); let recents = Post::get_recents_for_author(&*conn, &user, 5); let user_id = user.id.clone(); let n_followers = user.get_followers(&*conn).len(); Template::render("users/details", json!({ "user": serde_json::to_value(user).unwrap(), "account": account, "recents": recents.into_iter().map(|p| { json!({ "post": p, "author": ({ let author = &p.get_authors(&*conn)[0]; let mut json = serde_json::to_value(author).unwrap(); json["fqn"] = serde_json::Value::String(author.get_fqn(&*conn)); json }), "url": format!("/~/{}/{}/", p.get_blog(&*conn).actor_id, p.slug), "date": p.creation_date.timestamp() }) }).collect::>(), "is_self": account.map(|a| a.id == user_id).unwrap_or(false), "n_followers": n_followers })) } #[get("/@//follow")] fn follow(name: String, conn: DbConn, user: User) -> Redirect { let target = User::find_by_fqn(&*conn, name.clone()).unwrap(); follows::Follow::insert(&*conn, follows::NewFollow { follower_id: user.id, following_id: target.id }); let mut act = Follow::default(); act.set_actor_link::(user.clone().into_id()).unwrap(); act.set_object_object(user.into_activity(&*conn)).unwrap(); act.object_props.set_id_string(format!("{}/follow/{}", user.ap_url, target.ap_url)).unwrap(); broadcast(&*conn, &user, act, vec![target]); Redirect::to(format!("/@/{}", name).as_ref()) } #[get("/@//followers", rank = 2)] fn followers(name: String, conn: DbConn, account: Option) -> Template { let user = User::find_by_fqn(&*conn, name.clone()).unwrap(); let user_id = user.id.clone(); Template::render("users/followers", json!({ "user": serde_json::to_value(user.clone()).unwrap(), "followers": user.get_followers(&*conn).into_iter().map(|f| { let fqn = f.get_fqn(&*conn); let mut json = serde_json::to_value(f).unwrap(); json["fqn"] = serde_json::Value::String(fqn); json }).collect::>(), "account": account, "is_self": account.map(|a| a.id == user_id).unwrap_or(false) })) } #[get("/@/", format = "application/activity+json", rank = 1)] fn activity_details(name: String, conn: DbConn) -> ActivityPub { let user = User::find_local(&*conn, name).unwrap(); user.as_activity_pub(&*conn) } #[get("/users/new")] fn new(user: Option) -> Template { Template::render("users/new", json!({ "account": user })) } #[get("/@//edit")] fn edit(name: String, user: User) -> Option