Browse Source

[checkins] /where command with last chat checkins

Innokentii Enikeev 3 năm trước cách đây
mục cha
commit
2ffed021dc
1 tập tin đã thay đổi với 30 bổ sung10 xóa
  1. 30 10
      plugins/checkins.lisp

+ 30 - 10
plugins/checkins.lisp

@@ -8,7 +8,7 @@
 
 (def-db-init
   (db-execute "create table if not exists checkins_chats (from_id, chat_id, primary key (from_id, chat_id)) without rowid")
-  (db-execute "create table if not exists checkins_history (ts, from_id, lat, lon, place_id, place_name)"))
+  (db-execute "create table if not exists checkins_history (ts, from_id, from_user, lat, lon, place_id, place_name, place_icon, place_address)"))
 
 (defun db/get-chats (from-id)
   (mapcar #'car (db-select "select chat_id from checkins_chats where from_id = ?" from-id)))
@@ -16,10 +16,16 @@
   (db-execute "insert into checkins_chats (from_id, chat_id) values (?, ?)" from-id chat-id))
 (defun db/del-chat (from-id chat-id)
   (db-execute "delete from checkins_chats where from_id = ? and chat_id = ?" from-id chat-id))
-(defun db/add-checkin (from-id lat lon place-id place-name)
-  (db-execute "insert into checkins_history (ts, from_id, lat, lon, place_id, place_name) values (?, ?, ?, ?, ?, ?)"
+(defun db/add-checkin (from-id from-user lat lon place-id place-name place-icon place-address)
+  (db-execute "insert into checkins_history (ts, from_id, from_user, lat, lon, place_id, place_name, place_icon, place_address) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
               (local-time:timestamp-to-unix (local-time:now))
-              from-id lat lon place-id place-name))
+              from-id from-user lat lon place-id place-name placeicon place-address))
+
+(defparameter +chat-checkins-window+ (* 60 60 24) "how long to look for checkins")
+(defun db/get-chat-checkins (chat-id)
+  (db-select "with numbered as (select h.*, row_number() over (partition by h.from_id order by h.ts desc) as rn from checkins_history h inner join checkins_chats c on h.from_id = c.from_id where c.chat_id = ? and h.ts >= ? order by h.ts desc) select ts, from_id, from_user, lat, lon, place_id, place_name, place_icon, place_address from numbered where rn = 1"
+             chat-id (- (local-time:timestamp-to-unix (local-time:now))
+                         +chat-checkins-window+)))
 
 (defun get-icon (res)
   (let ((cat (keyify (aget "category" res)))
@@ -105,21 +111,27 @@
         (format nil "@~a" username)
         (format nil "~a ~a" first-name last-name))))
 
+(defun checkin-message (checkin)
+  (destructuring-bind (ts from-id from-name lat lon place-id place-name place-icon place-address)
+      checkin
+    (declare (ignore from-id lat lon place-id))
+    (format nil "~a ~@[~A~]~@[ в ~A~]~@[ (~A)~]~@[ @ ~a~]"
+            (or place-icon "📍") from-name place-name place-address
+            (when ts (format-ts (local-time:unix-to-timestamp ts))))))
+
 (defun checkin (from place lat lon)
   (let* ((from-id (agets from "id"))
+         (from-name (get-name from))
          (place-id (agets place :id))
          (place-name (agets place :name))
          (place-icon (agets place :icon))
-         (place-address (agets place :address))
-         (message (format nil "~a ~@[~A~]~@[ в ~A~]~@[ (~A)~]"
-                          (or place-icon "📍")
-                          (get-name from) place-name place-address)))
-    (db/add-checkin from-id lat lon place-id place-name)
+         (place-address (agets place :address)))
+    (db/add-checkin from-id from-name lat lon place-id place-name place-icon place-address)
     (loop for chat-id in (db/get-chats from-id)
           do (telegram-send-venue chat-id lat lon
                                   (format nil "~a ~@[~A~]~@[ в ~A~]"
                                           (or place-icon "📍")
-                                          (get-name from) place-name)
+                                          from-name place-name)
                                   place-address)
           ;;do (bot-send-message message :chat-id chat-id :parse-mode "markdown")
           )
@@ -184,3 +196,11 @@
                         :reply-markup (telegram-inline-keyboard-markup
                                        `(((:text "Перейти"
                                            :url ,(format nil "tg://user?id=~a" *bot-user-id*))))))))
+
+(def-message-cmd-handler handler-where (:where)
+  (let ((checkins (db/get-chat-checkins *chat-id*)))
+    (bot-send-message
+     (if checkins
+         (format nil "~{~a~^~%~}" (mapcar #'checkin-message checkins))
+         "Никто не чекинился последнее время")
+     :parse-mode "markdown")))