-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle incoming edit messages (#263)
- Loading branch information
Showing
21 changed files
with
385 additions
and
69 deletions.
There are no files selected for viewing
12 changes: 0 additions & 12 deletions
12
.sqlx/query-234d1525bc9a0646e4ab3f79e7edbe5f74fff34c57b2a0ba165fbac575f600e2.json
This file was deleted.
Oops, something went wrong.
20 changes: 16 additions & 4 deletions
20
...810230cf12761c28c48a1abdf74e49fbf042.json → ...335b9690b70c640a7820c298559c9b007f28.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
.sqlx/query-6d5d549e04f958f194c3c13fb6a974e2c33824667c9a6fe9370456d659798b34.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
18 changes: 15 additions & 3 deletions
18
...9388c697a6ad39125ced78295b90bfc2d523.json → ...9ca20aace2261befc099e641b8d0ba692b29.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
DROP INDEX idx_messages_edit; | ||
|
||
DROP INDEX idx_messages_quote; | ||
|
||
ALTER TABLE messages | ||
DROP COLUMN edited; | ||
|
||
ALTER TABLE messages | ||
DROP COLUMN edit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- edit points to the original edited message | ||
-- | ||
-- for a chain of edits original, edit1, edit2, ... each edit points to the arrived_at field of | ||
-- the message original | ||
ALTER TABLE messages | ||
ADD COLUMN edit INTEGER; | ||
|
||
ALTER TABLE messages | ||
ADD COLUMN edited BOOLEAN NOT NULL DEFAULT FALSE; | ||
|
||
CREATE INDEX idx_messages_quote | ||
ON messages (quote); | ||
|
||
CREATE INDEX idx_messages_edit | ||
ON messages (edit); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
use anyhow::Context; | ||
use presage::libsignal_service::content::Metadata; | ||
use presage::proto::sync_message::Sent; | ||
use presage::proto::{DataMessage, EditMessage, SyncMessage}; | ||
use tracing::warn; | ||
use uuid::Uuid; | ||
|
||
use crate::app::App; | ||
use crate::data::{ChannelId, Message}; | ||
use crate::storage::MessageId; | ||
|
||
impl App { | ||
pub(super) fn handle_sync_message( | ||
&mut self, | ||
metadata: Metadata, | ||
sync_message: SyncMessage, | ||
) -> anyhow::Result<()> { | ||
let Some(channel_id) = sync_message.channel_id() else { | ||
warn!("dropping a sync message not attached to a channel"); | ||
return Ok(()); | ||
}; | ||
|
||
// edit message | ||
if let Some(Sent { | ||
edit_message: | ||
Some(EditMessage { | ||
target_sent_timestamp: Some(target_sent_timestamp), | ||
data_message: | ||
Some(DataMessage { | ||
body: Some(body), | ||
timestamp: Some(arrived_at), | ||
.. | ||
}), | ||
}), | ||
.. | ||
}) = sync_message.sent | ||
{ | ||
let from_id = metadata.sender.uuid; | ||
// Note: target_sent_timestamp points to the previous edit or the original message | ||
let edited = self | ||
.storage | ||
.message(MessageId::new(channel_id, target_sent_timestamp)) | ||
.context("no message to edit")?; | ||
|
||
// get original message | ||
let mut original = if let Some(arrived_at) = edited.edit { | ||
// previous edit => get original message | ||
self.storage | ||
.message(MessageId::new(channel_id, arrived_at)) | ||
.context("no original edited message")? | ||
.into_owned() | ||
} else { | ||
// original message => first edit | ||
let original = edited.into_owned(); | ||
|
||
// preserve body of the original message; it is replaced below | ||
let mut preserved = original.clone(); | ||
preserved.arrived_at = original.arrived_at + 1; | ||
preserved.edit = Some(original.arrived_at); | ||
self.storage.store_message(channel_id, preserved); | ||
|
||
original | ||
}; | ||
|
||
// store the incoming edit | ||
self.storage.store_message( | ||
channel_id, | ||
Message { | ||
edit: Some(original.arrived_at), | ||
..Message::text(from_id, arrived_at, body.clone()) | ||
}, | ||
); | ||
|
||
// override the body of the original message | ||
original.message.replace(body); | ||
original.edited = true; | ||
self.storage.store_message(channel_id, original); | ||
|
||
let channel_idx = self | ||
.channels | ||
.items | ||
.iter() | ||
.position(|id| id == &channel_id) | ||
.context("editing message in non-existent channel")?; | ||
self.touch_channel(channel_idx); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
trait MessageExt { | ||
fn sender_id(&self) -> Option<Uuid>; | ||
|
||
/// Get a channel id a message | ||
fn channel_id(&self) -> Option<ChannelId>; | ||
} | ||
|
||
impl MessageExt for SyncMessage { | ||
fn sender_id(&self) -> Option<Uuid> { | ||
todo!() | ||
} | ||
|
||
fn channel_id(&self) -> Option<ChannelId> { | ||
// only sent sync message are attached to a conversation | ||
let sent = self.sent.as_ref()?; | ||
if let Some(uuid) = sent | ||
.destination_service_id | ||
.as_ref() | ||
.and_then(|id| id.parse().ok()) | ||
{ | ||
Some(ChannelId::User(uuid)) | ||
} else { | ||
let group_v2 = sent | ||
.message | ||
.as_ref() | ||
.and_then(|message| message.group_v2.as_ref()) | ||
.or_else(|| { | ||
sent.edit_message | ||
.as_ref()? | ||
.data_message | ||
.as_ref()? | ||
.group_v2 | ||
.as_ref() | ||
})?; | ||
ChannelId::from_master_key_bytes(group_v2.master_key.as_deref()?).ok() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.