summaryrefslogtreecommitdiffstats
path: root/server/src/api/community.rs
diff options
context:
space:
mode:
authorDessalines <tyhou13@gmx.com>2020-04-21 10:25:29 -0400
committerDessalines <tyhou13@gmx.com>2020-04-21 10:25:29 -0400
commitf0026065f5f809d2038ca91d7880becb39b0f9a7 (patch)
treefdb76bb60d89bc56f1e60deaa2a5d62fb5a9edc8 /server/src/api/community.rs
parent2f4b3a4f833ce277ab989626a6210167ce47c29e (diff)
parentb98fa1274151f61b13679aa30f332686bc9645f3 (diff)
Merge branch 'master' into federation_merge_from_master_1
Diffstat (limited to 'server/src/api/community.rs')
-rw-r--r--server/src/api/community.rs199
1 files changed, 162 insertions, 37 deletions
diff --git a/server/src/api/community.rs b/server/src/api/community.rs
index 0c0ddcee..01bfc40b 100644
--- a/server/src/api/community.rs
+++ b/server/src/api/community.rs
@@ -1,9 +1,4 @@
use super::*;
-use crate::apub::activities::follow_community;
-use crate::apub::signatures::generate_actor_keypair;
-use crate::apub::{make_apub_endpoint, EndpointType};
-use diesel::PgConnection;
-use std::str::FromStr;
#[derive(Serialize, Deserialize)]
pub struct GetCommunity {
@@ -58,7 +53,7 @@ pub struct BanFromCommunity {
auth: String,
}
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, Clone)]
pub struct BanFromCommunityResponse {
user: UserView,
banned: bool,
@@ -72,7 +67,7 @@ pub struct AddModToCommunity {
auth: String,
}
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, Clone)]
pub struct AddModToCommunityResponse {
moderators: Vec<CommunityModeratorView>,
}
@@ -116,8 +111,14 @@ pub struct TransferCommunity {
auth: String,
}
-impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
- fn perform(&self, conn: &PgConnection) -> Result<GetCommunityResponse, Error> {
+impl Perform for Oper<GetCommunity> {
+ type Response = GetCommunityResponse;
+
+ fn perform(
+ &self,
+ pool: Pool<ConnectionManager<PgConnection>>,
+ websocket_info: Option<WebsocketInfo>,
+ ) -> Result<GetCommunityResponse, Error> {
let data: &GetCommunity = &self.data;
let user_id: Option<i32> = match &data.auth {
@@ -131,6 +132,8 @@ impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
None => None,
};
+ let conn = pool.get()?;
+
let community = match data.id {
Some(id) => Community::read(&conn, id)?,
None => {
@@ -160,18 +163,44 @@ impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
let creator_user = admins.remove(creator_index);
admins.insert(0, creator_user);
- // Return the jwt
- Ok(GetCommunityResponse {
+ let online = if let Some(ws) = websocket_info {
+ if let Some(id) = ws.id {
+ ws.chatserver.do_send(JoinCommunityRoom {
+ community_id: community.id,
+ id,
+ });
+ }
+
+ // TODO
+ 1
+ // let fut = async {
+ // ws.chatserver.send(GetCommunityUsersOnline {community_id}).await.unwrap()
+ // };
+ // Runtime::new().unwrap().block_on(fut)
+ } else {
+ 0
+ };
+
+ let res = GetCommunityResponse {
community: community_view,
moderators,
admins,
- online: 0,
- })
+ online,
+ };
+
+ // Return the jwt
+ Ok(res)
}
}
-impl Perform<CommunityResponse> for Oper<CreateCommunity> {
- fn perform(&self, conn: &PgConnection) -> Result<CommunityResponse, Error> {
+impl Perform for Oper<CreateCommunity> {
+ type Response = CommunityResponse;
+
+ fn perform(
+ &self,
+ pool: Pool<ConnectionManager<PgConnection>>,
+ _websocket_info: Option<WebsocketInfo>,
+ ) -> Result<CommunityResponse, Error> {
let data: &CreateCommunity = &self.data;
let claims = match Claims::decode(&data.auth) {
@@ -195,6 +224,8 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
let user_id = claims.id;
+ let conn = pool.get()?;
+
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
return Err(APIError::err("site_ban").into());
@@ -256,8 +287,14 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
}
}
-impl Perform<CommunityResponse> for Oper<EditCommunity> {
- fn perform(&self, conn: &PgConnection) -> Result<CommunityResponse, Error> {
+impl Perform for Oper<EditCommunity> {
+ type Response = CommunityResponse;
+
+ fn perform(
+ &self,
+ pool: Pool<ConnectionManager<PgConnection>>,
+ websocket_info: Option<WebsocketInfo>,
+ ) -> Result<CommunityResponse, Error> {
let data: &EditCommunity = &self.data;
if let Err(slurs) = slur_check(&data.name) {
@@ -281,6 +318,8 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
let user_id = claims.id;
+ let conn = pool.get()?;
+
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
return Err(APIError::err("site_ban").into());
@@ -342,14 +381,36 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
let community_view = CommunityView::read(&conn, data.edit_id, Some(user_id))?;
- Ok(CommunityResponse {
+ let res = CommunityResponse {
community: community_view,
- })
+ };
+
+ if let Some(ws) = websocket_info {
+ // Strip out the user id and subscribed when sending to others
+ let mut res_sent = res.clone();
+ res_sent.community.user_id = None;
+ res_sent.community.subscribed = None;
+
+ ws.chatserver.do_send(SendCommunityRoomMessage {
+ op: UserOperation::EditCommunity,
+ response: res_sent,
+ community_id: data.edit_id,
+ my_id: ws.id,
+ });
+ }
+
+ Ok(res)
}
}
-impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
- fn perform(&self, conn: &PgConnection) -> Result<ListCommunitiesResponse, Error> {
+impl Perform for Oper<ListCommunities> {
+ type Response = ListCommunitiesResponse;
+
+ fn perform(
+ &self,
+ pool: Pool<ConnectionManager<PgConnection>>,
+ _websocket_info: Option<WebsocketInfo>,
+ ) -> Result<ListCommunitiesResponse, Error> {
let data: &ListCommunities = &self.data;
let user_claims: Option<Claims> = match &data.auth {
@@ -372,6 +433,8 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
let sort = SortType::from_str(&data.sort)?;
+ let conn = pool.get()?;
+
let communities = CommunityQueryBuilder::create(&conn)
.sort(&sort)
.for_user(user_id)
@@ -385,8 +448,14 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
}
}
-impl Perform<CommunityResponse> for Oper<FollowCommunity> {
- fn perform(&self, conn: &PgConnection) -> Result<CommunityResponse, Error> {
+impl Perform for Oper<FollowCommunity> {
+ type Response = CommunityResponse;
+
+ fn perform(
+ &self,
+ pool: Pool<ConnectionManager<PgConnection>>,
+ _websocket_info: Option<WebsocketInfo>,
+ ) -> Result<CommunityResponse, Error> {
let data: &FollowCommunity = &self.data;
let claims = match Claims::decode(&data.auth) {
@@ -396,7 +465,9 @@ impl Perform<CommunityResponse> for Oper<FollowCommunity> {
let user_id = claims.id;
- let community = Community::read(conn, data.community_id)?;
+ let conn = pool.get()?;
+
+ let community = Community::read(&conn, data.community_id)?;
if community.local {
let community_follower_form = CommunityFollowerForm {
community_id: data.community_id,
@@ -416,8 +487,8 @@ impl Perform<CommunityResponse> for Oper<FollowCommunity> {
}
} else {
// TODO: still have to implement unfollow
- let user = User_::read(conn, user_id)?;
- follow_community(&community, &user, conn)?;
+ let user = User_::read(&conn, user_id)?;
+ follow_community(&community, &user, &conn)?;
// TODO: this needs to return a "pending" state, until Accept is received from the remote server
}
@@ -429,8 +500,14 @@ impl Perform<CommunityResponse> for Oper<FollowCommunity> {
}
}
-impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
- fn perform(&self, conn: &PgConnection) -> Result<GetFollowedCommunitiesResponse, Error> {
+impl Perform for Oper<GetFollowedCommunities> {
+ type Response = GetFollowedCommunitiesResponse;
+
+ fn perform(
+ &self,
+ pool: Pool<ConnectionManager<PgConnection>>,
+ _websocket_info: Option<WebsocketInfo>,
+ ) -> Result<GetFollowedCommunitiesResponse, Error> {
let data: &GetFollowedCommunities = &self.data;
let claims = match Claims::decode(&data.auth) {
@@ -440,6 +517,8 @@ impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
let user_id = claims.id;
+ let conn = pool.get()?;
+
let communities: Vec<CommunityFollowerView> =
match CommunityFollowerView::for_user(&conn, user_id) {
Ok(communities) => communities,
@@ -451,8 +530,14 @@ impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
}
}
-impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
- fn perform(&self, conn: &PgConnection) -> Result<BanFromCommunityResponse, Error> {
+impl Perform for Oper<BanFromCommunity> {
+ type Response = BanFromCommunityResponse;
+
+ fn perform(
+ &self,
+ pool: Pool<ConnectionManager<PgConnection>>,
+ websocket_info: Option<WebsocketInfo>,
+ ) -> Result<BanFromCommunityResponse, Error> {
let data: &BanFromCommunity = &self.data;
let claims = match Claims::decode(&data.auth) {
@@ -467,6 +552,8 @@ impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
user_id: data.user_id,
};
+ let conn = pool.get()?;
+
if data.ban {
match CommunityUserBan::ban(&conn, &community_user_ban_form) {
Ok(user) => user,
@@ -497,15 +584,32 @@ impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
let user_view = UserView::read(&conn, data.user_id)?;
- Ok(BanFromCommunityResponse {
+ let res = BanFromCommunityResponse {
user: user_view,
banned: data.ban,
- })
+ };
+
+ if let Some(ws) = websocket_info {
+ ws.chatserver.do_send(SendCommunityRoomMessage {
+ op: UserOperation::BanFromCommunity,
+ response: res.clone(),
+ community_id: data.community_id,
+ my_id: ws.id,
+ });
+ }
+
+ Ok(res)
}
}
-impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
- fn perform(&self, conn: &PgConnection) -> Result<AddModToCommunityResponse, Error> {
+impl Perform for Oper<AddModToCommunity> {
+ type Response = AddModToCommunityResponse;
+
+ fn perform(
+ &self,
+ pool: Pool<ConnectionManager<PgConnection>>,
+ websocket_info: Option<WebsocketInfo>,
+ ) -> Result<AddModToCommunityResponse, Error> {
let data: &AddModToCommunity = &self.data;
let claims = match Claims::decode(&data.auth) {
@@ -520,6 +624,8 @@ impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
user_id: data.user_id,
};
+ let conn = pool.get()?;
+
if data.added {
match CommunityModerator::join(&conn, &community_moderator_form) {
Ok(user) => user,
@@ -543,12 +649,29 @@ impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
let moderators = CommunityModeratorView::for_community(&conn, data.community_id)?;
- Ok(AddModToCommunityResponse { moderators })
+ let res = AddModToCommunityResponse { moderators };
+
+ if let Some(ws) = websocket_info {
+ ws.chatserver.do_send(SendCommunityRoomMessage {
+ op: UserOperation::AddModToCommunity,
+ response: res.clone(),
+ community_id: data.community_id,
+ my_id: ws.id,
+ });
+ }
+
+ Ok(res)
}
}
-impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
- fn perform(&self, conn: &PgConnection) -> Result<GetCommunityResponse, Error> {
+impl Perform for Oper<TransferCommunity> {
+ type Response = GetCommunityResponse;
+
+ fn perform(
+ &self,
+ pool: Pool<ConnectionManager<PgConnection>>,
+ _websocket_info: Option<WebsocketInfo>,
+ ) -> Result<GetCommunityResponse, Error> {
let data: &TransferCommunity = &self.data;
let claims = match Claims::decode(&data.auth) {
@@ -558,6 +681,8 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
let user_id = claims.id;
+ let conn = pool.get()?;
+
let read_community = Community::read(&conn, data.community_id)?;
let site_creator_id = Site::read(&conn, 1)?.creator_id;