Skip to content

Commit

Permalink
Revert "feat: remove get_channels_by_user"
Browse files Browse the repository at this point in the history
This reverts commit c630bc3.
  • Loading branch information
uonr committed Sep 15, 2024
1 parent 820c034 commit 919347b
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/legacy/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export interface LoggedIn {
user: User;
settings: Settings;
mySpaces: SpaceWithMember[];
myChannels: ChannelWithMember[];
}

export interface SettingsUpdated {
Expand Down
1 change: 1 addition & 0 deletions apps/legacy/src/api/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface GetMe {
user: User;
settings: Settings;
mySpaces: SpaceWithMember[];
myChannels: ChannelWithMember[];
}

export interface EditUser {
Expand Down
4 changes: 2 additions & 2 deletions apps/legacy/src/hooks/useGetMe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export const useGetMe = (dispatch: Dispatch, finish: () => void): void => {
const me = await get('/users/get_me');
if (me.isOk) {
if (me.value) {
const { user, mySpaces, settings } = me.value;
dispatch({ type: 'LOGGED_IN', user, mySpaces, settings });
const { user, mySpaces, myChannels, settings } = me.value;
dispatch({ type: 'LOGGED_IN', user, myChannels, mySpaces, settings });
} else {
dispatch({ type: 'LOGGED_OUT' });
}
Expand Down
6 changes: 5 additions & 1 deletion apps/legacy/src/reducers/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ export interface ProfileState {
const login = (state: ProfileState | undefined, action: LoggedIn): ProfileState => {
const { user, settings } = action;
let spaces: OrderedMap<Id, SpaceWithMember> = OrderedMap<Id, SpaceWithMember>();
let channels: OrderedMap<Id, ChannelWithMember> = OrderedMap<Id, ChannelWithMember>();
for (const s of action.mySpaces) {
spaces = spaces.set(s.space.id, s);
}
for (const c of action.myChannels) {
channels = channels.set(c.channel.id, c);
}

return { user, channels: state?.channels ?? OrderedMap(), spaces, settings };
return { user, channels, spaces, settings };
};

const editUser = ({ channels, spaces, settings }: ProfileState, { user }: UserEdited): ProfileState => {
Expand Down
8 changes: 7 additions & 1 deletion apps/server/bindings/GetMe.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions apps/server/sql/channels/get_channels_by_user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SELECT
c AS "channel!: Channel",
cm AS "member!: ChannelMember"
FROM
channel_members cm
INNER JOIN channels c ON cm.channel_id = c.id
AND c.deleted = FALSE
INNER JOIN space_members sm ON cm.user_id = sm.user_id
AND c.space_id = sm.space_id
WHERE
cm.user_id = $1
AND cm.is_joined;

9 changes: 9 additions & 0 deletions apps/server/src/channels/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,14 @@ async fn export(req: Request<impl Body>) -> Result<Vec<Message>, AppError> {
.map_err(Into::into)
}

async fn my_channels(req: Request<impl Body>) -> Result<Vec<ChannelWithMember>, AppError> {
let session = authenticate(&req).await?;

Channel::get_by_user(&db::get().await, session.user_id)
.await
.map_err(Into::into)
}

pub async fn check_channel_name_exists(req: Request<impl Body>) -> Result<bool, AppError> {
let CheckChannelName { space_id, name } = parse_query(req.uri())?;
let channel = Channel::get_by_name(&db::get().await, space_id, &name).await?;
Expand All @@ -468,6 +476,7 @@ pub async fn router(req: Request<impl Body>, path: &str) -> Result<hyper::Respon
("/query_with_related", Method::GET) => query_with_related(req).await.map(ok_response),
("/members", Method::GET) => members(req).await.map(ok_response),
("/by_space", Method::GET) => by_space(req).await.map(ok_response),
("/my", Method::GET) => my_channels(req).await.map(ok_response),
("/create", Method::POST) => create(req).await.map(ok_response),
("/edit", Method::POST) => edit(req).await.map(ok_response),
("/edit_master", Method::POST) => edit_masters(req).await.map(ok_response),
Expand Down
11 changes: 10 additions & 1 deletion apps/server/src/channels/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use ts_rs::TS;
use uuid::Uuid;

use crate::channels::api::{ChannelMemberWithUser, ChannelWithMaybeMember};
use crate::channels::api::{ChannelMemberWithUser, ChannelWithMaybeMember, ChannelWithMember};
use crate::db;
use crate::error::ModelError;
use crate::spaces::{Space, SpaceMember};
Expand Down Expand Up @@ -183,6 +183,15 @@ impl Channel {
.await?;
Ok(rows.into_iter().map(|row| (row.channel_id, row.max_pos)).collect())
}

pub async fn get_by_user<'c, T: sqlx::PgExecutor<'c>>(
db: T,
user_id: Uuid,
) -> Result<Vec<ChannelWithMember>, sqlx::Error> {
sqlx::query_file_as!(ChannelWithMember, "sql/channels/get_channels_by_user.sql", user_id)
.fetch_all(db)
.await
}
}

#[derive(Debug, Serialize, Deserialize, Clone, TS)]
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/users/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::User;
use crate::channels::api::ChannelWithMember;
use crate::spaces::api::SpaceWithMember;
use serde::{Deserialize, Serialize};
use ts_rs::TS;
Expand Down Expand Up @@ -42,6 +43,7 @@ pub struct GetMe {
pub user: User,
#[ts(type = "unknown")]
pub settings: serde_json::Value,
pub my_channels: Vec<ChannelWithMember>,
pub my_spaces: Vec<SpaceWithMember>,
}

Expand Down
5 changes: 5 additions & 0 deletions apps/server/src/users/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::api::{Login, LoginReturn, Register, ResetPassword, ResetPasswordConfirm, ResetPasswordTokenCheck};
use super::models::User;
use crate::channels::Channel;
use crate::error::{AppError, Find, ValidationFailed};
use crate::interface;
use crate::interface::{missing, ok_response, parse_body, parse_query};
Expand Down Expand Up @@ -91,11 +92,13 @@ pub async fn get_me(req: Request<impl Body>) -> Result<Response<Vec<u8>>, AppErr
let user = User::get_by_id(&mut *conn, &session.user_id).await?;
if let Some(user) = user {
let my_spaces = Space::get_by_user(&mut *conn, &user.id).await?;
let my_channels = Channel::get_by_user(&mut *conn, user.id).await?;
let settings = UserExt::get_settings(&mut *conn, user.id).await?;

let mut response = ok_response(Some(GetMe {
user,
settings,
my_channels,
my_spaces,
}));
if is_authenticate_use_cookie(req.headers()) {
Expand Down Expand Up @@ -137,11 +140,13 @@ pub async fn login<B: Body>(req: Request<B>) -> Result<Response<Vec<u8>>, AppErr
let token = session::token(&session);
let token = if form.with_token { Some(token) } else { None };
let my_spaces = Space::get_by_user(&mut *conn, &user.id).await?;
let my_channels = Channel::get_by_user(&mut *conn, user.id).await?;
let settings = UserExt::get_settings(&mut *conn, user.id).await?;
let me = GetMe {
user,
settings: settings.clone(),
my_spaces,
my_channels,
};
let mut response = ok_response(LoginReturn { me, token });
let headers = response.headers_mut();
Expand Down

0 comments on commit 919347b

Please sign in to comment.