co-core-board 0.1.0

Kanban Board Core.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 1io BRANDGUARDIAN GmbH

use anyhow::anyhow;
use cid::Cid;
use co_api::{
	co, BlockStorage, BlockStorageExt, CoList, CoListIndex, CoMap, CoTryStreamExt, CoreBlockStorage, IsDefault,
	LazyTransaction, Link, OptionLink, Reducer, ReducerAction, Tags,
};
use futures::{pin_mut, FutureExt, TryStreamExt};
use std::future::ready;

pub type ListName = String;
pub type TaskId = String;

/// Board actions.
#[co]
pub enum BoardAction {
	BoardRename(String),
	BoardTagsInsert(Tags),
	BoardTagsRemove(Tags),
	ListCreate { list: List, after: Option<ListName> },
	ListArrange { name: ListName, after: Option<ListName> },
	ListDelete { name: ListName, move_tasks_to_list: Option<ListName> },
	ListTagsInsert(ListName, Tags),
	ListTagsRemove(ListName, Tags),
	// ListTasksDelete(ListName),
	// ListTasksMove { from: ListName, to: ListName },
	TaskCreate { list: ListName, task: Task, after: Option<TaskId> },
	TaskMove { from_list: Option<ListName>, list: ListName, task: TaskId, after: Option<TaskId>, lock: TaskLock },
	TaskArrange { task: TaskId, after: Option<TaskId> },
	TaskDelete(TaskId),
	TaskRename(TaskId, String),
	TaskPayloadChange(TaskId, Option<Cid>),
	TaskTagsInsert(TaskId, Tags),
	TaskTagsRemove(TaskId, Tags),
}

#[co(state)]
pub struct Board {
	/// Board name.
	#[serde(rename = "n", default, skip_serializing_if = "String::is_empty")]
	pub name: String,

	/// Board lists.
	#[serde(rename = "l", default, skip_serializing_if = "CoList::is_empty")]
	pub lists: CoList<List>,

	/// Board tags.
	#[serde(rename = "t", default, skip_serializing_if = "Tags::is_empty")]
	pub tags: Tags,

	/// Board tasks.
	#[serde(rename = "i", default, skip_serializing_if = "CoMap::is_empty")]
	pub tasks: CoMap<TaskId, Task>,
}
impl Reducer<BoardAction> for Board {
	async fn reduce(
		state_link: OptionLink<Self>,
		event_link: Link<ReducerAction<BoardAction>>,
		storage: &CoreBlockStorage,
	) -> Result<Link<Self>, anyhow::Error> {
		let event = storage.get_value(&event_link).await?;
		let mut state = storage.get_value_or_default(&state_link).await?;
		reduce(storage, &mut state, event.payload).await?;
		Ok(storage.set_value(&state).await?)
	}
}

#[co]
pub struct List {
	/// List name.
	#[serde(rename = "n")]
	pub name: ListName,

	/// List tasks.
	#[serde(rename = "i", default, skip_serializing_if = "CoList::is_empty")]
	pub tasks: CoList<TaskId>,

	/// List tags.
	#[serde(rename = "t", default, skip_serializing_if = "Tags::is_empty")]
	pub tags: Tags,
}
impl List {
	pub fn new(name: impl Into<ListName>) -> Self {
		Self { name: name.into(), tags: Default::default(), tasks: Default::default() }
	}
}

#[co]
pub struct Task {
	/// Task unique id.
	#[serde(rename = "u")]
	pub id: TaskId,

	/// Task name.
	#[serde(rename = "n")]
	pub name: String,

	/// Task tags.
	#[serde(rename = "t", default, skip_serializing_if = "Tags::is_empty")]
	pub tags: Tags,

	/// Task payload.
	#[serde(rename = "p", default, skip_serializing_if = "Option::is_none")]
	pub payload: Option<Cid>,

	/// Task exclusive lock identifier.
	#[serde(rename = "l", default, skip_serializing_if = "IsDefault::is_default")]
	pub lock: Option<String>,
}

#[co]
#[derive(Default)]
pub enum TaskLock {
	/// No lock.
	/// Fail the operation if the subject is locked.
	#[default]
	None,

	/// Force operation if the subject is locked.
	Force,

	/// Use or apply a lock.
	/// Fail the operation if the subject is locked with a different lock.
	Lock(String),

