invitation ajustments
This commit is contained in:
1 parent
6ac21ccf4c
commit
7eed94d82c
5 files changed
+188
-54
No files matched your search
@@ -669,6 +669,45 @@ impl MeowClient {
|
||||
server_urls: &[String],
|
||||
space: Option<&str>,
|
||||
) -> Result<Vec<u8>> {
|
||||
let (_, contact, answer, si) = self.prepare_answer(offer_bytes, contact_name, server_urls, space)?;
|
||||
self.spaces[si].state.contacts.push(contact);
|
||||
self.save_space(si)?;
|
||||
Ok(answer.encode_to_vec())
|
||||
}
|
||||
|
||||
/// Step 2 for an offer received by any channel (QR code, file, nearby transfer):
|
||||
/// answer it and send the answer to the initiator's answer mailbox, which then
|
||||
/// completes the handshake in the background. Returns the initiator's name.
|
||||
/// The contact and its answer are saved together.
|
||||
pub async fn accept_invitation(
|
||||
&mut self,
|
||||
offer_bytes: &[u8],
|
||||
contact_name: Option<&str>,
|
||||
server_urls: &[String],
|
||||
space: Option<&str>,
|
||||
) -> Result<String> {
|
||||
let (info, contact, answer, si) = self.prepare_answer(offer_bytes, contact_name, server_urls, space)?;
|
||||
let queued = outbox::item(
|
||||
&contact.id,
|
||||
vec![],
|
||||
&invitation::answer_deposit(&info, &answer),
|
||||
info.answer_servers.clone(),
|
||||
);
|
||||
self.spaces[si].state.contacts.push(contact);
|
||||
self.spaces[si].state.outbox.push(queued);
|
||||
self.save_space(si)?;
|
||||
self.flush(false).await?;
|
||||
Ok(info.initiator_name)
|
||||
}
|
||||
|
||||
/// Validate an offer and build the answering contact (not stored yet).
|
||||
fn prepare_answer(
|
||||
&mut self,
|
||||
offer_bytes: &[u8],
|
||||
contact_name: Option<&str>,
|
||||
server_urls: &[String],
|
||||
space: Option<&str>,
|
||||
) -> Result<(OfferInfo, Contact, v2::InvitationMessage, usize)> {
|
||||
let info = self.inspect_invitation(offer_bytes)?;
|
||||
let name = contact_name.unwrap_or(&info.initiator_name).to_string();
|
||||
self.ensure_free_name(&name)?;
|
||||
@@ -678,9 +717,7 @@ impl MeowClient {
|
||||
let servers = self.resolve_servers(server_urls)?;
|
||||
let (contact, answer) = invitation::answer(offer_bytes, &name, self.nickname(), servers, now())?;
|
||||
let si = self.target_space(space)?;
|
||||
self.spaces[si].state.contacts.push(contact);
|
||||
self.save_space(si)?;
|
||||
Ok(answer.encode_to_vec())
|
||||
Ok((info, contact, answer, si))
|
||||
}
|
||||
|
||||
fn by_invitation(&self, id: &[u8]) -> Result<(usize, usize)> {
|
||||
@@ -774,22 +811,8 @@ impl MeowClient {
|
||||
let Resp::InvFetched(f) = resp else {
|
||||
return Err(Error::Network("unexpected response".into()));
|
||||
};
|
||||
let info = self.inspect_invitation(&f.payload)?;
|
||||
let answer_bytes = self
|
||||
.answer_invitation(&f.payload, contact_name, server_urls, space)
|
||||
.await?;
|
||||
let answer = v2::InvitationMessage::decode(answer_bytes.as_slice()).map_err(meow_core::Error::from)?;
|
||||
let (si, ci) = self.by_invitation(&info.invitation_id)?;
|
||||
let queued = outbox::item(
|
||||
&self.spaces[si].state.contacts[ci].id,
|
||||
vec![],
|
||||
&invitation::answer_deposit(&info, &answer),
|
||||
info.answer_servers.clone(),
|
||||
);
|
||||
self.spaces[si].state.outbox.push(queued);
|
||||
self.save_space(si)?;
|
||||
self.flush(false).await?;
|
||||
Ok(info.initiator_name)
|
||||
self.accept_invitation(&f.payload, contact_name, server_urls, space)
|
||||
.await
|
||||
}
|
||||
|
||||
// ───────────────────────── messaging ─────────────────────────
|
||||
|
||||
@@ -820,3 +820,32 @@ async fn background_task_wakes_up_on_new_contacts() {
|
||||
bob.stop().await;
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// One-shot invitation (QR code, file, nearby): only the offer travels out of band.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn one_shot_invitation_completes_through_servers() {
|
||||
let servers = vec![spawn_server().await];
|
||||
let dir = tmpdir();
|
||||
let mut alice = client(&dir, "alice", &servers).await;
|
||||
let mut bob = client(&dir, "bob", &servers).await;
|
||||
let offer = alice
|
||||
.create_invitation("Bob", &[], "scan me", 3600, None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(bob.accept_invitation(&offer, None, &[], None).await.unwrap(), "alice");
|
||||
// Accepting twice is refused.
|
||||
assert!(bob.accept_invitation(&offer, Some("other"), &[], None).await.is_err());
|
||||
assert_eq!(
|
||||
no_receipts(alice.sync(Duration::ZERO).await.unwrap()),
|
||||
vec![Event::InvitationAccepted { contact: "Bob".into() }]
|
||||
);
|
||||
bob.sync(Duration::ZERO).await.unwrap();
|
||||
alice.sync(Duration::ZERO).await.unwrap();
|
||||
assert_eq!(alice.contact("Bob").unwrap().state, State::Active);
|
||||
assert_eq!(bob.contact("alice").unwrap().state, State::Active);
|
||||
assert_eq!(
|
||||
alice.contact("Bob").unwrap().safety_number(),
|
||||
bob.contact("alice").unwrap().safety_number()
|
||||
);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
Reference in new issue
Block a user