2019-08-14 02:52:43 +00:00
|
|
|
#![recursion_limit = "512"]
|
2019-09-07 15:35:05 +00:00
|
|
|
#[macro_use]
|
|
|
|
pub extern crate strum_macros;
|
|
|
|
#[macro_use]
|
|
|
|
pub extern crate lazy_static;
|
|
|
|
#[macro_use]
|
|
|
|
pub extern crate failure;
|
2019-03-21 01:22:31 +00:00
|
|
|
pub extern crate actix;
|
|
|
|
pub extern crate actix_web;
|
2019-03-23 01:42:57 +00:00
|
|
|
pub extern crate bcrypt;
|
2019-09-07 15:35:05 +00:00
|
|
|
pub extern crate chrono;
|
2020-07-10 18:15:41 +00:00
|
|
|
pub extern crate diesel;
|
2019-09-07 15:35:05 +00:00
|
|
|
pub extern crate dotenv;
|
|
|
|
pub extern crate jsonwebtoken;
|
2020-03-14 21:03:05 +00:00
|
|
|
extern crate log;
|
2020-04-03 04:12:05 +00:00
|
|
|
pub extern crate openssl;
|
2020-03-28 22:02:49 +00:00
|
|
|
pub extern crate rss;
|
2019-09-07 15:35:05 +00:00
|
|
|
pub extern crate serde;
|
|
|
|
pub extern crate serde_json;
|
2019-12-26 19:48:13 +00:00
|
|
|
pub extern crate sha2;
|
2019-09-07 15:35:05 +00:00
|
|
|
pub extern crate strum;
|
2019-04-21 07:26:26 +00:00
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
pub mod api;
|
2019-03-21 01:22:31 +00:00
|
|
|
pub mod apub;
|
2020-07-10 18:15:41 +00:00
|
|
|
pub mod code_migrations;
|
2020-04-19 22:08:25 +00:00
|
|
|
pub mod rate_limit;
|
2020-07-01 12:54:29 +00:00
|
|
|
pub mod request;
|
2019-12-31 12:55:33 +00:00
|
|
|
pub mod routes;
|
2019-11-15 02:08:25 +00:00
|
|
|
pub mod version;
|
2019-11-21 19:27:52 +00:00
|
|
|
pub mod websocket;
|
2019-02-28 06:02:55 +00:00
|
|
|
|
2020-07-10 18:15:41 +00:00
|
|
|
use crate::request::{retry, RecvError};
|
2020-07-01 12:54:29 +00:00
|
|
|
use actix_web::{client::Client, dev::ConnectionInfo};
|
2020-03-13 15:08:42 +00:00
|
|
|
use log::error;
|
2020-03-07 23:31:13 +00:00
|
|
|
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
|
|
|
|
use serde::Deserialize;
|
2019-03-05 03:52:09 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
|
2020-04-19 22:08:25 +00:00
|
|
|
pub type ConnectionId = usize;
|
|
|
|
pub type PostId = i32;
|
|
|
|
pub type CommunityId = i32;
|
|
|
|
pub type UserId = i32;
|
|
|
|
pub type IPAddr = String;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct LemmyError {
|
|
|
|
inner: failure::Error,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<T> for LemmyError
|
|
|
|
where
|
|
|
|
T: Into<failure::Error>,
|
|
|
|
{
|
|
|
|
fn from(t: T) -> Self {
|
|
|
|
LemmyError { inner: t.into() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:15:41 +00:00
|
|
|
impl std::fmt::Display for LemmyError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
self.inner.fmt(f)
|
2020-02-03 03:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:15:41 +00:00
|
|
|
impl actix_web::error::ResponseError for LemmyError {}
|
2019-10-30 03:35:39 +00:00
|
|
|
|
2020-03-07 23:31:13 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
pub struct IframelyResponse {
|
|
|
|
title: Option<String>,
|
|
|
|
description: Option<String>,
|
|
|
|
thumbnail_url: Option<String>,
|
|
|
|
html: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
pub async fn fetch_iframely(client: &Client, url: &str) -> Result<IframelyResponse, LemmyError> {
|
2020-03-09 16:50:28 +00:00
|
|
|
let fetch_url = format!("http://iframely/oembed?url={}", url);
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let mut response = retry(|| client.get(&fetch_url).send()).await?;
|
|
|
|
|
|
|
|
let res: IframelyResponse = response
|
|
|
|
.json()
|
|
|
|
.await
|
|
|
|
.map_err(|e| RecvError(e.to_string()))?;
|
2020-03-07 23:31:13 +00:00
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2020-06-10 22:22:57 +00:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
|
|
pub struct PictrsResponse {
|
|
|
|
files: Vec<PictrsFile>,
|
|
|
|
msg: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
|
|
pub struct PictrsFile {
|
|
|
|
file: String,
|
|
|
|
delete_token: String,
|
2020-03-07 23:31:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
pub async fn fetch_pictrs(client: &Client, image_url: &str) -> Result<PictrsResponse, LemmyError> {
|
|
|
|
is_image_content_type(client, image_url).await?;
|
2020-05-11 23:06:12 +00:00
|
|
|
|
2020-03-07 23:31:13 +00:00
|
|
|
let fetch_url = format!(
|
2020-06-10 22:22:57 +00:00
|
|
|
"http://pictrs:8080/image/download?url={}",
|
|
|
|
utf8_percent_encode(image_url, NON_ALPHANUMERIC) // TODO this might not be needed
|
2020-03-07 23:31:13 +00:00
|
|
|
);
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let mut response = retry(|| client.get(&fetch_url).send()).await?;
|
|
|
|
|
|
|
|
let response: PictrsResponse = response
|
|
|
|
.json()
|
|
|
|
.await
|
|
|
|
.map_err(|e| RecvError(e.to_string()))?;
|
|
|
|
|
|
|
|
if response.msg == "ok" {
|
|
|
|
Ok(response)
|
2020-06-10 22:22:57 +00:00
|
|
|
} else {
|
2020-07-01 12:54:29 +00:00
|
|
|
Err(format_err!("{}", &response.msg).into())
|
2020-06-10 22:22:57 +00:00
|
|
|
}
|
2020-03-07 23:31:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn fetch_iframely_and_pictrs_data(
|
|
|
|
client: &Client,
|
2020-03-07 23:31:13 +00:00
|
|
|
url: Option<String>,
|
|
|
|
) -> (
|
|
|
|
Option<String>,
|
|
|
|
Option<String>,
|
|
|
|
Option<String>,
|
|
|
|
Option<String>,
|
|
|
|
) {
|
2020-05-11 18:01:10 +00:00
|
|
|
match &url {
|
|
|
|
Some(url) => {
|
|
|
|
// Fetch iframely data
|
|
|
|
let (iframely_title, iframely_description, iframely_thumbnail_url, iframely_html) =
|
2020-07-01 12:54:29 +00:00
|
|
|
match fetch_iframely(client, url).await {
|
2020-05-11 18:01:10 +00:00
|
|
|
Ok(res) => (res.title, res.description, res.thumbnail_url, res.html),
|
|
|
|
Err(e) => {
|
|
|
|
error!("iframely err: {}", e);
|
|
|
|
(None, None, None, None)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-10 22:22:57 +00:00
|
|
|
// Fetch pictrs thumbnail
|
|
|
|
let pictrs_thumbnail = match iframely_thumbnail_url {
|
2020-07-01 12:54:29 +00:00
|
|
|
Some(iframely_thumbnail_url) => match fetch_pictrs(client, &iframely_thumbnail_url).await {
|
2020-06-10 22:22:57 +00:00
|
|
|
Ok(res) => Some(res.files[0].file.to_owned()),
|
2020-05-11 18:01:10 +00:00
|
|
|
Err(e) => {
|
2020-06-10 22:22:57 +00:00
|
|
|
error!("pictrs err: {}", e);
|
2020-05-11 18:01:10 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
2020-05-07 00:40:36 +00:00
|
|
|
// Try to generate a small thumbnail if iframely is not supported
|
2020-07-01 12:54:29 +00:00
|
|
|
None => match fetch_pictrs(client, &url).await {
|
2020-06-10 22:22:57 +00:00
|
|
|
Ok(res) => Some(res.files[0].file.to_owned()),
|
2020-05-11 18:01:10 +00:00
|
|
|
Err(e) => {
|
2020-06-10 22:22:57 +00:00
|
|
|
error!("pictrs err: {}", e);
|
2020-05-11 18:01:10 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
(
|
|
|
|
iframely_title,
|
|
|
|
iframely_description,
|
|
|
|
iframely_html,
|
2020-06-10 22:22:57 +00:00
|
|
|
pictrs_thumbnail,
|
2020-05-11 18:01:10 +00:00
|
|
|
)
|
|
|
|
}
|
2020-03-07 23:31:13 +00:00
|
|
|
None => (None, None, None, None),
|
2020-05-11 18:01:10 +00:00
|
|
|
}
|
2020-03-07 23:31:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 18:15:41 +00:00
|
|
|
pub async fn is_image_content_type(client: &Client, test: &str) -> Result<(), LemmyError> {
|
|
|
|
let response = retry(|| client.get(test).send()).await?;
|
|
|
|
|
|
|
|
if response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Type")
|
|
|
|
.ok_or_else(|| format_err!("No Content-Type header"))?
|
|
|
|
.to_str()?
|
|
|
|
.starts_with("image/")
|
|
|
|
{
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(format_err!("Not an image type.").into())
|
|
|
|
}
|
2020-03-28 22:02:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 18:02:25 +00:00
|
|
|
pub fn get_ip(conn_info: &ConnectionInfo) -> String {
|
|
|
|
conn_info
|
2020-07-08 06:20:08 +00:00
|
|
|
.realip_remote_addr()
|
2020-04-19 22:08:25 +00:00
|
|
|
.unwrap_or("127.0.0.1:12345")
|
|
|
|
.split(':')
|
|
|
|
.next()
|
|
|
|
.unwrap_or("127.0.0.1")
|
|
|
|
.to_string()
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:15:41 +00:00
|
|
|
pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
|
|
|
|
where
|
|
|
|
F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
|
|
|
|
T: Send + 'static,
|
|
|
|
{
|
|
|
|
let pool = pool.clone();
|
|
|
|
let res = actix_web::web::block(move || {
|
|
|
|
let conn = pool.get()?;
|
|
|
|
let res = (f)(&conn);
|
|
|
|
Ok(res) as Result<_, LemmyError>
|
|
|
|
})
|
|
|
|
.await?;
|
2020-05-28 18:07:36 +00:00
|
|
|
|
2020-07-10 18:15:41 +00:00
|
|
|
Ok(res)
|
2020-06-20 09:33:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 03:52:09 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-07-10 18:15:41 +00:00
|
|
|
use crate::is_image_content_type;
|
2019-03-23 01:42:57 +00:00
|
|
|
|
2020-05-11 23:06:12 +00:00
|
|
|
#[test]
|
|
|
|
fn test_image() {
|
2020-07-01 12:54:29 +00:00
|
|
|
actix_rt::System::new("tset_image").block_on(async move {
|
2020-07-10 18:15:41 +00:00
|
|
|
let client = actix_web::client::Client::default();
|
|
|
|
assert!(is_image_content_type(&client, "https://1734811051.rsc.cdn77.org/data/images/full/365645/as-virus-kills-navajos-in-their-homes-tribal-women-provide-lifeline.jpg?w=600?w=650").await.is_ok());
|
|
|
|
assert!(is_image_content_type(&client,
|
|
|
|
"https://twitter.com/BenjaminNorton/status/1259922424272957440?s=20"
|
|
|
|
)
|
|
|
|
.await.is_err()
|
|
|
|
);
|
|
|
|
});
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2020-03-07 23:31:13 +00:00
|
|
|
// These helped with testing
|
|
|
|
// #[test]
|
|
|
|
// fn test_iframely() {
|
2020-07-01 12:54:29 +00:00
|
|
|
// let res = fetch_iframely(client, "https://www.redspark.nu/?p=15341").await;
|
2020-03-07 23:31:13 +00:00
|
|
|
// assert!(res.is_ok());
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn test_pictshare() {
|
|
|
|
// let res = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpg");
|
|
|
|
// assert!(res.is_ok());
|
|
|
|
// let res_other = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpgaoeu");
|
|
|
|
// assert!(res_other.is_err());
|
|
|
|
// }
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|