	/// Use and unlock after the operation.
	/// Fail the operation if the subject is locked with a different lock.
	Unlock(String),
}

async fn reduce<S>(storage: &S, state: &mut Board, action: BoardAction) -> Result<(), anyhow::Error>
where
	S: BlockStorage + Clone + 'static,
{
	// open
	let mut transaction = BoardTransaction {
		storage: storage.clone(),
		lists: LazyTransaction::new(storage.clone(), state.lists.clone()),
		tasks: LazyTransaction::new(storage.clone(), state.tasks.clone()),
	};

	// reduce
	match action {
		BoardAction::BoardRename(name) => reduce_board_rename(state, name).boxed().await?,
		BoardAction::BoardTagsInsert(tags) => reduce_board_tags_insert(state, tags).boxed().await?,
		BoardAction::BoardTagsRemove(tags) => reduce_board_tags_remove(state, tags).boxed().await?,
		BoardAction::ListCreate { list, after } => reduce_list_create(&mut transaction, list, after).boxed().await?,
		BoardAction::ListArrange { name, after } => reduce_list_arrange(&mut transaction, name, after).boxed().await?,
		BoardAction::ListDelete { name, move_tasks_to_list } => {
			reduce_list_delete(&mut transaction, name, move_tasks_to_list).boxed().await?
		},
		BoardAction::ListTagsInsert(name, tags) => {
			reduce_list_tags_insert(&mut transaction, name, tags).boxed().await?
		},
		BoardAction::ListTagsRemove(name, tags) => {
			reduce_list_tags_remove(&mut transaction, name, tags).boxed().await?
		},
		BoardAction::TaskCreate { list, task, after } => {
			reduce_task_create(&mut transaction, list, task, after).boxed().await?
		},
		BoardAction::TaskMove { from_list, list, task, after, lock } => {
			reduce_task_move(&mut transaction, from_list, list, task, after, lock)
				.boxed()
				.await?
		},
		BoardAction::TaskArrange { task, after } => reduce_task_arrange(&mut transaction, task, after).boxed().await?,
		BoardAction::TaskDelete(task) => reduce_task_delete(&mut transaction, task).boxed().await?,
		BoardAction::TaskRename(task, name) => reduce_task_rename(&mut transaction, task, name).boxed().await?,
		BoardAction::TaskPayloadChange(task, cid) => {
			reduce_task_payload_change(&mut transaction, task, cid).boxed().await?
		},
		BoardAction::TaskTagsInsert(task, tags) => {
			reduce_task_tags_insert(&mut transaction, task, tags).boxed().await?
		},
		BoardAction::TaskTagsRemove(task, tags) => {
			reduce_task_tags_remove(&mut transaction, task, tags).boxed().await?
		},
	}

	// store
	if transaction.lists.is_mut_access() {
		state.lists = transaction.lists.get_mut().await?.store().await?;
	}
	if transaction.tasks.is_mut_access() {
		state.tasks = transaction.tasks.get_mut().await?.store().await?;
	}

	// result
	Ok(())
}

struct BoardTransaction<S>
where
	S: BlockStorage + Clone + 'static,
{
	storage: S,
	lists: LazyTransaction<S, CoList<List>>,
	tasks: LazyTransaction<S, CoMap<TaskId, Task>>,
}
impl<S> BoardTransaction<S>
where
	S: BlockStorage + Clone + 'static,
{
	/// Find list by name by scanning lists.
	async fn find_list_by_name(&mut self, name: &str) -> Result<Option<(CoListIndex, List)>, anyhow::Error> {
		Ok(self
			.lists
			.get()
			.await?
			.stream()
			.try_filter(|item| ready(item.1.name == name))
			.try_first()
			.await?)
	}

	/// Fint task's list by scanning all lists.
	async fn find_task_list(&mut self, id: &TaskId) -> Result<Option<(CoListIndex, List, CoListIndex)>, anyhow::Error> {
		Ok(self
			.lists
			.get()
			.await?
			.stream()
			.try_filter_map(|(index, list)| {
				let storage = self.storage.clone();
				async move {
					Ok(list
						.tasks
						.stream(&storage)
						.try_filter(|(_, task)| ready(task == id))
						.try_first()
						.await?
						.map(|(task_index, _task_id)| (index, list, task_index)))
				}
			})
			.try_first()
			.await?)
	}

	/// Get task by id.
	async fn task(&mut self, task_id: &TaskId) -> Result<Task, anyhow::Error> {
		self.tasks
			.get()
			.await?
			.get(task_id)
			.await?
			.ok_or_else(|| anyhow!("Task not found: {}", task_id))
	}
}

