chiark / gitweb /
UNFINISHED examine
authorSimon Tatham <anakin@pobox.com>
Mon, 1 Jan 2024 10:11:07 +0000 (10:11 +0000)
committerSimon Tatham <anakin@pobox.com>
Mon, 1 Jan 2024 10:11:07 +0000 (10:11 +0000)
src/activity_stack.rs
src/client.rs
src/editor.rs
src/file.rs
src/tui.rs

index 0d9b724a73f299e3626e09f281c60a22383bfb6f..0a78da14ccd68964320e99910e1acea2df247e04 100644 (file)
@@ -14,7 +14,7 @@ pub enum UtilityActivity {
     LogsMenu2,
     EgoLog,
     ExitMenu,
-    ExamineUser(String),
+    ExamineUser(String),        // the account _id_, not its name
     ListUserFollowers(String),
     ListUserFollowees(String),
     InfoStatus(String),
index a9b4e035c8ed2afcbbbbacf4003bc956b5e245e8..c80260e27a1d4f380f2f5cc9db4034fd5527770a 100644 (file)
@@ -595,4 +595,29 @@ impl Client {
 
         Ok(updates)
     }
+
+    pub fn account_by_name(&mut self, name: &str) ->
+        Result<Account, ClientError>
+    {
+        let (url, req) = self.api_request(
+            Req::get("accounts/lookup").param("acct", name))?;
+        let rsp = req.send()?;
+        let rspstatus = rsp.status();
+        let ac: Account = if !rspstatus.is_success() {
+            Err(ClientError::UrlError(url.clone(), rspstatus.to_string()))
+        } else {
+            match serde_json::from_str(&rsp.text()?) {
+                Ok(ac) => Ok(ac),
+                Err(e) => Err(ClientError::UrlError(
+                    url.clone(), e.to_string())),
+            }
+        }?;
+        if ac.acct != name && self.fq(&ac.acct) != name {
+            return Err(ClientError::UrlError(
+                url.clone(), format!("request returned wrong account name {}",
+                                     &ac.acct)));
+        }
+        self.cache_account(&ac);
+        Ok(ac)
+    }
 }
index fbb7ae5c4d77fbf9be3af312f67de244206e464d..0e00ee63d19dbd619b4a6332a9e368e506163560 100644 (file)
@@ -527,12 +527,12 @@ struct BottomLineEditorOverlay {
     prompt: ColouredString,
     promptwidth: usize,
     ed: SingleLineEditor,
-    result: Box<dyn Fn(&str) -> LogicalAction>,
+    result: Box<dyn Fn(&str, &mut Client) -> LogicalAction>,
 }
 
 impl BottomLineEditorOverlay {
-    fn new(prompt: ColouredString, result: Box<dyn Fn(&str) -> LogicalAction>)
-           -> Self
+    fn new(prompt: ColouredString,
+           result: Box<dyn Fn(&str, &mut Client) -> LogicalAction>) -> Self
     {
         let promptwidth = prompt.width();
         BottomLineEditorOverlay {
@@ -563,11 +563,11 @@ impl ActivityState for BottomLineEditorOverlay {
         (vec! { &self.prompt + buffer }, cursorpos)
     }
 
-    fn handle_keypress(&mut self, key: OurKey, _client: &mut Client) ->
+    fn handle_keypress(&mut self, key: OurKey, client: &mut Client) ->
         LogicalAction
     {
         if self.ed.handle_keypress(key) {
-            (self.result)(&self.ed.core.text)
+            (self.result)(&self.ed.core.text, client)
         } else {
             LogicalAction::Nothing
         }
@@ -577,12 +577,21 @@ impl ActivityState for BottomLineEditorOverlay {
 pub fn get_user_to_examine() -> Box<dyn ActivityState> {
     Box::new(BottomLineEditorOverlay::new(
         ColouredString::plain("Examine User: "),
-        Box::new(move |s| {
+        Box::new(move |s, client| {
+            let s = s.trim();
+            let s = s.strip_prefix("@").unwrap_or(s);
             if s.is_empty() {
                 LogicalAction::PopOverlaySilent
             } else {
-                LogicalAction::Goto(
-                    UtilityActivity::ExamineUser(s.to_owned()).into())
+                match dbg!(client.account_by_name(s)) {
+                    Ok(account) => LogicalAction::Goto(
+                        UtilityActivity::ExamineUser(account.id).into()),
+
+                    // FIXME: it would be nice to discriminate errors
+                    // better here, and maybe return anything worse
+                    // than 'user not found' to the Error Log
+                    Err(_) => LogicalAction::PopOverlayBeep,
+                }
             }
         })
     ))
index a93aa9fb428ce0fe91344808c7e605928f54829e..cf38202f5a7a33cdd78f9eae9150baa4f2331f46 100644 (file)
@@ -63,6 +63,20 @@ impl FileDataSource for FeedSource {
     fn extendable(&self) -> bool { true }
 }
 
+struct SingletonSource {}
+
+impl FileDataSource for SingletonSource {
+    fn get(&self, _client: &mut Client) -> (Vec<String>, isize) {
+        (vec! { "".to_owned() }, 0)
+    }
+    fn init(&self, _client: &mut Client) -> Result<(), ClientError> { Ok(()) }
+    fn try_extend(&self, _client: &mut Client) -> Result<bool, ClientError> {
+        Ok(false)
+    }
+    fn updated(&self, _feeds_updated: &HashSet<FeedId>) -> bool { false }
+    fn extendable(&self) -> bool { false }
+}
+
 trait FileType {
     type Item: TextFragment + Sized;
 
@@ -108,6 +122,17 @@ impl FileType for EgoNotificationFeedType {
     }
 }
 
+struct ExamineUserFileType {}
+impl FileType for ExamineUserFileType {
+    type Item = EditorHeaderSeparator; // FIXME
+
+    fn get_from_client(_id: &str, _client: &mut Client) ->
+        Result<Self::Item, ClientError>
+    {
+        Ok(EditorHeaderSeparator::new())
+    }
+}
+
 struct FileContents<Type: FileType, Source: FileDataSource> {
     source: Source,
     header: FileHeader,
@@ -602,3 +627,11 @@ pub fn ego_log(client: &mut Client) ->
             "HHHHHHHHHHHKKKHHKHHKHHKH"))?;
     Ok(Box::new(file))
 }
+
+pub fn examine_user(client: &mut Client, account_id: &str) ->
+    Result<Box<dyn ActivityState>, ClientError>
+{
+    let file = File::<ExamineUserFileType, _>::new(
+        client, SingletonSource{}, ColouredString::uniform(account_id, '!'))?;
+    Ok(Box::new(file))
+}
index eae9a5c66bdcd3469a66842b2643658fe35b40e2..c63c5b1230b227d618226048d76612ef85ba51da 100644 (file)
@@ -443,6 +443,8 @@ fn new_activity_state(activity: Activity, client: &mut Client) ->
             ego_log(client),
         Activity::Overlay(OverlayActivity::GetUserToExamine) =>
             Ok(get_user_to_examine()),
+        Activity::Util(UtilityActivity::ExamineUser(ref name)) =>
+            examine_user(client, name),
         _ => todo!(),
     };