Explorar o código

saver - use saved chat location info to infer nautical timezone to get better notification time

Innocenty Enikeev %!s(int64=8) %!d(string=hai) anos
pai
achega
704e2b6267
Modificáronse 1 ficheiros con 17 adicións e 7 borrados
  1. 17 7
      plugins/saver.lisp

+ 17 - 7
plugins/saver.lisp

@@ -1,6 +1,6 @@
 (in-package #:chatikbot)
 
-(defsetting *saver-default-timezone* 3 "Default timezone for *saver-notify-hour* calculation. GMT+3")
+(defsetting *saver-default-timezone* -3 "Default timezone for *saver-notify-hour* calculation. GMT+3")
 (defsetting *saver-notify-hour* 11 "Notify with upcoming payments and saves at this time")
 
 (defstruct saver/payment name amount schedule started)
@@ -214,15 +214,25 @@
             (%saver/format-time (getf period-info :next-salary))
             (/ (getf period-info :total-period) 100))))
 
+(defun %saver/is-ok-to-notify (chat-id &optional (moment (get-universal-time)))
+  (let ((tz (or
+             (when (boundp '*chat-locations*)
+               (let ((chat-loc (aget chat-id (symbol-value '*chat-locations*))))
+                 (when chat-loc
+                   (round (- 7.5 (aget "latitude" chat-loc)) 15)))) ;; Nautical time
+             *saver-default-timezone*)))
+    (>= (nth 2 (multiple-value-list (decode-universal-time moment tz)))
+       *saver-notify-hour*)))
+
 (defcron process-saver (:hour '*)
-  (let* ((moment (get-universal-time))
-         (hour (nth 2 (multiple-value-list (decode-universal-time moment *saver-default-timezone*)))))
-    (when (>= hour *saver-notify-hour*)
-      (dolist (cp (saver/find-today-payments moment))
+  (let ((moment (get-universal-time)))
+    (dolist (cp (saver/find-today-payments moment))
+      (when (%saver/is-ok-to-notify (car cp) moment)
         (db-transaction
           (bot-send-message (car cp) (%saver/format-payment-notification (cdr cp) moment) :parse-mode "markdown")
-          (db/saver/set-payment-notified (car cp) (saver/payment-name (cdr cp)) moment)))
-      (dolist (cs (saver/find-today-salaries moment))
+          (db/saver/set-payment-notified (car cp) (saver/payment-name (cdr cp)) moment))))
+    (dolist (cs (saver/find-today-salaries moment))
+      (when (%saver/is-ok-to-notify (car cs) moment)
         (db-transaction
           (bot-send-message (car cs)
                             (%saver/format-salary-notification (cdr cs)