hidden contact invite
This commit is contained in:
1 parent
ce44ee48b3
commit
7eace57528
6 files changed
+172
-37
No files matched your search
@@ -154,6 +154,10 @@ struct ServerSel {
|
||||
/// Comma-separated server URLs or names (default: all my servers).
|
||||
#[arg(short, long, value_delimiter = ',')]
|
||||
servers: Vec<String>,
|
||||
/// Create the contact directly in the hidden space opened by this password
|
||||
/// (created if needed). Unlock it with -u for the next steps.
|
||||
#[arg(long)]
|
||||
space_password: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
@@ -386,6 +390,7 @@ async fn run() -> Result<()> {
|
||||
&message,
|
||||
&invitation_password,
|
||||
DEFAULT_INVITATION_TTL,
|
||||
sel.space_password.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
println!("{url}");
|
||||
@@ -398,7 +403,13 @@ async fn run() -> Result<()> {
|
||||
}) => {
|
||||
let mut c = ctx.open().await?;
|
||||
let who = c
|
||||
.accept_published_invitation(&url, &invitation_password, name.as_deref(), &sel.servers)
|
||||
.accept_published_invitation(
|
||||
&url,
|
||||
&invitation_password,
|
||||
name.as_deref(),
|
||||
&sel.servers,
|
||||
sel.space_password.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
println!("answered invitation from {who}");
|
||||
}
|
||||
@@ -497,7 +508,13 @@ async fn invitation(ctx: &Ctx, cmd: InvitationCmd) -> Result<()> {
|
||||
} => {
|
||||
let mut c = ctx.open().await?;
|
||||
let offer = c
|
||||
.create_invitation(&contact, &sel.servers, &message, DEFAULT_INVITATION_TTL)
|
||||
.create_invitation(
|
||||
&contact,
|
||||
&sel.servers,
|
||||
&message,
|
||||
DEFAULT_INVITATION_TTL,
|
||||
sel.space_password.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
let out = out.unwrap_or_else(|| PathBuf::from(format!("{}-{contact}.mwiv2", c.nickname())));
|
||||
std::fs::write(&out, offer)?;
|
||||
@@ -522,7 +539,9 @@ async fn invitation(ctx: &Ctx, cmd: InvitationCmd) -> Result<()> {
|
||||
let mut c = ctx.open().await?;
|
||||
let offer = std::fs::read(&file)?;
|
||||
let initiator = c.inspect_invitation(&offer)?.initiator_name;
|
||||
let answer = c.answer_invitation(&offer, name.as_deref(), &sel.servers).await?;
|
||||
let answer = c
|
||||
.answer_invitation(&offer, name.as_deref(), &sel.servers, sel.space_password.as_deref())
|
||||
.await?;
|
||||
let out = out.unwrap_or_else(|| PathBuf::from(format!("{}-{initiator}.mwiv2", c.nickname())));
|
||||
std::fs::write(&out, answer)?;
|
||||
println!("{}", out.display());
|
||||
|
||||
@@ -343,6 +343,12 @@ impl MeowClient {
|
||||
/// Move a contact, with its history and files, into the hidden space opened by
|
||||
/// `password` (created if none). Several contacts may share a space.
|
||||
pub async fn hide_contact(&mut self, name: &str, password: &str) -> Result<()> {
|
||||
let target = self.hidden_space(password)?;
|
||||
self.move_contact(name, target)
|
||||
}
|
||||
|
||||
/// Index of the hidden space opened by `password`, created if none.
|
||||
fn hidden_space(&mut self, password: &str) -> Result<usize> {
|
||||
let found = lock(&self.store)?.unlock(password)?;
|
||||
let id = match found {
|
||||
Some(id) => id,
|
||||
@@ -351,8 +357,15 @@ impl MeowClient {
|
||||
if id == SpaceId::MAIN {
|
||||
return Err(Error::Invalid("this is the main password".into()));
|
||||
}
|
||||
let target = self.space_index(id)?;
|
||||
self.move_contact(name, target)
|
||||
self.space_index(id)
|
||||
}
|
||||
|
||||
/// Where a new contact goes: the main space, or the hidden space of `password`.
|
||||
fn target_space(&mut self, space: Option<&str>) -> Result<usize> {
|
||||
match space {
|
||||
None => Ok(0),
|
||||
Some(pw) => self.hidden_space(pw),
|
||||
}
|
||||
}
|
||||
|
||||
/// Move a hidden contact back to the main space.
|
||||
@@ -572,7 +585,10 @@ impl MeowClient {
|
||||
}
|
||||
|
||||
// ───────────────────────── invitations ─────────────────────────
|
||||
// New contacts are created in the main space; hide them afterwards if needed.
|
||||
// `space`: `None` creates the contact in the main space; `Some(password)` creates it
|
||||
// directly in that hidden space (opened, or created if needed), so it never touches
|
||||
// the main space. Later steps find the contact in any *unlocked* space: a hidden
|
||||
// space must be unlocked for its pending invitations to progress.
|
||||
|
||||
/// Step 1: create an invitation for `contact_name`. Returns the encoded offer.
|
||||
pub async fn create_invitation(
|
||||
@@ -581,12 +597,14 @@ impl MeowClient {
|
||||
server_urls: &[String],
|
||||
message: &str,
|
||||
ttl: u64,
|
||||
space: Option<&str>,
|
||||
) -> Result<Vec<u8>> {
|
||||
self.ensure_free_name(contact_name)?;
|
||||
let servers = self.resolve_servers(server_urls)?;
|
||||
let offer = invitation::create_offer(contact_name, self.nickname(), servers, message, now(), ttl)?;
|
||||
self.spaces[0].state.contacts.push(offer.contact);
|
||||
self.save_space(0)?;
|
||||
let si = self.target_space(space)?;
|
||||
self.spaces[si].state.contacts.push(offer.contact);
|
||||
self.save_space(si)?;
|
||||
Ok(offer.offer_bytes)
|
||||
}
|
||||
|
||||
@@ -600,6 +618,7 @@ impl MeowClient {
|
||||
offer_bytes: &[u8],
|
||||
contact_name: Option<&str>,
|
||||
server_urls: &[String],
|
||||
space: Option<&str>,
|
||||
) -> Result<Vec<u8>> {
|
||||
let info = self.inspect_invitation(offer_bytes)?;
|
||||
let name = contact_name.unwrap_or(&info.initiator_name).to_string();
|
||||
@@ -609,8 +628,9 @@ impl MeowClient {
|
||||
}
|
||||
let servers = self.resolve_servers(server_urls)?;
|
||||
let (contact, answer) = invitation::answer(offer_bytes, &name, self.nickname(), servers, now())?;
|
||||
self.spaces[0].state.contacts.push(contact);
|
||||
self.save_space(0)?;
|
||||
let si = self.target_space(space)?;
|
||||
self.spaces[si].state.contacts.push(contact);
|
||||
self.save_space(si)?;
|
||||
Ok(answer.encode_to_vec())
|
||||
}
|
||||
|
||||
@@ -652,8 +672,11 @@ impl MeowClient {
|
||||
message: &str,
|
||||
password: &str,
|
||||
ttl: u64,
|
||||
space: Option<&str>,
|
||||
) -> Result<String> {
|
||||
let offer = self.create_invitation(contact_name, server_urls, message, ttl).await?;
|
||||
let offer = self
|
||||
.create_invitation(contact_name, server_urls, message, ttl, space)
|
||||
.await?;
|
||||
let server = self.resolve_servers(server_urls)?.remove(0);
|
||||
let resp = self
|
||||
.transport
|
||||
@@ -682,6 +705,7 @@ impl MeowClient {
|
||||
password: &str,
|
||||
contact_name: Option<&str>,
|
||||
server_urls: &[String],
|
||||
space: Option<&str>,
|
||||
) -> Result<String> {
|
||||
let (base, code) = url
|
||||
.rsplit_once("/inv/")
|
||||
@@ -702,7 +726,9 @@ impl MeowClient {
|
||||
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).await?;
|
||||
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(
|
||||
|
||||
@@ -92,9 +92,12 @@ async fn file_and_server_invitations_then_messaging() {
|
||||
let mut remote = client(&dir, "remote", &servers).await;
|
||||
|
||||
// ── file invitation ──
|
||||
let offer = bob.create_invitation("alice", &[], "hello alice", 3600).await.unwrap();
|
||||
let offer = bob
|
||||
.create_invitation("alice", &[], "hello alice", 3600, None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(alice.inspect_invitation(&offer).unwrap().initiator_name, "bob");
|
||||
let answer = alice.answer_invitation(&offer, None, &[]).await.unwrap();
|
||||
let answer = alice.answer_invitation(&offer, None, &[], None).await.unwrap();
|
||||
let confirm = bob.accept_answer(&answer).await.unwrap();
|
||||
assert_eq!(bob.contact("alice").unwrap().state, State::ConfirmSent);
|
||||
bob.send_text("alice", "sent before finalize").await.unwrap();
|
||||
@@ -143,15 +146,15 @@ async fn file_and_server_invitations_then_messaging() {
|
||||
|
||||
// ── server invitation ──
|
||||
let url = bob
|
||||
.publish_invitation("remote", &[], "via server", "secret", 3600)
|
||||
.publish_invitation("remote", &[], "via server", "secret", 3600, None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(remote
|
||||
.accept_published_invitation(&url, "wrong", None, &[])
|
||||
.accept_published_invitation(&url, "wrong", None, &[], None)
|
||||
.await
|
||||
.is_err());
|
||||
let who = remote
|
||||
.accept_published_invitation(&url, "secret", None, &[])
|
||||
.accept_published_invitation(&url, "secret", None, &[], None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(who, "bob");
|
||||
@@ -330,8 +333,8 @@ async fn no_message_lost_when_the_client_dies() {
|
||||
// This test counts what stays on the server: no receipts traffic.
|
||||
no_receipt_settings(&mut bob).await;
|
||||
no_receipt_settings(&mut alice).await;
|
||||
let offer = bob.create_invitation("alice", &[], "", 3600).await.unwrap();
|
||||
let answer = alice.answer_invitation(&offer, None, &[]).await.unwrap();
|
||||
let offer = bob.create_invitation("alice", &[], "", 3600, None).await.unwrap();
|
||||
let answer = alice.answer_invitation(&offer, None, &[], None).await.unwrap();
|
||||
let confirm = bob.accept_answer(&answer).await.unwrap();
|
||||
alice.finalize_invitation(&confirm).await.unwrap();
|
||||
bob.sync(Duration::ZERO).await.unwrap();
|
||||
@@ -370,8 +373,11 @@ async fn no_message_lost_when_the_client_dies() {
|
||||
// the temporary answer mailbox of a server invitation...
|
||||
let mut carol = client(&dir, "carol", &servers).await;
|
||||
no_receipt_settings(&mut carol).await;
|
||||
let link = bob.publish_invitation("carol", &[], "", "", 3600).await.unwrap();
|
||||
carol.accept_published_invitation(&link, "", None, &[]).await.unwrap();
|
||||
let link = bob.publish_invitation("carol", &[], "", "", 3600, None).await.unwrap();
|
||||
carol
|
||||
.accept_published_invitation(&link, "", None, &[], None)
|
||||
.await
|
||||
.unwrap();
|
||||
bob.sync(Duration::ZERO).await.unwrap(); // step 2 → 3, answer mailbox retired
|
||||
carol.sync(Duration::ZERO).await.unwrap(); // step 3 → 4
|
||||
bob.sync(Duration::ZERO).await.unwrap(); // ack
|
||||
@@ -395,8 +401,8 @@ async fn no_message_lost_when_the_client_dies() {
|
||||
|
||||
/// File invitation from `a` to `b`, completed.
|
||||
async fn connect(a: &mut MeowClient, b: &mut MeowClient, b_name: &str) {
|
||||
let offer = a.create_invitation(b_name, &[], "", 3600).await.unwrap();
|
||||
let answer = b.answer_invitation(&offer, None, &[]).await.unwrap();
|
||||
let offer = a.create_invitation(b_name, &[], "", 3600, None).await.unwrap();
|
||||
let answer = b.answer_invitation(&offer, None, &[], None).await.unwrap();
|
||||
let confirm = a.accept_answer(&answer).await.unwrap();
|
||||
b.finalize_invitation(&confirm).await.unwrap();
|
||||
a.sync(Duration::ZERO).await.unwrap();
|
||||
@@ -707,3 +713,61 @@ async fn background_sync_publishes_events() {
|
||||
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["after stop"]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
// ───────────── invitations straight into hidden spaces (roadmap lot 5.10) ─────────────
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn invitations_straight_into_hidden_spaces() {
|
||||
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;
|
||||
|
||||
// Server flow, hidden on both sides.
|
||||
let url = alice
|
||||
.publish_invitation("bob", &[], "", "", 3600, Some("a-secret"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(alice.is_hidden("bob").unwrap());
|
||||
assert_eq!(
|
||||
bob.accept_published_invitation(&url, "", None, &[], Some("b-secret"))
|
||||
.await
|
||||
.unwrap(),
|
||||
"alice"
|
||||
);
|
||||
assert!(bob.is_hidden("alice").unwrap());
|
||||
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();
|
||||
bob.send_text("alice", "secret hello").await.unwrap();
|
||||
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["secret hello"]);
|
||||
|
||||
// File flow, hidden on the answering side only.
|
||||
let mut carol = client(&dir, "carol", &servers).await;
|
||||
let offer = alice.create_invitation("carol", &[], "", 3600, None).await.unwrap();
|
||||
carol
|
||||
.answer_invitation(&offer, None, &[], Some("c-secret"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(carol.is_hidden("alice").unwrap());
|
||||
|
||||
// Reopened without the space passwords: nothing; with them: the contacts.
|
||||
drop((alice, bob, carol));
|
||||
let path = |name: &str| dir.join(format!("{name}.mwid2"));
|
||||
let mut alice = MeowClient::open(&path("alice"), "pw").await.unwrap();
|
||||
let mut bob = MeowClient::open(&path("bob"), "pw").await.unwrap();
|
||||
let carol = MeowClient::open(&path("carol"), "pw").await.unwrap();
|
||||
assert_eq!(names(&alice), vec!["carol"]);
|
||||
assert!(names(&bob).is_empty());
|
||||
assert!(names(&carol).is_empty());
|
||||
assert!(alice.unlock("a-secret").await.unwrap());
|
||||
assert!(bob.unlock("b-secret").await.unwrap());
|
||||
assert_eq!(alice.contact("bob").unwrap().state, State::Active);
|
||||
assert_eq!(alice.history("bob").unwrap().len(), 1);
|
||||
alice.send_text("bob", "still secret").await.unwrap();
|
||||
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["still secret"]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
Reference in new issue
Block a user