async fn reduce_task_tags_remove<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	task_id: TaskId,
	tags: Tags,
) -> Result<(), anyhow::Error> {
	let mut task = transaction
		.tasks
		.get()
		.await?
		.get(&task_id)
		.await?
		.ok_or(anyhow!("Task not found: {}", task_id))?;

	// apply
	task.tags.clear(Some(&tags));

	// store
	transaction.tasks.get_mut().await?.insert(task_id, task).await?;

	Ok(())
}

async fn reduce_task_tags_insert<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	task_id: TaskId,
	mut tags: Tags,
) -> Result<(), anyhow::Error> {
	let mut task = transaction
		.tasks
		.get()
		.await?
		.get(&task_id)
		.await?
		.ok_or(anyhow!("Task not found: {}", task_id))?;

	// apply
	task.tags.append(&mut tags);

	// store
	transaction.tasks.get_mut().await?.insert(task_id, task).await?;

	Ok(())
}

async fn reduce_task_payload_change<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	task_id: TaskId,
	payload: Option<Cid>,
) -> Result<(), anyhow::Error> {
	let mut task = transaction
		.tasks
		.get()
		.await?
		.get(&task_id)
		.await?
		.ok_or(anyhow!("Task not found: {}", task_id))?;

	// apply
	if task.payload != payload {
		// set
		task.payload = payload;

		// store
		transaction.tasks.get_mut().await?.insert(task_id, task).await?;
	}
	Ok(())
}

async fn reduce_task_rename<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	task_id: TaskId,
	name: String,
) -> Result<(), anyhow::Error> {
	let mut task = transaction
		.tasks
		.get()
		.await?
		.get(&task_id)
		.await?
		.ok_or(anyhow!("Task not found: {}", task_id))?;

	// apply
	if task.name != name {
		// set
		task.name = name;

		// store
		transaction.tasks.get_mut().await?.insert(task_id, task).await?;
	}
	Ok(())
}

async fn reduce_task_delete<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	task_id: TaskId,
) -> Result<(), anyhow::Error> {
	// find task list
	let (list_index, mut list, task_index) = transaction
		.find_task_list(&task_id)
		.await?
		.ok_or(anyhow!("Task list not found: {}", task_id))?;

	// remove
	transaction
		.tasks
		.get_mut()
		.await?
		.remove(task_id.clone())
		.await?
		.ok_or(anyhow!("Task not found: {}", task_id))?;

	// remove from list
	list.tasks.remove(&transaction.storage, task_index).await?;

	// store list
	transaction.lists.get_mut().await?.set(list_index, list).await?;

	Ok(())
}

async fn reduce_task_arrange<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	task_id: TaskId,
	after: Option<TaskId>,
) -> Result<(), anyhow::Error> {
	// find task list
	let (list_index, mut list, task_index) = transaction
		.find_task_list(&task_id)
		.await?
		.ok_or(anyhow!("Task list not found: {}", task_id))?;

	// after index
	let mut list_tasks = list.tasks.open(&transaction.storage).await?;
	let task_after_index = if let Some(after) = &after {
		list_tasks
			.stream()
			.try_filter(|item| ready(&item.1 == after))
			.try_first()
			.await?
			.map(|(index, _)| index)
	} else {
		None
	};

	// remove
	list_tasks.remove(task_index).await?;

	// insert
	if let Some(task_after_index) = task_after_index {
		list_tasks.insert(task_after_index, task_id).await?;
	} else {
		list_tasks.push(task_id).await?;
	}

	// store list
	list.tasks = list_tasks.store().await?;
	transaction.lists.get_mut().await?.set(list_index, list).await?;

	Ok(())
}

