2020-09-24 13:53:21 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate strum_macros;
|
|
|
|
|
|
|
|
use crate::chat_server::ChatServer;
|
|
|
|
use actix::Addr;
|
|
|
|
use background_jobs::QueueHandle;
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{source::secret::Secret, DbPool};
|
2021-09-22 15:57:09 +00:00
|
|
|
use lemmy_utils::{settings::structs::Settings, LemmyError};
|
2020-09-24 13:53:21 +00:00
|
|
|
use reqwest::Client;
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
pub mod chat_server;
|
|
|
|
pub mod handlers;
|
|
|
|
pub mod messages;
|
2021-02-09 18:26:06 +00:00
|
|
|
pub mod routes;
|
2021-08-17 18:04:58 +00:00
|
|
|
pub mod send;
|
2020-09-24 13:53:21 +00:00
|
|
|
|
|
|
|
pub struct LemmyContext {
|
2021-10-21 17:25:35 +00:00
|
|
|
pool: DbPool,
|
|
|
|
chat_server: Addr<ChatServer>,
|
|
|
|
client: Client,
|
|
|
|
activity_queue: QueueHandle,
|
|
|
|
settings: Settings,
|
|
|
|
secret: Secret,
|
2020-09-24 13:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LemmyContext {
|
|
|
|
pub fn create(
|
|
|
|
pool: DbPool,
|
|
|
|
chat_server: Addr<ChatServer>,
|
|
|
|
client: Client,
|
|
|
|
activity_queue: QueueHandle,
|
2021-09-22 15:57:09 +00:00
|
|
|
settings: Settings,
|
|
|
|
secret: Secret,
|
2020-09-24 13:53:21 +00:00
|
|
|
) -> LemmyContext {
|
|
|
|
LemmyContext {
|
|
|
|
pool,
|
|
|
|
chat_server,
|
|
|
|
client,
|
|
|
|
activity_queue,
|
2021-09-22 15:57:09 +00:00
|
|
|
settings,
|
|
|
|
secret,
|
2020-09-24 13:53:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2021-09-22 15:57:09 +00:00
|
|
|
pub fn settings(&self) -> Settings {
|
|
|
|
// TODO hacky solution to be able to hotload the settings.
|
|
|
|
Settings::get()
|
|
|
|
}
|
|
|
|
pub fn secret(&self) -> &Secret {
|
|
|
|
&self.secret
|
|
|
|
}
|
2020-09-24 13:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
2021-09-22 15:57:09 +00:00
|
|
|
settings: self.settings.clone(),
|
|
|
|
secret: self.secret.clone(),
|
2020-09-24 13:53:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct WebsocketResponse<T> {
|
|
|
|
op: String,
|
|
|
|
data: T,
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
pub fn serialize_websocket_message<OP, Response>(
|
|
|
|
op: &OP,
|
2020-09-24 13:53:21 +00:00
|
|
|
data: &Response,
|
|
|
|
) -> Result<String, LemmyError>
|
|
|
|
where
|
|
|
|
Response: Serialize,
|
2021-03-25 19:30:15 +00:00
|
|
|
OP: ToString,
|
2020-09-24 13:53:21 +00:00
|
|
|
{
|
|
|
|
let response = WebsocketResponse {
|
|
|
|
op: op.to_string(),
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
Ok(serde_json::to_string(&response)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(EnumString, ToString, Debug, Clone)]
|
|
|
|
pub enum UserOperation {
|
|
|
|
Login,
|
|
|
|
GetCaptcha,
|
|
|
|
MarkCommentAsRead,
|
|
|
|
SaveComment,
|
|
|
|
CreateCommentLike,
|
2020-10-25 02:59:13 +00:00
|
|
|
CreateCommentReport,
|
|
|
|
ResolveCommentReport,
|
|
|
|
ListCommentReports,
|
2020-09-24 13:53:21 +00:00
|
|
|
CreatePostLike,
|
|
|
|
LockPost,
|
|
|
|
StickyPost,
|
2021-11-23 14:15:43 +00:00
|
|
|
MarkPostAsRead,
|
2020-09-24 13:53:21 +00:00
|
|
|
SavePost,
|
2020-10-25 02:59:13 +00:00
|
|
|
CreatePostReport,
|
|
|
|
ResolvePostReport,
|
|
|
|
ListPostReports,
|
2020-10-21 00:31:01 +00:00
|
|
|
GetReportCount,
|
2021-10-16 10:43:41 +00:00
|
|
|
GetUnreadCount,
|
2020-09-24 13:53:21 +00:00
|
|
|
FollowCommunity,
|
|
|
|
GetReplies,
|
2021-03-10 22:33:55 +00:00
|
|
|
GetPersonMentions,
|
|
|
|
MarkPersonMentionAsRead,
|
2020-09-24 13:53:21 +00:00
|
|
|
GetModlog,
|
|
|
|
BanFromCommunity,
|
|
|
|
AddModToCommunity,
|
|
|
|
AddAdmin,
|
2021-03-10 22:33:55 +00:00
|
|
|
BanPerson,
|
2020-09-24 13:53:21 +00:00
|
|
|
Search,
|
2021-07-20 07:00:20 +00:00
|
|
|
ResolveObject,
|
2020-09-24 13:53:21 +00:00
|
|
|
MarkAllAsRead,
|
|
|
|
SaveUserSettings,
|
|
|
|
TransferCommunity,
|
|
|
|
TransferSite,
|
|
|
|
PasswordReset,
|
|
|
|
PasswordChange,
|
|
|
|
MarkPrivateMessageAsRead,
|
|
|
|
UserJoin,
|
|
|
|
GetSiteConfig,
|
|
|
|
SaveSiteConfig,
|
|
|
|
PostJoin,
|
|
|
|
CommunityJoin,
|
2020-10-27 00:25:18 +00:00
|
|
|
ModJoin,
|
2021-04-01 21:39:01 +00:00
|
|
|
ChangePassword,
|
2021-08-19 14:12:49 +00:00
|
|
|
GetSiteMetadata,
|
2021-08-19 20:54:15 +00:00
|
|
|
BlockCommunity,
|
|
|
|
BlockPerson,
|
2020-09-24 13:53:21 +00:00
|
|
|
}
|
2021-03-25 19:30:15 +00:00
|
|
|
|
|
|
|
#[derive(EnumString, ToString, Debug, Clone)]
|
|
|
|
pub enum UserOperationCrud {
|
|
|
|
// Site
|
|
|
|
CreateSite,
|
|
|
|
GetSite,
|
|
|
|
EditSite,
|
|
|
|
// Community
|
|
|
|
CreateCommunity,
|
|
|
|
ListCommunities,
|
|
|
|
GetCommunity,
|
|
|
|
EditCommunity,
|
|
|
|
DeleteCommunity,
|
|
|
|
RemoveCommunity,
|
|
|
|
// Post
|
|
|
|
CreatePost,
|
|
|
|
GetPost,
|
|
|
|
GetPosts,
|
|
|
|
EditPost,
|
|
|
|
DeletePost,
|
|
|
|
RemovePost,
|
|
|
|
// Comment
|
|
|
|
CreateComment,
|
2021-11-23 15:53:48 +00:00
|
|
|
GetComment,
|
2021-03-25 19:30:15 +00:00
|
|
|
GetComments,
|
|
|
|
EditComment,
|
|
|
|
DeleteComment,
|
|
|
|
RemoveComment,
|
|
|
|
// User
|
|
|
|
Register,
|
|
|
|
GetPersonDetails,
|
|
|
|
DeleteAccount,
|
|
|
|
// Private Message
|
|
|
|
CreatePrivateMessage,
|
|
|
|
GetPrivateMessages,
|
|
|
|
EditPrivateMessage,
|
|
|
|
DeletePrivateMessage,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait OperationType {}
|
|
|
|
|
|
|
|
impl OperationType for UserOperationCrud {}
|
|
|
|
|
|
|
|
impl OperationType for UserOperation {}
|