forge
Explore
Log in

Don't you dare go Hollow

What happens on a task is an event

Browse files at this commit

09816e867c4e00d5f317d092b8545bfac7c823b2Ahmet Kaan GümüşParent: 40e817f
  • feat
    • what happens on a task is an event
Show full message
✨ feat(core): what happens on a task is an event
crates/izlek-core/src/detail.rsmodified+4 -0
@@ -132,6 +132,7 @@ pub enum ActivityKind {
132132 Moved,
133133 Unblocked,
134134 Deleted,
135+ Commented,
135136 /// A kind written by a newer version than the one reading it. The stored
136137 /// detail is shown as-is rather than dropping the row.
137138 Other(String),
@@ -152,6 +153,7 @@ impl ActivityKind {
152153 ActivityKind::Moved => "moved",
153154 ActivityKind::Unblocked => "unblocked",
154155 ActivityKind::Deleted => "deleted",
156+ ActivityKind::Commented => "commented",
155157 ActivityKind::Other(raw) => raw,
156158 }
157159 }
@@ -170,6 +172,7 @@ impl ActivityKind {
170172 "moved" => ActivityKind::Moved,
171173 "unblocked" => ActivityKind::Unblocked,
172174 "deleted" => ActivityKind::Deleted,
175+ "commented" => ActivityKind::Commented,
173176 other => ActivityKind::Other(other.to_string()),
174177 }
175178 }
@@ -205,6 +208,7 @@ impl ActivityEntry {
205208 ActivityKind::Moved => format!("moved {detail}"),
206209 ActivityKind::Unblocked => format!("unblocked this task — {detail}"),
207210 ActivityKind::Deleted => "deleted this task".to_string(),
211+ ActivityKind::Commented => "commented".to_string(),
208212 ActivityKind::Other(_) => detail.to_string(),
209213 }
210214 }
crates/izlek-core/src/mail.rsmodified+3 -0
@@ -611,6 +611,9 @@ impl Engine {
611611 "{} deleted {} — {}, the last task it was waiting on.",
612612 actor, freeing.cause_key, freeing.cause_title
613613 ),
614+ // Not a wired path yet — S4's work — so this only needs to be
615+ // exhaustive, not right.
616+ (Event::Happened(_), _) => format!("{} touched it.", actor),
614617 };
615618 let body = format!(
616619 "{key} — {title}\n\n{happened}\n\n{when}\n\n{base}/?task={id}\n",
crates/izlek-core/src/store/mod.rsmodified+45 -7
@@ -206,6 +206,23 @@ pub struct NewTask<'a> {
206206 pub created_by: &'a str,
207207 }
208208
209+/// What `create_task` wrote: the task, and the id of the Created activity row
210+/// it filed alongside it, so a caller that needs to name the event — a mail
211+/// retry, one day — does not have to go read the trail back.
212+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
213+pub struct TaskCreated {
214+ pub row: TaskRow,
215+ pub activity_id: String,
216+}
217+
218+/// What `add_comment` wrote: the comment's id, and the id of the Commented
219+/// activity row it filed alongside it in the same transaction.
220+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
221+pub struct CommentWritten {
222+ pub comment_id: String,
223+ pub activity_id: String,
224+}
225+
209226 /// What makes a rule fire.
210227 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
211228 pub enum Trigger {
@@ -406,7 +423,21 @@ pub struct Deletion {
406423 pub event: Option<Freeing>,
407424 }
408425
409-/// The two things that can owe a mail. Both are committed facts with an id, an
426+/// One row of a task's activity trail, as an event a rule can fire on. Covers
427+/// everything `record_activity` writes that is not already its own event
428+/// shape — a comment today, whatever else `ActivityKind` grows later.
429+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
430+pub struct ActivityEvent {
431+ pub id: String,
432+ pub task_id: String,
433+ pub board_id: String,
434+ pub kind: ActivityKind,
435+ pub actor_id: String,
436+ pub detail: String,
437+ pub at: OffsetDateTime,
438+}
439+
440+/// The things that can owe a mail. Each is a committed fact with an id, an
410441 /// actor and a moment, and a retry rebuilds its mail from whichever one the
411442 /// ledger row points at.
412443 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
@@ -415,6 +446,8 @@ pub enum Event {
415446 Moved(crate::board::Transition),
416447 /// A blocker was deleted and let something go.
417448 Freed(Freeing),
449+ /// An activity row that is neither of the above — a comment, today.
450+ Happened(ActivityEvent),
418451 }
419452
420453 impl Event {
@@ -422,6 +455,7 @@ impl Event {
422455 match self {
423456 Event::Moved(transition) => &transition.id,
424457 Event::Freed(freeing) => &freeing.id,
458+ Event::Happened(activity) => &activity.id,
425459 }
426460 }
427461
@@ -431,6 +465,7 @@ impl Event {
431465 match self {
432466 Event::Moved(transition) => &transition.actor_id,
433467 Event::Freed(freeing) => &freeing.actor_id,
468+ Event::Happened(activity) => &activity.actor_id,
434469 }
435470 }
436471
@@ -439,6 +474,7 @@ impl Event {
439474 match self {
440475 Event::Moved(transition) => transition.at,
441476 Event::Freed(freeing) => freeing.at,
477+ Event::Happened(activity) => activity.at,
442478 }
443479 }
444480 }
@@ -603,7 +639,7 @@ pub trait Store: BoardReads + DetailReads + 'static {
603639
604640 /// Writes a task and hands it the next key on its board, in one
605641 /// transaction, so two writers cannot both take `DZ-14`.
606- async fn create_task(&self, new: NewTask<'_>) -> Result<TaskRow>;
642+ async fn create_task(&self, new: NewTask<'_>) -> Result<TaskCreated>;
607643
608644 /// Idempotent: assigning someone twice is not an error.
609645 async fn assign_task(&self, task_id: &str, user_id: &str) -> Result<()>;
@@ -631,14 +667,16 @@ pub trait Store: BoardReads + DetailReads + 'static {
631667 ) -> Result<()>;
632668
633669 /// The author is the session's user, decided by the handler. There is no
634- /// author field on the form.
670+ /// author field on the form. Writes the comment and its Commented
671+ /// activity row in the same transaction, so one never exists without the
672+ /// other.
635673 async fn add_comment(
636674 &self,
637675 task_id: &str,
638676 author_id: &str,
639677 body: &str,
640678 at: OffsetDateTime,
641- ) -> Result<String>;
679+ ) -> Result<CommentWritten>;
642680
643681 /// Hangs a file off a task. The bytes go into the database file with the
644682 /// row: there is no second place for a Izlek deployment to keep, and no
@@ -717,8 +755,8 @@ pub trait Store: BoardReads + DetailReads + 'static {
717755 /// only; nothing here writes.
718756 async fn deletion_cost(&self, task_id: &str) -> Result<Option<DeletionCost>>;
719757
720- /// Appends one line to a task's activity trail. `actor_id` is `None` when
721- /// the system did it rather than a person.
758+ /// Appends one line to a task's activity trail and returns its row id.
759+ /// `actor_id` is `None` when the system did it rather than a person.
722760 async fn record_activity(
723761 &self,
724762 task_id: &str,
@@ -726,7 +764,7 @@ pub trait Store: BoardReads + DetailReads + 'static {
726764 kind: &ActivityKind,
727765 detail: &str,
728766 at: OffsetDateTime,
729- ) -> Result<()>;
767+ ) -> Result<String>;
730768
731769 // -- mail rules --------------------------------------------------------
732770
crates/izlek-core/src/store/turso_store.rsmodified+92 -30
@@ -15,9 +15,10 @@ use uuid::Uuid;
1515
1616 use super::secret;
1717 use super::{
18- ActivityLine, Attachment, Audience, Deletion, Event, Freeing, MailDecision, MailOutcome,
19- MailRule, MailSend, NewAttachment, NewSender, NewTask, NewUser, Recipient, Result, SendKind,
20- SendState, SenderTest, Session, SigninLink, Store, StoreError, Trigger, User, Workspace,
18+ ActivityEvent, ActivityLine, Attachment, Audience, CommentWritten, Deletion, Event, Freeing,
19+ MailDecision, MailOutcome, MailRule, MailSend, NewAttachment, NewSender, NewTask, NewUser,
20+ Recipient, Result, SendKind, SendState, SenderTest, Session, SigninLink, Store, StoreError,
21+ TaskCreated, Trigger, User, Workspace,
2122 };
2223 use crate::Role;
2324 use crate::board::{BoardMeta, BoardReads, Column, Moved, Person, TaskRow, Transition};
@@ -1178,8 +1179,9 @@ impl Store for TursoStore {
11781179 })
11791180 }
11801181
1181- async fn create_task(&self, new: NewTask<'_>) -> Result<TaskRow> {
1182+ async fn create_task(&self, new: NewTask<'_>) -> Result<TaskCreated> {
11821183 let id = Uuid::new_v4().to_string();
1184+ let activity_id = Uuid::new_v4().to_string();
11831185 let now = now_text()?;
11841186 let deadline = new.deadline.map(day_text).transpose()?;
11851187
@@ -1248,7 +1250,7 @@ impl Store for TursoStore {
12481250 "INSERT INTO activity (id, task_id, actor_id, kind, detail, created_at) \
12491251 VALUES (?1, ?2, ?3, ?4, '', ?5)",
12501252 params![
1251- Uuid::new_v4().to_string(),
1253+ activity_id.clone(),
12521254 id.clone(),
12531255 new.created_by,
12541256 ActivityKind::Created.as_str(),
@@ -1274,14 +1276,17 @@ impl Store for TursoStore {
12741276 tx.commit().await.map_err(backend)?;
12751277
12761278 let (task_key, position) = written;
1277- Ok(TaskRow {
1278- id,
1279- task_key,
1280- title: new.title.to_string(),
1281- column_id: new.column_id.to_string(),
1282- deadline: new.deadline,
1283- position,
1284- done_at: None,
1279+ Ok(TaskCreated {
1280+ row: TaskRow {
1281+ id,
1282+ task_key,
1283+ title: new.title.to_string(),
1284+ column_id: new.column_id.to_string(),
1285+ deadline: new.deadline,
1286+ position,
1287+ done_at: None,
1288+ },
1289+ activity_id,
12851290 })
12861291 }
12871292
@@ -1422,17 +1427,50 @@ impl Store for TursoStore {
14221427 author_id: &str,
14231428 body: &str,
14241429 at: OffsetDateTime,
1425- ) -> Result<String> {
1426- let id = Uuid::new_v4().to_string();
1427- self.conn
1428- .execute(
1430+ ) -> Result<CommentWritten> {
1431+ let comment_id = Uuid::new_v4().to_string();
1432+ let activity_id = Uuid::new_v4().to_string();
1433+ let when = stamp(at)?;
1434+
1435+ let mut conn = self.tx_conn().await?;
1436+ let tx = conn
1437+ .transaction_with_behavior(TransactionBehavior::Immediate)
1438+ .await
1439+ .map_err(backend)?;
1440+
1441+ let written = async {
1442+ tx.execute(
14291443 "INSERT INTO comment (id, task_id, author_id, body, created_at) \
14301444 VALUES (?1, ?2, ?3, ?4, ?5)",
1431- params![id.clone(), task_id, author_id, body, stamp(at)?],
1445+ params![comment_id.clone(), task_id, author_id, body, when.clone()],
14321446 )
1433- .await
1434- .map_err(backend)?;
1435- Ok(id)
1447+ .await?;
1448+ tx.execute(
1449+ "INSERT INTO activity (id, task_id, actor_id, kind, detail, created_at) \
1450+ VALUES (?1, ?2, ?3, ?4, '', ?5)",
1451+ params![
1452+ activity_id.clone(),
1453+ task_id,
1454+ author_id,
1455+ ActivityKind::Commented.as_str(),
1456+ when.clone()
1457+ ],
1458+ )
1459+ .await?;
1460+ Ok::<_, turso::Error>(())
1461+ }
1462+ .await;
1463+
1464+ if let Err(e) = written {
1465+ let _ = tx.rollback().await;
1466+ return Err(backend(e));
1467+ }
1468+ tx.commit().await.map_err(backend)?;
1469+
1470+ Ok(CommentWritten {
1471+ comment_id,
1472+ activity_id,
1473+ })
14361474 }
14371475
14381476 async fn add_attachment(&self, new: NewAttachment<'_>) -> Result<String> {
@@ -2009,13 +2047,14 @@ impl Store for TursoStore {
20092047 kind: &ActivityKind,
20102048 detail: &str,
20112049 at: OffsetDateTime,
2012- ) -> Result<()> {
2050+ ) -> Result<String> {
2051+ let id = Uuid::new_v4().to_string();
20132052 self.conn
20142053 .execute(
20152054 "INSERT INTO activity (id, task_id, actor_id, kind, detail, created_at) \
20162055 VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
20172056 params![
2018- Uuid::new_v4().to_string(),
2057+ id.clone(),
20192058 task_id,
20202059 actor_id,
20212060 kind.as_str(),
@@ -2025,7 +2064,7 @@ impl Store for TursoStore {
20252064 )
20262065 .await
20272066 .map_err(backend)?;
2028- Ok(())
2067+ Ok(id)
20292068 }
20302069
20312070 // -- mail rules --------------------------------------------------------
@@ -2148,13 +2187,36 @@ impl Store for TursoStore {
21482187 )
21492188 .await?
21502189 {
2151- Some(row) => Ok(Some(Event::Freed(Freeing {
2190+ Some(row) => {
2191+ return Ok(Some(Event::Freed(Freeing {
2192+ id: text(&row, 0)?,
2193+ board_id: text(&row, 1)?,
2194+ cause_key: text(&row, 2)?,
2195+ cause_title: text(&row, 3)?,
2196+ actor_id: text(&row, 4)?,
2197+ at: parse_stamp(&text(&row, 5)?)?,
2198+ })));
2199+ }
2200+ None => {}
2201+ }
2202+ match self
2203+ .one_row(
2204+ "SELECT activity.id, activity.task_id, task.board_id, activity.kind, \
2205+ activity.actor_id, activity.detail, activity.created_at \
2206+ FROM activity JOIN task ON task.id = activity.task_id \
2207+ WHERE activity.id = ?1",
2208+ params![event_id],
2209+ )
2210+ .await?
2211+ {
2212+ Some(row) => Ok(Some(Event::Happened(ActivityEvent {
21522213 id: text(&row, 0)?,
2153- board_id: text(&row, 1)?,
2154- cause_key: text(&row, 2)?,
2155- cause_title: text(&row, 3)?,
2156- actor_id: text(&row, 4)?,
2157- at: parse_stamp(&text(&row, 5)?)?,
2214+ task_id: text(&row, 1)?,
2215+ board_id: text(&row, 2)?,
2216+ kind: ActivityKind::parse(&text(&row, 3)?),
2217+ actor_id: opt_text(&row, 4)?.unwrap_or_default(),
2218+ detail: text(&row, 5)?,
2219+ at: parse_stamp(&text(&row, 6)?)?,
21582220 }))),
21592221 None => Ok(None),
21602222 }
crates/izlek-core/tests/store.rsmodified+64 -4
@@ -9,8 +9,8 @@ use std::path::PathBuf;
99 use izlek_core::Role;
1010 use izlek_core::auth::{Token, hash_password};
1111 use izlek_core::store::{
12- Audience, MailOutcome, MailRule, NewAttachment, NewSender, NewUser, SendKind, SendState, Store,
13- StoreError, Trigger, TursoStore, User,
12+ Audience, Event, MailOutcome, MailRule, NewAttachment, NewSender, NewUser, SendKind,
13+ SendState, Store, StoreError, Trigger, TursoStore, User,
1414 };
1515 use time::{Duration, OffsetDateTime};
1616 use uuid::Uuid;
@@ -1869,6 +1869,7 @@ async fn add_task(
18691869 })
18701870 .await
18711871 .unwrap()
1872+ .row
18721873 .id
18731874 }
18741875
@@ -2538,8 +2539,9 @@ async fn a_task_detail_costs_eight_queries_whatever_it_carries() {
25382539 assert_eq!(detail.comments.len(), 20);
25392540 assert_eq!(detail.assignees.len(), 20);
25402541 assert_eq!(detail.blocks.len(), 20);
2541- // Twenty moves plus the line create_task wrote.
2542- assert_eq!(detail.activity.len(), 21);
2542+ // Twenty comments (each its own Commented line), twenty moves, plus the
2543+ // line create_task wrote.
2544+ assert_eq!(detail.activity.len(), 41);
25432545 assert_eq!(
25442546 counted.count(),
25452547 8,
@@ -4381,3 +4383,61 @@ async fn the_activity_feed_is_the_whole_workspace_newest_first() {
43814383 assert_eq!(feed[1].task_id, task_a);
43824384 assert_eq!(feed[1].actor_name.as_deref(), Some("Ada"));
43834385 }
4386+
4387+#[tokio::test]
4388+async fn a_comment_leaves_one_activity_row_that_resolves_as_an_event() {
4389+ let (scratch, workspace, admin) = workspace_with_admin().await;
4390+ let store = &scratch.store;
4391+ let task = add_task(store, &workspace, "Backlog", "note me", None, &admin).await;
4392+
4393+ let written = store
4394+ .add_comment(&task, &admin, "hello", OffsetDateTime::now_utc())
4395+ .await
4396+ .unwrap();
4397+
4398+ let activity = store.activity_for_task(&task).await.unwrap();
4399+ let commented: Vec<_> = activity
4400+ .iter()
4401+ .filter(|line| line.kind == ActivityKind::Commented)
4402+ .collect();
4403+ assert_eq!(commented.len(), 1);
4404+ assert_eq!(commented[0].id, written.activity_id);
4405+
4406+ match store.event(&written.activity_id).await.unwrap() {
4407+ Some(Event::Happened(event)) => {
4408+ assert_eq!(event.task_id, task);
4409+ assert_eq!(event.actor_id, admin);
4410+ assert_eq!(event.kind, ActivityKind::Commented);
4411+ }
4412+ other => panic!("expected Event::Happened, got {other:?}"),
4413+ }
4414+}
4415+
4416+#[tokio::test]
4417+async fn a_created_task_s_activity_id_resolves_as_an_event() {
4418+ let (scratch, workspace, admin) = workspace_with_admin().await;
4419+ let store = &scratch.store;
4420+ let board = store.board(&workspace).await.unwrap().unwrap();
4421+ let column_id = column_named(store, &workspace, "Backlog").await;
4422+
4423+ let created = store
4424+ .create_task(NewTask {
4425+ board_id: &board.id,
4426+ column_id: &column_id,
4427+ title: "brand new",
4428+ description: "",
4429+ deadline: None,
4430+ created_by: &admin,
4431+ })
4432+ .await
4433+ .unwrap();
4434+
4435+ match store.event(&created.activity_id).await.unwrap() {
4436+ Some(Event::Happened(event)) => {
4437+ assert_eq!(event.task_id, created.row.id);
4438+ assert_eq!(event.actor_id, admin);
4439+ assert_eq!(event.kind, ActivityKind::Created);
4440+ }
4441+ other => panic!("expected Event::Happened, got {other:?}"),
4442+ }
4443+}
crates/izlek-web/src/logs.rsmodified+4 -0
@@ -100,6 +100,7 @@ fn activity_sentence(kind: &izlek_core::detail::ActivityKind, detail: &str) -> S
100100 ActivityKind::Moved => format!("moved {detail}"),
101101 ActivityKind::Unblocked => format!("unblocked this task — {detail}"),
102102 ActivityKind::Deleted => "deleted this task".to_string(),
103+ ActivityKind::Commented => "commented".to_string(),
103104 ActivityKind::Other(_) => detail.to_string(),
104105 }
105106 }
@@ -129,6 +130,9 @@ async fn event_happened(
129130 .map(|column| format!("moved to {}", column.name)))
130131 }
131132 Event::Freed(_) => Ok(Some("unblocked".to_string())),
133+ // Not a wired path yet — S4's work — so this only needs to be
134+ // exhaustive, not right.
135+ Event::Happened(activity) => Ok(Some(activity.kind.as_str().to_string())),
132136 }
133137 }
134138