async fn reduce_task_move<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	from_list: Option<ListName>,
	list_name: ListName,
	task_id: TaskId,
	after: Option<TaskId>,
	lock: TaskLock,
) -> Result<(), anyhow::Error> {
	// lock
	task_lock(transaction, &task_id, &lock).await?;

	// find source list and source list task index
	let (source_list_index, mut source_list, mut source_list_tasks, source_task_index) =
		if let Some(from_list) = &from_list {
			let (source_list_index, source_list) = transaction
				.find_list_by_name(from_list)
				.await?
				.ok_or(anyhow!("List not found: {}", from_list))?;
			let list_tasks = source_list.tasks.open(&transaction.storage).await?;
			let source_task_index = list_tasks
				.stream()
				.try_filter(|item| ready(item.1 == task_id))
				.try_first()
				.await?
				.map(|(index, _)| index)
				.ok_or(anyhow!("Task not found: {} in list: {}", task_id, source_list.name))?;
			(source_list_index, source_list, list_tasks, source_task_index)
		} else {
			let (source_list_index, source_list, source_task_index) = transaction
				.find_task_list(&task_id)
				.await?
				.ok_or(anyhow!("Task list not found: {}", task_id))?;
			let list_tasks = source_list.tasks.open(&transaction.storage).await?;
			(source_list_index, source_list, list_tasks, source_task_index)
		};

	// find target list
	let (list_index, mut list) = transaction
		.find_list_by_name(&list_name)
		.await?
		.ok_or(anyhow!("List not found: {}", list_name))?;
	let mut list_tasks = list.tasks.open(&transaction.storage).await?;

	// find target list index
	let task_after_index = if let Some(after) = &after {
		list_tasks
			.stream()
			.try_filter(|item| ready(&item.1 == after))
			.try_first()
			.await?
			.map(|(index, _)| index)
	} else {
		None
	};

	// remove task from source list
	source_list_tasks.remove(source_task_index).await?;

	// add task to target list
	if let Some(task_after_index) = task_after_index {
		list_tasks.insert(task_after_index, task_id.clone()).await?;
	} else {
		list_tasks.push(task_id.clone()).await?;
	}

	// store
	source_list.tasks = source_list_tasks.store().await?;
	transaction.lists.get_mut().await?.set(source_list_index, source_list).await?;
	list.tasks = list_tasks.store().await?;
	transaction.lists.get_mut().await?.set(list_index, list).await?;

	// result
	Ok(())
}

async fn reduce_task_create<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	list: ListName,
	task: Task,
	after: Option<TaskId>,
) -> Result<(), anyhow::Error> {
	let task_id = task.id.clone();

	// find list
	let (list_index, mut list) = transaction
		.find_list_by_name(&list)
		.await?
		.ok_or(anyhow!("List not found: {}", list))?;

	// validate id is unique
	if transaction.tasks.get().await?.contains_key(&task_id).await? {
		return Err(anyhow!("Task exists: {}", task_id));
	}

	// create task
	transaction.tasks.get_mut().await?.insert(task_id.clone(), task).await?;

	// add to list
	let mut list_tasks = list.tasks.open(&transaction.storage).await?;
	let task_after_index = if let Some(after) = &after {
		list_tasks
			.stream()
			.try_filter(|item| ready(&item.1 == after))
			.try_first()
			.await?
			.map(|(index, _)| index)
	} else {
		None
	};
	if let Some(task_after_index) = task_after_index {
		list_tasks.insert(task_after_index, task_id).await?;
	} else {
		list_tasks.push(task_id).await?;
	}

	// store list
	list.tasks = list_tasks.store().await?;
	transaction.lists.get_mut().await?.set(list_index, list).await?;

	Ok(())
}

async fn reduce_list_tags_remove<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	name: String,
	tags: Tags,
) -> Result<(), anyhow::Error> {
	// find
	let (list_index, mut list) = transaction
		.find_list_by_name(&name)
		.await?
		.ok_or(anyhow!("List not found: {}", name))?;

	// insert
	list.tags.clear(Some(&tags));

	// store
	transaction.lists.get_mut().await?.set(list_index, list).await?;
	Ok(())
}

async fn reduce_list_tags_insert<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	name: String,
	mut tags: Tags,
) -> Result<(), anyhow::Error> {
	// find
	let (list_index, mut list) = transaction
		.find_list_by_name(&name)
		.await?
		.ok_or(anyhow!("List not found: {}", name))?;

	// insert
	list.tags.append(&mut tags);

	// store
	transaction.lists.get_mut().await?.set(list_index, list).await?;
	Ok(())
}

