Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions crates/chatd/src/adapters/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,17 @@ impl Drive for LiveDrive {
}
}

/// Said when sign-out cannot reach the phone, so this computer may stay in its paired-device list.
/// Said when a sign-out could not ask the phone to drop this computer: it cannot reach the
/// phone, or the pairing has nothing to unpair with.
pub const STILL_LISTED: &str =
"This computer may still be listed under Device pairing on your phone. Remove it there.";

/// The answer to a sign-out that could not ask the phone to drop this computer. Why is for the
/// log: the person is signing out, and the one thing they can do about it is on the phone.
fn still_listed() -> Refusal {
Refusal::Refused(STILL_LISTED.to_owned())
}

fn parse(session: &str) -> Result<Saved, String> {
let saved: Saved = serde_json::from_str(session)
.map_err(|_| "the Messages session is unreadable".to_owned())?;
Expand Down Expand Up @@ -937,7 +944,7 @@ pub fn start(ctx: Context, session: &str, inbox: Receiver<Command>) -> Result<()
fake::record(STILL_LISTED);
}
tracing::warn!("chatd: Messages: {STILL_LISTED}");
let _ = reply.send(Err(Refusal::Refused(STILL_LISTED.to_owned())));
let _ = reply.send(Err(still_listed()));
}
(None, command) => offline(&ctx, command),
},
Expand Down Expand Up @@ -2757,11 +2764,14 @@ fn serve(account: &Arc<Account>, command: Command) {
let _ = reply.send(url.ok_or(Refusal::Unsupported));
}
Command::SignOut(reply) => {
if let Err(why) = account.drive.unpair() {
// Signed out here whatever the phone said. When it could not be asked to drop this
// computer, the answer says so for the person: the phone may still list it.
let unpaired = account.drive.unpair().map_err(|why| {
tracing::warn!("chatd: Messages: {why}");
}
still_listed()
});
account.drive.close();
let _ = reply.send(Ok(()));
let _ = reply.send(unpaired);
}
Command::SetMute {
conversation_id,
Expand Down
17 changes: 13 additions & 4 deletions crates/chatd/src/adapters/messages/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
//! `revoke`, `revoke-offline`, `auth`.
//! `archive` files the chat as archived. `spam` files it as spam. `again` repeats the
//! chat with a new title and no status, which must keep the folder it already had.
//! `silent` makes the phone stop answering sends, history, and files. `answer` clears that
//! and tells the adapter the phone is back.
//! `silent` makes the phone stop answering sends, history, files and an unpair. `answer` clears
//! that and tells the adapter the phone is back.
//! `hold` pauses a file upload until `go`, so a cancel can land while it is still unsent.
//! WHO is `grace` or `alan`. `status` reports that code on the last text this computer sent.

use super::{Drive, Event, Saved};
use super::{Drive, Event, Saved, STILL_LISTED};
use crate::adapters::messages::bugle::{Chat, ContactEntry, Mark, MediaPart, Note, Participant, Piece, MESSAGE_PAGE, PHONE_QUIET, UNPAIRED};
use crate::adapters::script;
use std::io::Write;
Expand Down Expand Up @@ -71,10 +71,12 @@ pub struct Fake {
last: Arc<Mutex<Option<SentCopy>>>,
/// A scripted upload waits while this is set (`hold` / `go` on `messages.ctl`).
hold: Arc<AtomicBool>,
/// The saved session's pairing attempt, empty for a QR pairing.
pairing_id: String,
}

impl Fake {
pub fn open(_saved: &Saved) -> Fake {
pub fn open(saved: &Saved) -> Fake {
let revoked = protocol::support_dir().join("messages.revoked");
if revoked.is_file() {
let _ = std::fs::remove_file(&revoked);
Expand All @@ -90,6 +92,7 @@ impl Fake {
sent: AtomicU32::new(0),
last: Arc::default(),
hold: Arc::default(),
pairing_id: saved.pairing_id.clone(),
};
}
let (events, heard) = channel();
Expand Down Expand Up @@ -177,6 +180,7 @@ impl Fake {
sent: AtomicU32::new(0),
last: Arc::default(),
hold: Arc::default(),
pairing_id: saved.pairing_id.clone(),
};
let (events, closed, refuse, quiet, last, hold) = (
fake.events.clone(),
Expand Down Expand Up @@ -1377,7 +1381,12 @@ impl Drive for Fake {
record(&format!("delete-chat {conversation}"));
Ok(())
}
/// The real drive's rule: a QR pairing has no attempt to revoke, so nothing is asked.
fn unpair(&self) -> Result<(), String> {
if self.pairing_id.is_empty() {
return Err(STILL_LISTED.to_owned());
}
self.unanswered()?;
record("unpair");
Ok(())
}
Expand Down
15 changes: 13 additions & 2 deletions crates/chatd/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,11 @@ impl Chat {
}

fn status(&self) -> Response {
self.status_saying("")
}

/// The status, with `notice` as the one thing to tell the person about the request it answers.
fn status_saying(&self, notice: &str) -> Response {
let (call, running) = self.hub.calls.describe();
let links = self.hub.links();
let keychain = self.hub.keychain();
Expand Down Expand Up @@ -907,6 +912,7 @@ impl Chat {
keychain,
started: self.hub.started,
session: protocol::log_policy::session_id().to_owned(),
notice: notice.to_owned(),
}
}

Expand Down Expand Up @@ -4151,11 +4157,16 @@ impl Chat {
// Taken out of the map first, so nothing else is asked of it; kept until it has had
// its time to end the session on the network, since dropping it stops its threads.
let adapter = lock(&self.adapters).remove(&account);
// What the network would not do, in words for the person. The account is signed out
// either way, so it rides on the answer instead of failing the request.
let mut notice = String::new();
if let Some(adapter) = adapter {
let (reply, done) = channel();
adapter.command(Command::SignOut(reply));
// Answered, or dropped unanswered by an adapter with nothing to end, or too slow.
let _ = done.recv_timeout(SIGN_OUT);
if let Ok(Err(Refusal::Refused(words))) = done.recv_timeout(SIGN_OUT) {
notice = words;
}
}
let stored = match session::read(account.network) {
session::Stored::Found(session) => Some(session),
Expand Down Expand Up @@ -4188,7 +4199,7 @@ impl Chat {
self.forget(&account);
self.hub.set_link(&account, Link::SignedOut, "");
self.hub.changed(Change::Status);
self.status()
self.status_saying(&notice)
}

/// A device transfer moved this account to another device: it is forgotten here without
Expand Down
88 changes: 57 additions & 31 deletions crates/chatd/tests/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const GRACE: &str = "phone-grace";
const ALAN: &str = "phone-alan";
const CLUB: &str = "phone-club";
const SESSION: &str = r#"{"account":"phone-ada","name":"Ada Quill"}"#;
/// The same account paired through its Google account, which names a pairing attempt to revoke.
/// `SESSION` alone is a QR pairing, which has none.
const GOOGLE_SESSION: &str = r#"{"account":"phone-ada","name":"Ada Quill","pairing_id":"attempt-1"}"#;
/// What a sign-out says when the phone was not asked to drop this computer.
const STILL_LISTED: &str = "This computer may still be listed under Device pairing on your phone. Remove it there.";

fn key(id: &str) -> Key {
Key {
Expand All @@ -46,6 +51,11 @@ impl World {
World::start(Some(SESSION))
}

/// Paired through the Google account, so a sign-out has a pairing attempt to revoke.
fn google() -> World {
World::start(Some(GOOGLE_SESSION))
}

/// A scripted conversation of 100 messages, so a scroll can ask for the pages under the newest.
fn many() -> World {
World::open(Some(SESSION), true, false)
Expand Down Expand Up @@ -304,6 +314,26 @@ impl World {
}
}

/// Signs the account out and answers with what the answer tells the person: empty when the
/// phone was asked to drop this computer, a sentence when it was not.
fn sign_out(&self) -> String {
match self.ask(Request::SignOut {
account: account(),
erase: false,
}) {
Response::Status { accounts, notice, .. } => {
assert!(
accounts
.iter()
.all(|status| status.key != account() || status.link != Link::Connected),
"{accounts:?}"
);
notice
}
other => panic!("{other:?}"),
}
}

fn changed<T>(
&self,
mut since: u64,
Expand Down Expand Up @@ -1489,22 +1519,10 @@ fn three_texts_count_as_three_and_a_chat_read_on_the_phone_clears() {

#[test]
fn signing_out_asks_the_phone_to_drop_this_computer_once() {
let world = World::linked();
let world = World::google();
world.connected();
match world.ask(Request::SignOut {
account: account(),
erase: false,
}) {
Response::Status { accounts, .. } => {
assert!(
accounts
.iter()
.all(|status| status.key != account() || status.link != Link::Connected),
"{accounts:?}"
);
}
other => panic!("{other:?}"),
}
// Asked and answered: there is nothing to tell the person.
assert_eq!(world.sign_out(), "");
world.heard("unpair");
assert_eq!(
world.calls().iter().filter(|call| *call == "unpair").count(),
Expand All @@ -1517,25 +1535,33 @@ fn signing_out_offline_says_the_phone_may_still_list_this_computer() {
let world = World::offline_phone();
// The socket answers before the account's threads exist. Wait until the phone is unreachable.
world.heard("offline");
match world.ask(Request::SignOut {
account: account(),
erase: false,
}) {
Response::Status { accounts, .. } => {
assert!(
accounts
.iter()
.all(|status| status.key != account() || status.link != Link::Connected),
"{accounts:?}"
);
}
other => panic!("{other:?}"),
}
world.heard(
"This computer may still be listed under Device pairing on your phone. Remove it there.",
assert_eq!(world.sign_out(), STILL_LISTED);
world.heard(STILL_LISTED);
}

#[test]
fn signing_out_a_qr_pairing_says_the_phone_may_still_list_this_computer() {
// Paired by QR code: there is no pairing attempt to revoke, so the phone is never asked.
let world = World::linked();
world.connected();
assert_eq!(world.sign_out(), STILL_LISTED);
assert!(
!world.calls().iter().any(|call| call == "unpair"),
"the phone was asked: {:#?}",
world.calls()
);
}

#[test]
fn signing_out_to_a_phone_that_does_not_answer_says_the_phone_may_still_list_this_computer() {
let world = World::google();
world.connected();
// The phone stops answering, as one that is off or out of reach does.
world.steer("silent");
world.heard("silent");
assert_eq!(world.sign_out(), STILL_LISTED);
}

#[test]
fn a_video_is_a_video_and_a_voice_note_has_a_waveform() {
chatd::adapters::messages::prove_message_media().expect("a video and a voice note");
Expand Down
19 changes: 19 additions & 0 deletions crates/protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3303,6 +3303,8 @@ pub enum Request {
/// Forgets the account's session and everything stored for it. Waits up to 5 s for the
/// network to end the session first. A Tiny server's encrypted history stays on this device
/// (sealed, its key in the keychain) for a later sign-in there, unless `erase` is set.
/// The account is signed out whatever the network said; when it could not be told, the
/// answer's `notice` says so.
SignOut {
account: AccountKey,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
Expand Down Expand Up @@ -4962,6 +4964,11 @@ pub enum Response {
/// Shared by the window, chatd, and mediad for this run. Empty on an older chatd.
#[serde(default, skip_serializing_if = "String::is_empty")]
session: String,
/// Something the person should know about the request this answers, in words. A sign-out
/// sets it when the network could not be told to end the session, so it may still list
/// this device. Empty on every other answer.
#[serde(default, skip_serializing_if = "String::is_empty")]
notice: String,
},
Changes {
/// Pass this to the next `Wait`.
Expand Down Expand Up @@ -5802,6 +5809,18 @@ mod tests {
assert_eq!(serde_json::to_value(pending).unwrap()["pending"], true);
}

#[test]
fn a_status_notice_is_explicit_and_old_status_answers_still_decode() {
let mut value = serde_json::json!({"op": "status", "networks": [], "accounts": [], "media": "stopped", "call": null, "seq": 3});
let quiet: super::Response = serde_json::from_value(value.clone()).unwrap();
assert!(matches!(quiet, super::Response::Status { ref notice, .. } if notice.is_empty()));
assert!(serde_json::to_value(quiet).unwrap().get("notice").is_none());
value["notice"] = serde_json::json!("Remove it there.");
let said: super::Response = serde_json::from_value(value).unwrap();
assert!(matches!(said, super::Response::Status { ref notice, .. } if notice.as_str() == "Remove it there."));
assert_eq!(serde_json::to_value(said).unwrap()["notice"], "Remove it there.");
}

use super::*;

fn row(kind: ConversationKind, unread: u32, mentions: u32, muted: bool) -> Conversation {
Expand Down
20 changes: 19 additions & 1 deletion crates/tinyclient/src/gui/settings/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl SettingsWindow {
};
let shell = shell.read(cx);
let accounts = shell.snapshot.as_ref().map(|s| s.accounts.clone()).unwrap_or_default();
let (signing_in, failed) = (shell.signing_in, shell.sign_in_failed.clone());
let (signing_in, failed, said) = (shell.signing_in, shell.sign_in_failed.clone(), shell.sign_out_said.clone());
let mut tiny_line = None;
let mut pane = div().flex().flex_col().gap(px(20.)).child(self.devices_card(cx));
for network in NETWORK_ORDER {
Expand Down Expand Up @@ -360,6 +360,18 @@ impl SettingsWindow {
.children(failed.as_ref().filter(|(at, _)| *at == network).map(|(_, words)| {
div().pt(px(6.)).px(px(super::controls::side())).type_small().text_color(theme::danger()).child(words.clone())
}))
// The account is gone, but the network could not be told: chatd's words stay
// here until dismissed (08 §6).
.children(said.iter().find(|(at, _)| *at == network).map(|(_, words)| {
div()
.pt(px(6.))
.px(px(super::controls::side()))
.flex()
.items_start()
.gap(px(8.))
.child(div().flex_1().min_w_0().type_small().text_color(theme::text()).child(words.clone()))
.child(button(("sign-out-said", network as usize), ButtonKind::Ghost, "Dismiss").on_click(cx.listener(move |this, _, _, cx| this.dismiss_sign_out_said(network, cx))))
}))
.children(tiny_line.take()),
);
}
Expand Down Expand Up @@ -1549,6 +1561,12 @@ impl SettingsWindow {
cx.notify();
}

/// Puts away the line a sign-out left under `network`.
fn dismiss_sign_out_said(&mut self, network: Network, cx: &mut Context<Self>) {
let _ = self.shell.update(cx, |shell, cx| shell.dismiss_sign_out_said(network, cx));
cx.notify();
}

/// The sign-out question (§6), over the whole window.
pub(super) fn sign_out_modal(&self, account: &AccountStatus, cx: &mut Context<Self>) -> gpui::AnyElement {
let network = network_name(account.key.network);
Expand Down
1 change: 1 addition & 0 deletions crates/tinyclient/src/gui/shot_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,7 @@ pub fn answer(request: &Request) -> std::io::Result<Response> {
started: 1,
// No session: a shot starts nothing that would share it.
session: String::new(),
notice: String::new(),
},
// Nothing ever changes: the wait runs out and is asked again, as a quiet chatd's does.
// The loopback call changes by itself (its picture, its clock): say so a few times a second
Expand Down
Loading
Loading