| @@ -1,7 +1,7 @@ |
| 1 | 1 | mod access_token; |
| 2 | 2 | mod channel; |
| 3 | 3 | mod channel_member; |
| 4 | | −// mod channel_parent; |
| 4 | +mod channel_parent; |
| 5 | 5 | mod contact; |
| 6 | 6 | mod follower; |
| 7 | 7 | mod language_server; |
| @@ -39,7 +39,10 @@ use sea_orm::{ · 31 unmodified lines |
| 39 | 39 | DbErr, FromQueryResult, IntoActiveModel, IsolationLevel, JoinType, QueryOrder, QuerySelect, |
| 40 | 40 | Statement, TransactionTrait, |
| 41 | 41 | }; |
| 42 | | −use sea_query::{Alias, Expr, OnConflict, Query, SelectStatement}; |
| 42 | +use sea_query::{ |
| 43 | + Alias, ColumnRef, CommonTableExpression, Expr, OnConflict, Order, Query, QueryStatementWriter, |
| 44 | + SelectStatement, UnionType, WithClause, |
| 45 | +}; |
| 43 | 46 | use serde::{Deserialize, Serialize}; |
| 44 | 47 | pub use signup::{Invite, NewSignup, WaitlistSummary}; |
| 45 | 48 | use sqlx::migrate::{Migrate, Migration, MigrationSource}; |
| @@ -3032,7 +3035,11 @@ impl Database { · 2986 unmodified lines |
| 3032 | 3035 | |
| 3033 | 3036 | // channels |
| 3034 | 3037 | |
| 3035 | | − pub async fn create_channel(&self, name: &str) -> Result<ChannelId> { |
| 3038 | + pub async fn create_root_channel(&self, name: &str) -> Result<ChannelId> { |
| 3039 | + self.create_channel(name, None).await |
| 3040 | + } |
| 3041 | + |
| 3042 | + pub async fn create_channel(&self, name: &str, parent: Option<ChannelId>) -> Result<ChannelId> { |
| 3036 | 3043 | self.transaction(move |tx| async move { |
| 3037 | 3044 | let tx = tx; |
| 3038 | 3045 | |
| @@ -3043,10 +3050,21 @@ impl Database { · 4 unmodified lines |
| 3043 | 3050 | |
| 3044 | 3051 | let channel = channel.insert(&*tx).await?; |
| 3045 | 3052 | |
| 3053 | + if let Some(parent) = parent { |
| 3054 | + channel_parent::ActiveModel { |
| 3055 | + child_id: ActiveValue::Set(channel.id), |
| 3056 | + parent_id: ActiveValue::Set(parent), |
| 3057 | + } |
| 3058 | + .insert(&*tx) |
| 3059 | + .await?; |
| 3060 | + } |
| 3061 | + |
| 3046 | 3062 | Ok(channel.id) |
| 3047 | | − }).await |
| 3063 | + }) |
| 3064 | + .await |
| 3048 | 3065 | } |
| 3049 | 3066 | |
| 3067 | + // Property: Members are only |
| 3050 | 3068 | pub async fn add_channel_member(&self, channel_id: ChannelId, user_id: UserId) -> Result<()> { |
| 3051 | 3069 | self.transaction(move |tx| async move { |
| 3052 | 3070 | let tx = tx; |
| @@ -3060,139 +3078,108 @@ impl Database { · 7 unmodified lines |
| 3060 | 3078 | channel_membership.insert(&*tx).await?; |
| 3061 | 3079 | |
| 3062 | 3080 | Ok(()) |
| 3063 | | − }).await |
| 3081 | + }) |
| 3082 | + .await |
| 3064 | 3083 | } |
| 3065 | 3084 | |
| 3066 | | − pub async fn get_channels(&self, user_id: UserId) -> Vec<ChannelId> { |
| 3085 | + pub async fn get_channels(&self, user_id: UserId) -> Result<Vec<Channel>> { |
| 3067 | 3086 | self.transaction(|tx| async move { |
| 3068 | 3087 | let tx = tx; |
| 3069 | 3088 | |
| 3089 | + // This is the SQL statement we want to generate: |
| 3090 | + let sql = r#" |
| 3091 | + WITH RECURSIVE channel_tree(child_id, parent_id, depth) AS ( |
| 3092 | + SELECT channel_id as child_id, NULL as parent_id, 0 |
| 3093 | + FROM channel_members |
| 3094 | + WHERE user_id = ? |
| 3095 | + UNION ALL |
| 3096 | + SELECT channel_parents.child_id, channel_parents.parent_id, channel_tree.depth + 1 |
| 3097 | + FROM channel_parents, channel_tree |
| 3098 | + WHERE channel_parents.parent_id = channel_tree.child_id |
| 3099 | + ) |
| 3100 | + SELECT channel_tree.child_id as id, channels.name, channel_tree.parent_id |
| 3101 | + FROM channel_tree |
| 3102 | + JOIN channels ON channels.id = channel_tree.child_id |
| 3103 | + ORDER BY channel_tree.depth; |
| 3104 | + "#; |
| 3105 | + |
| 3106 | + // let root_channel_ids_query = SelectStatement::new() |
| 3107 | + // .column(channel_member::Column::ChannelId) |
| 3108 | + // .expr(Expr::value("NULL")) |
| 3109 | + // .from(channel_member::Entity.table_ref()) |
| 3110 | + // .and_where( |
| 3111 | + // Expr::col(channel_member::Column::UserId) |
| 3112 | + // .eq(Expr::cust_with_values("?", vec![user_id])), |
| 3113 | + // ); |
| 3114 | + |
| 3115 | + // let build_tree_query = SelectStatement::new() |
| 3116 | + // .column(channel_parent::Column::ChildId) |
| 3117 | + // .column(channel_parent::Column::ParentId) |
| 3118 | + // .expr(Expr::col(Alias::new("channel_tree.depth")).add(1i32)) |
| 3119 | + // .from(Alias::new("channel_tree")) |
| 3120 | + // .and_where( |
| 3121 | + // Expr::col(channel_parent::Column::ParentId) |
| 3122 | + // .equals(Alias::new("channel_tree"), Alias::new("child_id")), |
| 3123 | + // ) |
| 3124 | + // .to_owned(); |
| 3125 | + |
| 3126 | + // let common_table_expression = CommonTableExpression::new() |
| 3127 | + // .query( |
| 3128 | + // root_channel_ids_query |
| 3129 | + // .union(UnionType::Distinct, build_tree_query) |
| 3130 | + // .to_owned(), |
| 3131 | + // ) |
| 3132 | + // .column(Alias::new("child_id")) |
| 3133 | + // .column(Alias::new("parent_id")) |
| 3134 | + // .column(Alias::new("depth")) |
| 3135 | + // .table_name(Alias::new("channel_tree")) |
| 3136 | + // .to_owned(); |
| 3137 | + |
| 3138 | + // let select = SelectStatement::new() |
| 3139 | + // .expr_as( |
| 3140 | + // Expr::col(Alias::new("channel_tree.child_id")), |
| 3141 | + // Alias::new("id"), |
| 3142 | + // ) |
| 3143 | + // .column(channel::Column::Name) |
| 3144 | + // .column(Alias::new("channel_tree.parent_id")) |
| 3145 | + // .from(Alias::new("channel_tree")) |
| 3146 | + // .inner_join( |
| 3147 | + // channel::Entity.table_ref(), |
| 3148 | + // Expr::eq( |
| 3149 | + // channel::Column::Id.into_expr(), |
| 3150 | + // Expr::tbl(Alias::new("channel_tree"), Alias::new("child_id")), |
| 3151 | + // ), |
| 3152 | + // ) |
| 3153 | + // .order_by(Alias::new("channel_tree.child_id"), Order::Asc) |
| 3154 | + // .to_owned(); |
| 3155 | + |
| 3156 | + // let with_clause = WithClause::new() |
| 3157 | + // .recursive(true) |
| 3158 | + // .cte(common_table_expression) |
| 3159 | + // .to_owned(); |
| 3160 | + |
| 3161 | + // let query = select.with(with_clause); |
| 3162 | + |
| 3163 | + // let query = SelectStatement::new() |
| 3164 | + // .column(ColumnRef::Asterisk) |
| 3165 | + // .from_subquery(query, Alias::new("channel_tree") |
| 3166 | + // .to_owned(); |
| 3167 | + |
| 3168 | + // let stmt = self.pool.get_database_backend().build(&query); |
| 3169 | + |
| 3170 | + let stmt = Statement::from_sql_and_values( |
| 3171 | + self.pool.get_database_backend(), |
| 3172 | + sql, |
| 3173 | + vec![user_id.into()], |
| 3174 | + ); |
| 3175 | + |
| 3176 | + Ok(channel_parent::Entity::find() |
| 3177 | + .from_raw_sql(stmt) |
| 3178 | + .into_model::<Channel>() |
| 3179 | + .all(&*tx) |
| 3180 | + .await?) |
| 3070 | 3181 | }) |
| 3071 | | − // let user = user::Model { |
| 3072 | | − // id: user_id, |
| 3073 | | − // ..Default::default() |
| 3074 | | − // }; |
| 3075 | | − // let mut channel_ids = user |
| 3076 | | − // .find_related(channel_member::Entity) |
| 3077 | | − // .select_only() |
| 3078 | | − // .column(channel_member::Column::ChannelId) |
| 3079 | | − // .all(&*tx) |
| 3080 | | − // .await; |
| 3081 | | − |
| 3082 | | − // // let descendants = Alias::new("descendants"); |
| 3083 | | − // // let cte_referencing = SelectStatement::new() |
| 3084 | | − // // .column(channel_parent::Column::ChildId) |
| 3085 | | − // // .from(channel::Entity) |
| 3086 | | − // // .and_where( |
| 3087 | | − // // Expr::col(channel_parent::Column::ParentId) |
| 3088 | | − // // .in_subquery(SelectStatement::new().from(descendants).take()) |
| 3089 | | − // // ); |
| 3090 | | − |
| 3091 | | − // // /* |
| 3092 | | − // // WITH RECURSIVE descendant_ids(id) AS ( |
| 3093 | | − // // $1 |
| 3094 | | − // // UNION ALL |
| 3095 | | − // // SELECT child_id as id FROM channel_parents WHERE parent_id IN descendants |
| 3096 | | − // // ) |
| 3097 | | − // // SELECT * from channels where id in descendant_ids |
| 3098 | | − // // */ |
| 3099 | | − |
| 3100 | | − |
| 3101 | | − // // // WITH RECURSIVE descendants(id) AS ( |
| 3102 | | − // // // // SQL QUERY FOR SELECTING Initial IDs |
| 3103 | | − // // // UNION |
| 3104 | | − // // // SELECT id FROM ancestors WHERE p.parent = id |
| 3105 | | − // // // ) |
| 3106 | | − // // // SELECT * FROM descendants; |
| 3107 | | − |
| 3108 | | − |
| 3109 | | − |
| 3110 | | − // // // let descendant_channel_ids = |
| 3111 | | − |
| 3112 | | − |
| 3113 | | − |
| 3114 | | − // // // let query = sea_query::Query::with().recursive(true); |
| 3115 | | − |
| 3116 | | − |
| 3117 | | − // // for id_path in id_paths { |
| 3118 | | − // // // |
| 3119 | | − // // } |
| 3120 | | − |
| 3121 | | − |
| 3122 | | − // // // zed/public/plugins |
| 3123 | | − // // // zed/public/plugins/js |
| 3124 | | − // // // zed/zed-livekit |
| 3125 | | − // // // livekit/zed-livekit |
| 3126 | | − // // // zed - 101 |
| 3127 | | − // // // livekit - 500 |
| 3128 | | − // // // zed-livekit - 510 |
| 3129 | | − // // // public - 150 |
| 3130 | | − // // // plugins - 200 |
| 3131 | | − // // // js - 300 |
| 3132 | | − // // // |
| 3133 | | − // // // Channel, Parent - edges |
| 3134 | | − // // // 510 - 500 |
| 3135 | | − // // // 510 - 101 |
| 3136 | | − // // // |
| 3137 | | − // // // Given the channel 'Zed' (101) |
| 3138 | | − // // // Select * from EDGES where parent = 101 => 510 |
| 3139 | | − // // // |
| 3140 | | − |
| 3141 | | − |
| 3142 | | − // // "SELECT * from channels where id_path like '$1?'" |
| 3143 | | − |
| 3144 | | − // // // https://www.postgresql.org/docs/current/queries-with.html |
| 3145 | | − // // // https://www.sqlite.org/lang_with.html |
| 3146 | | − |
| 3147 | | − // // "SELECT channel_id from channel_ancestors where ancestor_id IN $()" |
| 3148 | | − |
| 3149 | | − // // // | channel_id | ancestor_ids | |
| 3150 | | − // // // 150 150 |
| 3151 | | − // // // 150 101 |
| 3152 | | − // // // 200 101 |
| 3153 | | − // // // 300 101 |
| 3154 | | − // // // 200 150 |
| 3155 | | − // // // 300 150 |
| 3156 | | − // // // 300 200 |
| 3157 | | − // // // |
| 3158 | | − // // // // | channel_id | ancestor_ids | |
| 3159 | | − // // // 150 101 |
| 3160 | | − // // // 200 101 |
| 3161 | | − // // // 300 101 |
| 3162 | | − // // // 200 150 |
| 3163 | | − // // // 300 [150, 200] |
| 3164 | | − |
| 3165 | | − // // channel::Entity::find() |
| 3166 | | − // // .filter(channel::Column::IdPath.like(id_paths.unwrap())) |
| 3167 | | − |
| 3168 | | − // // dbg!(&id_paths.unwrap()[0].id_path); |
| 3169 | | − |
| 3170 | | − // // // let mut channel_members_by_channel_id = HashMap::new(); |
| 3171 | | − // // // for channel_member in channel_members { |
| 3172 | | − // // // channel_members_by_channel_id |
| 3173 | | − // // // .entry(channel_member.channel_id) |
| 3174 | | − // // // .or_insert_with(Vec::new) |
| 3175 | | − // // // .push(channel_member); |
| 3176 | | − // // // } |
| 3177 | | − |
| 3178 | | − // // // let mut channel_messages = channel_message::Entity::find() |
| 3179 | | − // // // .filter(channel_message::Column::ChannelId.in_selection(channel_ids)) |
| 3180 | | − // // // .all(&*tx) |
| 3181 | | − // // // .await?; |
| 3182 | | − |
| 3183 | | − // // // let mut channel_messages_by_channel_id = HashMap::new(); |
| 3184 | | − // // // for channel_message in channel_messages { |
| 3185 | | − // // // channel_messages_by_channel_id |
| 3186 | | − // // // .entry(channel_message.channel_id) |
| 3187 | | − // // // .or_insert_with(Vec::new) |
| 3188 | | − // // // .push(channel_message); |
| 3189 | | − // // // } |
| 3190 | | − |
| 3191 | | − // // todo!(); |
| 3192 | | − // // // Ok(channels) |
| 3193 | | − // Err(Error("not implemented")) |
| 3194 | | − // }) |
| 3195 | | − // .await |
| 3182 | + .await |
| 3196 | 3183 | } |
| 3197 | 3184 | |
| 3198 | 3185 | async fn transaction<F, Fut, T>(&self, f: F) -> Result<T> |
| @@ -3440,6 +3427,13 @@ pub struct NewUserResult { · 241 unmodified lines |
| 3440 | 3427 | pub signup_device_id: Option<String>, |
| 3441 | 3428 | } |
| 3442 | 3429 | |
| 3430 | +#[derive(FromQueryResult, Debug, PartialEq)] |
| 3431 | +pub struct Channel { |
| 3432 | + pub id: ChannelId, |
| 3433 | + pub name: String, |
| 3434 | + pub parent_id: Option<ChannelId>, |
| 3435 | +} |
| 3436 | + |
| 3443 | 3437 | fn random_invite_code() -> String { |
| 3444 | 3438 | nanoid::nanoid!(16) |
| 3445 | 3439 | } |