This commit is contained in:
@@ -61,6 +61,7 @@ GET /health
|
||||
GET /v1/tasks?date=YYYY-MM-DD
|
||||
GET /v1/tasks/archived
|
||||
POST /v1/tasks
|
||||
PUT /v1/tasks/{id}
|
||||
POST /v1/tasks/{id}/complete
|
||||
POST /v1/tasks/{id}/archive
|
||||
POST /v1/tasks/{id}/restore
|
||||
|
||||
@@ -126,6 +126,9 @@ fn handle_task_action(mut request: Request, store: &Store, path: &str) {
|
||||
};
|
||||
|
||||
let changed = match (request.method(), segments.get(3).copied()) {
|
||||
(&Method::Put, None) if segments.len() == 3 => {
|
||||
parse_json::<CreateTask>(&mut request).and_then(|input| store.update_task(id, input))
|
||||
}
|
||||
(&Method::Post, Some("complete")) if segments.len() == 4 => store.complete_task(id),
|
||||
(&Method::Post, Some("archive")) if segments.len() == 4 => store.archive_task(id),
|
||||
(&Method::Post, Some("restore")) if segments.len() == 4 => store.restore_task(id),
|
||||
|
||||
@@ -195,6 +195,46 @@ impl Store {
|
||||
Ok(row.get(0))
|
||||
}
|
||||
|
||||
pub fn update_task(&self, id: i64, task: CreateTask) -> Result<bool> {
|
||||
validate_public_title(&task.public_title)?;
|
||||
validate_encrypted_fields(
|
||||
task.encrypted_payload.as_deref(),
|
||||
task.encryption_version,
|
||||
task.content_id.as_deref(),
|
||||
)?;
|
||||
let scheduled_date = parse_date(&task.scheduled_date)?;
|
||||
let reminder_time = parse_reminder(task.reminder_time.as_deref())?;
|
||||
|
||||
let mut client = self.connect()?;
|
||||
client
|
||||
.execute(
|
||||
"UPDATE tasks
|
||||
SET public_title = $1,
|
||||
encrypted_payload = $2,
|
||||
encryption_version = $3,
|
||||
content_id = $4,
|
||||
section = $5,
|
||||
priority = $6,
|
||||
scheduled_date = $7,
|
||||
reminder_time = $8,
|
||||
notification_sent_at = NULL
|
||||
WHERE id = $9 AND status != 'archived'",
|
||||
&[
|
||||
&task.public_title.trim(),
|
||||
&task.encrypted_payload,
|
||||
&task.encryption_version,
|
||||
&task.content_id,
|
||||
&task.section.as_str(),
|
||||
&task.priority.as_str(),
|
||||
&scheduled_date,
|
||||
&reminder_time,
|
||||
&id,
|
||||
],
|
||||
)
|
||||
.map(|changed| changed == 1)
|
||||
.map_err(db_error)
|
||||
}
|
||||
|
||||
pub fn list_tasks(&self, date: &str) -> Result<Vec<Task>> {
|
||||
let date = parse_date(date)?;
|
||||
let mut client = self.connect()?;
|
||||
|
||||
@@ -46,7 +46,8 @@ Keys:
|
||||
- `h`/`l`: previous/next day; `t`: today.
|
||||
- `j`/`k`: move selection; `r`: refresh; `q`: quit.
|
||||
- `a`: add a task or routine; `Ctrl+S`: save a form; `Esc`: cancel.
|
||||
- On Tasks, `Space` completes, `s` reschedules, and `x` archives after confirmation.
|
||||
- On Tasks, `e` edits all task fields, `Space` completes, `s` reschedules, and
|
||||
`x` archives after confirmation.
|
||||
- On Archive, `u` restores to the original day and section; `x` permanently
|
||||
deletes after confirmation.
|
||||
- On Devices, `a` registers a public age recipient and `x` revokes it after
|
||||
|
||||
@@ -51,6 +51,17 @@ impl ApiClient {
|
||||
self.send_json("/v1/tasks", task)
|
||||
}
|
||||
|
||||
pub fn update_task(&self, id: i64, task: &CreateTask) -> Result<(), String> {
|
||||
let body = serde_json::to_string(task).map_err(|error| error.to_string())?;
|
||||
let response = self
|
||||
.authorize(minreq::put(self.url(&format!("/v1/tasks/{id}"))))
|
||||
.with_header("Content-Type", "application/json")
|
||||
.with_body(body)
|
||||
.send()
|
||||
.map_err(transport_error)?;
|
||||
check_status(response)
|
||||
}
|
||||
|
||||
pub fn create_routine(&self, routine: &CreateRoutine) -> Result<(), String> {
|
||||
self.send_json("/v1/routines", routine)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ pub enum View {
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum FormKind {
|
||||
Task,
|
||||
EditTask { task_id: i64 },
|
||||
Routine,
|
||||
Reschedule { task_id: i64 },
|
||||
Device,
|
||||
@@ -186,6 +187,7 @@ impl App {
|
||||
KeyCode::Char('u') if self.view == View::Archive => self.restore_selected(),
|
||||
KeyCode::Char('x') if self.view == View::Devices => self.ask_revoke_device(),
|
||||
KeyCode::Char('s') if self.view == View::Tasks => self.open_reschedule_form(),
|
||||
KeyCode::Char('e') if self.view == View::Tasks => self.open_edit_task_form(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -256,6 +258,7 @@ impl App {
|
||||
let form = self.form.take().expect("form is present");
|
||||
let result = match form.kind {
|
||||
FormKind::Task => self.submit_task(&form),
|
||||
FormKind::EditTask { task_id } => self.submit_edit_task(task_id, &form),
|
||||
FormKind::Routine => self.submit_routine(&form),
|
||||
FormKind::Reschedule { task_id } => {
|
||||
self.api.reschedule_task(task_id, form.fields[0].display())
|
||||
@@ -275,13 +278,24 @@ impl App {
|
||||
}
|
||||
|
||||
fn submit_task(&self, form: &Form) -> Result<(), String> {
|
||||
let task = self.task_input(form, self.date.to_string())?;
|
||||
self.api.create_task(&task)
|
||||
}
|
||||
|
||||
fn submit_edit_task(&self, task_id: i64, form: &Form) -> Result<(), String> {
|
||||
let date = required(form.fields[6].display(), "date")?;
|
||||
let task = self.task_input(form, date)?;
|
||||
self.api.update_task(task_id, &task)
|
||||
}
|
||||
|
||||
fn task_input(&self, form: &Form, scheduled_date: String) -> Result<CreateTask, String> {
|
||||
let private_title = optional(form.fields[1].display());
|
||||
let notes = optional(form.fields[2].display());
|
||||
let recipients = self.recipients_for(&private_title, ¬es)?;
|
||||
let encrypted = self
|
||||
.crypto
|
||||
.encrypt_optional(private_title, notes, &recipients)?;
|
||||
self.api.create_task(&CreateTask {
|
||||
Ok(CreateTask {
|
||||
public_title: required(form.fields[0].display(), "public title")?,
|
||||
encrypted_payload: encrypted.encrypted_payload,
|
||||
encryption_version: encrypted.encryption_version,
|
||||
@@ -289,7 +303,7 @@ impl App {
|
||||
section: form.fields[3].display().to_owned(),
|
||||
priority: form.fields[4].display().to_owned(),
|
||||
reminder_time: optional(form.fields[5].display()),
|
||||
scheduled_date: self.date.to_string(),
|
||||
scheduled_date,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -357,6 +371,18 @@ impl App {
|
||||
});
|
||||
}
|
||||
|
||||
fn open_edit_task_form(&mut self) {
|
||||
let Some(task) = self.tasks.get(self.selected) else {
|
||||
self.status = "no task selected".to_owned();
|
||||
return;
|
||||
};
|
||||
if task.encrypted_payload.is_some() && task.decryption_failed {
|
||||
self.status = "cannot edit task: private fields could not be decrypted".to_owned();
|
||||
return;
|
||||
}
|
||||
self.form = Some(edit_task_form(task));
|
||||
}
|
||||
|
||||
fn complete_selected(&mut self) {
|
||||
let Some(task) = self.tasks.get(self.selected) else {
|
||||
self.status = "no task selected".to_owned();
|
||||
@@ -468,6 +494,38 @@ fn task_form() -> Form {
|
||||
}
|
||||
}
|
||||
|
||||
fn edit_task_form(task: &Task) -> Form {
|
||||
Form {
|
||||
kind: FormKind::EditTask { task_id: task.id },
|
||||
fields: vec![
|
||||
text_field(
|
||||
"Public title",
|
||||
"required; Telegram uses this",
|
||||
task.public_title.clone(),
|
||||
),
|
||||
text_field(
|
||||
"Private title",
|
||||
"optional; encrypted",
|
||||
task.private_title.clone().unwrap_or_default(),
|
||||
),
|
||||
text_field(
|
||||
"Notes",
|
||||
"optional; encrypted",
|
||||
task.private_notes.clone().unwrap_or_default(),
|
||||
),
|
||||
choice_field_selected("Section", &["morning", "work", "evening"], &task.section),
|
||||
choice_field_selected("Priority", &["must", "should", "optional"], &task.priority),
|
||||
text_field(
|
||||
"Reminder",
|
||||
"optional HH:MM",
|
||||
task.reminder_time.clone().unwrap_or_default(),
|
||||
),
|
||||
text_field("Date", "YYYY-MM-DD", task.scheduled_date.clone()),
|
||||
],
|
||||
active: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn routine_form() -> Form {
|
||||
Form {
|
||||
kind: FormKind::Routine,
|
||||
@@ -519,6 +577,24 @@ fn choice_field(label: &'static str, options: &'static [&'static str]) -> Field
|
||||
}
|
||||
}
|
||||
|
||||
fn choice_field_selected(
|
||||
label: &'static str,
|
||||
options: &'static [&'static str],
|
||||
value: &str,
|
||||
) -> Field {
|
||||
Field {
|
||||
label,
|
||||
hint: "←/→",
|
||||
value: FieldValue::Choice {
|
||||
options,
|
||||
selected: options
|
||||
.iter()
|
||||
.position(|option| *option == value)
|
||||
.unwrap_or(0),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn cycle_choice(field: &mut Field, forward: bool) {
|
||||
if let FieldValue::Choice { options, selected } = &mut field.value {
|
||||
*selected = if forward {
|
||||
@@ -651,6 +727,52 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edit_form_is_prefilled_from_decrypted_task() {
|
||||
let mut app = App::new(
|
||||
ApiClient::new("http://127.0.0.1:1".to_owned(), None),
|
||||
Crypto::disabled(),
|
||||
);
|
||||
let mut existing = task();
|
||||
existing.public_title = "Public".to_owned();
|
||||
existing.private_title = Some("Private".to_owned());
|
||||
existing.private_notes = Some("Notes".to_owned());
|
||||
existing.section = "evening".to_owned();
|
||||
existing.priority = "optional".to_owned();
|
||||
existing.reminder_time = Some("18:30".to_owned());
|
||||
app.tasks.push(existing);
|
||||
|
||||
app.handle_key(KeyEvent::new(KeyCode::Char('e'), KeyModifiers::NONE));
|
||||
let form = app.form.as_ref().unwrap();
|
||||
assert!(matches!(form.kind, FormKind::EditTask { task_id: 42 }));
|
||||
assert_eq!(form.fields[0].display(), "Public");
|
||||
assert_eq!(form.fields[1].display(), "Private");
|
||||
assert_eq!(form.fields[2].display(), "Notes");
|
||||
assert_eq!(form.fields[3].display(), "evening");
|
||||
assert_eq!(form.fields[4].display(), "optional");
|
||||
assert_eq!(form.fields[5].display(), "18:30");
|
||||
assert_eq!(form.fields[6].display(), "2026-10-15");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edit_is_blocked_when_private_payload_cannot_be_decrypted() {
|
||||
let mut app = App::new(
|
||||
ApiClient::new("http://127.0.0.1:1".to_owned(), None),
|
||||
Crypto::disabled(),
|
||||
);
|
||||
let mut existing = task();
|
||||
existing.encrypted_payload = Some("ciphertext".to_owned());
|
||||
existing.decryption_failed = true;
|
||||
app.tasks.push(existing);
|
||||
|
||||
app.handle_key(KeyEvent::new(KeyCode::Char('e'), KeyModifiers::NONE));
|
||||
assert!(app.form.is_none());
|
||||
assert_eq!(
|
||||
app.status,
|
||||
"cannot edit task: private fields could not be decrypted"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn active_device_requires_confirmation_before_revocation() {
|
||||
let mut app = App::new(
|
||||
|
||||
@@ -187,7 +187,7 @@ pub fn render(frame: &mut Frame, app: &App) {
|
||||
|
||||
let help = match app.view {
|
||||
View::Tasks => {
|
||||
"q quit Tab routines h/l day t today j/k move a add Space complete s move x archive r refresh"
|
||||
"q quit Tab routines h/l day t today j/k move a add e edit Space complete s move x archive r refresh"
|
||||
}
|
||||
View::Routines => "q quit Tab tasks j/k move a add routine r refresh",
|
||||
View::Archive => "q quit Tab tasks j/k move u restore x delete permanently r refresh",
|
||||
@@ -207,6 +207,7 @@ pub fn render(frame: &mut Frame, app: &App) {
|
||||
frame.render_widget(Clear, area);
|
||||
let title = match form.kind {
|
||||
FormKind::Task => format!("New task for {}", app.date),
|
||||
FormKind::EditTask { .. } => "Edit task".to_owned(),
|
||||
FormKind::Routine => "New routine".to_owned(),
|
||||
FormKind::Reschedule { .. } => "Reschedule task".to_owned(),
|
||||
FormKind::Device => "Register encryption device".to_owned(),
|
||||
|
||||
Reference in New Issue
Block a user