1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
/// This module provides API model definitions & associated methods.
pub mod api_models {
/// This module provides models related to [User]
pub mod users {
use chrono::{DateTime, Utc};
use serde_derive::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
/// Base User model
pub struct User {
/// Username
pub username: String,
/// Email (may not be present based on instance settings & associated request)
pub email: Option<String>,
/// Creation D/T (may not be present based on instance settings & associated request)
pub created: Option<DateTime<Utc>>,
}
}
/// This module provides models related to [Post]
pub mod posts {
use chrono::{DateTime, Utc};
use derive_builder::Builder;
use reqwest::Method;
use serde_derive::{Deserialize, Serialize};
use crate::api_client::{ApiError, Client};
use super::collections::{Collection, MovePost, MoveResult};
#[derive(Clone, Debug, Serialize, Deserialize)]
/// Enum describing the appearance/font of a post
pub enum PostAppearance {
#[serde(rename = "sans")]
///
SansSerif,
#[serde(rename = "serif")]
#[serde(alias = "norm")]
#[serde(alias = "serif")]
///
Serif,
#[serde(rename = "wrap")]
///
Wrap,
#[serde(rename = "mono")]
///
Mono,
#[serde(rename = "code")]
///
Code,
}
#[derive(Clone, Debug, Serialize, Deserialize, Builder)]
/// Struct describing a pending update to a [Post]
pub struct PostUpdate {
#[serde(skip_serializing)]
/// [Client] instance
pub client: Option<Client>,
#[serde(skip_serializing)]
/// Post ID
pub id: String,
/// Post token, if not owned
pub token: Option<String>,
/// New post body
pub body: String,
/// New post title
pub title: Option<String>,
/// New post font
pub font: Option<PostAppearance>,
/// New post language
pub lang: Option<String>,
/// New post RTL
pub rtl: bool,
}
impl PostUpdate {
/// Dispatches an update request to the server.
pub async fn update(&self) -> Result<Post, ApiError> {
if let Some(client) = self.client.clone() {
client
.api()
.post::<Post, PostUpdate>(
format!("/posts/{}", self.id).as_str(),
Some(self.clone()),
)
.await
.and_then(|mut p| Ok(p.with_client(client.clone())))
} else {
Err(ApiError::UsageError {})
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
/// Main struct describing a single Post
pub struct Post {
///
pub client: Option<Client>,
///
pub id: String,
///
pub slug: Option<String>,
///
pub appearance: Option<PostAppearance>,
///
pub language: Option<String>,
///
pub rtl: bool,
///
pub created: Option<DateTime<Utc>>,
///
pub title: Option<String>,
///
pub body: String,
///
pub tags: Vec<String>,
///
pub views: Option<u64>,
///
pub collection: Option<Collection>,
///
pub token: Option<String>,
}
impl Post {
#[doc(hidden)]
pub fn with_client(&mut self, client: Client) -> Self {
self.client = Some(client);
self.clone()
}
/// Returns a [PostUpdateBuilder] initialized with a [Client], the correct ID, and the specified body text
pub fn build_update(&self, body: String) -> PostUpdateBuilder {
PostUpdateBuilder::default()
.client(self.client.clone())
.id(self.id.clone())
.body(body)
.clone()
}
/// Dispatches an update with an existing [PostUpdate]
pub async fn update(&self, update: PostUpdate) -> Result<Post, ApiError> {
if let Some(client) = self.client.clone() {
client
.api()
.post::<Post, PostUpdate>(
format!("/posts/{}", self.id).as_str(),
Some(update.clone()),
)
.await
.and_then(|mut p| Ok(p.with_client(client.clone())))
} else {
Err(ApiError::UsageError {})
}
}
/// Deletes this post
pub async fn delete(&self) -> Result<(), ApiError> {
if let Some(client) = self.client.clone() {
let mut request = client
.api()
.request(format!("/posts/{}", self.id).as_str(), Method::DELETE)
.unwrap();
if !client.is_authenticated() && self.token.is_some() {
request = request.query(&[("token", self.token.clone().unwrap())]);
}
if let Ok(result) = request.send().await {
client.api().extract_response(result).await
} else {
Err(ApiError::ConnectionError {})
}
} else {
Err(ApiError::UsageError {})
}
}
/// Moves the post to a [Collection] by its alias
pub async fn move_to(&self, collection: &str) -> Result<MoveResult, ApiError> {
if let Some(client) = self.client.clone() {
match client.collections().get(collection).await {
Ok(coll) => {
match client.is_authenticated() {
true => coll.take_posts(&[MovePost::new(&self.id)]).await,
false => coll.take_posts(&[MovePost {id: self.id.clone(), token: self.token.clone()}]).await
}.and_then(|v| {
match v.get(0) {
Some(item) => match item {
Ok(result) => Ok(result.clone()),
Err(result) => Ok(result.clone())
},
None => Err(ApiError::UnknownError { })
}
})
},
Err(e) => Err(e)
}
} else {
Err(ApiError::UsageError {})
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Builder)]
/// Post creation struct
pub struct PostCreation {
#[serde(skip_serializing)]
/// [Client] instance
pub client: Option<Client>,
#[serde(skip_serializing)]
/// Collection to post to, if desired
pub collection: Option<String>,
/// Post body
pub body: String,
/// Post title
pub title: Option<String>,
/// Post font
pub font: Option<PostAppearance>,
/// Post language
pub lang: Option<String>,
/// Post RTL
pub rtl: Option<bool>,
/// Specific post creation DT
pub created: Option<DateTime<Utc>>,
}
impl PostCreation {
/// Publishes the described post to the server
pub async fn publish(&self) -> Result<Post, ApiError> {
if let Some(client) = self.client.clone() {
if let Some(collection) = self.collection.clone() {
client
.api()
.post::<Post, PostCreation>(
format!("/collections/{collection}/post").as_str(),
Some(self.clone()),
)
.await
.and_then(|mut v| Ok(v.with_client(client.clone())))
} else {
client
.api()
.post::<Post, PostCreation>("/posts", Some(self.clone()))
.await
.and_then(|mut v| Ok(v.with_client(client.clone())))
}
} else {
Err(ApiError::UsageError {})
}
}
}
}
#[doc(hidden)]
pub mod responses {
use std::fmt::Debug;
use super::users::User;
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Login {
pub access_token: String,
pub user: User,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ResponseModel {
pub code: u16,
pub data: Value,
}
}
#[doc(hidden)]
pub mod requests {
use serde_derive::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Login {
pub alias: String,
pub pass: String,
}
}
/// This module provides models related to [Collection]
pub mod collections {
use derive_builder::Builder;
use serde_derive::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use crate::api_client::{ApiError, Client};
use super::posts::Post;
#[derive(Clone, Debug, Serialize, Deserialize)]
/// A struct describing a post to move into a collection
pub struct MovePost {
/// Post ID
pub id: String,
/// Post token, if post isn't owned
pub token: Option<String>,
}
impl MovePost {
/// Creates a new MovePost with just an ID
pub fn new(id: &str) -> Self {
MovePost {
id: id.to_string(),
token: None,
}
}
/// Creates a new MovePost with an ID and token
pub fn new_with_token(id: &str, token: &str) -> Self {
MovePost {
id: id.to_string(),
token: Some(token.to_string()),
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
/// Describes the result of a single post move operation
pub enum MoveResult {
/// Successful operation
Success {
/// Operation status code
code: u32,
/// Affected [Post]
post: Post
},
/// Failed operation
Error {
/// Operation status code
code: u32,
/// Operation status text
error_msg: String
},
}
#[derive(Clone, Debug, Serialize, Deserialize)]
/// A struct describing how to pin or unpin a post to a collection
pub struct PinPost {
/// Post ID
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
/// Pin position (should not be used with `unpin`)
pub postion: Option<u64>
}
impl PinPost {
/// Creates a new PinPost with an ID
pub fn new(id: &str) -> Self {
PinPost {
id: id.to_string(),
postion: None
}
}
/// Creates a new PinPost with an ID and a position
pub fn new_at_position(id: &str, position: u64) -> Self {
PinPost {
id: id.to_string(),
postion: Some(position),
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
/// Describes the result of a single pin/unpin operation
pub enum PinResult {
/// Successful operation
Success {
/// Operation status code
code: u32,
/// Post ID
id: String
},
/// Failed operation
Error {
/// Operation status code
code: u32,
/// Operation status text
error_msg: String
},
}
#[derive(Clone, Debug, Serialize, Deserialize)]
/// A struct describing a single Collection entity
pub struct Collection {
///
pub client: Option<Client>,
///
pub alias: String,
///
pub title: String,
///
pub description: Option<String>,
///
pub style_sheet: Option<String>,
///
pub public: bool,
///
pub views: Option<u64>,
///
pub verification_link: Option<String>,
///
pub total_posts: Option<u64>,
}
impl Collection {
#[doc(hidden)]
pub fn with_client(&mut self, client: Client) -> Self {
self.client = Some(client);
self.clone()
}
/// Creates a [CollectionUpdateBuilder] with defaults set
pub fn build_update(&self) -> CollectionUpdateBuilder {
CollectionUpdateBuilder::default()
.alias(Some(self.alias.clone()))
.client(self.client.clone())
.clone()
}
/// Updates a collection from an existing [CollectionUpdate]
pub async fn update(&self, update: CollectionUpdate) -> Result<Collection, ApiError> {
if let Some(client) = self.client.clone() {
client
.api()
.post::<Collection, CollectionUpdate>(
format!("/collections/{}", self.alias).as_str(),
Some(update.clone()),
)
.await
.and_then(|mut p| Ok(p.with_client(client.clone())))
} else {
Err(ApiError::UsageError {})
}
}
/// Deletes this [Collection]
pub async fn delete(&self) -> Result<(), ApiError> {
if let Some(client) = self.client.clone() {
client
.api()
.delete(format!("/collections/{}", self.alias).as_str())
.await
} else {
Err(ApiError::UsageError {})
}
}
/// Returns all [Post]s belonging to this collection
pub async fn get_posts(&self) -> Result<Vec<Post>, ApiError> {
if let Some(client) = self.client.clone() {
client
.api()
.get::<Vec<Post>>(format!("/collections/{}/posts", self.alias).as_str())
.await
.and_then(|mut v| {
Ok(v.iter_mut()
.map(|x| x.with_client(client.clone()))
.collect())
})
} else {
Err(ApiError::UsageError {})
}
}
/// Returns a single [Post] belonging to this collection
pub async fn get_post(&self, slug: String) -> Result<Post, ApiError> {
if let Some(client) = self.client.clone() {
client
.api()
.get::<Post>(format!("/collections/{}/posts/{}", self.alias, slug).as_str())
.await
.and_then(|mut v| Ok(v.with_client(client.clone())))
} else {
Err(ApiError::UsageError {})
}
}
/// Moves a set of [Post]s into this collection
pub async fn take_posts(
&self,
posts: &[MovePost],
) -> Result<Vec<Result<MoveResult, MoveResult>>, ApiError> {
if let Some(client) = self.client.clone() {
let result = client
.api()
.post::<Vec<MoveResult>, &[MovePost]>(
format!("/collections/{}/collect", self.alias).as_str(),
Some(posts),
)
.await;
match result {
Ok(results) => Ok(results
.iter()
.map::<Result<MoveResult, MoveResult>, _>(|r| match r {
MoveResult::Success { code, post } => Ok(MoveResult::Success {
code: code.clone(),
post: post.clone().with_client(client.clone()),
}),
MoveResult::Error { code, error_msg } => Err(MoveResult::Error {
code: code.clone(),
error_msg: error_msg.clone(),
}),
})
.collect()),
Err(e) => Err(e),
}
} else {
Err(ApiError::UsageError {})
}
}
/// Pins a set of [Post]s in this collection
pub async fn pin_posts(
&self,
posts: &[PinPost],
) -> Result<Vec<Result<PinResult, PinResult>>, ApiError> {
if let Some(client) = self.client.clone() {
let result = client
.api()
.post::<Vec<PinResult>, &[PinPost]>(
format!("/collections/{}/pin", self.alias).as_str(),
Some(posts),
)
.await;
match result {
Ok(results) => Ok(results
.iter()
.map::<Result<PinResult, PinResult>, _>(|r| match r {
PinResult::Success { code, id } => Ok(PinResult::Success {
code: code.clone(),
id: id.clone()
}),
PinResult::Error { code, error_msg } => Err(PinResult::Error {
code: code.clone(),
error_msg: error_msg.clone(),
}),
})
.collect()),
Err(e) => Err(e),
}
} else {
Err(ApiError::UsageError {})
}
}
/// Unpins a set of [Post]s from this collection
pub async fn unpin_posts(&self, posts: &[String]) -> Result<Vec<Result<PinResult, PinResult>>, ApiError> {
if let Some(client) = self.client.clone() {
let result = client
.api()
.post::<Vec<PinResult>, Vec<PinPost>>(
format!("/collections/{}/unpin", self.alias).as_str(),
Some(posts.iter().map(|v| PinPost::new(v.as_str())).collect()),
)
.await;
match result {
Ok(results) => Ok(results
.iter()
.map::<Result<PinResult, PinResult>, _>(|r| match r {
PinResult::Success { code, id } => Ok(PinResult::Success {
code: code.clone(),
id: id.clone()
}),
PinResult::Error { code, error_msg } => Err(PinResult::Error {
code: code.clone(),
error_msg: error_msg.clone(),
}),
})
.collect()),
Err(e) => Err(e),
}
} else {
Err(ApiError::UsageError {})
}
}
}
#[derive(Clone, Debug, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
/// Enum describing a collection's visibility
pub enum CollectionVisibility {
///
Unlisted = 0,
///
Public = 1,
///
Private = 2,
///
Password = 4,
}
#[derive(Clone, Debug, Serialize, Deserialize, Builder)]
/// Struct describing a collection update
pub struct CollectionUpdate {
#[serde(skip_serializing)]
/// [Client] instance
pub client: Option<Client>,
#[serde(skip_serializing)]
/// Collection alias to update
pub alias: Option<String>,
/// New title
pub title: Option<String>,
/// New description
pub description: Option<String>,
/// New style sheet
pub style_sheet: Option<String>,
/// New script (Write.as only)
pub script: Option<String>,
/// New visibility level
pub visibility: Option<CollectionVisibility>,
/// New password (only [CollectionVisibility::Password])
pub pass: Option<String>,
/// Whether to enable Mathjax support
pub mathjax: bool,
}
impl CollectionUpdate {
/// Publish the update request to the server
pub async fn update(&self) -> Result<Collection, ApiError> {
if let Some(client) = self.client.clone() {
if let Some(alias) = self.alias.clone() {
client
.api()
.post::<Collection, CollectionUpdate>(
format!("/collections/{}", alias).as_str(),
Some(self.clone()),
)
.await
.and_then(|mut p| Ok(p.with_client(client.clone())))
} else {
Err(ApiError::UsageError {})
}
} else {
Err(ApiError::UsageError {})
}
}
}
}
}