/
org-trello-controller.el
1476 lines (1335 loc) · 63.7 KB
/
org-trello-controller.el
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; org-trello-controller.el --- Controller of org-trello mode
;; Copyright (C) 2015-2021 Antoine R. Dumont (@ardumont) <antoine.romain.dumont@gmail.com>
;; Author: Antoine R. Dumont (@ardumont) <antoine.romain.dumont@gmail.com>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(if (version< emacs-version "27")
(eval-when-compile (require 'cl))
(eval-when-compile (require 'cl-lib)))
(require 'org-trello-setup)
(require 'org-trello-log)
(require 'org-trello-buffer)
(require 'org-trello-data)
(require 'org-trello-hash)
(require 'org-trello-api)
(require 'org-trello-entity)
(require 'org-trello-cbx)
(require 'org-trello-action)
(require 'org-trello-backend)
(require 'org-trello-buffer)
(require 'org-trello-input)
(require 'org-trello-proxy)
(require 'org-trello-deferred)
(require 'dash)
(require 's)
(defun orgtrello-controller-log-success (prefix-log)
"Return a function to log the success with PREFIX-LOG as prefix string."
(-partial #'orgtrello-log-msg orgtrello-log-info (format "%s DONE."
prefix-log)))
(defun orgtrello-controller-log-error (prefix-log error-msg)
"Return a function to log the error with PREFIX-LOG and ERROR-MSG."
(-partial #'orgtrello-log-msg orgtrello-log-error (format "%s FAILED. %s"
prefix-log
error-msg)))
(defun orgtrello-controller--list-user-entries (props)
"List the users entries from properties PROPS."
(-filter (-compose
(-partial 'string-match-p org-trello--label-key-user-prefix)
'car) props))
(defun orgtrello-controller--hmap-id-name (org-keywords props)
"Given an ORG-KEYWORDS and a properties PROPS, return a map.
This map is a key/value of (trello-id, trello-list-name-and-org-keyword-name).
If either org-keywords or properties is nil, return an empty hash-map."
(if (or (null org-keywords) (null props))
(orgtrello-hash-empty-hash)
(--reduce-from (orgtrello-hash-puthash-data
(orgtrello-buffer-org-get-property it props) it acc)
(orgtrello-hash-empty-hash)
org-keywords)))
(defun orgtrello-controller-setup-properties (&optional args)
"Setup `org-trello' properties according to `org-mode' setup in buffer.
Return :ok.
ARGS is not used."
(orgtrello-action-reload-setup)
(let* ((org-keywords (orgtrello-buffer-filtered-kwds))
(org-file-properties (orgtrello-buffer-org-file-properties))
(org-trello-users (orgtrello-controller--list-user-entries
org-file-properties)))
(setq
org-trello--org-keyword-trello-list-names org-keywords
org-trello--hmap-list-orgkeyword-id-name (orgtrello-controller--hmap-id-name
org-keywords
org-file-properties)
org-trello--hmap-users-id-name (orgtrello-hash-make-transpose-properties
org-trello-users)
org-trello--hmap-users-name-id (orgtrello-hash-make-properties
org-trello-users)
org-trello--user-logged-in (or (orgtrello-buffer-me)
(orgtrello-setup-user-logged-in)))
(when org-trello-add-tags
(mapc (-partial #'add-to-list 'org-tag-alist)
'(("red" . ?r)
("green" . ?g)
("yellow" . ?y)
("blue" . ?b)
("purple" . ?p)
("orange" . ?o))))
:ok))
(defun orgtrello-controller-control-properties (&optional args)
"Org-trello needs some header buffer properties set (board id, list ids, ...).
Return :ok if ok, or the error message if problems.
ARGS is not used."
(let ((hmap-count
(hash-table-count org-trello--hmap-list-orgkeyword-id-name)))
(if (and (orgtrello-buffer-org-file-properties)
(orgtrello-buffer-board-id)
(= (length org-trello--org-keyword-trello-list-names) hmap-count))
:ok
"Setup problem.\nEither you did not connect your org-mode buffer with a trello board, to correct this:\n * attach to a board through C-c o I or M-x org-trello-install-board-metadata\n * or create a board from scratch with C-c o b or M-x org-trello-create-board-and-install-metadata).\nEither your org-mode's todo keyword list and your trello board lists are not named the same way (which they must).\nFor this, connect to trello and rename your board's list according to your org-mode's todo list.\nAlso, you can specify on your org-mode buffer the todo list you want to work with, for example: #+TODO: TODO DOING | DONE FAIL (hit C-c C-c to refresh the setup)")))
(defun orgtrello-controller-migrate-user-setup (&optional args)
"Migrate user's setup file.
From:
- ~/.trello/config.el to ~/.emacs.d/.trello/<trello-login>.el.
- Also the names of the constants have changed to *consumer-key* to
org-trello-consumer-key and from *access-key* to org-trello-access-key.
ARGS is not used."
(when (file-exists-p org-trello--old-config-dir) ;; old setup, migrate
(-let (((consumer-key access-key)
(if *consumer-key*
`(,*consumer-key* ,*access-token*)
`(,org-trello-consumer-key ,org-trello-access-token)))
(user-login (orgtrello-buffer-me)))
;; load old setup
(load org-trello--old-config-file)
;; persist setup
(orgtrello-controller--do-install-config-file
user-login
consumer-key
access-key)
;; delete old setup file
(delete-directory org-trello--old-config-dir 'with-contents)))
:ok)
(defun orgtrello-controller-migrate-user-buffer (&optional args)
"Migrate user's setup file according to latest development.
ARGS is not used."
(when (orgtrello-buffer-to-migrate-p)
(orgtrello-log-msg orgtrello-log-warn "Migrate the buffer's keys to the correct new one (https://org-trello.github.io/migration.html#081).")
(orgtrello-buffer-migrate-buffer))
:ok)
(defun orgtrello-controller-config-file (&optional username)
"Determine the configuration file as per user logged in.
If USERNAME is supplied, do not look into the current buffer."
(format org-trello--config-file
(if username username (orgtrello-setup-user-logged-in))))
(defun orgtrello-controller-user-config-files ()
"List the actual possible users."
(when (file-exists-p org-trello--config-dir)
(directory-files org-trello--config-dir 'full-name "^.*\.el")))
(defalias 'orgtrello-controller-user-account-from-config-file 'file-name-base)
(defun orgtrello-controller-list-user-accounts (user-config-files)
"Given a list of USER-CONFIG-FILES, return the trello accounts list."
(mapcar #'orgtrello-controller-user-account-from-config-file user-config-files))
(defun orgtrello-controller--choose-account (accounts)
"Let the user decide which ACCOUNTS (s)he wants to use.
Return such account name."
(orgtrello-input-read-string-completion
"Select org-trello account (TAB to complete): "
accounts))
(defun orgtrello-controller-set-account (&optional args)
"Set the org-trello account.
ARGS is not used."
(let ((user-account-elected
(-if-let (user-account (orgtrello-buffer-me))
user-account ;; if already set, keep using the actual account
;; otherwise, user not logged in, determine which account to use
(let ((user-accounts (orgtrello-controller-list-user-accounts
(orgtrello-controller-user-config-files))))
(if (= 1 (length user-accounts))
(car user-accounts)
(orgtrello-controller--choose-account user-accounts))))))
(orgtrello-setup-set-user-logged-in user-account-elected)
:ok))
(defun orgtrello-controller-load-keys (&optional args)
"Load the credentials keys from the configuration file.
ARGS is not used."
(let ((user-config-file (orgtrello-controller-config-file)))
(if (and (file-exists-p user-config-file) (load user-config-file))
:ok
"Setup problem - Problem during credentials loading (consumer-key and read/write access-token) - C-c o i or M-x org-trello-install-key-and-token")))
(defun orgtrello-controller-control-keys (&optional args)
"Check `org-trello-consumer-key' and `org-trello-access-token' are set.
Returns :ok if everything is ok, or the error message if problems.
ARGS is not used."
(if (and org-trello-consumer-key org-trello-access-token)
:ok
"Setup problem - You need to install the consumer-key and the read/write access-token - C-c o i or M-x org-trello-install-key-and-token"))
(defun orgtrello-controller--on-entity-p (entity)
"Compute if the org-trello ENTITY exists.
If it does not not, error."
(if entity
:ok
"You need to be on an org-trello entity (card/checklist/item) for this action to occur!"))
(defun orgtrello-controller--right-level-p (entity)
"Compute if the ENTITY level is correct (not higher than level 4)."
(if (and entity (< (-> entity
orgtrello-data-current
orgtrello-data-entity-level)
org-trello--out-of-bounds-level))
:ok
"Wrong level. Do not deal with entity other than card/checklist/item!"))
(defun orgtrello-controller--already-synced-p (entity)
"Compute if the ENTITY has already been synchronized."
(if (-> entity orgtrello-data-current orgtrello-data-entity-id)
:ok
"Entity must be synchronized with trello first!"))
(defun orgtrello-controller--entity-mandatory-name-ok-p (simple-entity)
"Ensure SIMPLE-ENTITY can be synced regarding the mandatory data."
(if simple-entity
(let* ((level (orgtrello-data-entity-level simple-entity))
(name (orgtrello-data-entity-name simple-entity)))
(if (and name (< 0 (length name)))
:ok
(cond ((= level org-trello--card-level)
org-trello--error-sync-card-missing-name)
((= level org-trello--checklist-level)
org-trello--error-sync-checklist-missing-name)
((= level org-trello--item-level)
org-trello--error-sync-item-missing-name))))
:ok))
(defun orgtrello-controller--mandatory-name-ok-p (entity)
"Ensure ENTITY can be synced regarding the mandatory data."
(-> entity
orgtrello-data-current
orgtrello-controller--entity-mandatory-name-ok-p))
(defun orgtrello-controller-checks-then-delete-simple ()
"Do the deletion of an entity."
(orgtrello-action-functional-controls-then-do
'(orgtrello-controller--on-entity-p
orgtrello-controller--right-level-p
orgtrello-controller--already-synced-p)
(orgtrello-buffer-safe-entry-full-metadata)
'orgtrello-controller-delete-card
(current-buffer)))
(defun orgtrello-controller-delete-card (full-meta &optional buffer-name)
"Execute on FULL-META the ACTION.
BUFFER-NAME to specify the buffer with which we currently work."
(with-current-buffer buffer-name
(let* ((current (orgtrello-data-current full-meta))
(marker (orgtrello-buffer--compute-marker-from-entry current)))
(orgtrello-buffer-set-marker-if-not-present current marker)
(orgtrello-data-put-entity-id marker current)
(eval (orgtrello-proxy-delete-entity current)))))
(defun orgtrello-controller--indent-card (entity)
"Indent properly the card information (whatever the ENTITY is)."
(orgtrello-buffer-indent-card-description)
(orgtrello-buffer-indent-card-data)
:ok)
(defun orgtrello-controller-checks-then-sync-card-to-trello ()
"Check then do the actual sync if everything is ok."
(orgtrello-action-functional-controls-then-do
'(orgtrello-controller--on-entity-p
orgtrello-controller--right-level-p
orgtrello-controller--mandatory-name-ok-p
orgtrello-controller--indent-card)
(orgtrello-buffer-safe-entry-full-metadata)
'orgtrello-controller-sync-card-to-trello
(current-buffer)))
(defun orgtrello-controller-sync-card-to-trello (full-meta &optional buffer-name)
"Do the actual card creation/update - from card to item.
FULL-META is actually dismissed and recomputed here.
BUFFER-NAME is the buffer on to which act."
(let ((current-checksum (orgtrello-buffer-card-checksum))
(previous-checksum (orgtrello-buffer-get-card-local-checksum)))
(if (string= current-checksum previous-checksum)
(orgtrello-log-msg orgtrello-log-info
"Card already synchronized, nothing to do!")
(orgtrello-log-msg orgtrello-log-info
"Synchronizing card on board '%s'..."
(orgtrello-buffer-board-name))
(org-show-subtree) ;; show subtree, otherwise org-trello/org-trello/#53
(-> buffer-name
orgtrello-buffer-build-org-card-structure
orgtrello-controller-execute-sync-entity-structure))))
(defun orgtrello-controller-do-sync-buffer-to-trello ()
"Full `org-mode' file synchronization."
(orgtrello-log-msg orgtrello-log-warn
"Synchronizing org-mode file to board '%s'. This may take some time, some coffee may be a good idea..."
(orgtrello-buffer-board-name))
(-> (current-buffer)
orgtrello-buffer-build-org-entities
orgtrello-controller-execute-sync-entity-structure))
(defun orgtrello-controller--sync-buffer-with-trello-data (data)
"Update the current buffer with DATA (entities and adjacency)."
(-let (((entities adjacencies) data))
(goto-char (point-max)) ;; go at the end of the file
(maphash
(lambda (new-id entity)
(when (orgtrello-data-entity-card-p entity)
(orgtrello-buffer-write-card new-id entity entities adjacencies)))
entities)
(goto-char (point-min)) ;; go back to the beginning of file
(ignore-errors (org-sort-entries t ?o)) ;; sort entries on keywords & ignore errors (e.g. nothing to sort)
(org-global-cycle '(4)))) ;; fold all entries
(defun orgtrello-controller--cleanup-org-entries ()
"Cleanup org-entries from the buffer.
Does not preserve position."
(goto-char (point-min))
(outline-next-heading)
(orgtrello-buffer-remove-overlays (point-at-bol) (point-max))
(kill-region (point-at-bol) (point-max)))
(defun orgtrello-controller-sync-buffer-with-trello-cards (buffer-name
org-trello-cards)
"Synchronize the buffer BUFFER-NAME with the ORG-TRELLO-CARDS."
(with-local-quit
(with-current-buffer buffer-name
(save-excursion
(let ((entities-from-org-buffer
(orgtrello-buffer-build-org-entities buffer-name)))
(-> org-trello-cards
orgtrello-backend-compute-org-trello-card-from
(orgtrello-data-merge-entities-trello-and-org
entities-from-org-buffer)
((lambda (entry) (orgtrello-controller--cleanup-org-entries) entry)) ;; hack to clean the org entries just before synchronizing the buffer
orgtrello-controller--sync-buffer-with-trello-data))))))
(defun orgtrello-controller--retrieve-archive-cards (data)
"Retrieve the archive cards from DATA.
DATA is a list of (board-id &rest buffer-name point-start)."
(-> data
car
orgtrello-api-get-archived-cards
(orgtrello-query-http-trello 'sync)
(cons data)))
(defun orgtrello-controller--retrieve-full-cards (data)
"Retrieve the full cards from DATA.
DATA is a list of (archive-cards board-id &rest buffer-name point-start).
Return the initial list + the full cards."
(-let* (((archive-cards board-id &rest) data)
(cards (-> board-id
orgtrello-api-get-full-cards
(orgtrello-query-http-trello 'sync))))
(cons cards data)))
(defun orgtrello-controller--sync-buffer-with-archived-and-trello-cards (data)
"Update buffer at point with DATA.
DATA is a list of (cards archive-cards board-id buffername) in this order."
(-let (((trello-cards trello-archived-cards _ buffer-name &rest) data))
;; first archive the cards that needs to be
(orgtrello-log-msg orgtrello-log-debug
"Archived trello-cards: %S"
trello-archived-cards)
(orgtrello-buffer-archive-cards trello-archived-cards)
;; Then update the buffer with the other opened trello cards
(orgtrello-log-msg orgtrello-log-debug
"Opened trello-cards: %S"
trello-cards)
(->> trello-cards
(mapcar #'orgtrello-data-to-org-trello-card)
(orgtrello-controller-sync-buffer-with-trello-cards
buffer-name))
;; keep the state clean and returns it
data))
(defun orgtrello-controller--after-sync-buffer-with-trello-cards (data)
"Given the state DATA, save the buffer and return at initial point."
(-let (((_ _ _ buffer-name board-name point-start) data))
(orgtrello-buffer-save-buffer buffer-name)
(goto-char point-start)))
(defun orgtrello-controller-do-sync-buffer-from-trello ()
"Full `org-mode' file synchronization.
Beware, this will block Emacs as the request is synchronous."
(let* ((buffer-name (current-buffer))
(board-name (orgtrello-buffer-board-name))
(point-start (point))
(board-id (orgtrello-buffer-board-id))
(prefix-log (format "Sync trello board '%s' to buffer '%s'..."
board-name
buffer-name)))
(orgtrello-deferred-eval-computation
(list board-id buffer-name board-name point-start)
`('orgtrello-controller--retrieve-archive-cards
'orgtrello-controller--retrieve-full-cards
'orgtrello-controller--sync-buffer-with-archived-and-trello-cards
'orgtrello-controller--after-sync-buffer-with-trello-cards
(orgtrello-controller-log-success ,prefix-log))
prefix-log)))
(defun orgtrello-controller--check-user-account (data)
"Check the user account in DATA is ok.
DATA is a list of (user)."
(let ((user-me (car data)))
(orgtrello-log-msg
orgtrello-log-info
(if user-me
(format "Account %s configured! Everything is ok!"
(orgtrello-data-entity-username user-me))
"There is a problem with your credentials.\nMake sure you used M-x org-trello-install-key-and-token and this installed correctly the consumer-key and access-token.\nSee http://org-trello.github.io/trello-setup.html#credentials for more information."))))
(defun orgtrello-controller--fetch-user-logged-in (data)
"Fetch the user logged in and return the result in DATA.
DATA is a list of (boards buffername)."
(-> (orgtrello-api-get-me)
(orgtrello-query-http-trello 'sync)
(cons data)))
(defun orgtrello-controller-check-trello-connection ()
"Full `org-mode' file synchronization.
Beware, this will block Emacs as the request is synchronous."
(orgtrello-deferred-eval-computation
nil
'('orgtrello-controller--fetch-user-logged-in
'orgtrello-controller--check-user-account)
"Checking trello connection..."
'no-success-log))
(defun orgtrello-controller--map-cards-to-computations (entities-adjacencies)
"Given an ENTITIES-ADJACENCIES structure, map to computations.
The ENTITY-STRUCTURE is self contained.
Entities at first position, adjacency list in second position.
Synchronization is done here."
(lexical-let ((entities (car entities-adjacencies))
(entities-adjacencies entities-adjacencies)
(card-computations))
(maphash (lambda (id entity)
(when (and (orgtrello-data-entity-card-p entity)
(eq :ok
(orgtrello-controller--entity-mandatory-name-ok-p
entity)))
(-> entity
(orgtrello-proxy-sync-entity entities-adjacencies)
(push card-computations))))
entities)
(nreverse card-computations)))
(defun orgtrello-controller-execute-sync-entity-structure (entities-adjacencies)
"Execute synchronization of ENTITY-STRUCTURE.
ENTITIES-ADJACENCIES is self contained.
Entities at first position, adjacency list in second position (children).
Synchronization is done here.
Along the way, the buffer BUFFER-NAME is written with new information."
(-if-let (card-computations (orgtrello-controller--map-cards-to-computations
entities-adjacencies))
(orgtrello-proxy-execute-async-computations
card-computations
"card(s) sync ok!"
"FAILURE! cards(s) sync KO!")
(orgtrello-log-msg orgtrello-log-info "No card(s) to sync.")))
(defun orgtrello-controller-compute-and-overwrite-card (buffer-name
org-trello-card)
"Given BUFFER-NAME & ORG-TRELLO-CARD, compute, merge & update the buffer."
(when org-trello-card
(with-local-quit
(with-current-buffer buffer-name
(save-excursion
(let* ((card-id (orgtrello-data-entity-id org-trello-card))
(region (orgtrello-entity-card-region))
(entities-from-org-buffer (apply
'orgtrello-buffer-build-org-entities
(cons buffer-name region)))
(entities-from-trello (orgtrello-backend-compute-org-trello-card-from
(list org-trello-card)))
(merged-entities (orgtrello-data-merge-entities-trello-and-org
entities-from-trello
entities-from-org-buffer))
(entities (car merged-entities))
(entities-adj (cadr merged-entities)))
(orgtrello-buffer-overwrite-card region
(gethash card-id entities)
entities
entities-adj)))))))
(defun orgtrello-controller-checks-then-sync-card-from-trello ()
"Check then do the actual sync if everything is ok."
(orgtrello-action-functional-controls-then-do
'(orgtrello-controller--on-entity-p
orgtrello-controller--right-level-p
orgtrello-controller--already-synced-p)
(orgtrello-buffer-safe-entry-full-metadata)
'orgtrello-controller-sync-card-from-trello
(current-buffer)))
(defun orgtrello-controller--retrieve-full-card (data)
"Retrieve the full card from the DATA.
DATA is a list of (card-meta buffer-name point-start)."
(-> data
car
orgtrello-data-entity-id
orgtrello-api-get-full-card
(orgtrello-query-http-trello 'sync)
(cons data)))
(defun orgtrello-controller--sync-buffer-with-trello-card (data)
"Sync the buffer with DATA.
DATA is a list of (full-card card-meta buffer-name point-start)."
(-let (((trello-card _ buffer-name &rest) data))
(orgtrello-log-msg orgtrello-log-debug "trello-card: %S" trello-card)
(->> trello-card
orgtrello-data-to-org-trello-card
(orgtrello-controller-compute-and-overwrite-card buffer-name))
data))
(defun orgtrello-controller--after-sync-buffer-with-trello-card (data)
"Given the state DATA, save the buffer and return at initial point."
(-let (((_ _ buffer-name card-name point-start) data))
(orgtrello-buffer-save-buffer buffer-name)
(goto-char point-start)))
(defun orgtrello-controller-sync-card-from-trello (full-meta &optional buffer-name)
"Entity FULL-META synchronization (with its structure) from `trello'.
BUFFER-NAME is the actual buffer to work on."
(let* ((point-start (point))
(card-meta (progn
(unless (orgtrello-entity-card-at-pt)
(orgtrello-entity-back-to-card))
(orgtrello-data-current
(orgtrello-buffer-entry-get-full-metadata))))
(card-name (orgtrello-data-entity-name card-meta))
(prefix-log (format "Sync trello card '%s' to buffer '%s'..."
card-name
buffer-name)))
(orgtrello-deferred-eval-computation
(list card-meta buffer-name card-name point-start)
'('orgtrello-controller--retrieve-full-card
'orgtrello-controller--sync-buffer-with-trello-card
'orgtrello-controller--after-sync-buffer-with-trello-card)
prefix-log)))
(defun orgtrello-controller--do-delete-card ()
"Delete the card."
(when (orgtrello-entity-card-at-pt)
(orgtrello-controller-checks-then-delete-simple)))
(defun orgtrello-controller-do-delete-entities ()
"Launch a batch deletion of every single entities present on the buffer.
SYNC flag permit to synchronize the http query."
(let ((do-it (if orgtrello-with-check-on-sensible-actions
(orgtrello-input-confirm "Do you want to delete all cards? ")
t)))
(when do-it
(org-map-entries 'orgtrello-controller--do-delete-card t 'file))))
(defun orgtrello-controller-checks-and-do-archive-card ()
"Check the functional requirements, then if everything is ok, archive the card."
(let ((buffer-name (current-buffer)))
(with-current-buffer buffer-name
(save-excursion
(let ((card-meta (progn
(when (orgtrello-entity-org-checkbox-p)
(orgtrello-entity-back-to-card))
(orgtrello-buffer-entry-get-full-metadata))))
(orgtrello-action-functional-controls-then-do
'(orgtrello-controller--right-level-p
orgtrello-controller--already-synced-p)
card-meta
'orgtrello-controller-do-archive-card
buffer-name))))))
(defun orgtrello-controller--archive-that-card (data)
"Archive the card present in the DATA structure.
DATA is a list of (card-meta card-name buffer-name point-start)."
(-let (((card-meta card-name &rest) data))
(-> card-meta
orgtrello-data-entity-id
orgtrello-api-archive-card
(orgtrello-query-http-trello 'sync)
(cons data))))
(defun orgtrello-controller--sync-buffer-with-archive (data) ;; org archive
"Update buffer with archive DATA."
(-let (((_ _ card-name buffer-name point-start) data))
(with-current-buffer buffer-name
(goto-char point-start)
(org-archive-subtree))
(orgtrello-buffer-save-buffer buffer-name)))
(defun orgtrello-controller-do-archive-card (full-meta &optional buffer-name)
"Archive current FULL-META at point.
BUFFER-NAME specifies the buffer onto which we work."
(save-excursion
(let* ((point-start (point))
(card-meta (orgtrello-data-current full-meta))
(card-name (orgtrello-data-entity-name card-meta))
(prefix-log (format "Archive card '%s'..." card-name)))
(orgtrello-deferred-eval-computation
(list card-meta card-name buffer-name point-start)
'('orgtrello-controller--archive-that-card
'orgtrello-controller--sync-buffer-with-archive)
prefix-log))))
(defun orgtrello-controller--do-install-config-file (user-login
consumer-key
access-token
&optional overwrite-ask)
"Persist the setup file with USER-LOGIN, CONSUMER-KEY and ACCESS-TOKEN.
OVERWRITE-ASK is a flag to set to prevent overwriting."
(let ((new-user-config-file (orgtrello-controller-config-file user-login)))
(make-directory org-trello--config-dir 'do-create-parent-if-need-be)
(with-temp-file new-user-config-file
(erase-buffer)
(goto-char (point-min))
(insert (format "(setq org-trello-consumer-key \"%s\"
org-trello-access-token \"%s\")\n"
consumer-key access-token))
(write-file new-user-config-file overwrite-ask))))
(defun orgtrello-controller--open-access-token-consumer-key-url (data)
"Browse url for the consumer-key/access-token url from DATA."
(browse-url (orgtrello-setup-compute-url "/1/appKey/generate"))
data)
(defun orgtrello-controller--open-ask-permissions-url (data)
"Open the ask permissions url.
DATA is not read."
(let ((consumer-key (s-trim (orgtrello-input-read-not-empty "Consumer key: "))))
(->> consumer-key
(format "/1/authorize?response_type=token&name=org-trello&scope=read,write&expiration=never&key=%s")
orgtrello-setup-compute-url
browse-url)
(cons consumer-key data)))
(defun orgtrello-controller--read-access-token (data)
"Input the access token.
Returns DATA."
(-> (orgtrello-input-read-not-empty "Access token: ")
s-trim
(cons data)))
(defun orgtrello-controller--install-key-and-token-login-from-data (data)
"Install key and token from DATA."
(-let (((access-token consumer-key user-login) data))
(orgtrello-controller--do-install-config-file user-login consumer-key access-token 'do-ask-for-overwrite)))
(defun orgtrello-controller-do-install-key-and-token ()
"Procedure to install consumer-key and access token as user config-file."
(let* ((user-login
(orgtrello-input-read-not-empty
"Trello login account (you need to be logged accordingly in trello.com as we cannot check this for you): "))
(user-config-file (orgtrello-controller-config-file user-login))
(prefix-log "Install key and token..."))
(if (file-exists-p user-config-file)
(orgtrello-log-msg
orgtrello-log-info
"Configuration for user %s already existing (file %s), skipping."
user-login user-config-file)
(orgtrello-deferred-eval-computation
(list user-login)
'('orgtrello-controller--open-access-token-consumer-key-url
'orgtrello-controller--open-ask-permissions-url
'orgtrello-controller--read-access-token
'orgtrello-controller--install-key-and-token-login-from-data)
prefix-log))))
(defun orgtrello-controller--name-id (entities)
"Given a list of ENTITIES, return a map of (id, name)."
(--reduce-from
(orgtrello-hash-puthash-data
(orgtrello-data-entity-name it)
(orgtrello-data-entity-id it)
acc)
(orgtrello-hash-empty-hash) entities))
(defun orgtrello-controller--list-boards ()
"Return the map of the existing boards associated to the current account.
Synchronous request."
(orgtrello-query-http-trello (orgtrello-api-get-boards "open") 'sync))
(defun orgtrello-controller--list-board-lists (board-id)
"Return the map of the existing list of the board with id BOARD-ID.
Synchronous request."
(orgtrello-query-http-trello (orgtrello-api-get-lists board-id) 'sync))
(defun orgtrello-controller-choose-board (boards)
"Given a BOARDS map, ask the user to choose from.
This returns the identifier of such board."
(-> (orgtrello-input-read-string-completion
"Board to install (TAB to complete): "
(orgtrello-hash-keys boards))
(gethash boards)))
(defun orgtrello-controller--convention-property-name (name)
"Given a NAME, use the org property convention used in org-file headers."
(->> name
(replace-regexp-in-string " " "-")
(replace-regexp-in-string "[()]" "")))
(defun orgtrello-controller--delete-buffer-property (property-name)
"A simple routine to delete a #+PROPERTY: PROPERTY-NAME from the buffer."
(save-excursion
(goto-char (point-min))
(while (search-forward property-name nil t)
(delete-region (point-at-bol) (point-at-eol))
(delete-blank-lines))))
(defun orgtrello-controller-compute-property (prop-name &optional prop-value)
"Compute a formatted entry from PROP-NAME and optional PROP-VALUE."
(format "#+PROPERTY: %s %s" prop-name (if prop-value prop-value "")))
(defun orgtrello-controller--compute-hash-name-id-to-list (users-hash-name-id)
"Compute the hashmap of name-id to list from USERS-HASH-NAME-ID."
(let ((res-list nil))
(maphash
(lambda (name id)
(--> name
(replace-regexp-in-string org-trello--label-key-user-prefix "" it)
(format "%s%s" org-trello--label-key-user-prefix it)
(orgtrello-controller-compute-property it id)
(push it res-list)))
users-hash-name-id)
res-list))
(defun orgtrello-controller--remove-properties-file (org-keywords
users-hash-name-id
user-me
&optional update-todo-kwds)
"Remove the current org-trello header metadata.
ORG-KEYWORDS is the `org-mode' keywords
USERS-HASH-NAME-ID is a map of username to id
USER-ME is the user's name
UPDATE-TODO-KWDS is the org list of keywords.
Works only on properties file."
(with-current-buffer (current-buffer)
(apply 'narrow-to-region (orgtrello-buffer-global-properties-region))
;; compute the list of properties to purge
(->> `(":PROPERTIES"
,(orgtrello-controller-compute-property
org-trello--property-board-name)
,(orgtrello-controller-compute-property
org-trello--property-board-id)
,@(-map (-compose #'orgtrello-controller-compute-property
(-partial #'orgtrello-controller--convention-property-name))
org-keywords)
,@(orgtrello-controller--compute-hash-name-id-to-list
users-hash-name-id)
,(orgtrello-controller-compute-property org-trello--property-user-me
user-me)
,(when update-todo-kwds "#+TODO: ")
,@(-map (-partial 'format "#+PROPERTY: %s")
(orgtrello-buffer-colors))
":END:")
(mapc 'orgtrello-controller--delete-buffer-property))
(widen)
(save-excursion
(goto-char (point-min))
(delete-blank-lines))))
(defun orgtrello-controller--properties-labels (board-labels)
"Compute properties labels from BOARD-LABELS.
\[Dict symbol String\] -> [String]"
(let ((res-list))
(-map (lambda (hashm)
(message "color key: %S" (gethash :color hashm "grey"))
(-> (format "#+PROPERTY: %s %s"
(gethash (gethash :color hashm)
orgtrello-setup-data-color-keywords
:grey)
(gethash :name hashm ""))
s-trim-right
(push res-list)))
board-labels)
res-list))
(defun orgtrello-controller--compute-metadata (board-name
board-id
board-lists-hash-name-id
board-users-hash-name-id
user-me
board-labels
&optional update-todo-keywords)
"Compute the org-trello metadata to dump on header file.
BOARD-NAME the current board's name
BOARD-ID the current board's id
BOARD-LISTS-HASH-NAME-ID is a map of the board's trello list name to ids
BOARD-USERS-HASH-NAME-ID is a map of username to id
USER-ME is the user's name
BOARD-LABELS the board's labels
UPDATE-TODO-KEYWORDS is the org list of keywords."
`(":PROPERTIES:"
,(orgtrello-controller-compute-property org-trello--property-board-name
board-name)
,(orgtrello-controller-compute-property org-trello--property-board-id
board-id)
,@(orgtrello-controller--compute-board-lists-hash-name-id
board-lists-hash-name-id)
,(if update-todo-keywords
(orgtrello-controller--properties-compute-todo-keywords-as-string
board-lists-hash-name-id) "")
,@(orgtrello-controller--properties-compute-users-ids
board-users-hash-name-id)
,@(orgtrello-controller--properties-labels board-labels)
,(format "#+PROPERTY: %s %s"
org-trello--property-user-me
(if user-me user-me org-trello--user-logged-in))
":END:"))
(defun orgtrello-controller--compute-keyword-separation (name)
"Given a insensitive keyword NAME, return a string '| done' or the keyword."
(if (string= "done" (downcase name))
(format "| %s" name)
name))
(defun orgtrello-controller--compute-board-lists-hash-name-id
(board-lists-hash-name-id)
"Compute board lists of key/name from BOARD-LISTS-HASH-NAME-ID."
(let ((res-list))
(maphash
(lambda (name id)
(--> (orgtrello-controller--convention-property-name name)
(format "#+PROPERTY: %s %s" it id)
(push it res-list)))
board-lists-hash-name-id)
res-list))
(defun orgtrello-controller--properties-compute-todo-keywords-as-string
(board-lists-hash-name-id)
"Compute org keywords from the BOARD-LISTS-HASH-NAME-ID."
(s-join " " `("#+TODO:"
,@(let ((res-list))
(maphash
(lambda (name _)
(-> name
orgtrello-controller--convention-property-name
orgtrello-controller--compute-keyword-separation
(push res-list)))
board-lists-hash-name-id)
(nreverse res-list)))))
(defun orgtrello-controller--properties-compute-users-ids
(board-users-hash-name-id)
"Given BOARD-USERS-HASH-NAME-ID, compute the properties for users."
(let ((res-list))
(maphash (lambda (name id) (--> name
(format "#+PROPERTY: %s%s %s"
org-trello--label-key-user-prefix
it
id)
(push it res-list)))
board-users-hash-name-id)
res-list))
(defun orgtrello-controller--update-orgmode-file-with-properties
(board-name
board-id
board-lists-hash-name-id
board-users-hash-name-id
user-me
board-labels
&optional update-todo-keywords)
"Update the orgmode file with the needed headers for org-trello to work.
BOARD-NAME the current board's name
BOARD-ID the current board's id
BOARD-LISTS-HASH-NAME-ID is a map of the board's trello list name to ids
BOARD-USERS-HASH-NAME-ID is a map of username to id
USER-ME is the user's name
BOARD-LABELS the board's labels
UPDATE-TODO-KEYWORDS is the org list of keywords."
(with-current-buffer (current-buffer)
(goto-char (point-min))
(set-buffer-file-coding-system 'utf-8-auto) ;; force utf-8
(->> (orgtrello-controller--compute-metadata
board-name
board-id
board-lists-hash-name-id
board-users-hash-name-id
user-me
board-labels
update-todo-keywords)
(mapc (lambda (it) (insert it "\n"))))
(goto-char (point-min))
(org-cycle)))
(defun orgtrello-controller--fetch-boards (data)
"Fetch open boards and return the result in DATA.
DATA is a list (buffername)."
(-> (orgtrello-controller--list-boards)
(cons data)))
(defun orgtrello-controller--choose-board-id (data)
"Choose the board-id routine.
DATA is a list of (user-logged-in boards buffername)"
(-let (((user-logged-in boards &rest) data))
(-> boards
orgtrello-controller--name-id
orgtrello-controller-choose-board
(cons data))))
(defun orgtrello-controller--fetch-board-information (data)
"Retrieve the detailed information on board from DATA.
DATA is a list of (board-id user-logged-in boards buffername)."
(-> data
car
orgtrello-api-get-board
(orgtrello-query-http-trello 'sync)
(cons data)))
(defun orgtrello-controller--update-buffer-with-board-metadata (data)
"Update properties in buffer from DATA.
DATA is a list of (board board-id user-logged-in boards buffername)."
(-let* (((board board-id user-logged-in &rest) data)
(members
(->> board
orgtrello-data-entity-memberships
orgtrello-controller--compute-user-properties
orgtrello-controller--compute-user-properties-hash)))
(orgtrello-controller-do-write-board-metadata
board-id
(orgtrello-data-entity-name board)
(if (hash-table-p user-logged-in)
(orgtrello-data-entity-username user-logged-in)
user-logged-in)
(orgtrello-data-entity-lists board)
(orgtrello-data-entity-labels board)
members)
data))
(defun orgtrello-controller--save-buffer-and-reload-setup (data)
"Save the buffer and reload the setup from DATA.
DATA is a list and the buffername is the last element of it."
(let ((buffer-name (car (last data))))
(orgtrello-buffer-save-buffer buffer-name)
(orgtrello-action-reload-setup)))
(defun orgtrello-controller-do-install-board-and-lists ()
"Command to install the list boards."
(let ((buffer-name (current-buffer))
(prefix-log "Install board metadata in buffer..."))
(orgtrello-deferred-eval-computation
(list buffer-name)
'('orgtrello-controller--fetch-boards ;; [[boards] buffer]
'orgtrello-controller--fetch-user-logged-in ;; [user [board] buffer]
'orgtrello-controller--choose-board-id ;; [board-id user [board] buffer]
'orgtrello-controller--fetch-board-information ;; [board board-id user [board] buffer]
'orgtrello-controller--update-buffer-with-board-metadata ;; [board board-id user [board] buffer]
'orgtrello-controller--save-buffer-and-reload-setup)
prefix-log)))
(defun orgtrello-controller--close-board (data)
"Close the board present in DATA.
DATA is a list of (board-id user boards buffername)."
(-let (((board-id _ _ &rest) data))
(-> data
car
orgtrello-api-close-board
(orgtrello-query-http-trello 'sync))
data))
(defun orgtrello-controller-do-close-board ()
"Command to install the list boards."
(orgtrello-deferred-eval-computation
nil
'('orgtrello-controller--fetch-boards ;; [[boards]]
'orgtrello-controller--fetch-user-logged-in ;; [user [board]]
'orgtrello-controller--choose-board-id ;; [board-id [board]]
'orgtrello-controller--close-board)
"Close board according to your wishes buffer..."))
(defun orgtrello-controller--compute-user-properties (memberships-map)
"Given a map MEMBERSHIPS-MAP, extract the map of user information."
(mapcar 'orgtrello-data-entity-member memberships-map))
(defun orgtrello-controller--compute-user-properties-hash (user-properties)
"Compute user's properties from USER-PROPERTIES."
(--reduce-from (orgtrello-hash-puthash-data
(orgtrello-data-entity-username it)
(orgtrello-data-entity-id it)
acc)
(orgtrello-hash-empty-hash)
user-properties))
(defun orgtrello-controller--create-board (board-name &optional board-desc)
"Create a board with name BOARD-NAME and optionally a BOARD-DESC."
(orgtrello-log-msg orgtrello-log-info
"Creating board '%s' with description '%s'"
board-name
board-desc)
(orgtrello-query-http-trello
(orgtrello-api-add-board board-name board-desc)
'sync))