diff --git a/Cargo.lock b/Cargo.lock index 3a5e0f09a..1044e7653 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1103,12 +1103,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" -[[package]] -name = "dotenv" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" - [[package]] name = "dtoa" version = "0.4.6" @@ -1846,7 +1840,6 @@ dependencies = [ "chrono", "diesel", "diesel_migrations", - "dotenv", "env_logger", "futures", "http", @@ -1858,6 +1851,7 @@ dependencies = [ "lemmy_rate_limit", "lemmy_structs", "lemmy_utils", + "lemmy_websocket", "log", "openssl", "percent-encoding", @@ -1888,8 +1882,6 @@ dependencies = [ "log", "serde 1.0.116", "serde_json", - "strum", - "strum_macros", ] [[package]] @@ -1915,6 +1907,29 @@ dependencies = [ "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]] name = "lettre" version = "0.9.3" diff --git a/Cargo.toml b/Cargo.toml index f04866847..eb7756008 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ members = [ "lemmy_db", "lemmy_structs", "lemmy_rate_limit", + "lemmy_websocket", ] [dependencies] @@ -19,9 +20,9 @@ lemmy_utils = { path = "./lemmy_utils" } lemmy_db = { path = "./lemmy_db" } lemmy_structs = { path = "./lemmy_structs" } lemmy_rate_limit = { path = "./lemmy_rate_limit" } +lemmy_websocket = { path = "./lemmy_websocket" } diesel = "1.4" diesel_migrations = "1.4" -dotenv = "0.15" activitystreams = "0.7.0-alpha.4" activitystreams-ext = "0.1.0-alpha.2" bcrypt = "0.8" diff --git a/src/apub/activities.rs b/lemmy_apub/src/activities.rs similarity index 91% rename from src/apub/activities.rs rename to lemmy_apub/src/activities.rs index dc1892693..ee28a8d37 100644 --- a/src/apub/activities.rs +++ b/lemmy_apub/src/activities.rs @@ -1,13 +1,11 @@ -use crate::{ - apub::{activity_queue::send_activity, community::do_announce, insert_activity}, - LemmyContext, -}; +use crate::apub::{activity_queue::send_activity, community::do_announce, insert_activity}; use activitystreams::{ base::{Extends, ExtendsExt}, object::AsObject, }; use lemmy_db::{community::Community, user::User_}; use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError}; +use lemmy_websocket::LemmyContext; use serde::{export::fmt::Debug, Serialize}; use url::{ParseError, Url}; use uuid::Uuid; diff --git a/src/apub/activity_queue.rs b/lemmy_apub/src/activity_queue.rs similarity index 100% rename from src/apub/activity_queue.rs rename to lemmy_apub/src/activity_queue.rs diff --git a/src/apub/comment.rs b/lemmy_apub/src/comment.rs similarity index 99% rename from src/apub/comment.rs rename to lemmy_apub/src/comment.rs index fc19ec334..763ae9b00 100644 --- a/src/apub/comment.rs +++ b/lemmy_apub/src/comment.rs @@ -18,7 +18,6 @@ use crate::{ ToApub, }, DbPool, - LemmyContext, }; use activitystreams::{ activity::{ @@ -53,6 +52,7 @@ use lemmy_utils::{ utils::{convert_datetime, remove_slurs, scrape_text_for_mentions, MentionData}, LemmyError, }; +use lemmy_websocket::LemmyContext; use log::debug; use serde::Deserialize; use serde_json::Error; diff --git a/src/apub/community.rs b/lemmy_apub/src/community.rs similarity index 99% rename from src/apub/community.rs rename to lemmy_apub/src/community.rs index 54b2957ed..b68d5bd15 100644 --- a/src/apub/community.rs +++ b/lemmy_apub/src/community.rs @@ -15,7 +15,6 @@ use crate::{ ToApub, }, DbPool, - LemmyContext, }; use activitystreams::{ activity::{ @@ -52,6 +51,7 @@ use lemmy_utils::{ utils::{check_slurs, check_slurs_opt, convert_datetime}, LemmyError, }; +use lemmy_websocket::LemmyContext; use serde::Deserialize; use url::Url; diff --git a/src/apub/extensions/group_extensions.rs b/lemmy_apub/src/extensions/group_extensions.rs similarity index 100% rename from src/apub/extensions/group_extensions.rs rename to lemmy_apub/src/extensions/group_extensions.rs diff --git a/src/apub/extensions/mod.rs b/lemmy_apub/src/extensions/mod.rs similarity index 100% rename from src/apub/extensions/mod.rs rename to lemmy_apub/src/extensions/mod.rs diff --git a/src/apub/extensions/page_extension.rs b/lemmy_apub/src/extensions/page_extension.rs similarity index 100% rename from src/apub/extensions/page_extension.rs rename to lemmy_apub/src/extensions/page_extension.rs diff --git a/src/apub/extensions/signatures.rs b/lemmy_apub/src/extensions/signatures.rs similarity index 100% rename from src/apub/extensions/signatures.rs rename to lemmy_apub/src/extensions/signatures.rs diff --git a/src/apub/fetcher.rs b/lemmy_apub/src/fetcher.rs similarity index 99% rename from src/apub/fetcher.rs rename to lemmy_apub/src/fetcher.rs index 5d772f062..0f1f1663a 100644 --- a/src/apub/fetcher.rs +++ b/lemmy_apub/src/fetcher.rs @@ -9,7 +9,6 @@ use crate::{ APUB_JSON_CONTENT_TYPE, }, request::{retry, RecvError}, - LemmyContext, }; use activitystreams::{base::BaseExt, collection::OrderedCollection, object::Note, prelude::*}; use anyhow::{anyhow, Context}; @@ -31,6 +30,7 @@ use lemmy_db::{ }; use lemmy_structs::{blocking, site::SearchResponse}; use lemmy_utils::{apub::get_apub_protocol_string, location_info, LemmyError}; +use lemmy_websocket::LemmyContext; use log::debug; use reqwest::Client; use serde::Deserialize; diff --git a/src/apub/inbox/activities/announce.rs b/lemmy_apub/src/inbox/activities/announce.rs similarity index 77% rename from src/apub/inbox/activities/announce.rs rename to lemmy_apub/src/inbox/activities/announce.rs index 47607a05d..52b571994 100644 --- a/src/apub/inbox/activities/announce.rs +++ b/lemmy_apub/src/inbox/activities/announce.rs @@ -1,17 +1,14 @@ -use crate::{ - apub::inbox::{ - activities::{ - create::receive_create, - delete::receive_delete, - dislike::receive_dislike, - like::receive_like, - remove::receive_remove, - undo::receive_undo, - update::receive_update, - }, - shared_inbox::{get_community_id_from_activity, receive_unhandled_activity}, +use crate::apub::inbox::{ + activities::{ + create::receive_create, + delete::receive_delete, + dislike::receive_dislike, + like::receive_like, + remove::receive_remove, + undo::receive_undo, + update::receive_update, }, - LemmyContext, + shared_inbox::{get_community_id_from_activity, receive_unhandled_activity}, }; use activitystreams::{ activity::*, @@ -21,6 +18,7 @@ use activitystreams::{ use actix_web::HttpResponse; use anyhow::Context; use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::LemmyContext; pub async fn receive_announce( activity: AnyBase, diff --git a/src/apub/inbox/activities/create.rs b/lemmy_apub/src/inbox/activities/create.rs similarity index 90% rename from src/apub/inbox/activities/create.rs rename to lemmy_apub/src/inbox/activities/create.rs index 961991a65..60b02c585 100644 --- a/src/apub/inbox/activities/create.rs +++ b/lemmy_apub/src/inbox/activities/create.rs @@ -1,15 +1,12 @@ -use crate::{ - apub::{ - inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, - }, - ActorType, - FromApub, - PageExt, +use crate::apub::{ + inbox::shared_inbox::{ + announce_if_community_is_local, + get_user_from_activity, + receive_unhandled_activity, }, - LemmyContext, + ActorType, + FromApub, + PageExt, }; use activitystreams::{activity::Create, base::AnyBase, object::Note, prelude::*}; use actix_web::HttpResponse; @@ -20,14 +17,13 @@ use lemmy_db::{ post::{Post, PostForm}, post_view::PostView, }; -use lemmy_structs::{ - blocking, - comment::CommentResponse, - post::PostResponse, - send_local_notifs, - websocket::{SendComment, SendPost, UserOperation}, -}; +use lemmy_structs::{blocking, comment::CommentResponse, post::PostResponse, send_local_notifs}; use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError}; +use lemmy_websocket::{ + messages::{SendComment, SendPost}, + LemmyContext, + UserOperation, +}; pub async fn receive_create( activity: AnyBase, diff --git a/src/apub/inbox/activities/delete.rs b/lemmy_apub/src/inbox/activities/delete.rs similarity index 94% rename from src/apub/inbox/activities/delete.rs rename to lemmy_apub/src/inbox/activities/delete.rs index 131588bac..5cbe004e5 100644 --- a/src/apub/inbox/activities/delete.rs +++ b/lemmy_apub/src/inbox/activities/delete.rs @@ -1,17 +1,14 @@ -use crate::{ - apub::{ - fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, - inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, - }, - ActorType, - FromApub, - GroupExt, - PageExt, +use crate::apub::{ + fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, + inbox::shared_inbox::{ + announce_if_community_is_local, + get_user_from_activity, + receive_unhandled_activity, }, - LemmyContext, + ActorType, + FromApub, + GroupExt, + PageExt, }; use activitystreams::{activity::Delete, base::AnyBase, object::Note, prelude::*}; use actix_web::HttpResponse; @@ -31,9 +28,13 @@ use lemmy_structs::{ comment::CommentResponse, community::CommunityResponse, post::PostResponse, - websocket::{SendComment, SendCommunityRoomMessage, SendPost, UserOperation}, }; use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::{ + messages::{SendComment, SendCommunityRoomMessage, SendPost}, + LemmyContext, + UserOperation, +}; pub async fn receive_delete( activity: AnyBase, diff --git a/src/apub/inbox/activities/dislike.rs b/lemmy_apub/src/inbox/activities/dislike.rs similarity index 89% rename from src/apub/inbox/activities/dislike.rs rename to lemmy_apub/src/inbox/activities/dislike.rs index df9bd8486..a1f23f475 100644 --- a/src/apub/inbox/activities/dislike.rs +++ b/lemmy_apub/src/inbox/activities/dislike.rs @@ -1,15 +1,12 @@ -use crate::{ - apub::{ - fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, - inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, - }, - FromApub, - PageExt, +use crate::apub::{ + fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, + inbox::shared_inbox::{ + announce_if_community_is_local, + get_user_from_activity, + receive_unhandled_activity, }, - LemmyContext, + FromApub, + PageExt, }; use activitystreams::{activity::Dislike, base::AnyBase, object::Note, prelude::*}; use actix_web::HttpResponse; @@ -21,13 +18,13 @@ use lemmy_db::{ post_view::PostView, Likeable, }; -use lemmy_structs::{ - blocking, - comment::CommentResponse, - post::PostResponse, - websocket::{SendComment, SendPost, UserOperation}, -}; +use lemmy_structs::{blocking, comment::CommentResponse, post::PostResponse}; use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::{ + messages::{SendComment, SendPost}, + LemmyContext, + UserOperation, +}; pub async fn receive_dislike( activity: AnyBase, diff --git a/src/apub/inbox/activities/like.rs b/lemmy_apub/src/inbox/activities/like.rs similarity index 89% rename from src/apub/inbox/activities/like.rs rename to lemmy_apub/src/inbox/activities/like.rs index ce067c879..8a9e2a675 100644 --- a/src/apub/inbox/activities/like.rs +++ b/lemmy_apub/src/inbox/activities/like.rs @@ -1,15 +1,12 @@ -use crate::{ - apub::{ - fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, - inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, - }, - FromApub, - PageExt, +use crate::apub::{ + fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, + inbox::shared_inbox::{ + announce_if_community_is_local, + get_user_from_activity, + receive_unhandled_activity, }, - LemmyContext, + FromApub, + PageExt, }; use activitystreams::{activity::Like, base::AnyBase, object::Note, prelude::*}; use actix_web::HttpResponse; @@ -21,13 +18,13 @@ use lemmy_db::{ post_view::PostView, Likeable, }; -use lemmy_structs::{ - blocking, - comment::CommentResponse, - post::PostResponse, - websocket::{SendComment, SendPost, UserOperation}, -}; +use lemmy_structs::{blocking, comment::CommentResponse, post::PostResponse}; use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::{ + messages::{SendComment, SendPost}, + LemmyContext, + UserOperation, +}; pub async fn receive_like( activity: AnyBase, diff --git a/src/apub/inbox/activities/mod.rs b/lemmy_apub/src/inbox/activities/mod.rs similarity index 100% rename from src/apub/inbox/activities/mod.rs rename to lemmy_apub/src/inbox/activities/mod.rs diff --git a/src/apub/inbox/activities/remove.rs b/lemmy_apub/src/inbox/activities/remove.rs similarity index 94% rename from src/apub/inbox/activities/remove.rs rename to lemmy_apub/src/inbox/activities/remove.rs index e91a65c03..2b018dbff 100644 --- a/src/apub/inbox/activities/remove.rs +++ b/lemmy_apub/src/inbox/activities/remove.rs @@ -1,18 +1,15 @@ -use crate::{ - apub::{ - fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, - inbox::shared_inbox::{ - announce_if_community_is_local, - get_community_id_from_activity, - get_user_from_activity, - receive_unhandled_activity, - }, - ActorType, - FromApub, - GroupExt, - PageExt, +use crate::apub::{ + fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, + inbox::shared_inbox::{ + announce_if_community_is_local, + get_community_id_from_activity, + get_user_from_activity, + receive_unhandled_activity, }, - LemmyContext, + ActorType, + FromApub, + GroupExt, + PageExt, }; use activitystreams::{activity::Remove, base::AnyBase, object::Note, prelude::*}; use actix_web::HttpResponse; @@ -32,9 +29,13 @@ use lemmy_structs::{ comment::CommentResponse, community::CommunityResponse, post::PostResponse, - websocket::{SendComment, SendCommunityRoomMessage, SendPost, UserOperation}, }; use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::{ + messages::{SendComment, SendCommunityRoomMessage, SendPost}, + LemmyContext, + UserOperation, +}; pub async fn receive_remove( activity: AnyBase, diff --git a/src/apub/inbox/activities/undo.rs b/lemmy_apub/src/inbox/activities/undo.rs similarity index 98% rename from src/apub/inbox/activities/undo.rs rename to lemmy_apub/src/inbox/activities/undo.rs index c8b4d35bf..7268e270b 100644 --- a/src/apub/inbox/activities/undo.rs +++ b/lemmy_apub/src/inbox/activities/undo.rs @@ -1,17 +1,14 @@ -use crate::{ - apub::{ - fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, - inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, - }, - ActorType, - FromApub, - GroupExt, - PageExt, +use crate::apub::{ + fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, + inbox::shared_inbox::{ + announce_if_community_is_local, + get_user_from_activity, + receive_unhandled_activity, }, - LemmyContext, + ActorType, + FromApub, + GroupExt, + PageExt, }; use activitystreams::{ activity::*, @@ -37,9 +34,13 @@ use lemmy_structs::{ comment::CommentResponse, community::CommunityResponse, post::PostResponse, - websocket::{SendComment, SendCommunityRoomMessage, SendPost, UserOperation}, }; use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::{ + messages::{SendComment, SendCommunityRoomMessage, SendPost}, + LemmyContext, + UserOperation, +}; pub async fn receive_undo( activity: AnyBase, diff --git a/src/apub/inbox/activities/update.rs b/lemmy_apub/src/inbox/activities/update.rs similarity index 88% rename from src/apub/inbox/activities/update.rs rename to lemmy_apub/src/inbox/activities/update.rs index ccf7534bc..acf51539a 100644 --- a/src/apub/inbox/activities/update.rs +++ b/lemmy_apub/src/inbox/activities/update.rs @@ -1,16 +1,13 @@ -use crate::{ - apub::{ - fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, - inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, - }, - ActorType, - FromApub, - PageExt, +use crate::apub::{ + fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, + inbox::shared_inbox::{ + announce_if_community_is_local, + get_user_from_activity, + receive_unhandled_activity, }, - LemmyContext, + ActorType, + FromApub, + PageExt, }; use activitystreams::{activity::Update, base::AnyBase, object::Note, prelude::*}; use actix_web::HttpResponse; @@ -22,14 +19,13 @@ use lemmy_db::{ post_view::PostView, Crud, }; -use lemmy_structs::{ - blocking, - comment::CommentResponse, - post::PostResponse, - send_local_notifs, - websocket::{SendComment, SendPost, UserOperation}, -}; +use lemmy_structs::{blocking, comment::CommentResponse, post::PostResponse, send_local_notifs}; use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError}; +use lemmy_websocket::{ + messages::{SendComment, SendPost}, + LemmyContext, + UserOperation, +}; pub async fn receive_update( activity: AnyBase, diff --git a/src/apub/inbox/community_inbox.rs b/lemmy_apub/src/inbox/community_inbox.rs similarity index 94% rename from src/apub/inbox/community_inbox.rs rename to lemmy_apub/src/inbox/community_inbox.rs index 9dd010c3f..600f2d5d3 100644 --- a/src/apub/inbox/community_inbox.rs +++ b/lemmy_apub/src/inbox/community_inbox.rs @@ -1,12 +1,9 @@ -use crate::{ - apub::{ - check_is_apub_id_valid, - extensions::signatures::verify, - fetcher::get_or_fetch_and_upsert_user, - insert_activity, - ActorType, - }, - LemmyContext, +use crate::apub::{ + check_is_apub_id_valid, + extensions::signatures::verify, + fetcher::get_or_fetch_and_upsert_user, + insert_activity, + ActorType, }; use activitystreams::{ activity::{ActorAndObject, Follow, Undo}, @@ -22,6 +19,7 @@ use lemmy_db::{ }; use lemmy_structs::blocking; use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::LemmyContext; use log::debug; use serde::{Deserialize, Serialize}; use std::fmt::Debug; diff --git a/src/apub/inbox/mod.rs b/lemmy_apub/src/inbox/mod.rs similarity index 100% rename from src/apub/inbox/mod.rs rename to lemmy_apub/src/inbox/mod.rs diff --git a/src/apub/inbox/shared_inbox.rs b/lemmy_apub/src/inbox/shared_inbox.rs similarity index 88% rename from src/apub/inbox/shared_inbox.rs rename to lemmy_apub/src/inbox/shared_inbox.rs index da7951082..966a19454 100644 --- a/src/apub/inbox/shared_inbox.rs +++ b/lemmy_apub/src/inbox/shared_inbox.rs @@ -1,26 +1,23 @@ -use crate::{ - apub::{ - check_is_apub_id_valid, - community::do_announce, - extensions::signatures::verify, - fetcher::{ - get_or_fetch_and_upsert_actor, - get_or_fetch_and_upsert_community, - 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, +use crate::apub::{ + check_is_apub_id_valid, + community::do_announce, + extensions::signatures::verify, + fetcher::{ + get_or_fetch_and_upsert_actor, + get_or_fetch_and_upsert_community, + get_or_fetch_and_upsert_user, }, - 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::{ activity::{ActorAndObject, ActorAndObjectRef}, @@ -32,6 +29,7 @@ use actix_web::{web, HttpRequest, HttpResponse}; use anyhow::Context; use lemmy_db::user::User_; use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::LemmyContext; use log::debug; use serde::{Deserialize, Serialize}; use std::fmt::Debug; diff --git a/src/apub/inbox/user_inbox.rs b/lemmy_apub/src/inbox/user_inbox.rs similarity index 96% rename from src/apub/inbox/user_inbox.rs rename to lemmy_apub/src/inbox/user_inbox.rs index 5cbbedb6b..0376e2b10 100644 --- a/src/apub/inbox/user_inbox.rs +++ b/lemmy_apub/src/inbox/user_inbox.rs @@ -1,12 +1,9 @@ -use crate::{ - apub::{ - check_is_apub_id_valid, - extensions::signatures::verify, - fetcher::{get_or_fetch_and_upsert_actor, get_or_fetch_and_upsert_community}, - insert_activity, - FromApub, - }, - LemmyContext, +use crate::apub::{ + check_is_apub_id_valid, + extensions::signatures::verify, + fetcher::{get_or_fetch_and_upsert_actor, get_or_fetch_and_upsert_community}, + insert_activity, + FromApub, }; use activitystreams::{ activity::{Accept, ActorAndObject, Create, Delete, Undo, Update}, @@ -25,12 +22,9 @@ use lemmy_db::{ Crud, Followable, }; -use lemmy_structs::{ - blocking, - user::PrivateMessageResponse, - websocket::{SendUserRoomMessage, UserOperation}, -}; +use lemmy_structs::{blocking, user::PrivateMessageResponse}; use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation}; use log::debug; use serde::{Deserialize, Serialize}; use std::fmt::Debug; diff --git a/src/apub/post.rs b/lemmy_apub/src/post.rs similarity index 99% rename from src/apub/post.rs rename to lemmy_apub/src/post.rs index 85e00fb23..d6fb6f0bf 100644 --- a/src/apub/post.rs +++ b/lemmy_apub/src/post.rs @@ -15,7 +15,6 @@ use crate::{ ToApub, }, DbPool, - LemmyContext, }; use activitystreams::{ activity::{ @@ -47,6 +46,7 @@ use lemmy_utils::{ utils::{check_slurs, convert_datetime, remove_slurs}, LemmyError, }; +use lemmy_websocket::LemmyContext; use serde::Deserialize; use url::Url; diff --git a/src/apub/private_message.rs b/lemmy_apub/src/private_message.rs similarity index 99% rename from src/apub/private_message.rs rename to lemmy_apub/src/private_message.rs index 9e0549eeb..a5aed5565 100644 --- a/src/apub/private_message.rs +++ b/lemmy_apub/src/private_message.rs @@ -13,7 +13,6 @@ use crate::{ ToApub, }, DbPool, - LemmyContext, }; use activitystreams::{ activity::{ @@ -34,6 +33,7 @@ use lemmy_db::{ }; use lemmy_structs::blocking; use lemmy_utils::{location_info, utils::convert_datetime, LemmyError}; +use lemmy_websocket::LemmyContext; use url::Url; #[async_trait::async_trait(?Send)] diff --git a/src/apub/user.rs b/lemmy_apub/src/user.rs similarity index 99% rename from src/apub/user.rs rename to lemmy_apub/src/user.rs index fb650632e..e6846da22 100644 --- a/src/apub/user.rs +++ b/lemmy_apub/src/user.rs @@ -12,7 +12,6 @@ use crate::{ ToApub, }, DbPool, - LemmyContext, }; use activitystreams::{ activity::{ @@ -37,6 +36,7 @@ use lemmy_utils::{ utils::{check_slurs, check_slurs_opt, convert_datetime}, LemmyError, }; +use lemmy_websocket::LemmyContext; use serde::Deserialize; use url::Url; diff --git a/lemmy_db/src/lib.rs b/lemmy_db/src/lib.rs index fc660208d..40f6c3d26 100644 --- a/lemmy_db/src/lib.rs +++ b/lemmy_db/src/lib.rs @@ -4,14 +4,6 @@ extern crate diesel; extern crate strum_macros; #[macro_use] 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 diesel::{result::Error, *}; diff --git a/lemmy_rate_limit/src/lib.rs b/lemmy_rate_limit/src/lib.rs index 8f962bbe6..ecb812af8 100644 --- a/lemmy_rate_limit/src/lib.rs +++ b/lemmy_rate_limit/src/lib.rs @@ -1,9 +1,5 @@ #[macro_use] 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 futures::future::{ok, Ready}; diff --git a/lemmy_structs/Cargo.toml b/lemmy_structs/Cargo.toml index 9373b058c..6ba1dc802 100644 --- a/lemmy_structs/Cargo.toml +++ b/lemmy_structs/Cargo.toml @@ -16,7 +16,5 @@ log = "0.4" diesel = "1.4" actix = "0.10" actix-web = { version = "3.0" } -strum = "0.19" -strum_macros = "0.19" chrono = { version = "0.4", features = ["serde"] } serde_json = { version = "1.0", features = ["preserve_order"]} diff --git a/lemmy_structs/src/lib.rs b/lemmy_structs/src/lib.rs index 5b7688ef4..8d358d646 100644 --- a/lemmy_structs/src/lib.rs +++ b/lemmy_structs/src/lib.rs @@ -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 community; pub mod post; diff --git a/lemmy_structs/src/websocket.rs b/lemmy_structs/src/websocket.rs index bd09889c2..8b1378917 100644 --- a/lemmy_structs/src/websocket.rs +++ b/lemmy_structs/src/websocket.rs @@ -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( - op: &UserOperation, - data: &Response, -) -> Result -where - Response: Serialize, -{ - let response = WebsocketResponse { - op: op.to_string(), - data, - }; - Ok(serde_json::to_string(&response)?) -} - -#[derive(Serialize)] -struct WebsocketResponse { - 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, - 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")] -pub struct StandardMessage { - /// Id of the client session - pub id: ConnectionId, - /// Peer message - pub msg: String, -} - -#[derive(Message)] -#[rtype(result = "()")] -pub struct SendAllMessage { - pub op: UserOperation, - pub response: Response, - pub websocket_id: Option, -} - -#[derive(Message)] -#[rtype(result = "()")] -pub struct SendUserRoomMessage { - pub op: UserOperation, - pub response: Response, - pub recipient_id: UserId, - pub websocket_id: Option, -} - -#[derive(Message)] -#[rtype(result = "()")] -pub struct SendCommunityRoomMessage { - pub op: UserOperation, - pub response: Response, - pub community_id: CommunityId, - pub websocket_id: Option, -} - -#[derive(Message)] -#[rtype(result = "()")] -pub struct SendPost { - pub op: UserOperation, - pub post: PostResponse, - pub websocket_id: Option, -} - -#[derive(Message)] -#[rtype(result = "()")] -pub struct SendComment { - pub op: UserOperation, - pub comment: CommentResponse, - pub websocket_id: Option, -} - -#[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, -} diff --git a/lemmy_utils/src/lib.rs b/lemmy_utils/src/lib.rs index 9cc1fe025..9e2c7d4df 100644 --- a/lemmy_utils/src/lib.rs +++ b/lemmy_utils/src/lib.rs @@ -1,16 +1,5 @@ #[macro_use] 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 email; diff --git a/src/request.rs b/lemmy_utils/src/request.rs similarity index 100% rename from src/request.rs rename to lemmy_utils/src/request.rs diff --git a/lemmy_websocket/Cargo.toml b/lemmy_websocket/Cargo.toml new file mode 100644 index 000000000..d4490dc60 --- /dev/null +++ b/lemmy_websocket/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "lemmy_websocket" +version = "0.1.0" +authors = ["Felix Ableitner "] +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"] } diff --git a/src/websocket/chat_server.rs b/lemmy_websocket/src/chat_server.rs similarity index 95% rename from src/websocket/chat_server.rs rename to lemmy_websocket/src/chat_server.rs index 648047e0f..8346a32f6 100644 --- a/src/websocket/chat_server.rs +++ b/lemmy_websocket/src/chat_server.rs @@ -1,4 +1,4 @@ -use crate::LemmyContext; +use crate::{messages::*, serialize_websocket_message, LemmyContext, UserOperation}; use actix::prelude::*; use anyhow::Context as acontext; use background_jobs::QueueHandle; @@ -7,7 +7,7 @@ use diesel::{ PgConnection, }; use lemmy_rate_limit::RateLimit; -use lemmy_structs::{comment::*, post::*, websocket::*}; +use lemmy_structs::{comment::*, post::*}; use lemmy_utils::{ location_info, APIError, @@ -349,8 +349,6 @@ impl ChatServer { msg: StandardMessage, ctx: &mut Context, ) -> impl Future> { - let addr = ctx.address(); - let pool = self.pool.clone(); let rate_limiter = self.rate_limiter.clone(); let ip: IPAddr = match self.sessions.get(&msg.id) { @@ -358,26 +356,21 @@ impl ChatServer { None => "blank_ip".to_string(), }; - let client = self.client.clone(); - let activity_queue = self.activity_queue.clone(); + let context = LemmyContext { + 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; async move { - let msg = msg; let json: Value = serde_json::from_str(&msg.msg)?; let data = &json["data"].to_string(); let op = &json["op"].as_str().ok_or(APIError { message: "Unknown op type".to_string(), })?; - let user_operation: UserOperation = UserOperation::from_str(&op)?; - - let context = LemmyContext { - pool, - chat_server: addr, - client, - activity_queue, - }; - + let user_operation = UserOperation::from_str(&op)?; let fut = (message_handler)(context, msg.id, user_operation.clone(), data); match user_operation { UserOperation::Register => rate_limiter.register().wrap(ip, fut).await, diff --git a/src/websocket/handlers.rs b/lemmy_websocket/src/handlers.rs similarity index 98% rename from src/websocket/handlers.rs rename to lemmy_websocket/src/handlers.rs index b255c1b92..258098d62 100644 --- a/src/websocket/handlers.rs +++ b/lemmy_websocket/src/handlers.rs @@ -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 lemmy_db::naive_now; -use lemmy_structs::websocket::*; use log::{error, info}; use rand::Rng; use serde::Serialize; diff --git a/lemmy_websocket/src/lib.rs b/lemmy_websocket/src/lib.rs new file mode 100644 index 000000000..26b00a061 --- /dev/null +++ b/lemmy_websocket/src/lib.rs @@ -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, + pub client: Client, + pub activity_queue: QueueHandle, +} + +impl LemmyContext { + pub fn create( + pool: DbPool, + chat_server: Addr, + 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 { + &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 { + op: String, + data: T, +} + +pub fn serialize_websocket_message( + op: &UserOperation, + data: &Response, +) -> Result +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, +} diff --git a/lemmy_websocket/src/messages.rs b/lemmy_websocket/src/messages.rs new file mode 100644 index 000000000..d9f8320a8 --- /dev/null +++ b/lemmy_websocket/src/messages.rs @@ -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, + 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")] +pub struct StandardMessage { + /// Id of the client session + pub id: ConnectionId, + /// Peer message + pub msg: String, +} + +#[derive(Message)] +#[rtype(result = "()")] +pub struct SendAllMessage { + pub op: UserOperation, + pub response: Response, + pub websocket_id: Option, +} + +#[derive(Message)] +#[rtype(result = "()")] +pub struct SendUserRoomMessage { + pub op: UserOperation, + pub response: Response, + pub recipient_id: UserId, + pub websocket_id: Option, +} + +#[derive(Message)] +#[rtype(result = "()")] +pub struct SendCommunityRoomMessage { + pub op: UserOperation, + pub response: Response, + pub community_id: CommunityId, + pub websocket_id: Option, +} + +#[derive(Message)] +#[rtype(result = "()")] +pub struct SendPost { + pub op: UserOperation, + pub post: PostResponse, + pub websocket_id: Option, +} + +#[derive(Message)] +#[rtype(result = "()")] +pub struct SendComment { + pub op: UserOperation, + pub comment: CommentResponse, + pub websocket_id: Option, +} + +#[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, +} diff --git a/src/api/comment.rs b/src/api/comment.rs index 69853c3e7..5ad69c2ea 100644 --- a/src/api/comment.rs +++ b/src/api/comment.rs @@ -8,7 +8,6 @@ use crate::{ Perform, }, apub::{ApubLikeableType, ApubObjectType}, - LemmyContext, }; use actix_web::web::Data; use lemmy_db::{ @@ -24,12 +23,7 @@ use lemmy_db::{ Saveable, SortType, }; -use lemmy_structs::{ - blocking, - comment::*, - send_local_notifs, - websocket::{SendComment, UserOperation}, -}; +use lemmy_structs::{blocking, comment::*, send_local_notifs}; use lemmy_utils::{ apub::{make_apub_endpoint, EndpointType}, utils::{remove_slurs, scrape_text_for_mentions}, @@ -37,6 +31,7 @@ use lemmy_utils::{ ConnectionId, LemmyError, }; +use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation}; use std::str::FromStr; #[async_trait::async_trait(?Send)] diff --git a/src/api/community.rs b/src/api/community.rs index 77a945a64..c85f7c70e 100644 --- a/src/api/community.rs +++ b/src/api/community.rs @@ -1,7 +1,6 @@ use crate::{ api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, is_mod_or_admin, Perform}, apub::ActorType, - LemmyContext, }; use actix_web::web::Data; use anyhow::Context; @@ -22,16 +21,7 @@ use lemmy_db::{ Joinable, SortType, }; -use lemmy_structs::{ - blocking, - community::*, - websocket::{ - GetCommunityUsersOnline, - JoinCommunityRoom, - SendCommunityRoomMessage, - UserOperation, - }, -}; +use lemmy_structs::{blocking, community::*}; use lemmy_utils::{ apub::{generate_actor_keypair, make_apub_endpoint, EndpointType}, location_info, @@ -40,6 +30,11 @@ use lemmy_utils::{ ConnectionId, LemmyError, }; +use lemmy_websocket::{ + messages::{GetCommunityUsersOnline, JoinCommunityRoom, SendCommunityRoomMessage}, + LemmyContext, + UserOperation, +}; use std::str::FromStr; #[async_trait::async_trait(?Send)] diff --git a/src/api/mod.rs b/src/api/mod.rs index d3bb6956a..d20631057 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,4 +1,4 @@ -use crate::{api::claims::Claims, DbPool, LemmyContext}; +use crate::{api::claims::Claims, DbPool}; use actix_web::{web, web::Data}; use lemmy_db::{ community::Community, @@ -7,16 +7,9 @@ use lemmy_db::{ user::User_, Crud, }; -use lemmy_structs::{ - blocking, - comment::*, - community::*, - post::*, - site::*, - user::*, - websocket::{serialize_websocket_message, UserOperation}, -}; +use lemmy_structs::{blocking, comment::*, community::*, post::*, site::*, user::*}; use lemmy_utils::{APIError, ConnectionId, LemmyError}; +use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation}; use serde::Deserialize; pub mod claims; diff --git a/src/api/post.rs b/src/api/post.rs index ef5409d1f..fb44d4101 100644 --- a/src/api/post.rs +++ b/src/api/post.rs @@ -2,7 +2,6 @@ use crate::{ api::{check_community_ban, get_user_from_jwt, get_user_from_jwt_opt, is_mod_or_admin, Perform}, apub::{ApubLikeableType, ApubObjectType}, fetch_iframely_and_pictrs_data, - LemmyContext, }; use actix_web::web::Data; use lemmy_db::{ @@ -19,11 +18,7 @@ use lemmy_db::{ Saveable, SortType, }; -use lemmy_structs::{ - blocking, - post::*, - websocket::{GetPostUsersOnline, JoinPostRoom, SendPost, UserOperation}, -}; +use lemmy_structs::{blocking, post::*}; use lemmy_utils::{ apub::{make_apub_endpoint, EndpointType}, utils::{check_slurs, check_slurs_opt, is_valid_post_title}, @@ -31,6 +26,11 @@ use lemmy_utils::{ ConnectionId, LemmyError, }; +use lemmy_websocket::{ + messages::{GetPostUsersOnline, JoinPostRoom, SendPost}, + LemmyContext, + UserOperation, +}; use std::str::FromStr; use url::Url; diff --git a/src/api/site.rs b/src/api/site.rs index 3ff1e49d0..a59f1f667 100644 --- a/src/api/site.rs +++ b/src/api/site.rs @@ -2,7 +2,6 @@ use crate::{ api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform}, apub::fetcher::search_by_apub_id, version, - LemmyContext, }; use actix_web::web::Data; use anyhow::Context; @@ -22,12 +21,7 @@ use lemmy_db::{ SearchType, SortType, }; -use lemmy_structs::{ - blocking, - site::*, - user::Register, - websocket::{GetUsersOnline, SendAllMessage, UserOperation}, -}; +use lemmy_structs::{blocking, site::*, user::Register}; use lemmy_utils::{ location_info, settings::Settings, @@ -36,6 +30,11 @@ use lemmy_utils::{ ConnectionId, LemmyError, }; +use lemmy_websocket::{ + messages::{GetUsersOnline, SendAllMessage}, + LemmyContext, + UserOperation, +}; use log::{debug, info}; use std::str::FromStr; diff --git a/src/api/user.rs b/src/api/user.rs index 630f816de..c7977b810 100644 --- a/src/api/user.rs +++ b/src/api/user.rs @@ -2,7 +2,6 @@ use crate::{ api::{claims::Claims, get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform}, apub::ApubObjectType, captcha_espeak_wav_base64, - LemmyContext, }; use actix_web::web::Data; use anyhow::Context; @@ -34,18 +33,7 @@ use lemmy_db::{ ListingType, SortType, }; -use lemmy_structs::{ - blocking, - user::*, - websocket::{ - CaptchaItem, - CheckCaptcha, - JoinUserRoom, - SendAllMessage, - SendUserRoomMessage, - UserOperation, - }, -}; +use lemmy_structs::{blocking, user::*}; use lemmy_utils::{ apub::{generate_actor_keypair, make_apub_endpoint, EndpointType}, email::send_email, @@ -63,6 +51,11 @@ use lemmy_utils::{ ConnectionId, LemmyError, }; +use lemmy_websocket::{ + messages::{CaptchaItem, CheckCaptcha, JoinUserRoom, SendAllMessage, SendUserRoomMessage}, + LemmyContext, + UserOperation, +}; use log::error; use std::str::FromStr; diff --git a/src/apub/mod.rs b/src/apub/mod.rs deleted file mode 100644 index 5fd5da364..000000000 --- a/src/apub/mod.rs +++ /dev/null @@ -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, GroupExtension, PublicKeyExtension>; -type PersonExt = Ext1, PublicKeyExtension>; -type PageExt = Ext1; - -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(data: &T) -> HttpResponse -where - T: Serialize, -{ - HttpResponse::Ok() - .content_type(APUB_JSON_CONTENT_TYPE) - .json(data) -} - -fn create_apub_tombstone_response(data: &T) -> HttpResponse -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::>() - .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; - fn to_tombstone(&self) -> Result; -} - -/// Updated is actually the deletion time -fn create_tombstone( - deleted: bool, - object_id: &str, - updated: Option, - former_type: T, -) -> Result -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, - ) -> Result - 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( - apub: &T, - expected_domain: Option, -) -> Result -where - T: Base + AsBase, -{ - 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; - fn private_key(&self) -> Option; - - /// 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, LemmyError>; - - fn actor_id(&self) -> Result { - Url::parse(&self.actor_id_str()) - } - - // TODO move these to the db rows - fn get_inbox_url(&self) -> Result { - Url::parse(&format!("{}/inbox", &self.actor_id_str())) - } - - fn get_shared_inbox_url(&self) -> Result { - 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::parse(&format!("{}/outbox", &self.actor_id_str())) - } - - fn get_followers_url(&self) -> Result { - 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 { - 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 { - 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( - 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(()) -} diff --git a/src/lib.rs b/src/lib.rs index 2bab3552a..4f51a87dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,23 +1,6 @@ #![recursion_limit = "512"] #[macro_use] 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 apub; @@ -25,15 +8,9 @@ pub mod code_migrations; pub mod request; pub mod routes; pub mod version; -pub mod websocket; -use crate::{ - request::{retry, RecvError}, - websocket::chat_server::ChatServer, -}; -use actix::Addr; +use crate::request::{retry, RecvError}; use anyhow::anyhow; -use background_jobs::QueueHandle; use lemmy_db::DbPool; use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError}; use log::error; @@ -42,52 +19,6 @@ use reqwest::Client; use serde::Deserialize; use std::process::Command; -pub struct LemmyContext { - pub pool: DbPool, - pub chat_server: Addr, - pub client: Client, - pub activity_queue: QueueHandle, -} - -impl LemmyContext { - pub fn create( - pool: DbPool, - chat_server: Addr, - 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 { - &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)] pub struct IframelyResponse { title: Option, diff --git a/src/main.rs b/src/main.rs index 17c9a3f50..818216326 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,11 +23,10 @@ use lemmy_server::{ apub::activity_queue::create_activity_queue, code_migrations::run_advanced_migrations, routes::*, - websocket::chat_server::ChatServer, - LemmyContext, }; use lemmy_structs::blocking; use lemmy_utils::{settings::Settings, LemmyError, CACHE_CONTROL_REGEX}; +use lemmy_websocket::{chat_server::ChatServer, LemmyContext}; use reqwest::Client; use std::sync::Arc; use tokio::sync::Mutex; diff --git a/src/routes/api.rs b/src/routes/api.rs index 7e019168b..d8244fef0 100644 --- a/src/routes/api.rs +++ b/src/routes/api.rs @@ -1,7 +1,8 @@ -use crate::{api::Perform, LemmyContext}; +use crate::api::Perform; use actix_web::{error::ErrorBadRequest, *}; use lemmy_rate_limit::RateLimit; use lemmy_structs::{comment::*, community::*, post::*, site::*, user::*}; +use lemmy_websocket::LemmyContext; use serde::Deserialize; pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { diff --git a/src/routes/feeds.rs b/src/routes/feeds.rs index 9669bfcc0..45e1c5374 100644 --- a/src/routes/feeds.rs +++ b/src/routes/feeds.rs @@ -1,4 +1,4 @@ -use crate::{api::claims::Claims, LemmyContext}; +use crate::api::claims::Claims; use actix_web::{error::ErrorBadRequest, *}; use anyhow::anyhow; use chrono::{DateTime, NaiveDateTime, Utc}; @@ -15,6 +15,7 @@ use lemmy_db::{ }; use lemmy_structs::blocking; use lemmy_utils::{settings::Settings, utils::markdown_to_html, LemmyError}; +use lemmy_websocket::LemmyContext; use rss::{CategoryBuilder, ChannelBuilder, GuidBuilder, Item, ItemBuilder}; use serde::Deserialize; use std::str::FromStr; diff --git a/src/routes/nodeinfo.rs b/src/routes/nodeinfo.rs index 15c5a2357..efa689ca5 100644 --- a/src/routes/nodeinfo.rs +++ b/src/routes/nodeinfo.rs @@ -1,9 +1,10 @@ -use crate::{version, LemmyContext}; +use crate::version; use actix_web::{body::Body, error::ErrorBadRequest, *}; use anyhow::anyhow; use lemmy_db::site_view::SiteView; use lemmy_structs::blocking; use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError}; +use lemmy_websocket::LemmyContext; use serde::{Deserialize, Serialize}; use url::Url; diff --git a/src/routes/webfinger.rs b/src/routes/webfinger.rs index 53a87b124..40d100262 100644 --- a/src/routes/webfinger.rs +++ b/src/routes/webfinger.rs @@ -1,4 +1,3 @@ -use crate::LemmyContext; use actix_web::{error::ErrorBadRequest, web::Query, *}; use anyhow::anyhow; use lemmy_db::{community::Community, user::User_}; @@ -9,6 +8,7 @@ use lemmy_utils::{ WEBFINGER_COMMUNITY_REGEX, WEBFINGER_USER_REGEX, }; +use lemmy_websocket::LemmyContext; use serde::{Deserialize, Serialize}; #[derive(Deserialize)] diff --git a/src/routes/websocket.rs b/src/routes/websocket.rs index aaa7ef37a..9bfd6ca14 100644 --- a/src/routes/websocket.rs +++ b/src/routes/websocket.rs @@ -1,9 +1,12 @@ -use crate::{websocket::chat_server::ChatServer, LemmyContext}; use actix::prelude::*; use actix_web::*; use actix_web_actors::ws; -use lemmy_structs::websocket::{Connect, Disconnect, StandardMessage, WSMessage}; use lemmy_utils::utils::get_ip; +use lemmy_websocket::{ + chat_server::ChatServer, + messages::{Connect, Disconnect, StandardMessage, WSMessage}, + LemmyContext, +}; use log::{debug, error, info}; use std::time::{Duration, Instant}; diff --git a/src/websocket/mod.rs b/src/websocket/mod.rs deleted file mode 100644 index e892f78f8..000000000 --- a/src/websocket/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod chat_server; -pub mod handlers;