Move websocket code into separate workspace

This commit is contained in:
Felix Ableitner 2020-09-23 15:14:11 +02:00
parent c2cbfa1fff
commit 49e481e3ac
54 changed files with 539 additions and 957 deletions

33
Cargo.lock generated
View file

@ -1103,12 +1103,6 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0"
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]] [[package]]
name = "dtoa" name = "dtoa"
version = "0.4.6" version = "0.4.6"
@ -1846,7 +1840,6 @@ dependencies = [
"chrono", "chrono",
"diesel", "diesel",
"diesel_migrations", "diesel_migrations",
"dotenv",
"env_logger", "env_logger",
"futures", "futures",
"http", "http",
@ -1858,6 +1851,7 @@ dependencies = [
"lemmy_rate_limit", "lemmy_rate_limit",
"lemmy_structs", "lemmy_structs",
"lemmy_utils", "lemmy_utils",
"lemmy_websocket",
"log", "log",
"openssl", "openssl",
"percent-encoding", "percent-encoding",
@ -1888,8 +1882,6 @@ dependencies = [
"log", "log",
"serde 1.0.116", "serde 1.0.116",
"serde_json", "serde_json",
"strum",
"strum_macros",
] ]
[[package]] [[package]]
@ -1915,6 +1907,29 @@ dependencies = [
"url", "url",
] ]
[[package]]
name = "lemmy_websocket"
version = "0.1.0"
dependencies = [
"actix",
"anyhow",
"background-jobs",
"chrono",
"diesel",
"lemmy_db",
"lemmy_rate_limit",
"lemmy_structs",
"lemmy_utils",
"log",
"rand 0.7.3",
"reqwest",
"serde 1.0.116",
"serde_json",
"strum",
"strum_macros",
"tokio",
]
[[package]] [[package]]
name = "lettre" name = "lettre"
version = "0.9.3" version = "0.9.3"

View file

@ -12,6 +12,7 @@ members = [
"lemmy_db", "lemmy_db",
"lemmy_structs", "lemmy_structs",
"lemmy_rate_limit", "lemmy_rate_limit",
"lemmy_websocket",
] ]
[dependencies] [dependencies]
@ -19,9 +20,9 @@ lemmy_utils = { path = "./lemmy_utils" }
lemmy_db = { path = "./lemmy_db" } lemmy_db = { path = "./lemmy_db" }
lemmy_structs = { path = "./lemmy_structs" } lemmy_structs = { path = "./lemmy_structs" }
lemmy_rate_limit = { path = "./lemmy_rate_limit" } lemmy_rate_limit = { path = "./lemmy_rate_limit" }
lemmy_websocket = { path = "./lemmy_websocket" }
diesel = "1.4" diesel = "1.4"
diesel_migrations = "1.4" diesel_migrations = "1.4"
dotenv = "0.15"
activitystreams = "0.7.0-alpha.4" activitystreams = "0.7.0-alpha.4"
activitystreams-ext = "0.1.0-alpha.2" activitystreams-ext = "0.1.0-alpha.2"
bcrypt = "0.8" bcrypt = "0.8"

View file

@ -1,13 +1,11 @@
use crate::{ use crate::apub::{activity_queue::send_activity, community::do_announce, insert_activity};
apub::{activity_queue::send_activity, community::do_announce, insert_activity},
LemmyContext,
};
use activitystreams::{ use activitystreams::{
base::{Extends, ExtendsExt}, base::{Extends, ExtendsExt},
object::AsObject, object::AsObject,
}; };
use lemmy_db::{community::Community, user::User_}; use lemmy_db::{community::Community, user::User_};
use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError}; use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use serde::{export::fmt::Debug, Serialize}; use serde::{export::fmt::Debug, Serialize};
use url::{ParseError, Url}; use url::{ParseError, Url};
use uuid::Uuid; use uuid::Uuid;

View file

@ -18,7 +18,6 @@ use crate::{
ToApub, ToApub,
}, },
DbPool, DbPool,
LemmyContext,
}; };
use activitystreams::{ use activitystreams::{
activity::{ activity::{
@ -53,6 +52,7 @@ use lemmy_utils::{
utils::{convert_datetime, remove_slurs, scrape_text_for_mentions, MentionData}, utils::{convert_datetime, remove_slurs, scrape_text_for_mentions, MentionData},
LemmyError, LemmyError,
}; };
use lemmy_websocket::LemmyContext;
use log::debug; use log::debug;
use serde::Deserialize; use serde::Deserialize;
use serde_json::Error; use serde_json::Error;

View file

@ -15,7 +15,6 @@ use crate::{
ToApub, ToApub,
}, },
DbPool, DbPool,
LemmyContext,
}; };
use activitystreams::{ use activitystreams::{
activity::{ activity::{
@ -52,6 +51,7 @@ use lemmy_utils::{
utils::{check_slurs, check_slurs_opt, convert_datetime}, utils::{check_slurs, check_slurs_opt, convert_datetime},
LemmyError, LemmyError,
}; };
use lemmy_websocket::LemmyContext;
use serde::Deserialize; use serde::Deserialize;
use url::Url; use url::Url;

View file

@ -9,7 +9,6 @@ use crate::{
APUB_JSON_CONTENT_TYPE, APUB_JSON_CONTENT_TYPE,
}, },
request::{retry, RecvError}, request::{retry, RecvError},
LemmyContext,
}; };
use activitystreams::{base::BaseExt, collection::OrderedCollection, object::Note, prelude::*}; use activitystreams::{base::BaseExt, collection::OrderedCollection, object::Note, prelude::*};
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
@ -31,6 +30,7 @@ use lemmy_db::{
}; };
use lemmy_structs::{blocking, site::SearchResponse}; use lemmy_structs::{blocking, site::SearchResponse};
use lemmy_utils::{apub::get_apub_protocol_string, location_info, LemmyError}; use lemmy_utils::{apub::get_apub_protocol_string, location_info, LemmyError};
use lemmy_websocket::LemmyContext;
use log::debug; use log::debug;
use reqwest::Client; use reqwest::Client;
use serde::Deserialize; use serde::Deserialize;

View file

