1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-22 03:01:09 +00:00

basic page rendering!

This commit is contained in:
Felix Ableitner 2024-01-03 17:06:52 +01:00
parent 2ceab5a23c
commit 42d382d19e
37 changed files with 880 additions and 428 deletions

182
Cargo.lock generated
View file

@ -69,17 +69,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "ahash"
version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a824f2aa7e75a0c98c5a504fceb80649e9c35265d44525b5f94de4771a395cd"
dependencies = [
"getrandom",
"once_cell",
"version_check",
]
[[package]]
name = "ahash"
version = "0.8.7"
@ -308,18 +297,6 @@ version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]]
name = "bitvec"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]]
name = "block-buffer"
version = "0.10.4"
@ -345,29 +322,6 @@ version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]]
name = "bytecheck"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b6372023ac861f6e6dc89c8344a8f398fb42aaba2b5dbc649ca0c0e9dbcb627"
dependencies = [
"bytecheck_derive",
"ptr_meta",
"simdutf8",
"uuid",
]
[[package]]
name = "bytecheck_derive"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7ec4c6f261935ad534c0c22dbef2201b45918860eb1c574b972bd213a76af61"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "bytecount"
version = "0.6.7"
@ -536,26 +490,6 @@ dependencies = [
"toml 0.5.11",
]
[[package]]
name = "console_error_panic_hook"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
dependencies = [
"cfg-if",
"wasm-bindgen",
]
[[package]]
name = "console_log"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be8aed40e4edbf4d3b4431ab260b63fdc40f5780a4766824329ea0f1eefe3c0f"
dependencies = [
"log",
"web-sys",
]
[[package]]
name = "const_format"
version = "0.2.32"
@ -948,12 +882,6 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "funty"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "futures"
version = "0.3.29"
@ -1141,9 +1069,6 @@ name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
dependencies = [
"ahash 0.7.7",
]
[[package]]
name = "hashbrown"
@ -1157,7 +1082,7 @@ version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156"
dependencies = [
"ahash 0.8.7",
"ahash",
"allocator-api2",
]
@ -1382,10 +1307,7 @@ dependencies = [
"axum",
"axum-macros",
"bcrypt",
"cfg-if",
"chrono",
"console_error_panic_hook",
"console_log",
"diesel",
"diesel-derive-newtype",
"diesel_migrations",
@ -1409,10 +1331,9 @@ dependencies = [
"sha2",
"tokio",
"tower-http",
"tracing",
"url",
"uuid",
"wasm-bindgen",
"web-sys",
]
[[package]]
@ -1718,7 +1639,6 @@ dependencies = [
"js-sys",
"paste",
"pin-project",
"rkyv",
"rustc-hash",
"self_cell",
"serde",
@ -2302,26 +2222,6 @@ dependencies = [
"yansi 1.0.0-rc.1",
]
[[package]]
name = "ptr_meta"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1"
dependencies = [
"ptr_meta_derive",
]
[[package]]
name = "ptr_meta_derive"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "pulldown-cmark"
version = "0.9.3"
@ -2381,12 +2281,6 @@ dependencies = [
"syn 2.0.39",
]
[[package]]
name = "radium"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
[[package]]
name = "rand"
version = "0.8.5"
@ -2464,15 +2358,6 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "rend"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2571463863a6bd50c32f94402933f03457a3fbaf697a707c5be741e459f08fd"
dependencies = [
"bytecheck",
]
[[package]]
name = "reqwest"
version = "0.11.22"
@ -2543,35 +2428,6 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "rkyv"
version = "0.7.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "527a97cdfef66f65998b5f3b637c26f5a5ec09cc52a3f9932313ac645f4190f5"
dependencies = [
"bitvec",
"bytecheck",
"bytes",
"hashbrown 0.12.3",
"ptr_meta",
"rend",
"rkyv_derive",
"seahash",
"tinyvec",
"uuid",
]
[[package]]
name = "rkyv_derive"
version = "0.7.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5c462a1328c8e67e4d6dbad1eb0355dd43e8ab432c6e227a43657f16ade5033"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "rstml"
version = "0.11.2"
@ -2656,12 +2512,6 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "seahash"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b"
[[package]]
name = "security-framework"
version = "2.9.2"
@ -2875,12 +2725,6 @@ dependencies = [
"libc",
]
[[package]]
name = "simdutf8"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a"
[[package]]
name = "simple_asn1"
version = "0.6.2"
@ -3038,12 +2882,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417"
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "task-local-extensions"
version = "0.1.4"
@ -3258,9 +3096,16 @@ dependencies = [
"http 0.2.11",
"http-body 0.4.5",
"http-range-header",
"httpdate",
"mime",
"mime_guess",
"percent-encoding",
"pin-project-lite",
"tokio",
"tokio-util",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
@ -3672,15 +3517,6 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "wyz"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
dependencies = [
"tap",
]
[[package]]
name = "xxhash-rust"
version = "0.8.8"

View file

@ -4,27 +4,21 @@ version = "0.1.0"
edition = "2021"
[features]
default = ["ssr"]
ssr = [
"activitypub_federation",
"axum",
"axum-macros",
"tower-http",
"tower-http",
"diesel",
"diesel-derive-newtype",
"diesel_migrations",
"tokio",
"leptos_axum"
"tokio",
"leptos_axum",
"activitypub_federation"
]
csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"]
csr = ["leptos/csr", "leptos_meta/csr"]
[dependencies]
# shared
serde = { version = "1.0.192", features = ["derive"] }
url = { version = "2.4.1", features = ["serde"] }
reqwest = { version = "0.11.22", features = ["json"] }
log = "0.4"
# backend
activitypub_federation = { version = "0.5.0-beta.6", features = [
"axum",
"diesel",
@ -33,6 +27,10 @@ anyhow = "1.0.75"
async-trait = "0.1.74"
axum = { version = "0.6.20", optional = true }
axum-macros = { version = "0.3.8", optional = true }
leptos = "0.5.4"
leptos_meta = "0.5.4"
leptos_router = "0.5.4"
leptos_axum = { version = "0.5.4", optional = true }
bcrypt = "0.15.0"
chrono = { version = "0.4.31", features = ["serde"] }
diesel = { version = "2.1.4", features = [
@ -53,20 +51,15 @@ serde_json = "1.0.108"
sha2 = "0.10.8"
tokio = { version = "1.34.0", features = ["full"], optional = true }
uuid = { version = "1.6.1", features = ["serde"] }
tower-http = { version = "0.4.0", features = ["cors"], optional = true }
leptos_axum = { version = "0.5.4", optional = true }
# frontend
leptos = { version = "0.5.4", features = ["nightly"] }
leptos_meta = { version = "0.5.4", features = ["nightly"] }
leptos_router = { version = "0.5.4", features = ["nightly"] }
console_log = "1"
console_error_panic_hook = "0.1"
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["AbortController", "AbortSignal"] }
tower-http = { version = "0.4.0", features = ["cors", "fs"], optional = true }
serde = { version = "1.0.192", features = ["derive"] }
url = { version = "2.4.1", features = ["serde"] }
reqwest = { version = "0.11.22", features = ["json"] }
log = "0.4"
tracing = "0.1.40"
once_cell = "1.18.0"
[dev-dependencies]
once_cell = "1.18.0"
pretty_assertions = "1.4.0"
reqwest = "0.11.22"
@ -74,6 +67,7 @@ reqwest = "0.11.22"
output-name = "ibis"
bin-features = ["ssr"]
lib-features = ["csr"]
site-addr = "127.0.0.1:8131"
[lib]
crate-type = ["cdylib", "rlib"]

View file

@ -1,14 +1,16 @@
use crate::backend::database::article::{ArticleView, DbArticle, DbArticleForm};
use crate::backend::database::article::DbArticleForm;
use crate::backend::database::conflict::{ApiConflict, DbConflict, DbConflictForm};
use crate::backend::database::edit::{DbEdit, DbEditForm};
use crate::backend::database::edit::DbEditForm;
use crate::backend::database::instance::DbInstance;
use crate::backend::database::user::LocalUserView;
use crate::backend::database::version::EditVersion;
use crate::backend::database::MyDataHandle;
use crate::backend::error::MyResult;
use crate::backend::federation::activities::create_article::CreateArticle;
use crate::backend::federation::activities::submit_article_update;
use crate::backend::utils::generate_article_version;
use crate::common::EditVersion;
use crate::common::GetArticleData;
use crate::common::{ArticleView, DbArticle, DbEdit};
use activitypub_federation::config::Data;
use activitypub_federation::fetch::object_id::ObjectId;
use axum::extract::Query;
@ -118,19 +120,14 @@ pub(in crate::backend::api) async fn edit_article(
}
}
#[derive(Deserialize, Serialize, Clone)]
pub struct GetArticleData {
pub article_id: i32,
}
/// Retrieve an article by ID. It must already be stored in the local database.
#[debug_handler]
pub(in crate::backend::api) async fn get_article(
Query(query): Query<GetArticleData>,
data: Data<MyDataHandle>,
) -> MyResult<Json<ArticleView>> {
Ok(Json(DbArticle::read_view(
query.article_id,
Ok(Json(DbArticle::read_view_title(
&query.title,
&data.db_connection,
)?))
}

View file

@ -5,13 +5,13 @@ use crate::backend::api::instance::get_local_instance;
use crate::backend::api::user::login_user;
use crate::backend::api::user::register_user;
use crate::backend::api::user::validate;
use crate::backend::database::article::{ArticleView, DbArticle};
use crate::backend::database::conflict::{ApiConflict, DbConflict};
use crate::backend::database::edit::DbEdit;
use crate::backend::database::instance::DbInstance;
use crate::backend::database::user::LocalUserView;
use crate::backend::database::MyDataHandle;
use crate::backend::error::MyResult;
use crate::common::DbEdit;
use crate::common::{ArticleView, DbArticle};
use activitypub_federation::config::Data;
use activitypub_federation::fetch::object_id::ObjectId;
use axum::extract::Query;
@ -28,8 +28,8 @@ use axum::{
use axum::{Json, Router};
use axum_macros::debug_handler;
use futures::future::try_join_all;
use serde::{Deserialize, Serialize};
use log::warn;
use serde::{Deserialize, Serialize};
use url::Url;
pub mod article;
@ -83,7 +83,9 @@ async fn resolve_instance(
Query(query): Query<ResolveObject>,
data: Data<MyDataHandle>,
) -> MyResult<Json<DbInstance>> {
let instance: DbInstance = ObjectId::from(query.id).dereference(&data).await?;
// TODO: workaround because axum makes it hard to have multiple routes on /
let id = format!("{}instance", query.id);
let instance: DbInstance = ObjectId::parse(&id)?.dereference(&data).await?;
Ok(Json(instance))
}

View file

@ -1,42 +1,20 @@
use crate::backend::database::edit::DbEdit;
use crate::backend::database::schema::{article, edit};
use crate::backend::error::MyResult;
use crate::backend::federation::objects::edits_collection::DbEditCollection;
use crate::common::DbEdit;
use crate::common::EditVersion;
use crate::common::{ArticleView, DbArticle};
use activitypub_federation::fetch::collection_id::CollectionId;
use activitypub_federation::fetch::object_id::ObjectId;
use diesel::pg::PgConnection;
use diesel::ExpressionMethods;
use diesel::{
insert_into, AsChangeset, BoolExpressionMethods, Identifiable, Insertable,
PgTextExpressionMethods, QueryDsl, Queryable, RunQueryDsl, Selectable,
insert_into, AsChangeset, BoolExpressionMethods, Insertable, PgTextExpressionMethods, QueryDsl,
RunQueryDsl,
};
use serde::{Deserialize, Serialize};
use crate::backend::database::version::EditVersion;
use std::ops::DerefMut;
use std::sync::Mutex;
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Queryable, Selectable, Identifiable)]
#[diesel(table_name = article, check_for_backend(diesel::pg::Pg), belongs_to(DbInstance, foreign_key = instance_id))]
pub struct DbArticle {
pub id: i32,
pub title: String,
pub text: String,
pub ap_id: ObjectId<DbArticle>,
pub instance_id: i32,
pub local: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Queryable)]
#[diesel(table_name = article, check_for_backend(diesel::pg::Pg))]
pub struct ArticleView {
pub article: DbArticle,
pub latest_version: EditVersion,
pub edits: Vec<DbEdit>,
}
#[derive(Debug, Clone, Insertable, AsChangeset)]
#[diesel(table_name = article, check_for_backend(diesel::pg::Pg))]
pub struct DbArticleForm {
@ -47,6 +25,7 @@ pub struct DbArticleForm {
pub local: bool,
}
// TODO: get rid of unnecessary methods
impl DbArticle {
pub fn edits_id(&self) -> MyResult<CollectionId<DbEditCollection>> {
Ok(CollectionId::parse(&format!("{}/edits", self.ap_id))?)
@ -95,6 +74,22 @@ impl DbArticle {
})
}
pub fn read_view_title(title: &str, conn: &Mutex<PgConnection>) -> MyResult<ArticleView> {
let article: DbArticle = {
let mut conn = conn.lock().unwrap();
article::table
.filter(article::dsl::title.eq(title))
.get_result(conn.deref_mut())?
};
let latest_version = article.latest_edit_version(conn)?;
let edits: Vec<DbEdit> = DbEdit::read_for_article(&article, conn)?;
Ok(ArticleView {
article,
edits,
latest_version,
})
}
pub fn read_from_ap_id(
ap_id: &ObjectId<DbArticle>,
conn: &Mutex<PgConnection>,

View file

@ -1,13 +1,14 @@
use crate::backend::database::article::DbArticle;
use crate::backend::database::edit::DbEdit;
use crate::backend::database::schema::conflict;
use crate::backend::database::user::DbLocalUser;
use crate::backend::database::version::EditVersion;
use crate::backend::database::MyDataHandle;
use crate::backend::error::MyResult;
use crate::backend::federation::activities::submit_article_update;
use crate::backend::utils::generate_article_version;
use crate::common::DbArticle;
use crate::common::DbEdit;
use crate::common::EditVersion;
use activitypub_federation::config::Data;
use activitypub_federation::fetch::object_id::ObjectId;
use diesel::ExpressionMethods;
use diesel::{
delete, insert_into, Identifiable, Insertable, PgConnection, QueryDsl, Queryable, RunQueryDsl,
@ -75,7 +76,9 @@ impl DbConflict {
) -> MyResult<Option<ApiConflict>> {
let article = DbArticle::read(self.article_id, &data.db_connection)?;
// Make sure to get latest version from origin so that all conflicts can be resolved
let original_article = article.ap_id.dereference_forced(data).await?;
let original_article = ObjectId::parse(&article.ap_id)?
.dereference_forced(data)
.await?;
// create common ancestor version
let edits = DbEdit::read_for_article(&original_article, &data.db_connection)?;

View file

@ -1,35 +1,14 @@
use crate::backend::database::schema::edit;
use crate::backend::database::version::EditVersion;
use crate::backend::database::DbArticle;
use crate::backend::error::MyResult;
use crate::common::EditVersion;
use crate::common::{DbArticle, DbEdit};
use activitypub_federation::fetch::object_id::ObjectId;
use diesel::ExpressionMethods;
use diesel::{
insert_into, AsChangeset, Insertable, PgConnection, QueryDsl, Queryable, RunQueryDsl,
Selectable,
};
use diesel::{insert_into, AsChangeset, Insertable, PgConnection, QueryDsl, RunQueryDsl};
use diffy::create_patch;
use serde::{Deserialize, Serialize};
use std::ops::DerefMut;
use std::sync::Mutex;
/// Represents a single change to the article.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Queryable, Selectable)]
#[diesel(table_name = edit, check_for_backend(diesel::pg::Pg))]
pub struct DbEdit {
// TODO: we could use hash as primary key, but that gives errors on forking because
// the same edit is used for multiple articles
pub id: i32,
pub creator_id: i32,
/// UUID built from sha224 hash of diff
pub hash: EditVersion,
pub ap_id: ObjectId<DbEdit>,
pub diff: String,
pub article_id: i32,
/// First edit of an article always has `EditVersion::default()` here
pub previous_version_id: EditVersion,
}
#[derive(Debug, Clone, Insertable, AsChangeset)]
#[diesel(table_name = edit, check_for_backend(diesel::pg::Pg))]
pub struct DbEditForm {

View file

@ -1,4 +1,3 @@
use crate::backend::database::article::DbArticle;
use diesel::PgConnection;
use std::ops::Deref;
use std::sync::{Arc, Mutex};
@ -12,7 +11,7 @@ pub mod article;
pub mod conflict;
pub mod edit;
pub mod instance;
mod schema;
pub(crate) mod schema;
pub mod user;
pub mod version;

View file

@ -1,16 +1,8 @@
use crate::backend::error::MyResult;
use std::hash::Hash;
use diesel_derive_newtype::DieselNewType;
use serde::{Deserialize, Serialize};
use crate::common::EditVersion;
use sha2::{Digest, Sha256};
use uuid::Uuid;
/// The version hash of a specific edit. Generated by taking an SHA256 hash of the diff
/// and using the first 16 bytes so that it fits into UUID.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, DieselNewType)]
pub struct EditVersion(Uuid);
impl EditVersion {
pub fn new(diff: &str) -> MyResult<Self> {
let mut sha256 = Sha256::new();

View file

@ -1,5 +1,3 @@
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use std::fmt::{Display, Formatter};
pub type MyResult<T> = Result<T, Error>;
@ -22,8 +20,13 @@ where
}
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
(StatusCode::INTERNAL_SERVER_ERROR, format!("{}", self.0)).into_response()
#[cfg(feature = "ssr")]
impl axum::response::IntoResponse for Error {
fn into_response(self) -> axum::response::Response {
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
format!("{}", self.0),
)
.into_response()
}
}

View file

@ -1,8 +1,9 @@
use crate::backend::database::instance::DbInstance;
use crate::backend::database::{article::DbArticle, MyDataHandle};
use crate::backend::database::MyDataHandle;
use crate::backend::error::MyResult;
use crate::backend::federation::objects::article::ApubArticle;
use crate::backend::utils::generate_activity_id;
use crate::common::DbArticle;
use activitypub_federation::kinds::activity::CreateType;
use activitypub_federation::{
config::Data,

View file

@ -2,7 +2,9 @@ use crate::backend::database::instance::DbInstance;
use crate::backend::database::user::DbPerson;
use crate::backend::error::MyResult;
use crate::backend::federation::send_activity;
use crate::backend::{database::MyDataHandle, federation::activities::accept::Accept, generate_activity_id};
use crate::backend::{
database::MyDataHandle, federation::activities::accept::Accept, generate_activity_id,
};
use activitypub_federation::{
config::Data,
fetch::object_id::ObjectId,

View file

@ -1,11 +1,11 @@
use crate::backend::database::article::DbArticle;
use crate::backend::database::edit::{DbEdit, DbEditForm};
use crate::backend::database::edit::DbEditForm;
use crate::backend::database::instance::DbInstance;
use crate::backend::database::version::EditVersion;
use crate::backend::database::MyDataHandle;
use crate::backend::error::Error;
use crate::backend::federation::activities::update_local_article::UpdateLocalArticle;
use crate::backend::federation::activities::update_remote_article::UpdateRemoteArticle;
use crate::common::EditVersion;
use crate::common::{DbArticle, DbEdit};
use activitypub_federation::config::Data;
pub mod accept;
@ -35,7 +35,7 @@ pub async fn submit_article_update(
id: -1,
creator_id,
hash: form.hash,
ap_id: form.ap_id,
ap_id: form.ap_id.to_string(),
diff: form.diff,
article_id: form.article_id,
previous_version_id: form.previous_version_id,

View file

@ -1,10 +1,10 @@
use crate::backend::database::conflict::{DbConflict, DbConflictForm};
use crate::backend::database::instance::DbInstance;
use crate::backend::database::version::EditVersion;
use crate::backend::database::MyDataHandle;
use crate::backend::error::MyResult;
use crate::backend::federation::objects::edit::ApubEdit;
use crate::backend::utils::generate_activity_id;
use crate::common::EditVersion;
use activitypub_federation::kinds::activity::RejectType;
use activitypub_federation::{
config::Data, fetch::object_id::ObjectId, protocol::helpers::deserialize_one_or_many,

View file

@ -1,4 +1,4 @@
use crate::backend::database::{article::DbArticle, MyDataHandle};
use crate::backend::database::MyDataHandle;
use crate::backend::error::MyResult;
use crate::backend::federation::objects::article::ApubArticle;
@ -12,6 +12,7 @@ use activitypub_federation::{
traits::{ActivityHandler, Object},
};
use crate::common::DbArticle;
use serde::{Deserialize, Serialize};
use url::Url;

View file

@ -1,14 +1,14 @@
use crate::backend::database::MyDataHandle;
use crate::backend::error::MyResult;
use crate::backend::database::article::DbArticle;
use crate::backend::database::edit::DbEdit;
use crate::backend::database::instance::DbInstance;
use crate::backend::federation::activities::reject::RejectEdit;
use crate::backend::federation::activities::update_local_article::UpdateLocalArticle;
use crate::backend::federation::objects::edit::ApubEdit;
use crate::backend::federation::send_activity;
use crate::backend::utils::generate_activity_id;
use crate::common::DbArticle;
use crate::common::DbEdit;
use activitypub_federation::kinds::activity::UpdateType;
use activitypub_federation::{
config::Data,

View file

@ -3,9 +3,9 @@ use activitypub_federation::activity_sending::SendActivityTask;
use activitypub_federation::config::Data;
use activitypub_federation::protocol::context::WithContext;
use activitypub_federation::traits::{ActivityHandler, Actor};
use log::warn;
use serde::Serialize;
use std::fmt::Debug;
use log::warn;
use url::Url;
pub mod activities;

View file

@ -1,9 +1,10 @@
use crate::backend::database::article::DbArticleForm;
use crate::backend::database::instance::DbInstance;
use crate::backend::database::version::EditVersion;
use crate::backend::database::{article::DbArticle, MyDataHandle};
use crate::backend::database::MyDataHandle;
use crate::backend::error::Error;
use crate::backend::federation::objects::edits_collection::DbEditCollection;
use crate::common::DbArticle;
use crate::common::EditVersion;
use activitypub_federation::config::Data;
use activitypub_federation::fetch::collection_id::CollectionId;
use activitypub_federation::kinds::object::ArticleType;
@ -48,7 +49,7 @@ impl Object for DbArticle {
let local_instance = DbInstance::read_local_instance(&data.db_connection)?;
Ok(ApubArticle {
kind: Default::default(),
id: self.ap_id.clone(),
id: ObjectId::parse(&self.ap_id)?,
attributed_to: local_instance.ap_id.clone(),
to: vec![public(), local_instance.followers_url()?],
edits: self.edits_id()?,

View file

@ -1,8 +1,9 @@
use crate::backend::database::instance::DbInstance;
use crate::backend::database::{article::DbArticle, MyDataHandle};
use crate::backend::database::MyDataHandle;
use crate::backend::error::Error;
use crate::backend::federation::objects::article::ApubArticle;
use crate::common::DbArticle;
use activitypub_federation::kinds::collection::CollectionType;
use activitypub_federation::{
config::Data,

View file

@ -1,9 +1,9 @@
use crate::backend::database::article::DbArticle;
use crate::backend::database::edit::{DbEdit, DbEditForm};
use crate::backend::database::edit::DbEditForm;
use crate::backend::database::user::DbPerson;
use crate::backend::database::version::EditVersion;
use crate::backend::database::MyDataHandle;
use crate::backend::error::Error;
use crate::common::EditVersion;
use crate::common::{DbArticle, DbEdit};
use activitypub_federation::config::Data;
use activitypub_federation::fetch::object_id::ObjectId;
use activitypub_federation::traits::Object;
@ -48,11 +48,11 @@ impl Object for DbEdit {
let creator = DbPerson::read(self.creator_id, data)?;
Ok(ApubEdit {
kind: PatchType::Patch,
id: self.ap_id,
id: ObjectId::parse(&self.ap_id)?,
content: self.diff,
version: self.hash,
previous_version: self.previous_version_id,
object: article.ap_id,
object: ObjectId::parse(&article.ap_id)?,
attributed_to: creator.ap_id,
})
}

View file

@ -1,10 +1,10 @@
use crate::backend::database::article::DbArticle;
use crate::backend::database::MyDataHandle;
use crate::backend::error::Error;
use crate::backend::federation::objects::edit::ApubEdit;
use crate::common::DbArticle;
use crate::backend::database::edit::DbEdit;
use crate::backend::database::instance::DbInstance;
use crate::common::DbEdit;
use activitypub_federation::kinds::collection::OrderedCollectionType;
use activitypub_federation::{
config::Data,

View file

@ -1,12 +1,13 @@
use crate::backend::database::instance::{DbInstance, DbInstanceForm};
use crate::backend::database::MyDataHandle;
use crate::backend::error::{Error, MyResult};
use crate::backend::error::Error;
use crate::backend::federation::objects::articles_collection::DbArticleCollection;
use crate::backend::federation::send_activity;
use activitypub_federation::fetch::collection_id::CollectionId;
use activitypub_federation::kinds::actor::ServiceType;
use crate::backend::error::MyResult;
use activitypub_federation::traits::ActivityHandler;
use activitypub_federation::{
config::Data,
@ -17,7 +18,6 @@ use activitypub_federation::{
use chrono::{DateTime, Local, Utc};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]

View file

@ -1,8 +1,8 @@
use crate::backend::database::article::DbArticle;
use crate::backend::database::instance::DbInstance;
use crate::backend::database::user::DbPerson;
use crate::backend::database::MyDataHandle;
use crate::backend::error::{Error, MyResult};
use crate::backend::error::Error;
use crate::backend::error::MyResult;
use crate::backend::federation::activities::accept::Accept;
use crate::backend::federation::activities::create_article::CreateArticle;
use crate::backend::federation::activities::follow::Follow;
@ -10,10 +10,13 @@ use crate::backend::federation::activities::reject::RejectEdit;
use crate::backend::federation::activities::update_local_article::UpdateLocalArticle;
use crate::backend::federation::activities::update_remote_article::UpdateRemoteArticle;
use crate::backend::federation::objects::article::ApubArticle;
use crate::backend::federation::objects::articles_collection::{ArticleCollection, DbArticleCollection};
use crate::backend::federation::objects::articles_collection::{
ArticleCollection, DbArticleCollection,
};
use crate::backend::federation::objects::edits_collection::{ApubEditCollection, DbEditCollection};
use crate::backend::federation::objects::instance::ApubInstance;
use crate::backend::federation::objects::user::ApubUser;
use crate::common::DbArticle;
use activitypub_federation::axum::inbox::{receive_activity, ActivityData};
use activitypub_federation::axum::json::FederationJson;
use activitypub_federation::config::Data;
@ -32,8 +35,9 @@ use url::Url;
pub fn federation_routes() -> Router {
Router::new()
// TODO
//.route("/", get(http_get_instance))
// TODO: would be nice if this could be at / but axum doesnt properly support routing by headers
// https://github.com/tokio-rs/axum/issues/1654
.route("/instance", get(http_get_instance))
.route("/user/:name", get(http_get_person))
.route("/all_articles", get(http_get_all_articles))
.route("/article/:title", get(http_get_article))

View file

@ -1,8 +1,11 @@
use crate::backend::database::article::DbArticleForm;
use crate::backend::database::instance::{DbInstance, DbInstanceForm};
use crate::backend::database::MyData;
use crate::backend::error::MyResult;
use crate::backend::federation::routes::federation_routes;
use crate::backend::utils::generate_activity_id;
use crate::common::DbArticle;
use crate::frontend::app::App;
use activitypub_federation::config::{FederationConfig, FederationMiddleware};
use activitypub_federation::fetch::collection_id::CollectionId;
use activitypub_federation::fetch::object_id::ObjectId;
@ -15,12 +18,12 @@ use diesel::PgConnection;
use diesel_migrations::embed_migrations;
use diesel_migrations::EmbeddedMigrations;
use diesel_migrations::MigrationHarness;
use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes};
use std::net::ToSocketAddrs;
use std::sync::{Arc, Mutex};
use tower_http::cors::CorsLayer;
use log::info;
use leptos_axum::{generate_route_list, LeptosRoutes};
use leptos::*;use leptos_meta::*;use leptos_router::*;
use tower_http::services::ServeFile;
pub mod api;
pub mod database;
@ -28,7 +31,6 @@ pub mod error;
pub mod federation;
mod utils;
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
pub async fn start(hostname: &str, database_url: &str) -> MyResult<()> {
@ -47,57 +49,57 @@ pub async fn start(hostname: &str, database_url: &str) -> MyResult<()> {
.build()
.await?;
// Create local instance if it doesnt exist yet
// TODO: Move this into setup api call
let ap_id = ObjectId::parse(&format!("http://{}", hostname))?;
let articles_url = CollectionId::parse(&format!("http://{}/all_articles", hostname))?;
let inbox_url = format!("http://{}/inbox", hostname);
let keypair = generate_actor_keypair()?;
let form = DbInstanceForm {
ap_id,
articles_url,
inbox_url,
public_key: keypair.public_key,
private_key: Some(keypair.private_key),
last_refreshed_at: Local::now().into(),
local: true,
};
DbInstance::create(&form, &config.db_connection)?;
if DbInstance::read_local_instance(&config.db_connection).is_err() {
// TODO: workaround because axum makes it hard to have multiple routes on /
let ap_id = ObjectId::parse(&format!("http://{}/instance", hostname))?;
let articles_url = CollectionId::parse(&format!("http://{}/all_articles", hostname))?;
let inbox_url = format!("http://{}/inbox", hostname);
let keypair = generate_actor_keypair()?;
let form = DbInstanceForm {
ap_id,
articles_url,
inbox_url,
public_key: keypair.public_key,
private_key: Some(keypair.private_key),
last_refreshed_at: Local::now().into(),
local: true,
};
let instance = DbInstance::create(&form, &config.db_connection)?;
// Create the main page which is shown by default
let form = DbArticleForm {
title: "Main Page".to_string(),
text: "Hello world!".to_string(),
ap_id: ObjectId::parse("http://{hostname}/article/Main_Page")?,
instance_id: instance.id,
local: true,
};
DbArticle::create(&form, &config.db_connection)?;
}
let conf = get_configuration(Some("Cargo.toml")).await.unwrap();
let leptos_options = conf.leptos_options;
let addr = leptos_options.site_addr;
let mut leptos_options = conf.leptos_options;
let addr = hostname
.to_socket_addrs()?
.next()
.expect("Failed to lookup domain name");
leptos_options.site_addr = addr;
let routes = generate_route_list(App);
info!("Listening with axum on {hostname}");
let config = config.clone();
let app = Router::new()
.leptos_routes(&leptos_options, routes, || view! { <App/> } )
.leptos_routes(&leptos_options, routes, || view! { <App/> })
.with_state(leptos_options)
.route_service("/style.css", ServeFile::new("style.css"))
.nest("", federation_routes())
.nest("/api/v1", api_routes())
.layer(FederationMiddleware::new(config))
.layer(CorsLayer::permissive());
/*
let addr = hostname
.to_socket_addrs()?
.next()
.expect("Failed to lookup domain name");
*/
dbg!(&addr, &hostname);
Server::bind(&addr).serve(app.into_make_service()).await?;
Ok(())
}
#[component]
pub fn App() -> impl IntoView {
provide_meta_context();
view! {
<>
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<Stylesheet id="leptos" href="/pkg/ibis.css"/>
<Meta name="description" content="Leptos implementation of a HackerNews demo."/>
test
</>
}
}

View file

@ -1,6 +1,6 @@
use crate::backend::database::edit::DbEdit;
use crate::backend::database::version::EditVersion;
use crate::backend::error::MyResult;
use crate::common::DbEdit;
use crate::common::EditVersion;
use anyhow::anyhow;
use diffy::{apply, Patch};
use rand::{distributions::Alphanumeric, thread_rng, Rng};

56
src/common/mod.rs Normal file
View file

@ -0,0 +1,56 @@
#[cfg(feature = "ssr")]
use crate::backend::database::schema::{article, edit};
#[cfg(feature = "ssr")]
use diesel::{Identifiable, Queryable, Selectable};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Deserialize, Serialize, Clone)]
pub struct GetArticleData {
pub title: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[cfg_attr(feature = "ssr", derive(Queryable))]
#[cfg_attr(feature = "ssr", diesel(table_name = article, check_for_backend(diesel::pg::Pg)))]
pub struct ArticleView {
pub article: DbArticle,
pub latest_version: EditVersion,
pub edits: Vec<DbEdit>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[cfg_attr(feature = "ssr", derive(Queryable, Selectable, Identifiable))]
#[cfg_attr(feature = "ssr", diesel(table_name = article, check_for_backend(diesel::pg::Pg), belongs_to(DbInstance, foreign_key = instance_id)))]
pub struct DbArticle {
pub id: i32,
pub title: String,
pub text: String,
pub ap_id: String,
pub instance_id: i32,
pub local: bool,
}
/// Represents a single change to the article.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[cfg_attr(feature = "ssr", derive(Queryable, Selectable))]
#[cfg_attr(feature = "ssr", diesel(table_name = edit, check_for_backend(diesel::pg::Pg)))]
pub struct DbEdit {
// TODO: we could use hash as primary key, but that gives errors on forking because
// the same edit is used for multiple articles
pub id: i32,
pub creator_id: i32,
/// UUID built from sha224 hash of diff
pub hash: EditVersion,
pub ap_id: String,
pub diff: String,
pub article_id: i32,
/// First edit of an article always has `EditVersion::default()` here
pub previous_version_id: EditVersion,
}
/// The version hash of a specific edit. Generated by taking an SHA256 hash of the diff
/// and using the first 16 bytes so that it fits into UUID.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "ssr", derive(diesel_derive_newtype::DieselNewType))]
pub struct EditVersion(pub(crate) Uuid);

41
src/frontend/api.rs Normal file
View file

@ -0,0 +1,41 @@
use crate::common::ArticleView;
use crate::common::GetArticleData;
use anyhow::anyhow;
use once_cell::sync::Lazy;
use reqwest::{Client, RequestBuilder};
use serde::{Deserialize, Serialize};
pub static CLIENT: Lazy<Client> = Lazy::new(Client::new);
pub async fn get_article(hostname: &str, title: String) -> ArticleView {
let get_article = GetArticleData { title };
get_query::<ArticleView, _>(hostname, "article", Some(get_article.clone())).await
}
pub async fn get_query<T, R>(hostname: &str, endpoint: &str, query: Option<R>) -> T
where
T: for<'de> Deserialize<'de>,
R: Serialize,
{
let mut req = CLIENT.get(format!("http://{}/api/v1/{}", hostname, endpoint));
if let Some(query) = query {
req = req.query(&query);
}
handle_json_res::<T>(req).await
}
pub async fn handle_json_res<T>(req: RequestBuilder) -> T
where
T: for<'de> Deserialize<'de>,
{
let res = req.send().await.unwrap();
let status = res.status();
let text = res.text().await.unwrap();
if status == reqwest::StatusCode::OK {
serde_json::from_str(&text)
.map_err(|e| anyhow!("Json error on {text}: {e}"))
.unwrap()
} else {
Err(anyhow!("API error: {text}")).unwrap()
}
}

29
src/frontend/app.rs Normal file
View file

@ -0,0 +1,29 @@
use crate::frontend::article::Article;
use crate::frontend::nav::Nav;
use leptos::{component, view, IntoView};
use leptos_meta::provide_meta_context;
use leptos_meta::*;
use leptos_router::Route;
use leptos_router::Router;
use leptos_router::Routes;
#[component]
pub fn App() -> impl IntoView {
provide_meta_context();
view! {
<>
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<Stylesheet id="leptos" href="/style.css"/>
<Meta name="description" content="Leptos implementation of a HackerNews demo."/>
<Router>
<Nav />
<main>
<Routes>
<Route path="/" view=Article/>
</Routes>
</main>
</Router>
</>
}
}

30
src/frontend/article.rs Normal file
View file

@ -0,0 +1,30 @@
use crate::frontend::api::get_article;
use leptos::*;
use leptos_router::*;
#[component]
pub fn Article() -> impl IntoView {
let params = use_params_map();
let article = create_resource(
move || {
params
.get()
.get("title")
.cloned()
.unwrap_or("Main Page".to_string())
},
move |title| async move { get_article("localhost:8131", title).await },
);
view! {
<Suspense fallback=|| view! { "Loading..." }>
{move || article.get().map(|article|
view! {
<div class="item-view">
<h1>{article.article.title}</h1>
<div>{article.article.text}</div>
</div>
})}
</Suspense>
}
}

156
src/frontend/latest.rs Normal file
View file

@ -0,0 +1,156 @@
use crate::api;
use leptos::*;
use leptos_router::*;
fn category(from: &str) -> &'static str {
match from {
"new" => "newest",
"show" => "show",
"ask" => "ask",
"job" => "jobs",
_ => "news",
}
}
#[component]
pub fn Stories() -> impl IntoView {
let query = use_query_map();
let params = use_params_map();
let page = move || {
query
.with(|q| q.get("page").and_then(|page| page.parse::<usize>().ok()))
.unwrap_or(1)
};
let story_type = move || {
params
.with(|p| p.get("stories").cloned())
.unwrap_or_else(|| "top".to_string())
};
let stories = create_resource(
move || (page(), story_type()),
move |(page, story_type)| async move {
let path = format!("{}?page={}", category(&story_type), page);
api::fetch_api::<Vec<api::Story>>(&api::story(&path)).await
},
);
let (pending, set_pending) = create_signal(false);
let hide_more_link = move || {
stories.get().unwrap_or(None).unwrap_or_default().len() < 28
|| pending()
};
view! {
<div class="news-view">
<div class="news-list-nav">
<span>
{move || if page() > 1 {
view! {
<a class="page-link"
href=move || format!("/{}?page={}", story_type(), page() - 1)
attr:aria_label="Previous Page"
>
"< prev"
</a>
}.into_any()
} else {
view! {
<span class="page-link disabled" aria-hidden="true">
"< prev"
</span>
}.into_any()
}}
</span>
<span>"page " {page}</span>
<span class="page-link"
class:disabled=hide_more_link
aria-hidden=hide_more_link
>
<a href=move || format!("/{}?page={}", story_type(), page() + 1)
aria-label="Next Page"
>
"more >"
</a>
</span>
</div>
<main class="news-list">
<div>
<Transition
fallback=move || view! { <p>"Loading..."</p> }
set_pending
>
{move || match stories.get() {
None => None,
Some(None) => Some(view! { <p>"Error loading stories."</p> }.into_any()),
Some(Some(stories)) => {
Some(view! {
<ul>
<For
each=move || stories.clone()
key=|story| story.id
let:story
>
<Story story/>
</For>
</ul>
}.into_any())
}
}}
</Transition>
</div>
</main>
</div>
}
}
#[component]
fn Story(story: api::Story) -> impl IntoView {
view! {
<li class="news-item">
<span class="score">{story.points}</span>
<span class="title">
{if !story.url.starts_with("item?id=") {
view! {
<span>
<a href=story.url target="_blank" rel="noreferrer">
{story.title.clone()}
</a>
<span class="host">"("{story.domain}")"</span>
</span>
}.into_view()
} else {
let title = story.title.clone();
view! { <A href=format!("/stories/{}", story.id)>{title.clone()}</A> }.into_view()
}}
</span>
<br />
<span class="meta">
{if story.story_type != "job" {
view! {
<span>
{"by "}
{story.user.map(|user| view ! { <A href=format!("/users/{user}")>{user.clone()}</A>})}
{format!(" {} | ", story.time_ago)}
<A href=format!("/stories/{}", story.id)>
{if story.comments_count.unwrap_or_default() > 0 {
format!("{} comments", story.comments_count.unwrap_or_default())
} else {
"discuss".into()
}}
</A>
</span>
}.into_view()
} else {
let title = story.title.clone();
view! { <A href=format!("/item/{}", story.id)>{title.clone()}</A> }.into_view()
}}
</span>
{(story.story_type != "link").then(|| view! {
" "
<span class="label">{story.story_type}</span>
})}
</li>
}
}

View file

@ -1,3 +1,8 @@
pub mod api;
pub mod app;
pub mod article;
pub mod nav;
use leptos::error::Result;
use leptos::*;
use log::info;
@ -17,14 +22,14 @@ async fn fetch_cats(count: CatCount) -> Result<Vec<String>> {
let res = reqwest::get(&format!(
"https://api.thecatapi.com/v1/images/search?limit={count}",
))
.await?
.json::<Vec<Cat>>()
.await?
// extract the URL field for each cat
.into_iter()
.take(count)
.map(|cat| cat.url)
.collect::<Vec<_>>();
.await?
.json::<Vec<Cat>>()
.await?
// extract the URL field for each cat
.into_iter()
.take(count)
.map(|cat| cat.url)
.collect::<Vec<_>>();
Ok(res)
} else {
Ok(vec![])
@ -52,10 +57,7 @@ pub struct InstanceView {
}
async fn fetch_instance(url: &str) -> Result<InstanceView> {
let res = reqwest::get(url)
.await?
.json::<InstanceView>()
.await?;
let res = reqwest::get(url).await?.json::<InstanceView>().await?;
info!("{:?}", &res);
Ok(res)
}

22
src/frontend/nav.rs Normal file
View file

@ -0,0 +1,22 @@
use leptos::{component, view, IntoView};
use leptos_router::*;
#[component]
pub fn Nav() -> impl IntoView {
view! {
<div>
<nav class="inner">
<li>
<A href="/">
<strong>"Main Page"</strong>
</A>
</li>
<li>
<A href="/latest">
<strong>"Latest changes"</strong>
</A>
</li>
</nav>
</div>
}
}

View file

@ -1,3 +1,4 @@
#[cfg(feature = "ssr")]
pub mod backend;
pub mod frontend;
pub mod common;
pub mod frontend;

View file

@ -1,3 +1,4 @@
#[cfg(feature = "ssr")]
#[tokio::main]
pub async fn main() -> ibis::backend::error::MyResult<()> {
use log::LevelFilter;
@ -9,4 +10,7 @@ pub async fn main() -> ibis::backend::error::MyResult<()> {
let database_url = "postgres://ibis:password@localhost:5432/ibis";
ibis::backend::start("localhost:8131", database_url).await?;
Ok(())
}
}
#[cfg(not(feature = "ssr"))]
fn main() {}

326
style.css Normal file
View file

@ -0,0 +1,326 @@
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
font-size: 15px;
background-color: #f2f3f5;
margin: 0;
padding-top: 55px;
color: #34495e;
overflow-y: scroll
}
a {
color: #34495e;
text-decoration: none
}
.header {
background-color: #335d92;
position: fixed;
z-index: 999;
height: 55px;
top: 0;
left: 0;
right: 0
}
.header .inner {
max-width: 800px;
box-sizing: border-box;
margin: 0 auto;
padding: 15px 5px
}
.header a {
color: rgba(255, 255, 255, .8);
line-height: 24px;
transition: color .15s ease;
display: inline-block;
vertical-align: middle;
font-weight: 300;
letter-spacing: .075em;
margin-right: 1.8em
}
.header a:hover {
color: #fff
}
.header a.active {
color: #fff;
font-weight: 400
}
.header a:nth-child(6) {
margin-right: 0
}
.header .github {
color: #fff;
font-size: .9em;
margin: 0;
float: right
}
.logo {
width: 24px;
margin-right: 10px;
display: inline-block;
vertical-align: middle
}
.view {
max-width: 800px;
margin: 0 auto;
position: relative
}
.fade-enter-active,
.fade-exit-active {
transition: all .2s ease
}
.fade-enter,
.fade-exit-active {
opacity: 0
}
@media (max-width:860px) {
.header .inner {
padding: 15px 30px
}
}
@media (max-width:600px) {
.header .inner {
padding: 15px
}
.header a {
margin-right: 1em
}
.header .github {
display: none
}
}
.news-view {
padding-top: 45px
}
.news-list,
.news-list-nav {
background-color: #fff;
border-radius: 2px
}
.news-list-nav {
padding: 15px 30px;
position: fixed;
text-align: center;
top: 55px;
left: 0;
right: 0;
z-index: 998;
box-shadow: 0 1px 2px rgba(0, 0, 0, .1)
}
.news-list-nav .page-link {
margin: 0 1em
}
.news-list-nav .disabled {
color: #aaa
}
.news-list {
position: absolute;
margin: 30px 0;
width: 100%;
transition: all .5s cubic-bezier(.55, 0, .1, 1)
}
.news-list ul {
list-style-type: none;
padding: 0;
margin: 0
}
@media (max-width:600px) {
.news-list {
margin: 10px 0
}
}
.news-item {
background-color: #fff;
padding: 20px 30px 20px 80px;
border-bottom: 1px solid #eee;
position: relative;
line-height: 20px
}
.news-item .score {
color: #335d92;
font-size: 1.1em;
font-weight: 700;
position: absolute;
top: 50%;
left: 0;
width: 80px;
text-align: center;
margin-top: -10px
}
.news-item .host,
.news-item .meta {
font-size: .85em;
color: #626262
}
.news-item .host a,
.news-item .meta a {
color: #626262;
text-decoration: underline
}
.news-item .host a:hover,
.news-item .meta a:hover {
color: #335d92
}
.item-view-header {
background-color: #fff;
padding: 1.8em 2em 1em;
box-shadow: 0 1px 2px rgba(0, 0, 0, .1)
}
.item-view-header h1 {
display: inline;
font-size: 1.5em;
margin: 0;
margin-right: .5em
}
.item-view-header .host,
.item-view-header .meta,
.item-view-header .meta a {
color: #626262
}
.item-view-header .meta a {
text-decoration: underline
}
.item-view-comments {
background-color: #fff;
margin-top: 10px;
padding: 0 2em .5em
}
.item-view-comments-header {
margin: 0;
font-size: 1.1em;
padding: 1em 0;
position: relative
}
.item-view-comments-header .spinner {
display: inline-block;
margin: -15px 0
}
.comment-children {
list-style-type: none;
padding: 0;
margin: 0
}
@media (max-width:600px) {
.item-view-header h1 {
font-size: 1.25em
}
}
.comment-children .comment-children {
margin-left: 1.5em
}
.comment {
border-top: 1px solid #eee;
position: relative
}
.comment .by,
.comment .text,
.comment .toggle {
font-size: .9em;
margin: 1em 0
}
.comment .by {
color: #626262
}
.comment .by a {
color: #626262;
text-decoration: underline
}
.comment .text {
overflow-wrap: break-word
}
.comment .text a:hover {
color: #335d92
}
.comment .text pre {
white-space: pre-wrap
}
.comment .toggle {
background-color: #fffbf2;
padding: .3em .5em;
border-radius: 4px
}
.comment .toggle a {
color: #626262;
cursor: pointer
}
.comment .toggle.open {
padding: 0;
background-color: transparent;
margin-bottom: -.5em
}
.user-view {
background-color: #fff;
box-sizing: border-box;
padding: 2em 3em
}
.user-view h1 {
margin: 0;
font-size: 1.5em
}
.user-view .meta {
list-style-type: none;
padding: 0
}
.user-view .label {
display: inline-block;
min-width: 4em
}
.user-view .about {
margin: 1em 0
}
.user-view .links a {
text-decoration: underline
}

View file

@ -1,18 +1,19 @@
use anyhow::anyhow;
use ibis::api::article::{CreateArticleData, EditArticleData, ForkArticleData, GetArticleData};
use ibis::api::instance::FollowInstance;
use ibis::api::user::RegisterUserData;
use ibis::api::user::{LoginResponse, LoginUserData};
use ibis::api::ResolveObject;
use ibis::database::article::ArticleView;
use ibis::database::conflict::ApiConflict;
use ibis::database::instance::DbInstance;
use ibis::error::MyResult;
use ibis::start;
use ibis::backend::api::article::{CreateArticleData, EditArticleData, ForkArticleData};
use ibis::backend::api::instance::FollowInstance;
use ibis::backend::api::user::RegisterUserData;
use ibis::backend::api::user::{LoginResponse, LoginUserData};
use ibis::backend::api::ResolveObject;
use ibis::backend::database::conflict::ApiConflict;
use ibis::backend::database::instance::DbInstance;
use ibis::backend::error::MyResult;
use ibis::backend::start;
use ibis::common::ArticleView;
use ibis::frontend::api;
use ibis::frontend::api::get_query;
use once_cell::sync::Lazy;
use reqwest::{Client, RequestBuilder, StatusCode};
use reqwest::{Client, StatusCode};
use serde::de::Deserialize;
use serde::ser::Serialize;
use std::env::current_dir;
use std::fs::create_dir_all;
use std::process::{Command, Stdio};
@ -154,7 +155,7 @@ pub async fn create_article(instance: &IbisInstance, title: String) -> MyResult<
.post(format!("http://{}/api/v1/article", &instance.hostname))
.form(&create_form)
.bearer_auth(&instance.jwt);
let article: ArticleView = handle_json_res(req).await?;
let article: ArticleView = api::handle_json_res(req).await?;
// create initial edit to ensure that conflicts are generated (there are no conflicts on empty file)
let edit_form = EditArticleData {
@ -166,11 +167,6 @@ pub async fn create_article(instance: &IbisInstance, title: String) -> MyResult<
edit_article(instance, &edit_form).await
}
pub async fn get_article(hostname: &str, article_id: i32) -> MyResult<ArticleView> {
let get_article = GetArticleData { article_id };
get_query::<ArticleView, _>(hostname, "article", Some(get_article.clone())).await
}
pub async fn edit_article_with_conflict(
instance: &IbisInstance,
edit_form: &EditArticleData,
@ -179,7 +175,7 @@ pub async fn edit_article_with_conflict(
.patch(format!("http://{}/api/v1/article", instance.hostname))
.form(edit_form)
.bearer_auth(&instance.jwt);
handle_json_res(req).await
api::handle_json_res(req).await
}
pub async fn get_conflicts(instance: &IbisInstance) -> MyResult<Vec<ApiConflict>> {
@ -189,7 +185,7 @@ pub async fn get_conflicts(instance: &IbisInstance) -> MyResult<Vec<ApiConflict>
&instance.hostname
))
.bearer_auth(&instance.jwt);
handle_json_res(req).await
api::handle_json_res(req).await
}
pub async fn edit_article(
@ -198,7 +194,7 @@ pub async fn edit_article(
) -> MyResult<ArticleView> {
let edit_res = edit_article_with_conflict(instance, edit_form).await?;
assert!(edit_res.is_none());
get_article(&instance.hostname, edit_form.article_id).await
api::get_article(&instance.hostname, edit_form.article_id).await
}
pub async fn get<T>(hostname: &str, endpoint: &str) -> MyResult<T>
@ -208,18 +204,6 @@ where
get_query(hostname, endpoint, None::<i32>).await
}
pub async fn get_query<T, R>(hostname: &str, endpoint: &str, query: Option<R>) -> MyResult<T>
where
T: for<'de> Deserialize<'de>,
R: Serialize,
{
let mut req = CLIENT.get(format!("http://{}/api/v1/{}", hostname, endpoint));
if let Some(query) = query {
req = req.query(&query);
}
handle_json_res(req).await
}
pub async fn fork_article(
instance: &IbisInstance,
form: &ForkArticleData,
@ -228,21 +212,7 @@ pub async fn fork_article(
.post(format!("http://{}/api/v1/article/fork", instance.hostname))
.form(form)
.bearer_auth(&instance.jwt);
handle_json_res(req).await
}
pub async fn handle_json_res<T>(req: RequestBuilder) -> MyResult<T>
where
T: for<'de> Deserialize<'de>,
{
let res = req.send().await?;
let status = res.status();
let text = res.text().await?;
if status == StatusCode::OK {
Ok(serde_json::from_str(&text).map_err(|e| anyhow!("Json error on {text}: {e}"))?)
} else {
Err(anyhow!("API error: {text}").into())
}
api::handle_json_res(req).await
}
pub async fn follow_instance(instance: &IbisInstance, follow_instance: &str) -> MyResult<()> {
@ -251,7 +221,7 @@ pub async fn follow_instance(instance: &IbisInstance, follow_instance: &str) ->
id: Url::parse(&format!("http://{}", follow_instance))?,
};
let instance_resolved: DbInstance =
get_query(&instance.hostname, "resolve_instance", Some(resolve_form)).await?;
api::get_query(&instance.hostname, "resolve_instance", Some(resolve_form)).await?;
// send follow
let follow_form = FollowInstance {
@ -282,7 +252,7 @@ pub async fn register(hostname: &str, username: &str, password: &str) -> MyResul
let req = CLIENT
.post(format!("http://{}/api/v1/user/register", hostname))
.form(&register_form);
handle_json_res(req).await
api::handle_json_res(req).await
}
pub async fn login(
@ -297,5 +267,5 @@ pub async fn login(
let req = CLIENT
.post(format!("http://{}/api/v1/user/login", instance.hostname))
.form(&login_form);
handle_json_res(req).await
api::handle_json_res(req).await
}

View file

@ -3,17 +3,20 @@ extern crate ibis;
mod common;
use crate::common::{
create_article, edit_article, edit_article_with_conflict, follow_instance, get_article,
get_query, TestData, CLIENT, TEST_ARTICLE_DEFAULT_TEXT,
create_article, edit_article, edit_article_with_conflict, follow_instance, get, TestData,
CLIENT, TEST_ARTICLE_DEFAULT_TEXT,
};
use crate::common::{fork_article, handle_json_res, login};
use crate::common::{fork_article, login};
use crate::common::{get_conflicts, register};
use common::get;
use ibis::api::article::{CreateArticleData, EditArticleData, ForkArticleData};
use ibis::api::{ResolveObject, SearchArticleData};
use ibis::database::article::{ArticleView, DbArticle};
use ibis::database::instance::{DbInstance, InstanceView};
use ibis::error::MyResult;
use ibis::backend::api::article::{CreateArticleData, EditArticleData, ForkArticleData};
use ibis::backend::api::{ResolveObject, SearchArticleData};
use ibis::backend::database::instance::{DbInstance, InstanceView};
use ibis::backend::error::MyResult;
use ibis::common::ArticleView;
use ibis::common::DbArticle;
use ibis::frontend::api::get_article;
use ibis::frontend::api::get_query;
use ibis::frontend::api::handle_json_res;
use pretty_assertions::{assert_eq, assert_ne};
use url::Url;