Add canapi and try to use for the API
This commit is contained in:
@@ -7,6 +7,7 @@ authors = ["Baptiste Gelez <baptiste@gelez.xyz>"]
|
||||
activitypub = "0.1.1"
|
||||
ammonia = "1.2.0"
|
||||
bcrypt = "0.2"
|
||||
canapi = "0.1"
|
||||
heck = "0.3.0"
|
||||
lazy_static = "*"
|
||||
openssl = "0.10.11"
|
||||
@@ -25,6 +26,9 @@ version = "0.4"
|
||||
features = ["postgres", "r2d2", "chrono"]
|
||||
version = "1.3.2"
|
||||
|
||||
[dependencies.plume-api]
|
||||
path = "../plume-api"
|
||||
|
||||
[dependencies.plume-common]
|
||||
path = "../plume-common"
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ use activitypub::{
|
||||
link,
|
||||
object::{Note}
|
||||
};
|
||||
use canapi::Provider;
|
||||
use chrono;
|
||||
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, dsl::any};
|
||||
use serde_json;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
extern crate activitypub;
|
||||
extern crate ammonia;
|
||||
extern crate bcrypt;
|
||||
extern crate canapi;
|
||||
extern crate chrono;
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
@@ -10,6 +11,7 @@ extern crate heck;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate openssl;
|
||||
extern crate plume_api;
|
||||
extern crate plume_common;
|
||||
extern crate reqwest;
|
||||
extern crate rocket;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use activitypub::activity;
|
||||
use canapi::Provider;
|
||||
use chrono;
|
||||
use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
|
||||
|
||||
|
||||
@@ -3,11 +3,13 @@ use activitypub::{
|
||||
link,
|
||||
object::{Article, Tombstone}
|
||||
};
|
||||
use canapi::{Error, Provider};
|
||||
use chrono::{NaiveDateTime, TimeZone, Utc};
|
||||
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl, dsl::any};
|
||||
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl, dsl::any, Expression, BoolExpressionMethods};
|
||||
use heck::KebabCase;
|
||||
use serde_json;
|
||||
|
||||
use plume_api::posts::PostEndpoint;
|
||||
use plume_common::activity_pub::{
|
||||
Hashtag, Source,
|
||||
PUBLIC_VISIBILTY, Id, IntoId,
|
||||
@@ -55,6 +57,56 @@ pub struct NewPost {
|
||||
pub source: String,
|
||||
}
|
||||
|
||||
impl Provider<PgConnection> for Post {
|
||||
type Data = PostEndpoint;
|
||||
|
||||
fn get(conn: &PgConnection, id: i32) -> Result<PostEndpoint, Error> {
|
||||
Post::get(conn, id).map(|p| Ok(PostEndpoint {
|
||||
id: Some(p.id),
|
||||
title: Some(p.title.clone()),
|
||||
subtitle: Some(p.subtitle.clone()),
|
||||
content: Some(p.content.get().clone())
|
||||
})).unwrap_or(Err(Error::NotFound("Get Post".to_string())))
|
||||
}
|
||||
|
||||
fn list(conn: &PgConnection, filter: PostEndpoint) -> Vec<PostEndpoint> {
|
||||
let mut filters = Vec::new();
|
||||
if let Some(title) = filter.title {
|
||||
filters.push(posts::title.eq(title));
|
||||
}
|
||||
|
||||
let filters = filters.into_iter();
|
||||
let res = if let Some(first_filter) = filters.next() {
|
||||
posts::table.filter(filters.fold(first_filter, |q, f| q.and(f)))
|
||||
.get_results::<Post>(conn)
|
||||
} else {
|
||||
posts::table.get_results::<Post>(conn)
|
||||
};
|
||||
|
||||
res.map(|ps| ps.into_iter()
|
||||
.map(|p| PostEndpoint {
|
||||
id: Some(p.id),
|
||||
title: Some(p.title.clone()),
|
||||
subtitle: Some(p.subtitle.clone()),
|
||||
content: Some(p.content.get().clone())
|
||||
})
|
||||
.collect()
|
||||
).unwrap_or(vec![])
|
||||
}
|
||||
|
||||
fn create(conn: &PgConnection, query: PostEndpoint) -> Result<PostEndpoint, Error> {
|
||||
|
||||
}
|
||||
|
||||
fn update(conn: &PgConnection, id: i32, new_data: PostEndpoint) -> Result<PostEndpoint, Error> {
|
||||
|
||||
}
|
||||
|
||||
fn delete(conn: &PgConnection, id: i32) {
|
||||
Post::get(conn, id).map(|p| p.delete(conn));
|
||||
}
|
||||
}
|
||||
|
||||
impl Post {
|
||||
insert!(posts, NewPost);
|
||||
get!(posts);
|
||||
|
||||
Reference in New Issue
Block a user