Implement final move, link, unlink db APIs

This commit is contained in:
Mikayla 2023-09-14 20:29:29 -07:00
parent 7fa68a9aa4
commit 9afb67f2cf
No known key found for this signature in database
8 changed files with 160 additions and 174 deletions

View file

@ -2434,17 +2434,16 @@ async fn unlink_channel(
) -> Result<()> {
let db = session.db().await;
let channel_id = ChannelId::from_proto(request.channel_id);
let from = request.from.map(ChannelId::from_proto);
// Get the members before we remove it, so we know who to notify
let members = db.get_channel_members(channel_id).await?;
let from = ChannelId::from_proto(request.from);
db.unlink_channel(session.user_id, channel_id, from).await?;
let members = db.get_channel_members(from).await?;
let update = proto::UpdateChannels {
delete_channel_edge: vec![proto::ChannelEdge {
channel_id: channel_id.to_proto(),
parent_id: from.map(ChannelId::to_proto),
parent_id: from.to_proto(),
}],
..Default::default()
};
@ -2467,38 +2466,31 @@ async fn move_channel(
) -> Result<()> {
let db = session.db().await;
let channel_id = ChannelId::from_proto(request.channel_id);
let from_parent = request.from.map(ChannelId::from_proto);
let from_parent = ChannelId::from_proto(request.from);
let to = ChannelId::from_proto(request.to);
let mut members = db.get_channel_members(channel_id).await?;
let members_from = db.get_channel_members(channel_id).await?;
let channels_to_send: Vec<Channel> = db
.move_channel(session.user_id, channel_id, from_parent, to)
.await?;
let members_after = db.get_channel_members(channel_id).await?;
let members_to = db.get_channel_members(channel_id).await?;
members.extend(members_after);
members.sort();
members.dedup();
if let Some(from_parent) = from_parent {
let update = proto::UpdateChannels {
delete_channel_edge: vec![proto::ChannelEdge {
channel_id: channel_id.to_proto(),
parent_id: Some(from_parent.to_proto()),
}],
..Default::default()
};
let connection_pool = session.connection_pool().await;
for member_id in members {
for connection_id in connection_pool.user_connection_ids(member_id) {
session.peer.send(connection_id, update.clone())?;
}
let update = proto::UpdateChannels {
delete_channel_edge: vec![proto::ChannelEdge {
channel_id: channel_id.to_proto(),
parent_id: from_parent.to_proto(),
}],
..Default::default()
};
let connection_pool = session.connection_pool().await;
for member_id in members_from {
for connection_id in connection_pool.user_connection_ids(member_id) {
session.peer.send(connection_id, update.clone())?;
}
}
let connection_pool = session.connection_pool().await;
let update = proto::UpdateChannels {
channels: channels_to_send
.into_iter()
@ -2510,7 +2502,7 @@ async fn move_channel(
.collect(),
..Default::default()
};
for member_id in members {
for member_id in members_to {
for connection_id in connection_pool.user_connection_ids(member_id) {
session.peer.send(connection_id, update.clone())?;
}