async fn reduce_list_delete<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	name: String,
	move_tasks_to_list: Option<String>,
) -> Result<(), anyhow::Error> {
	// find
	let (list_index, list) = transaction
		.find_list_by_name(&name)
		.await?
		.ok_or(anyhow!("List not found: {}", name))?;

	// move tasks
	if let Some(move_tasks_to_list) = &move_tasks_to_list {
		let (_to_list_index, to_list) = transaction
			.find_list_by_name(move_tasks_to_list)
			.await?
			.ok_or(anyhow!("List not found: {}", name))?;
		if !list.tasks.is_empty() {
			let storage = transaction.storage.clone();
			let tasks = list.tasks.clone();
			let tasks = tasks.stream(&storage);
			pin_mut!(tasks);
			while let Some((_, task)) = tasks.try_next().await? {
				reduce_task_move(transaction, None, to_list.name.clone(), task, None, TaskLock::Force).await?;
			}
		}
	}

	// delete list
	transaction.lists.get_mut().await?.remove(list_index).await?;

	Ok(())
}

async fn reduce_list_arrange<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	name: String,
	after: Option<String>,
) -> Result<(), anyhow::Error> {
	// find
	let (list_index, list) = transaction
		.find_list_by_name(&name)
		.await?
		.ok_or(anyhow!("List not found: {}", name))?;

	// find after
	let after_index = if let Some(after) = &after {
		transaction.find_list_by_name(after).await?.map(|(index, _)| index)
	} else {
		None
	};

	// remove
	transaction.lists.get_mut().await?.remove(list_index).await?;

	// create
	if let Some(after_index) = after_index {
		transaction.lists.get_mut().await?.insert(after_index, list).await?;
	} else {
		transaction.lists.get_mut().await?.push(list).await?;
	}
	Ok(())
}

async fn reduce_list_create<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	list: List,
	after: Option<String>,
) -> Result<(), anyhow::Error> {
	// verify name not exists yet
	if transaction.find_list_by_name(&list.name).await?.is_some() {
		return Err(anyhow!("List already exists: {}", list.name));
	}

	// find after
	let after_index = if let Some(after) = &after {
		transaction.find_list_by_name(after).await?.map(|(index, _)| index)
	} else {
		None
	};

	// create
	if let Some(after_index) = after_index {
		transaction.lists.get_mut().await?.insert(after_index, list).await?;
	} else {
		transaction.lists.get_mut().await?.push(list).await?;
	}
	Ok(())
}

async fn reduce_board_tags_remove(state: &mut Board, tags: Tags) -> Result<(), anyhow::Error> {
	state.tags.clear(Some(&tags));
	Ok(())
}

async fn reduce_board_tags_insert(state: &mut Board, mut tags: Tags) -> Result<(), anyhow::Error> {
	state.tags.append(&mut tags);
	Ok(())
}

async fn reduce_board_rename(state: &mut Board, name: String) -> Result<(), anyhow::Error> {
	state.name = name;
	Ok(())
}

async fn task_lock<S: BlockStorage + Clone + 'static>(
	transaction: &mut BoardTransaction<S>,
	task_id: &TaskId,
	lock: &TaskLock,
) -> Result<(), anyhow::Error> {
	match lock {
		TaskLock::None => {
			let task = transaction.task(task_id).await?;
			if task.lock.is_some() {
				Err(anyhow!("Task locked"))
			} else {
				Ok(())
			}
		},
		TaskLock::Force => Ok(()),
		TaskLock::Lock(lock) => {
			let mut task = transaction.task(task_id).await?;
			match task.lock {
				Some(task_lock) if lock == &task_lock => Ok(()),
				Some(_task_lock) => Err(anyhow!("Task locked")),
				None => {
					task.lock = Some(lock.clone());
					transaction.tasks.get_mut().await?.insert(task_id.clone(), task).await?;
					Ok(())
				},
			}
		},
		TaskLock::Unlock(lock) => {
			let mut task = transaction.task(task_id).await?;
			match task.lock {
				Some(task_lock) if lock == &task_lock => {
					task.lock = None;
					transaction.tasks.get_mut().await?.insert(task_id.clone(), task).await?;
					Ok(())
				},
				Some(_task_lock) => Err(anyhow!("Task locked")),
				None => Ok(()),
			}
		},
	}
}