|
|
@@ -7,6 +7,7 @@
|
|
|
[logseq.common.util.date-time :as date-time-util]
|
|
|
[logseq.db :as ldb]
|
|
|
[logseq.db.frontend.property :as db-property]
|
|
|
+ [logseq.db.frontend.property.build :as db-property-build]
|
|
|
[logseq.db.frontend.property.type :as db-property-type]
|
|
|
[logseq.db.sqlite.util :as sqlite-util]
|
|
|
[logseq.outliner.pipeline :as outliner-pipeline]))
|
|
|
@@ -107,24 +108,24 @@
|
|
|
|
|
|
(defmulti handle-command (fn [action-id & _others] action-id))
|
|
|
|
|
|
-(defn- repeat-until-future-timestamp
|
|
|
+(defn- repeat-timestamp
|
|
|
[datetime recur-unit frequency period-f keep-week?]
|
|
|
- (let [now (t/now)]
|
|
|
- (if (t/after? datetime now)
|
|
|
- datetime
|
|
|
- (let [v (period-f (t/interval datetime now))
|
|
|
- delta (->> (Math/ceil (/ (if (zero? v) 1 v) frequency))
|
|
|
- (* frequency)
|
|
|
- recur-unit)
|
|
|
- result (t/plus datetime delta)
|
|
|
- w1 (t/day-of-week datetime)
|
|
|
- w2 (t/day-of-week result)]
|
|
|
- (if (and keep-week? (not= w1 w2))
|
|
|
- ;; next week
|
|
|
- (if (> w2 w1)
|
|
|
- (t/plus result (t/days (- 7 (- w2 w1))))
|
|
|
- (t/plus result (t/days (- w1 w2))))
|
|
|
- result)))))
|
|
|
+ (let [now (t/now)
|
|
|
+ v (if (t/after? datetime now)
|
|
|
+ 1
|
|
|
+ (period-f (t/interval datetime now)))
|
|
|
+ delta (->> (Math/ceil (/ (if (zero? v) 1 v) frequency))
|
|
|
+ (* frequency)
|
|
|
+ recur-unit)
|
|
|
+ result (t/plus datetime delta)
|
|
|
+ w1 (t/day-of-week datetime)
|
|
|
+ w2 (t/day-of-week result)]
|
|
|
+ (if (and keep-week? (not= w1 w2))
|
|
|
+ ;; next week
|
|
|
+ (if (> w2 w1)
|
|
|
+ (t/plus result (t/days (- 7 (- w2 w1))))
|
|
|
+ (t/plus result (t/days (- w1 w2))))
|
|
|
+ result)))
|
|
|
|
|
|
(defn- get-next-time
|
|
|
[current-value unit frequency]
|
|
|
@@ -142,18 +143,24 @@
|
|
|
(let [delta (recur-unit frequency)
|
|
|
next-time (case (:db/ident unit)
|
|
|
:logseq.property.repeat/recur-unit.year
|
|
|
- (repeat-until-future-timestamp default-timezone-time recur-unit frequency period-f false)
|
|
|
+ (repeat-timestamp default-timezone-time recur-unit frequency period-f false)
|
|
|
:logseq.property.repeat/recur-unit.month
|
|
|
- (repeat-until-future-timestamp default-timezone-time recur-unit frequency period-f false)
|
|
|
+ (repeat-timestamp default-timezone-time recur-unit frequency period-f false)
|
|
|
:logseq.property.repeat/recur-unit.week
|
|
|
- (repeat-until-future-timestamp default-timezone-time recur-unit frequency period-f true)
|
|
|
- (t/plus (t/now) delta))]
|
|
|
+ (repeat-timestamp default-timezone-time recur-unit frequency period-f true)
|
|
|
+ (t/plus default-timezone-time delta))]
|
|
|
(tc/to-long next-time)))))
|
|
|
|
|
|
-(defmethod handle-command :reschedule [_ db entity _datoms]
|
|
|
- (let [property-ident (or (:db/ident (:logseq.property.repeat/temporal-property entity))
|
|
|
- :logseq.property/scheduled)
|
|
|
- frequency (db-property/property-value-content (:logseq.property.repeat/recur-frequency entity))
|
|
|
+(defn- compute-reschedule-property-tx
|
|
|
+ [conn db entity property-ident]
|
|
|
+ (let [frequency (or (db-property/property-value-content (:logseq.property.repeat/recur-frequency entity))
|
|
|
+ (let [property (d/entity db :logseq.property.repeat/recur-frequency)
|
|
|
+ default-value-block (db-property-build/build-property-value-block property property 1)
|
|
|
+ default-value-tx-data [default-value-block
|
|
|
+ {:db/id (:db/id property)
|
|
|
+ :logseq.property/default-value [:block/uuid (:block/uuid default-value-block)]}]]
|
|
|
+ (d/transact! conn default-value-tx-data)
|
|
|
+ 1))
|
|
|
unit (:logseq.property.repeat/recur-unit entity)
|
|
|
property (d/entity db property-ident)
|
|
|
date? (= :date (:logseq.property/type property))
|
|
|
@@ -175,12 +182,28 @@
|
|
|
(when value
|
|
|
[[:db/add (:db/id entity) property-ident value]])))))))
|
|
|
|
|
|
-(defmethod handle-command :set-property [_ _db entity _datoms property value]
|
|
|
+(defmethod handle-command :reschedule [_ conn db entity _datoms]
|
|
|
+ (let [property-ident (or (:db/ident (:logseq.property.repeat/temporal-property entity))
|
|
|
+ :logseq.property/scheduled)
|
|
|
+ other-property-idents (cond
|
|
|
+ (and (= property-ident :logseq.property/scheduled)
|
|
|
+ (:logseq.property/deadline entity))
|
|
|
+ [:logseq.property/deadline]
|
|
|
+
|
|
|
+ (and (= property-ident :logseq.property/deadline)
|
|
|
+ (:logseq.property/scheduled entity))
|
|
|
+ [:logseq.property/scheduled]
|
|
|
+
|
|
|
+ :else
|
|
|
+ (filter (fn [p] (get entity p)) [:logseq.property/deadline :logseq.property/scheduled]))]
|
|
|
+ (mapcat #(compute-reschedule-property-tx conn db entity %) (distinct (cons property-ident other-property-idents)))))
|
|
|
+
|
|
|
+(defmethod handle-command :set-property [_ _db _conn entity _datoms property value]
|
|
|
(let [property' (get-property entity property)
|
|
|
value' (get-value entity property value)]
|
|
|
[[:db/add (:db/id entity) property' value']]))
|
|
|
|
|
|
-(defmethod handle-command :record-property-history [_ db entity datoms]
|
|
|
+(defmethod handle-command :record-property-history [_ _conn db entity datoms]
|
|
|
(let [changes (keep (fn [d]
|
|
|
(let [property (d/entity db (:a d))]
|
|
|
(when (and (true? (get property :logseq.property/enable-history?))
|
|
|
@@ -198,7 +221,7 @@
|
|
|
:logseq.property.history/property (:db/id property)})))
|
|
|
changes)))
|
|
|
|
|
|
-(defmethod handle-command :default [command _db entity datoms]
|
|
|
+(defmethod handle-command :default [command _conn _db entity datoms]
|
|
|
(throw (ex-info "Unhandled command"
|
|
|
{:command command
|
|
|
:entity entity
|
|
|
@@ -206,12 +229,12 @@
|
|
|
|
|
|
(defn execute-command
|
|
|
"Build tx-data"
|
|
|
- [db entity datoms [_command {:keys [actions]}]]
|
|
|
+ [conn db entity datoms [_command {:keys [actions]}]]
|
|
|
(mapcat (fn [action]
|
|
|
- (apply handle-command (first action) db entity datoms (rest action))) actions))
|
|
|
+ (apply handle-command (first action) conn db entity datoms (rest action))) actions))
|
|
|
|
|
|
(defn run-commands
|
|
|
- [{:keys [tx-data db-after]}]
|
|
|
+ [conn {:keys [tx-data db-after]}]
|
|
|
(let [db db-after]
|
|
|
(mapcat (fn [[e datoms]]
|
|
|
(let [entity (d/entity db e)
|
|
|
@@ -223,6 +246,6 @@
|
|
|
(every? #(satisfy-condition? db entity % datoms) tx-conditions))) @*commands)]
|
|
|
(mapcat
|
|
|
(fn [command]
|
|
|
- (execute-command db entity datoms command))
|
|
|
+ (execute-command conn db entity datoms command))
|
|
|
commands)))
|
|
|
(group-by :e tx-data))))
|