@ -1,17 +1,14 @@
use crate::{ use crate::apub::inbox::{
apub::inbox::{ activities::{
activities::{ create::receive_create,
create::receive_create, delete::receive_delete,
delete::receive_delete, dislike::receive_dislike,
dislike::receive_dislike, like::receive_like,
like::receive_like, remove::receive_remove,
remove::receive_remove, undo::receive_undo,
undo::receive_undo, update::receive_update,
update::receive_update,
},
shared_inbox::{get_community_id_from_activity, receive_unhandled_activity},
}, },
LemmyContext, shared_inbox::{get_community_id_from_activity, receive_unhandled_activity},
}; };
use activitystreams::{ use activitystreams::{
activity::*, activity::*,
@ -21,6 +18,7 @@ use activitystreams::{
use actix_web::HttpResponse; use actix_web::HttpResponse;
use anyhow::Context; use anyhow::Context;
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
pub async fn receive_announce( pub async fn receive_announce(
activity: AnyBase, activity: AnyBase,

View file

@ -1,15 +1,12 @@
use crate::{ use crate::apub::{
apub::{ inbox::shared_inbox::{
inbox::shared_inbox::{ announce_if_community_is_local,
announce_if_community_is_local, get_user_from_activity,
get_user_from_activity, receive_unhandled_activity,
receive_unhandled_activity,
},
ActorType,
FromApub,
PageExt,
}, },
LemmyContext, ActorType,
FromApub,
PageExt,
}; };
use activitystreams::{activity::Create, base::AnyBase, object::Note, prelude::*}; use activitystreams::{activity::Create, base::AnyBase, object::Note, prelude::*};
use actix_web::HttpResponse; use actix_web::HttpResponse;
@ -20,14 +17,13 @@ use lemmy_db::{
post::{Post, PostForm}, post::{Post, PostForm},
post_view::PostView, post_view::PostView,
}; };
use lemmy_structs::{ use lemmy_structs::{blocking, comment::CommentResponse, post::PostResponse, send_local_notifs};
blocking,
comment::CommentResponse,
post::PostResponse,
send_local_notifs,
websocket::{SendComment, SendPost, UserOperation},
};
use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError}; use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError};
use lemmy_websocket::{
messages::{SendComment, SendPost},
LemmyContext,
UserOperation,
};
pub async fn receive_create( pub async fn receive_create(
activity: AnyBase, activity: AnyBase,

View file

@ -1,17 +1,14 @@
use crate::{ use crate::apub::{
apub::{ fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, inbox::shared_inbox::{
inbox::shared_inbox::{ announce_if_community_is_local,
announce_if_community_is_local, get_user_from_activity,
get_user_from_activity, receive_unhandled_activity,
receive_unhandled_activity,
},
ActorType,
FromApub,
GroupExt,
PageExt,
}, },
LemmyContext, ActorType,
FromApub,
GroupExt,
PageExt,
}; };
use activitystreams::{activity::Delete, base::AnyBase, object::Note, prelude::*}; use activitystreams::{activity::Delete, base::AnyBase, object::Note, prelude::*};
use actix_web::HttpResponse; use actix_web::HttpResponse;
@ -31,9 +28,13 @@ use lemmy_structs::{
comment::CommentResponse, comment::CommentResponse,
community::CommunityResponse, community::CommunityResponse,
post::PostResponse, post::PostResponse,
websocket::{SendComment, SendCommunityRoomMessage, SendPost, UserOperation},
}; };
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{
messages::{SendComment, SendCommunityRoomMessage, SendPost},
LemmyContext,
UserOperation,
};
pub async fn receive_delete( pub async fn receive_delete(
activity: AnyBase, activity: AnyBase,

View file

@ -1,15 +1,12 @@
use crate::{ use crate::apub::{
apub::{ fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, inbox::shared_inbox::{
inbox::shared_inbox::{ announce_if_community_is_local,
announce_if_community_is_local, get_user_from_activity,
get_user_from_activity, receive_unhandled_activity,
receive_unhandled_activity,
},
FromApub,
PageExt,
}, },
LemmyContext, FromApub,
PageExt,
}; };
use activitystreams::{activity::Dislike, base::AnyBase, object::Note, prelude::*}; use activitystreams::{activity::Dislike, base::AnyBase, object::Note, prelude::*};
use actix_web::HttpResponse; use actix_web::HttpResponse;
@ -21,13 +18,13 @@ use lemmy_db::{
post_view::PostView, post_view::PostView,
Likeable, Likeable,
}; };
use lemmy_structs::{ use lemmy_structs::{blocking, comment::CommentResponse, post::PostResponse};
blocking,
comment::CommentResponse,
post::PostResponse,
websocket::{SendComment, SendPost, UserOperation},
};
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{
messages::{SendComment, SendPost},
LemmyContext,
UserOperation,
};
pub async fn receive_dislike( pub async fn receive_dislike(
activity: AnyBase, activity: AnyBase,

View file

@ -1,15 +1,12 @@
use crate::{ use crate::apub::{
apub::{ fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, inbox::shared_inbox::{
inbox::shared_inbox::{ announce_if_community_is_local,
announce_if_community_is_local, get_user_from_activity,
get_user_from_activity, receive_unhandled_activity,
receive_unhandled_activity,
},
FromApub,
PageExt,
}, },
LemmyContext, FromApub,
PageExt,
}; };
use activitystreams::{activity::Like, base::AnyBase, object::Note, prelude::*}; use activitystreams::{activity::Like, base::AnyBase, object::Note, prelude::*};
use actix_web::HttpResponse; use actix_web::HttpResponse;
@ -21,13 +18,13 @@ use lemmy_db::{
post_view::PostView, post_view::PostView,
Likeable, Likeable,
}; };
use lemmy_structs::{ use lemmy_structs::{blocking, comment::CommentResponse, post::PostResponse};
blocking,
comment::CommentResponse,
post::PostResponse,
websocket::{SendComment, SendPost, UserOperation},
};
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{
messages::{SendComment, SendPost},
LemmyContext,
UserOperation,
};
pub async fn receive_like( pub async fn receive_like(
activity: AnyBase, activity: AnyBase,

View file

@ -1,18 +1,15 @@
use crate::{ use crate::apub::{
apub::{ fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, inbox::shared_inbox::{
inbox::shared_inbox::{ announce_if_community_is_local,
announce_if_community_is_local, get_community_id_from_activity,
get_community_id_from_activity, get_user_from_activity,
get_user_from_activity, receive_unhandled_activity,
receive_unhandled_activity,
},
ActorType,
FromApub,
GroupExt,
PageExt,
}, },
LemmyContext, ActorType,
FromApub,
GroupExt,
PageExt,
}; };
use activitystreams::{activity::Remove, base::AnyBase, object::Note, prelude::*}; use activitystreams::{activity::Remove, base::AnyBase, object::Note, prelude::*};
use actix_web::HttpResponse; use actix_web::HttpResponse;
@ -32,9 +29,13 @@ use lemmy_structs::{
comment::CommentResponse, comment::CommentResponse,
community::CommunityResponse, community::CommunityResponse,
post::PostResponse, post::PostResponse,
websocket::{SendComment, SendCommunityRoomMessage, SendPost, UserOperation},
}; };
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{
messages::{SendComment, SendCommunityRoomMessage, SendPost},
LemmyContext,
UserOperation,
};
pub async fn receive_remove( pub async fn receive_remove(
activity: AnyBase, activity: AnyBase,

View file

@ -1,17 +1,14 @@
use crate::{ use crate::apub::{
apub::{ fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, inbox::shared_inbox::{
inbox::shared_inbox::{ announce_if_community_is_local,
announce_if_community_is_local, get_user_from_activity,
get_user_from_activity, receive_unhandled_activity,
receive_unhandled_activity,
},
ActorType,
FromApub,
GroupExt,
PageExt,
}, },
LemmyContext, ActorType,
FromApub,
GroupExt,
PageExt,
}; };
use activitystreams::{ use activitystreams::{
activity::*, activity::*,
@ -37,9 +34,13 @@ use lemmy_structs::{
comment::CommentResponse, comment::CommentResponse,
community::CommunityResponse, community::CommunityResponse,
post::PostResponse, post::PostResponse,
websocket::{SendComment, SendCommunityRoomMessage, SendPost, UserOperation},
}; };
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{
messages::{SendComment, SendCommunityRoomMessage, SendPost},
LemmyContext,
UserOperation,
};
pub async fn receive_undo( pub async fn receive_undo(
activity: AnyBase, activity: AnyBase,

View file

@ -1,16 +1,13 @@
use crate::{ use crate::apub::{
apub::{ fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, inbox::shared_inbox::{
inbox::shared_inbox::{ announce_if_community_is_local,
announce_if_community_is_local, get_user_from_activity,
get_user_from_activity, receive_unhandled_activity,
receive_unhandled_activity,
},
ActorType,
FromApub,
PageExt,
}, },
LemmyContext, ActorType,
FromApub,
PageExt,
}; };
use activitystreams::{activity::Update, base::AnyBase, object::Note, prelude::*}; use activitystreams::{activity::Update, base::AnyBase, object::Note, prelude::*};
use actix_web::HttpResponse; use actix_web::HttpResponse;
@ -22,14 +19,13 @@ use lemmy_db::{
post_view::PostView, post_view::PostView,
Crud, Crud,
}; };
use lemmy_structs::{ use lemmy_structs::{blocking, comment::CommentResponse, post::PostResponse, send_local_notifs};
blocking,
comment::CommentResponse,
post::PostResponse,
send_local_notifs,
websocket::{SendComment, SendPost, UserOperation},
};
use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError}; use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError};
use lemmy_websocket::{
messages::{SendComment, SendPost},
LemmyContext,
UserOperation,
};
pub async fn receive_update( pub async fn receive_update(
activity: AnyBase, activity: AnyBase,

View file

@ -1,12 +1,9 @@
use crate::{ use crate::apub::{
apub::{ check_is_apub_id_valid,
check_is_apub_id_valid, extensions::signatures::verify,
extensions::signatures::verify, fetcher::get_or_fetch_and_upsert_user,
fetcher::get_or_fetch_and_upsert_user, insert_activity,
insert_activity, ActorType,
ActorType,
},
LemmyContext,
}; };
use activitystreams::{ use activitystreams::{
activity::{ActorAndObject, Follow, Undo}, activity::{ActorAndObject, Follow, Undo},
@ -22,6 +19,7 @@ use lemmy_db::{
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
use log::debug; use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt::Debug; use std::fmt::Debug;

View file

@ -1,26 +1,23 @@
use crate::{ use crate::apub::{
apub::{ check_is_apub_id_valid,
check_is_apub_id_valid, community::do_announce,
community::do_announce, extensions::signatures::verify,
extensions::signatures::verify, fetcher::{
fetcher::{ get_or_fetch_and_upsert_actor,
get_or_fetch_and_upsert_actor, get_or_fetch_and_upsert_community,
get_or_fetch_and_upsert_community, get_or_fetch_and_upsert_user,
get_or_fetch_and_upsert_user,
},
inbox::activities::{
announce::receive_announce,
create::receive_create,
delete::receive_delete,
dislike::receive_dislike,
like::receive_like,
remove::receive_remove,
undo::receive_undo,
update::receive_update,
},
insert_activity,
}, },
LemmyContext, inbox::activities::{
announce::receive_announce,
create::receive_create,
delete::receive_delete,
dislike::receive_dislike,
like::receive_like,
remove::receive_remove,
undo::receive_undo,
update::receive_update,
},
insert_activity,
}; };
use activitystreams::{ use activitystreams::{
activity::{ActorAndObject, ActorAndObjectRef}, activity::{ActorAndObject, ActorAndObjectRef},
@ -32,6 +29,7 @@ use actix_web::{web, HttpRequest, HttpResponse};
use anyhow::Context; use anyhow::Context;
use lemmy_db::user::User_; use lemmy_db::user::User_;
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
use log::debug; use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt::Debug; use std::fmt::Debug;

View file

@ -1,12 +1,9 @@
use crate::{ use crate::apub::{
apub::{ check_is_apub_id_valid,
check_is_apub_id_valid, extensions::signatures::verify,
extensions::signatures::verify, fetcher::{get_or_fetch_and_upsert_actor, get_or_fetch_and_upsert_community},
fetcher::{get_or_fetch_and_upsert_actor, get_or_fetch_and_upsert_community}, insert_activity,
insert_activity, FromApub,
FromApub,
},
LemmyContext,
}; };
use activitystreams::{ use activitystreams::{
activity::{Accept, ActorAndObject, Create, Delete, Undo, Update}, activity::{Accept, ActorAndObject, Create, Delete, Undo, Update},
@ -25,12 +22,9 @@ use lemmy_db::{
Crud, Crud,
Followable, Followable,
}; };
use lemmy_structs::{ use lemmy_structs::{blocking, user::PrivateMessageResponse};
blocking,
user::PrivateMessageResponse,
websocket::{SendUserRoomMessage, UserOperation},
};
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation};
use log::debug; use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt::Debug; use std::fmt::Debug;

View file

@ -15,7 +15,6 @@ use crate::{
ToApub, ToApub,
}, },
DbPool, DbPool,
LemmyContext,
}; };
use activitystreams::{ use activitystreams::{
activity::{ activity::{
@ -47,6 +46,7 @@ use lemmy_utils::{
utils::{check_slurs, convert_datetime, remove_slurs}, utils::{check_slurs, convert_datetime, remove_slurs},
LemmyError, LemmyError,
}; };
use lemmy_websocket::LemmyContext;
use serde::Deserialize; use serde::Deserialize;
use url::Url; use url::Url;

View file

@ -13,7 +13,6 @@ use crate::{
ToApub, ToApub,
}, },
DbPool, DbPool,
LemmyContext,
}; };
use activitystreams::{ use activitystreams::{
activity::{ activity::{
@ -34,6 +33,7 @@ use lemmy_db::{
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, utils::convert_datetime, LemmyError}; use lemmy_utils::{location_info, utils::convert_datetime, LemmyError};
use lemmy_websocket::LemmyContext;
use url::Url; use url::Url;
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]

View file

@ -12,7 +12,6 @@ use crate::{
ToApub, ToApub,
}, },
DbPool, DbPool,
LemmyContext,
}; };
use activitystreams::{ use activitystreams::{
activity::{ activity::{
@ -37,6 +36,7 @@ use lemmy_utils::{
utils::{check_slurs, check_slurs_opt, convert_datetime}, utils::{check_slurs, check_slurs_opt, convert_datetime},
LemmyError, LemmyError,
}; };
use lemmy_websocket::LemmyContext;
use serde::Deserialize; use serde::Deserialize;
use url::Url; use url::Url;

View file

@ -4,14 +4,6 @@ extern crate diesel;
extern crate strum_macros; extern crate strum_macros;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
extern crate bcrypt;
extern crate chrono;
extern crate log;
extern crate regex;
extern crate serde;
extern crate serde_json;
extern crate sha2;
extern crate strum;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use diesel::{result::Error, *}; use diesel::{result::Error, *};

View file

@ -1,9 +1,5 @@
#[macro_use] #[macro_use]
extern crate strum_macros; extern crate strum_macros;
extern crate actix_web;
extern crate futures;
extern crate log;
extern crate tokio;
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform}; use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
use futures::future::{ok, Ready}; use futures::future::{ok, Ready};

View file

@ -16,7 +16,5 @@ log = "0.4"
diesel = "1.4" diesel = "1.4"
actix = "0.10" actix = "0.10"
actix-web = { version = "3.0" } actix-web = { version = "3.0" }
strum = "0.19"
strum_macros = "0.19"
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
serde_json = { version = "1.0", features = ["preserve_order"]} serde_json = { version = "1.0", features = ["preserve_order"]}

View file

@ -1,13 +1,3 @@
extern crate actix;
extern crate actix_web;
extern crate diesel;
extern crate log;
extern crate serde;
#[macro_use]
extern crate strum_macros;
extern crate chrono;
extern crate serde_json;
pub mod comment; pub mod comment;
pub mod community; pub mod community;
pub mod post; pub mod post;

View file

@ -1,215 +1 @@
use crate::{comment::CommentResponse, post::PostResponse};
use actix::{prelude::*, Recipient};
use lemmy_utils::{CommunityId, ConnectionId, IPAddr, LemmyError, PostId, UserId};
use serde::{Deserialize, Serialize};
pub fn serialize_websocket_message<Response>(
op: &UserOperation,
data: &Response,
) -> Result<String, LemmyError>
where
Response: Serialize,
{
let response = WebsocketResponse {
op: op.to_string(),
data,
};
Ok(serde_json::to_string(&response)?)
}
#[derive(Serialize)]
struct WebsocketResponse<T> {
op: String,
data: T,
}
#[derive(EnumString, ToString, Debug, Clone)]
pub enum UserOperation {
Login,
Register,
GetCaptcha,
CreateCommunity,
CreatePost,
ListCommunities,
ListCategories,
GetPost,
GetCommunity,
CreateComment,
EditComment,
DeleteComment,
RemoveComment,
MarkCommentAsRead,
SaveComment,
CreateCommentLike,
GetPosts,
CreatePostLike,
EditPost,
DeletePost,
RemovePost,
LockPost,
StickyPost,
SavePost,
EditCommunity,
DeleteCommunity,
RemoveCommunity,
FollowCommunity,
GetFollowedCommunities,
GetUserDetails,
GetReplies,
GetUserMentions,
MarkUserMentionAsRead,
GetModlog,
BanFromCommunity,
AddModToCommunity,
CreateSite,
EditSite,
GetSite,
AddAdmin,
BanUser,
Search,
MarkAllAsRead,
SaveUserSettings,
TransferCommunity,
TransferSite,
DeleteAccount,
PasswordReset,
PasswordChange,
CreatePrivateMessage,
EditPrivateMessage,
DeletePrivateMessage,
MarkPrivateMessageAsRead,
GetPrivateMessages,
UserJoin,
GetComments,
GetSiteConfig,
SaveSiteConfig,
PostJoin,
CommunityJoin,
}
/// Chat server sends this messages to session
#[derive(Message)]
#[rtype(result = "()")]
pub struct WSMessage(pub String);
/// Message for chat server communications
/// New chat session is created
#[derive(Message)]
#[rtype(usize)]
pub struct Connect {
pub addr: Recipient<WSMessage>,
pub ip: IPAddr,
}
/// Session is disconnected
#[derive(Message)]
#[rtype(result = "()")]
pub struct Disconnect {
pub id: ConnectionId,
pub ip: IPAddr,
}
/// The messages sent to websocket clients
#[derive(Serialize, Deserialize, Message)]
#[rtype(result = "Result<String, std::convert::Infallible>")]
pub struct StandardMessage {
/// Id of the client session
pub id: ConnectionId,
/// Peer message
pub msg: String,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct SendAllMessage<Response> {
pub op: UserOperation,
pub response: Response,
pub websocket_id: Option<ConnectionId>,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct SendUserRoomMessage<Response> {
pub op: UserOperation,
pub response: Response,
pub recipient_id: UserId,
pub websocket_id: Option<ConnectionId>,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct SendCommunityRoomMessage<Response> {
pub op: UserOperation,
pub response: Response,
pub community_id: CommunityId,
pub websocket_id: Option<ConnectionId>,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct SendPost {
pub op: UserOperation,
pub post: PostResponse,
pub websocket_id: Option<ConnectionId>,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct SendComment {
pub op: UserOperation,
pub comment: CommentResponse,
pub websocket_id: Option<ConnectionId>,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct JoinUserRoom {
pub user_id: UserId,
pub id: ConnectionId,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct JoinCommunityRoom {
pub community_id: CommunityId,
pub id: ConnectionId,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct JoinPostRoom {
pub post_id: PostId,
pub id: ConnectionId,
}
#[derive(Message)]
#[rtype(usize)]
pub struct GetUsersOnline;
#[derive(Message)]
#[rtype(usize)]
pub struct GetPostUsersOnline {
pub post_id: PostId,
}
#[derive(Message)]
#[rtype(usize)]
pub struct GetCommunityUsersOnline {
pub community_id: CommunityId,
}
#[derive(Message, Debug)]
#[rtype(result = "()")]
pub struct CaptchaItem {
pub uuid: String,
pub answer: String,
pub expires: chrono::NaiveDateTime,
}
#[derive(Message)]
#[rtype(bool)]
pub struct CheckCaptcha {
pub uuid: String,
pub answer: String,
}

View file

@ -1,16 +1,5 @@
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
extern crate actix_web;
extern crate anyhow;
extern crate comrak;
extern crate lettre;
extern crate lettre_email;
extern crate openssl;
extern crate rand;
extern crate regex;
extern crate serde_json;
extern crate thiserror;
extern crate url;
pub mod apub; pub mod apub;
pub mod email; pub mod email;

View file

@ -0,0 +1,24 @@
[package]
name = "lemmy_websocket"
version = "0.1.0"
authors = ["Felix Ableitner <me@nutomic.com>"]
edition = "2018"
[dependencies]
lemmy_utils = { path = "../lemmy_utils" }
lemmy_structs = { path = "../lemmy_structs" }
lemmy_db = { path = "../lemmy_db" }
lemmy_rate_limit = { path = "../lemmy_rate_limit" }
reqwest = { version = "0.10", features = ["json"] }
log = "0.4"
rand = "0.7"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"]}
actix = "0.10"
anyhow = "1.0"
diesel = "1.4"
background-jobs = " 0.8"
tokio = "0.2"
strum = "0.19"
strum_macros = "0.19"
chrono = { version = "0.4", features = ["serde"] }

View file

@ -1,4 +1,4 @@
use crate::LemmyContext; use crate::{messages::*, serialize_websocket_message, LemmyContext, UserOperation};
use actix::prelude::*; use actix::prelude::*;
use anyhow::Context as acontext; use anyhow::Context as acontext;
use background_jobs::QueueHandle; use background_jobs::QueueHandle;
@ -7,7 +7,7 @@ use diesel::{
PgConnection, PgConnection,
}; };
use lemmy_rate_limit::RateLimit; use lemmy_rate_limit::RateLimit;
use lemmy_structs::{comment::*, post::*, websocket::*}; use lemmy_structs::{comment::*, post::*};
use lemmy_utils::{ use lemmy_utils::{
location_info, location_info,
APIError, APIError,
@ -349,8 +349,6 @@ impl ChatServer {
msg: StandardMessage, msg: StandardMessage,
ctx: &mut Context<Self>, ctx: &mut Context<Self>,
) -> impl Future<Output = Result<String, LemmyError>> { ) -> impl Future<Output = Result<String, LemmyError>> {
let addr = ctx.address();
let pool = self.pool.clone();
let rate_limiter = self.rate_limiter.clone(); let rate_limiter = self.rate_limiter.clone();
let ip: IPAddr = match self.sessions.get(&msg.id) { let ip: IPAddr = match self.sessions.get(&msg.id) {
@ -358,26 +356,21 @@ impl ChatServer {
None => "blank_ip".to_string(), None => "blank_ip".to_string(),
}; };
let client = self.client.clone(); let context = LemmyContext {
let activity_queue = self.activity_queue.clone(); pool: self.pool.clone(),
chat_server: ctx.address(),
client: self.client.to_owned(),
activity_queue: self.activity_queue.to_owned(),
};
let message_handler = self.message_handler; let message_handler = self.message_handler;
async move { async move {
let msg = msg;
let json: Value = serde_json::from_str(&msg.msg)?; let json: Value = serde_json::from_str(&msg.msg)?;
let data = &json["data"].to_string(); let data = &json["data"].to_string();
let op = &json["op"].as_str().ok_or(APIError { let op = &json["op"].as_str().ok_or(APIError {
message: "Unknown op type".to_string(), message: "Unknown op type".to_string(),
})?; })?;
let user_operation: UserOperation = UserOperation::from_str(&op)?; let user_operation = UserOperation::from_str(&op)?;
let context = LemmyContext {
pool,
chat_server: addr,
client,
activity_queue,
};
let fut = (message_handler)(context, msg.id, user_operation.clone(), data); let fut = (message_handler)(context, msg.id, user_operation.clone(), data);
match user_operation { match user_operation {
UserOperation::Register => rate_limiter.register().wrap(ip, fut).await, UserOperation::Register => rate_limiter.register().wrap(ip, fut).await,

View file

@ -1,7 +1,9 @@
use crate::websocket::chat_server::{ChatServer, SessionInfo}; use crate::{
chat_server::{ChatServer, SessionInfo},
messages::*,
};
use actix::{Actor, Context, Handler, ResponseFuture}; use actix::{Actor, Context, Handler, ResponseFuture};
use lemmy_db::naive_now; use lemmy_db::naive_now;
use lemmy_structs::websocket::*;
use log::{error, info}; use log::{error, info};
use rand::Rng; use rand::Rng;
use serde::Serialize; use serde::Serialize;

144
lemmy_websocket/src/lib.rs Normal file
View file

@ -0,0 +1,144 @@
#[macro_use]
extern crate strum_macros;
use crate::chat_server::ChatServer;
use actix::Addr;
use background_jobs::QueueHandle;
use lemmy_db::DbPool;
use lemmy_utils::LemmyError;
use reqwest::Client;
use serde::Serialize;
pub mod chat_server;
pub mod handlers;
pub mod messages;
pub struct LemmyContext {
pub pool: DbPool,
pub chat_server: Addr<ChatServer>,
pub client: Client,
pub activity_queue: QueueHandle,
}
impl LemmyContext {
pub fn create(
pool: DbPool,
chat_server: Addr<ChatServer>,
client: Client,
activity_queue: QueueHandle,
) -> LemmyContext {
LemmyContext {
pool,
chat_server,
client,
activity_queue,
}
}
pub fn pool(&self) -> &DbPool {
&self.pool
}
pub fn chat_server(&self) -> &Addr<ChatServer> {
&self.chat_server
}
pub fn client(&self) -> &Client {
&self.client
}
pub fn activity_queue(&self) -> &QueueHandle {
&self.activity_queue
}
}
impl Clone for LemmyContext {
fn clone(&self) -> Self {
LemmyContext {
pool: self.pool.clone(),
chat_server: self.chat_server.clone(),
client: self.client.clone(),
activity_queue: self.activity_queue.clone(),
}
}
}
#[derive(Serialize)]
struct WebsocketResponse<T> {
op: String,
data: T,
}
pub fn serialize_websocket_message<Response>(
op: &UserOperation,
data: &Response,
) -> Result<String, LemmyError>
where
Response: Serialize,
{
let response = WebsocketResponse {
op: op.to_string(),
data,
};
Ok(serde_json::to_string(&response)?)
}
#[derive(EnumString, ToString, Debug, Clone)]
pub enum UserOperation {
Login,
Register,
GetCaptcha,
CreateCommunity,
CreatePost,
ListCommunities,
ListCategories,
GetPost,
GetCommunity,
CreateComment,
EditComment,
DeleteComment,
RemoveComment,
MarkCommentAsRead,
SaveComment,
CreateCommentLike,
GetPosts,
CreatePostLike,
EditPost,
DeletePost,
RemovePost,
LockPost,
StickyPost,
SavePost,
EditCommunity,
DeleteCommunity,
RemoveCommunity,
FollowCommunity,
GetFollowedCommunities,
GetUserDetails,
GetReplies,
GetUserMentions,
MarkUserMentionAsRead,
GetModlog,
BanFromCommunity,
AddModToCommunity,
CreateSite,
EditSite,
GetSite,
AddAdmin,
BanUser,
Search,
MarkAllAsRead,
SaveUserSettings,
TransferCommunity,
TransferSite,
DeleteAccount,
PasswordReset,
PasswordChange,
CreatePrivateMessage,
EditPrivateMessage,
DeletePrivateMessage,
MarkPrivateMessageAsRead,
GetPrivateMessages,
UserJoin,
GetComments,
GetSiteConfig,
SaveSiteConfig,
PostJoin,
CommunityJoin,
}

View file

@ -0,0 +1,132 @@
use crate::UserOperation;
use actix::{prelude::*, Recipient};
use lemmy_structs::{comment::CommentResponse, post::PostResponse};
use lemmy_utils::{CommunityId, ConnectionId, IPAddr, PostId, UserId};
use serde::{Deserialize, Serialize};
/// Chat server sends this messages to session
#[derive(Message)]
#[rtype(result = "()")]
pub struct WSMessage(pub String);
/// Message for chat server communications
/// New chat session is created
#[derive(Message)]
#[rtype(usize)]
pub struct Connect {
pub addr: Recipient<WSMessage>,
pub ip: IPAddr,
}
/// Session is disconnected
#[derive(Message)]
#[rtype(result = "()")]
pub struct Disconnect {
pub id: ConnectionId,
pub ip: IPAddr,
}
/// The messages sent to websocket clients
#[derive(Serialize, Deserialize, Message)]
#[rtype(result = "Result<String, std::convert::Infallible>")]
pub struct StandardMessage {
/// Id of the client session
pub id: ConnectionId,
/// Peer message
pub msg: String,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct SendAllMessage<Response> {
pub op: UserOperation,
pub response: Response,
pub websocket_id: Option<ConnectionId>,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct SendUserRoomMessage<Response> {
pub op: UserOperation,
pub response: Response,
pub recipient_id: UserId,
pub websocket_id: Option<ConnectionId>,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct SendCommunityRoomMessage<Response> {
pub op: UserOperation,
pub response: Response,
pub community_id: CommunityId,
pub websocket_id: Option<ConnectionId>,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct SendPost {
pub op: UserOperation,
pub post: PostResponse,
pub websocket_id: Option<ConnectionId>,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct SendComment {
pub op: UserOperation,
pub comment: CommentResponse,
pub websocket_id: Option<ConnectionId>,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct JoinUserRoom {
pub user_id: UserId,
pub id: ConnectionId,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct JoinCommunityRoom {
pub community_id: CommunityId,
pub id: ConnectionId,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct JoinPostRoom {
pub post_id: PostId,
pub id: ConnectionId,
}
#[derive(Message)]
#[rtype(usize)]
pub struct GetUsersOnline;
#[derive(Message)]
#[rtype(usize)]
pub struct GetPostUsersOnline {
pub post_id: PostId,
}
#[derive(Message)]
#[rtype(usize)]
pub struct GetCommunityUsersOnline {
pub community_id: CommunityId,
}
#[derive(Message, Debug)]
#[rtype(result = "()")]
pub struct CaptchaItem {
pub uuid: String,
pub answer: String,
pub expires: chrono::NaiveDateTime,
}
#[derive(Message)]
#[rtype(bool)]
pub struct CheckCaptcha {
pub uuid: String,
pub answer: String,
}

View file

@ -8,7 +8,6 @@ use crate::{
Perform, Perform,
}, },
apub::{ApubLikeableType, ApubObjectType}, apub::{ApubLikeableType, ApubObjectType},
LemmyContext,
}; };
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_db::{ use lemmy_db::{
@ -24,12 +23,7 @@ use lemmy_db::{
Saveable, Saveable,
SortType, SortType,
}; };
use lemmy_structs::{ use lemmy_structs::{blocking, comment::*, send_local_notifs};
blocking,
comment::*,
send_local_notifs,
websocket::{SendComment, UserOperation},
};
use lemmy_utils::{ use lemmy_utils::{
apub::{make_apub_endpoint, EndpointType}, apub::{make_apub_endpoint, EndpointType},
utils::{remove_slurs, scrape_text_for_mentions}, utils::{remove_slurs, scrape_text_for_mentions},
@ -37,6 +31,7 @@ use lemmy_utils::{
ConnectionId, ConnectionId,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};
use std::str::FromStr; use std::str::FromStr;
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]

View file

@ -1,7 +1,6 @@
use crate::{ use crate::{
api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, is_mod_or_admin, Perform}, api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, is_mod_or_admin, Perform},
apub::ActorType, apub::ActorType,
LemmyContext,
}; };
use actix_web::web::Data; use actix_web::web::Data;
use anyhow::Context; use anyhow::Context;
@ -22,16 +21,7 @@ use lemmy_db::{
Joinable, Joinable,
SortType, SortType,
}; };
use lemmy_structs::{ use lemmy_structs::{blocking, community::*};
blocking,
community::*,
websocket::{
GetCommunityUsersOnline,
JoinCommunityRoom,
SendCommunityRoomMessage,
UserOperation,
},
};
use lemmy_utils::{ use lemmy_utils::{
apub::{generate_actor_keypair, make_apub_endpoint, EndpointType}, apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
location_info, location_info,
@ -40,6 +30,11 @@ use lemmy_utils::{
ConnectionId, ConnectionId,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{
messages::{GetCommunityUsersOnline, JoinCommunityRoom, SendCommunityRoomMessage},
LemmyContext,
UserOperation,
};
use std::str::FromStr; use std::str::FromStr;
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]

View file

@ -1,4 +1,4 @@
use crate::{api::claims::Claims, DbPool, LemmyContext}; use crate::{api::claims::Claims, DbPool};
use actix_web::{web, web::Data}; use actix_web::{web, web::Data};
use lemmy_db::{ use lemmy_db::{
community::Community, community::Community,
@ -7,16 +7,9 @@ use lemmy_db::{
user::User_, user::User_,
Crud, Crud,
}; };
use lemmy_structs::{ use lemmy_structs::{blocking, comment::*, community::*, post::*, site::*, user::*};
blocking,
comment::*,
community::*,
post::*,
site::*,
user::*,
websocket::{serialize_websocket_message, UserOperation},
};
use lemmy_utils::{APIError, ConnectionId, LemmyError}; use lemmy_utils::{APIError, ConnectionId, LemmyError};
use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation};
use serde::Deserialize; use serde::Deserialize;
pub mod claims; pub mod claims;

View file

@ -2,7 +2,6 @@ use crate::{
api::{check_community_ban, get_user_from_jwt, get_user_from_jwt_opt, is_mod_or_admin, Perform}, api::{check_community_ban, get_user_from_jwt, get_user_from_jwt_opt, is_mod_or_admin, Perform},
apub::{ApubLikeableType, ApubObjectType}, apub::{ApubLikeableType, ApubObjectType},
fetch_iframely_and_pictrs_data, fetch_iframely_and_pictrs_data,
LemmyContext,
}; };
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_db::{ use lemmy_db::{
@ -19,11 +18,7 @@ use lemmy_db::{
Saveable, Saveable,
SortType, SortType,
}; };
use lemmy_structs::{ use lemmy_structs::{blocking, post::*};
blocking,
post::*,
websocket::{GetPostUsersOnline, JoinPostRoom, SendPost, UserOperation},
};
use lemmy_utils::{ use lemmy_utils::{
apub::{make_apub_endpoint, EndpointType}, apub::{make_apub_endpoint, EndpointType},
utils::{check_slurs, check_slurs_opt, is_valid_post_title}, utils::{check_slurs, check_slurs_opt, is_valid_post_title},
@ -31,6 +26,11 @@ use lemmy_utils::{
ConnectionId, ConnectionId,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{
messages::{GetPostUsersOnline, JoinPostRoom, SendPost},
LemmyContext,
UserOperation,
};
use std::str::FromStr; use std::str::FromStr;
use url::Url; use url::Url;

View file

@ -2,7 +2,6 @@ use crate::{
api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform}, api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform},
apub::fetcher::search_by_apub_id, apub::fetcher::search_by_apub_id,
version, version,
LemmyContext,
}; };
use actix_web::web::Data; use actix_web::web::Data;
use anyhow::Context; use anyhow::Context;
@ -22,12 +21,7 @@ use lemmy_db::{
SearchType, SearchType,
SortType, SortType,
}; };
use lemmy_structs::{ use lemmy_structs::{blocking, site::*, user::Register};
blocking,
site::*,
user::Register,
websocket::{GetUsersOnline, SendAllMessage, UserOperation},
};
use lemmy_utils::{ use lemmy_utils::{
location_info, location_info,
settings::Settings, settings::Settings,
@ -36,6 +30,11 @@ use lemmy_utils::{
ConnectionId, ConnectionId,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{
messages::{GetUsersOnline, SendAllMessage},
LemmyContext,
UserOperation,
};
use log::{debug, info}; use log::{debug, info};
use std::str::FromStr; use std::str::FromStr;

View file

@ -2,7 +2,6 @@ use crate::{
api::{claims::Claims, get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform}, api::{claims::Claims, get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform},
apub::ApubObjectType, apub::ApubObjectType,
captcha_espeak_wav_base64, captcha_espeak_wav_base64,
LemmyContext,
}; };
use actix_web::web::Data; use actix_web::web::Data;
use anyhow::Context; use anyhow::Context;
@ -34,18 +33,7 @@ use lemmy_db::{
ListingType, ListingType,
SortType, SortType,
}; };
use lemmy_structs::{ use lemmy_structs::{blocking, user::*};
blocking,
user::*,
websocket::{
CaptchaItem,
CheckCaptcha,
JoinUserRoom,
SendAllMessage,
SendUserRoomMessage,
UserOperation,
},
};
use lemmy_utils::{ use lemmy_utils::{
apub::{generate_actor_keypair, make_apub_endpoint, EndpointType}, apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
email::send_email, email::send_email,
@ -63,6 +51,11 @@ use lemmy_utils::{
ConnectionId, ConnectionId,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{
messages::{CaptchaItem, CheckCaptcha, JoinUserRoom, SendAllMessage, SendUserRoomMessage},
LemmyContext,
UserOperation,
};
use log::error; use log::error;
use std::str::FromStr; use std::str::FromStr;

View file

@ -1,364 +0,0 @@
pub mod activities;
pub mod activity_queue;
pub mod comment;
pub mod community;
pub mod extensions;
pub mod fetcher;
pub mod inbox;
pub mod post;
pub mod private_message;
pub mod user;
use crate::{
apub::extensions::{
group_extensions::GroupExtension,
page_extension::PageExtension,
signatures::{PublicKey, PublicKeyExtension},
},
request::{retry, RecvError},
routes::webfinger::WebFingerResponse,
DbPool,
LemmyContext,
};
use activitystreams::{
activity::Follow,
actor::{ApActor, Group, Person},
base::AsBase,
markers::Base,
object::{Page, Tombstone},
prelude::*,
};
use activitystreams_ext::{Ext1, Ext2};
use actix_web::{body::Body, HttpResponse};
use anyhow::{anyhow, Context};
use chrono::NaiveDateTime;
use lemmy_db::{activity::do_insert_activity, user::User_};
use lemmy_structs::blocking;
use lemmy_utils::{
apub::get_apub_protocol_string,
location_info,
settings::Settings,
utils::{convert_datetime, MentionData},
LemmyError,
};
use log::debug;
use reqwest::Client;
use serde::Serialize;
use url::{ParseError, Url};
type GroupExt = Ext2<ApActor<Group>, GroupExtension, PublicKeyExtension>;
type PersonExt = Ext1<ApActor<Person>, PublicKeyExtension>;
type PageExt = Ext1<Page, PageExtension>;
pub static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json";
/// Convert the data to json and turn it into an HTTP Response with the correct ActivityPub
/// headers.
fn create_apub_response<T>(data: &T) -> HttpResponse<Body>
where
T: Serialize,
{
HttpResponse::Ok()
.content_type(APUB_JSON_CONTENT_TYPE)
.json(data)
}
fn create_apub_tombstone_response<T>(data: &T) -> HttpResponse<Body>
where
T: Serialize,
{
HttpResponse::Gone()
.content_type(APUB_JSON_CONTENT_TYPE)
.json(data)
}
// Checks if the ID has a valid format, correct scheme, and is in the allowed instance list.
fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> {
let settings = Settings::get();
let domain = apub_id.domain().context(location_info!())?.to_string();
let local_instance = settings
.hostname
.split(':')
.collect::<Vec<&str>>()
.first()
.context(location_info!())?
.to_string();
if !settings.federation.enabled {
return if domain == local_instance {
Ok(())
} else {
Err(
anyhow!(
"Trying to connect with {}, but federation is disabled",
domain
)
.into(),
)
};
}
if apub_id.scheme() != get_apub_protocol_string() {
return Err(anyhow!("invalid apub id scheme: {:?}", apub_id.scheme()).into());
}
let mut allowed_instances = Settings::get().get_allowed_instances();
let blocked_instances = Settings::get().get_blocked_instances();
if !allowed_instances.is_empty() {
// need to allow this explicitly because apub activities might contain objects from our local
// instance. split is needed to remove the port in our federation test setup.
allowed_instances.push(local_instance);
if allowed_instances.contains(&domain) {
Ok(())
} else {
Err(anyhow!("{} not in federation allowlist", domain).into())
}
} else if !blocked_instances.is_empty() {
if blocked_instances.contains(&domain) {
Err(anyhow!("{} is in federation blocklist", domain).into())
} else {
Ok(())
}
} else {
panic!("Invalid config, both allowed_instances and blocked_instances are specified");
}
}
#[async_trait::async_trait(?Send)]
pub trait ToApub {
type Response;
async fn to_apub(&self, pool: &DbPool) -> Result<Self::Response, LemmyError>;
fn to_tombstone(&self) -> Result<Tombstone, LemmyError>;
}
/// Updated is actually the deletion time
fn create_tombstone<T>(
deleted: bool,
object_id: &str,
updated: Option<NaiveDateTime>,
former_type: T,
) -> Result<Tombstone, LemmyError>
where
T: ToString,
{
if deleted {
if let Some(updated) = updated {
let mut tombstone = Tombstone::new();
tombstone.set_id(object_id.parse()?);
tombstone.set_former_type(former_type.to_string());
tombstone.set_deleted(convert_datetime(updated));
Ok(tombstone)
} else {
Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
}
} else {
Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
}
}
#[async_trait::async_trait(?Send)]
pub trait FromApub {
type ApubType;
/// Converts an object from ActivityPub type to Lemmy internal type.
///
/// * `apub` The object to read from
/// * `context` LemmyContext which holds DB pool, HTTP client etc
/// * `expected_domain` If present, ensure that the apub object comes from the same domain as
/// this URL
///
async fn from_apub(
apub: &Self::ApubType,
context: &LemmyContext,
expected_domain: Option<Url>,
) -> Result<Self, LemmyError>
where
Self: Sized;
}
#[async_trait::async_trait(?Send)]
pub trait ApubObjectType {
async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_delete(
&self,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
}
pub(in crate::apub) fn check_actor_domain<T, Kind>(
apub: &T,
expected_domain: Option<Url>,
) -> Result<String, LemmyError>
where
T: Base + AsBase<Kind>,
{
let actor_id = if let Some(url) = expected_domain {
let domain = url.domain().context(location_info!())?;
apub.id(domain)?.context(location_info!())?
} else {
let actor_id = apub.id_unchecked().context(location_info!())?;
check_is_apub_id_valid(&actor_id)?;
actor_id
};
Ok(actor_id.to_string())
}
#[async_trait::async_trait(?Send)]
pub trait ApubLikeableType {
async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_like(&self, creator: &User_, context: &LemmyContext)
-> Result<(), LemmyError>;
}
#[async_trait::async_trait(?Send)]
pub trait ActorType {
fn actor_id_str(&self) -> String;
// TODO: every actor should have a public key, so this shouldnt be an option (needs to be fixed in db)
fn public_key(&self) -> Option<String>;
fn private_key(&self) -> Option<String>;
/// numeric id in the database, used for insert_activity
fn user_id(&self) -> i32;
// These two have default impls, since currently a community can't follow anything,
// and a user can't be followed (yet)
#[allow(unused_variables)]
async fn send_follow(
&self,
follow_actor_id: &Url,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_unfollow(
&self,
follow_actor_id: &Url,
context: &LemmyContext,
) -> Result<(), LemmyError>;
#[allow(unused_variables)]
async fn send_accept_follow(
&self,
follow: Follow,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_delete(
&self,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
/// For a given community, returns the inboxes of all followers.
async fn get_follower_inboxes(&self, pool: &DbPool) -> Result<Vec<Url>, LemmyError>;
fn actor_id(&self) -> Result<Url, ParseError> {
Url::parse(&self.actor_id_str())
}
// TODO move these to the db rows
fn get_inbox_url(&self) -> Result<Url, ParseError> {
Url::parse(&format!("{}/inbox", &self.actor_id_str()))
}
fn get_shared_inbox_url(&self) -> Result<Url, LemmyError> {
let actor_id = self.actor_id()?;
let url = format!(
"{}://{}{}/inbox",
&actor_id.scheme(),
&actor_id.host_str().context(location_info!())?,
if let Some(port) = actor_id.port() {
format!(":{}", port)
} else {
"".to_string()
},
);
Ok(Url::parse(&url)?)
}
fn get_outbox_url(&self) -> Result<Url, ParseError> {
Url::parse(&format!("{}/outbox", &self.actor_id_str()))
}
fn get_followers_url(&self) -> Result<Url, ParseError> {
Url::parse(&format!("{}/followers", &self.actor_id_str()))
}
fn get_following_url(&self) -> String {
format!("{}/following", &self.actor_id_str())
}
fn get_liked_url(&self) -> String {
format!("{}/liked", &self.actor_id_str())
}
fn get_public_key_ext(&self) -> Result<PublicKeyExtension, LemmyError> {
Ok(
PublicKey {
id: format!("{}#main-key", self.actor_id_str()),
owner: self.actor_id_str(),
public_key_pem: self.public_key().context(location_info!())?,
}
.to_ext(),
)
}
}
pub async fn fetch_webfinger_url(
mention: &MentionData,
client: &Client,
) -> Result<Url, LemmyError> {
let fetch_url = format!(
"{}://{}/.well-known/webfinger?resource=acct:{}@{}",
get_apub_protocol_string(),
mention.domain,
mention.name,
mention.domain
);
debug!("Fetching webfinger url: {}", &fetch_url);
let response = retry(|| client.get(&fetch_url).send()).await?;
let res: WebFingerResponse = response
.json()
.await
.map_err(|e| RecvError(e.to_string()))?;
let link = res
.links
.iter()
.find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
.ok_or_else(|| anyhow!("No application/activity+json link found."))?;
link
.href
.to_owned()
.map(|u| Url::parse(&u))
.transpose()?
.ok_or_else(|| anyhow!("No href found.").into())
}
pub async fn insert_activity<T>(
user_id: i32,
data: T,
local: bool,
pool: &DbPool,
) -> Result<(), LemmyError>
where
T: Serialize + std::fmt::Debug + Send + 'static,
{
blocking(pool, move |conn| {
do_insert_activity(conn, user_id, &data, local)
})
.await??;
Ok(())
}

View file

@ -1,23 +1,6 @@
#![recursion_limit = "512"] #![recursion_limit = "512"]
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
extern crate actix;
extern crate actix_web;
extern crate base64;
extern crate bcrypt;
extern crate captcha;
extern crate chrono;
extern crate diesel;
extern crate dotenv;
extern crate jsonwebtoken;
extern crate log;
extern crate openssl;
extern crate reqwest;
extern crate rss;
extern crate serde;
extern crate serde_json;
extern crate sha2;
extern crate strum;
pub mod api; pub mod api;
pub mod apub; pub mod apub;
@ -25,15 +8,9 @@ pub mod code_migrations;
pub mod request; pub mod request;
pub mod routes; pub mod routes;
pub mod version; pub mod version;
pub mod websocket;
use crate::{ use crate::request::{retry, RecvError};
request::{retry, RecvError},
websocket::chat_server::ChatServer,
};
use actix::Addr;
use anyhow::anyhow; use anyhow::anyhow;
use background_jobs::QueueHandle;
use lemmy_db::DbPool; use lemmy_db::DbPool;
use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError}; use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError};
use log::error; use log::error;
@ -42,52 +19,6 @@ use reqwest::Client;
use serde::Deserialize; use serde::Deserialize;
use std::process::Command; use std::process::Command;
pub struct LemmyContext {
pub pool: DbPool,
pub chat_server: Addr<ChatServer>,
pub client: Client,
pub activity_queue: QueueHandle,
}
impl LemmyContext {
pub fn create(
pool: DbPool,
chat_server: Addr<ChatServer>,
client: Client,
activity_queue: QueueHandle,
) -> LemmyContext {
LemmyContext {
pool,
chat_server,
client,
activity_queue,
}
}
pub fn pool(&self) -> &DbPool {
&self.pool
}
pub fn chat_server(&self) -> &Addr<ChatServer> {
&self.chat_server
}
pub fn client(&self) -> &Client {
&self.client
}
pub fn activity_queue(&self) -> &QueueHandle {
&self.activity_queue
}
}
impl Clone for LemmyContext {
fn clone(&self) -> Self {
LemmyContext {
pool: self.pool.clone(),
chat_server: self.chat_server.clone(),
client: self.client.clone(),
activity_queue: self.activity_queue.clone(),
}
}
}
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct IframelyResponse { pub struct IframelyResponse {
title: Option<String>, title: Option<String>,

View file

@ -23,11 +23,10 @@ use lemmy_server::{
apub::activity_queue::create_activity_queue, apub::activity_queue::create_activity_queue,
code_migrations::run_advanced_migrations, code_migrations::run_advanced_migrations,
routes::*, routes::*,
websocket::chat_server::ChatServer,
LemmyContext,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{settings::Settings, LemmyError, CACHE_CONTROL_REGEX}; use lemmy_utils::{settings::Settings, LemmyError, CACHE_CONTROL_REGEX};
use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
use reqwest::Client; use reqwest::Client;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;

View file

@ -1,7 +1,8 @@
use crate::{api::Perform, LemmyContext}; use crate::api::Perform;
use actix_web::{error::ErrorBadRequest, *}; use actix_web::{error::ErrorBadRequest, *};
use lemmy_rate_limit::RateLimit; use lemmy_rate_limit::RateLimit;
use lemmy_structs::{comment::*, community::*, post::*, site::*, user::*}; use lemmy_structs::{comment::*, community::*, post::*, site::*, user::*};
use lemmy_websocket::LemmyContext;
use serde::Deserialize; use serde::Deserialize;
pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {

View file

@ -1,4 +1,4 @@
use crate::{api::claims::Claims, LemmyContext}; use crate::api::claims::Claims;
use actix_web::{error::ErrorBadRequest, *}; use actix_web::{error::ErrorBadRequest, *};
use anyhow::anyhow; use anyhow::anyhow;
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{DateTime, NaiveDateTime, Utc};
@ -15,6 +15,7 @@ use lemmy_db::{
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{settings::Settings, utils::markdown_to_html, LemmyError}; use lemmy_utils::{settings::Settings, utils::markdown_to_html, LemmyError};
use lemmy_websocket::LemmyContext;
use rss::{CategoryBuilder, ChannelBuilder, GuidBuilder, Item, ItemBuilder}; use rss::{CategoryBuilder, ChannelBuilder, GuidBuilder, Item, ItemBuilder};
use serde::Deserialize; use serde::Deserialize;
use std::str::FromStr; use std::str::FromStr;

View file

@ -1,9 +1,10 @@
use crate::{version, LemmyContext}; use crate::version;
use actix_web::{body::Body, error::ErrorBadRequest, *}; use actix_web::{body::Body, error::ErrorBadRequest, *};
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_db::site_view::SiteView; use lemmy_db::site_view::SiteView;
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError}; use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url; use url::Url;

View file

@ -1,4 +1,3 @@
use crate::LemmyContext;
use actix_web::{error::ErrorBadRequest, web::Query, *}; use actix_web::{error::ErrorBadRequest, web::Query, *};
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_db::{community::Community, user::User_}; use lemmy_db::{community::Community, user::User_};
@ -9,6 +8,7 @@ use lemmy_utils::{
WEBFINGER_COMMUNITY_REGEX, WEBFINGER_COMMUNITY_REGEX,
WEBFINGER_USER_REGEX, WEBFINGER_USER_REGEX,
}; };
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -1,9 +1,12 @@
use crate::{websocket::chat_server::ChatServer, LemmyContext};
use actix::prelude::*; use actix::prelude::*;
use actix_web::*; use actix_web::*;
use actix_web_actors::ws; use actix_web_actors::ws;
use lemmy_structs::websocket::{Connect, Disconnect, StandardMessage, WSMessage};
use lemmy_utils::utils::get_ip; use lemmy_utils::utils::get_ip;
use lemmy_websocket::{
chat_server::ChatServer,
messages::{Connect, Disconnect, StandardMessage, WSMessage},
LemmyContext,
};
use log::{debug, error, info}; use log::{debug, error, info};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};

View file

@ -1,2 +0,0 @@
pub mod chat_server;
pub mod handlers;