use crate::graph::data::Data;
use crate::prelude::errors::ClientErr;
use crate::prelude::utils::GetPostResponse;
use crate::prelude::HttpConnection;
use serde::{Deserialize, Serialize};
#[cfg(feature = "web-sys")]
use web_sys::{File, FormData};
#[derive(Clone)]
pub struct PhotoApi {
base_url: String,
page_access_token: String,
}
impl PhotoApi {
pub fn new(base_url: String, page_access_token: String) -> PhotoApi {
PhotoApi {
base_url,
page_access_token,
}
}
#[cfg(feature = "web-sys")]
pub async fn post_by_file(
&self,
file: File,
photo_params: PhotoParams,
) -> Result<PhotoResponse, ClientErr> {
let form_data = self.clone().form_data(photo_params, file);
let base_url = self.base_url.replace("EDGE", "photos");
let url = base_url + "?access_token=" + &self.page_access_token;
let resp = HttpConnection::video_post::<PhotoResponse>(url, form_data).await?;
Ok(resp)
}
pub async fn post_by_url(&self, photo_params: PhotoParams) -> Result<PhotoResponse, ClientErr> {
let base_url = self.base_url.replace("EDGE", "photos");
let mut url = base_url + "?access_token=" + &self.page_access_token;
if !photo_params.url.is_empty() {
url = url + "&url=" + &photo_params.url;
}
if !photo_params.message.is_empty() {
url = url + "&message=" + &photo_params.message;
}
if !photo_params.published {
url = url + "&published=false";
}
if photo_params.temporary {
url = url + "&temporary=true";
}
if !photo_params.place.is_empty() {
url = url + "&place=" + &photo_params.place;
}
let resp = HttpConnection::get::<PhotoResponse>(url, "".to_string()).await?;
Ok(resp)
}
pub async fn all_photos(
&self,
photo_type: Option<PhotoType>,
) -> Result<Data<GetPostResponse>, ClientErr> {
let mut url = self.base_url.replace("EDGE", "photos");
if let Some(ptype) = photo_type {
url = url + "?type=" + ptype.as_str();
url = url + "&access_token=" + &self.page_access_token;
} else {
url = url + "?access_token=" + &self.page_access_token;
}
let resp = HttpConnection::get::<Data<GetPostResponse>>(url, "".to_string()).await?;
Ok(resp)
}
#[cfg(feature = "web-sys")]
fn form_data(self, photo_params: PhotoParams, file: File) -> FormData {
let form_data = FormData::new().unwrap();
form_data.append_with_blob("source", &file).ok();
if !photo_params.message.is_empty() {
form_data
.append_with_str("message", &photo_params.message)
.ok();
}
if !photo_params.published {
form_data.append_with_str("published", "false").ok();
}
if photo_params.temporary {
form_data.append_with_str("temporary", "true").ok();
}
if !photo_params.place.is_empty() {
form_data.append_with_str("place", &photo_params.place).ok();
}
form_data
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PhotoType {
Profile,
Uploaded,
}
impl PhotoType {
fn as_str(&self) -> &str {
match self {
PhotoType::Profile => "profile",
PhotoType::Uploaded => "uploaded",
}
}
}
#[derive(Clone, Serialize, Default)]
pub struct PhotoParams {
#[serde(skip_serializing_if = "String::is_empty")]
pub url: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub message: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub place: String,
#[serde(skip_serializing_if = "is_true")]
pub published: bool,
#[serde(skip_serializing_if = "is_false")]
pub temporary: bool,
#[serde(skip)]
pub file_path: String,
}
impl PhotoParams {
pub fn new() -> Self {
PhotoParams {
url: String::new(),
message: String::new(),
place: String::new(),
published: true,
temporary: false,
file_path: String::new(),
}
}
pub fn url(mut self, url: impl Into<String>) -> Self {
self.url = url.into();
self
}
pub fn message(mut self, message: impl Into<String>) -> Self {
self.message = message.into();
self
}
pub fn place(mut self, place: impl Into<String>) -> Self {
self.place = place.into();
self
}
pub fn published(mut self, published: bool) -> Self {
self.published = published;
self
}
pub fn temporary(mut self, temporary: bool) -> Self {
self.temporary = temporary;
self
}
pub fn file_path(mut self, file_path: impl Into<String>) -> Self {
self.file_path = file_path.into();
self
}
}
fn is_true(b: &bool) -> bool {
*b
}
fn is_false(b: &bool) -> bool {
!*b
}
#[derive(Clone, Deserialize)]
pub struct PhotoResponse {
pub id: String,
pub post_id: String,
}