1
0
Innocenty Enikeew 8 gadi atpakaļ
vecāks
revīzija
61e7fc501c
4 mainītis faili ar 35 papildinājumiem un 8 dzēšanām
  1. 5 3
      chat-cron.lisp
  2. 5 0
      common.lisp
  3. 1 5
      plugins/saver.lisp
  4. 24 0
      utils.lisp

+ 5 - 3
chat-cron.lisp

@@ -33,10 +33,12 @@
     (loop
        for (type chat-id schedule-text args last-run) in (db-select "select type, chat_id, schedule, args, last_run from chat_crons")
        for schedule = (apply #'clon:make-typed-cron-schedule (read-from-string schedule-text))
-       for next-time = (clon:next-time schedule :allow-now-p t :now (or last-run now))
-       when (and next-time (<= next-time now))
+       for next-time = (clon:next-time schedule :allow-now-p (not last-run) :now (or last-run now))
+       when (and next-time (<= (same-gmt-time-in-chat next-time chat-id) now))
        do (unwind-protect
-               (apply #'run-hooks :chat-cron (keyify type) chat-id schedule (read-from-string args))
+               (progn
+                 (log:info :chat-cron type chat-id schedule now next-time last-run)
+                 (apply #'run-hooks :chat-cron (keyify type) chat-id schedule (read-from-string args)))
             (db-execute "update chat_crons set last_run = ? where type = ? and chat_id = ? and schedule = ? and args = ?" next-time type chat-id schedule-text args)))))
 
 (defmacro def-chat-cron-handler (name (type &rest args) &body body)

+ 5 - 0
common.lisp

@@ -20,6 +20,7 @@
            :lists-get
            :*admins*
            :*bot-name*
+           :+hour+
            :+day+
            :add-hook
            :remove-hook
@@ -27,6 +28,7 @@
            :dekeyify
            :*settings*
            :defsetting
+           :*chat-default-timezone*
            :*backoff-start*
            :*backoff-max*
            :loop-with-error-backoff
@@ -56,6 +58,9 @@
            :format-size
            :format-interval
            :symbol-append
+           :get-chat-location
+           :get-chat-timezone
+           :same-gmt-time-in-chat
            :telegram-get-me
            :telegram-send-message
            :telegram-forward-message

+ 1 - 5
plugins/saver.lisp

@@ -3,7 +3,6 @@
   (:use :cl :chatikbot.common))
 (in-package :chatikbot.plugins.saver)
 
-(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")
 
 (defun %saver/parse-schedule (schedule)
@@ -224,10 +223,7 @@
             (/ (getf income-info :next-save) 100))))
 
 (defun %saver/is-ok-to-notify (chat-id &optional (moment (get-universal-time)))
-  (let ((tz (or (alexandria:when-let
-                    (chat-loc (aget chat-id (and (boundp '*chat-locations*) (symbol-value '*chat-locations*))))
-                  (round (- 7.5 (aget "latitude" chat-loc)) 15)) ;; Nautical time
-                *saver-default-timezone*)))
+  (let ((tz (get-chat-timezone chat-id)))
     (>= (nth 2 (multiple-value-list (decode-universal-time moment tz)))
        *saver-notify-hour*)))
 

+ 24 - 0
utils.lisp

@@ -4,7 +4,9 @@
   (:export :*admins*
            :*bot-name*
            :*hooks*
+           :+hour+
            :+day+
+           :*chat-default-timezone*
            :run-hooks
            :add-hook
            :remove-hook
@@ -45,6 +47,9 @@
            :format-size
            :format-interval
            :symbol-append
+           :get-chat-location
+           :get-chat-timezone
+           :same-gmt-time-in-chat
            :message-id
            :from-id
            :chat-id
@@ -74,6 +79,7 @@
 (defvar *admins* nil "Admins chat-ids")
 (defvar *bot-name* nil "bot name to properly handle text input")
 (defvar *hooks* (make-hash-table) "Hooks storage")
+(defparameter +hour+ (* 60 60) "Seconds in an hour")
 (defparameter +day+ (* 24 60 60) "Seconds in day")
 
 (defun run-hooks (event &rest arguments)
@@ -113,6 +119,7 @@
 (defmacro defsetting (var &optional val doc)
   `(progn (defvar ,var ,val ,doc)
           (push ',var *settings*)))
+(defsetting *chat-default-timezone* -3 "Default timezone for chat users. GMT+3")
 
 (defvar *backoff-start* 1 "Initial back-off")
 (defvar *backoff-max* 64 "Maximum back-off delay")
@@ -387,6 +394,23 @@ is replaced with replacement."
   (intern (apply #'concatenate 'string
                  (mapcar #'symbol-name symbols))))
 
+(defun get-chat-location (chat-id)
+  (let* ((forecast-package (find-package :chatikbot.plugins.forecast))
+         (chat-locations-sym (when forecast-package
+                               (intern "*CHAT-LOCATIONS*" forecast-package)))
+         (chat-locations (when (and chat-locations-sym (boundp chat-locations-sym))
+                           (symbol-value chat-locations-sym))))
+    (when chat-locations (aget chat-id chat-locations))))
+
+(defun get-chat-timezone (chat-id)
+  (or (let ((chat-loc (get-chat-location chat-id)))
+        (when chat-loc
+          (round (- 7.5 (aget "latitude" chat-loc)) 15))) ;; Nautical time
+      *chat-default-timezone*))
+
+(defun same-gmt-time-in-chat (ut chat-id)
+  (let ((tz (get-chat-timezone chat-id)))
+    (+ ut (* tz +hour+))))
 
 ;; Fix bug in local-time (following symlinks in /usr/share/zoneinfo/
 ;; leads to bad cutoff)