|
|
@@ -50,13 +50,14 @@
|
|
|
|
|
|
(defun git-append-latest-file (repo path text message)
|
|
|
(bt:with-recursive-lock-held ((git-get-repo-lock repo))
|
|
|
- (let ((repo-path (merge-pathnames path (legit:location repo))))
|
|
|
+ (let ((repo-path (merge-pathnames path (uiop:ensure-directory-pathname
|
|
|
+ (legit:location repo)))))
|
|
|
(dotimes (tries 5)
|
|
|
(let ((current (or (ignore-errors (git-read-latest-file repo path)) "")))
|
|
|
(uiop/stream:with-output-file (s repo-path
|
|
|
:if-exists :supersede
|
|
|
:if-does-not-exist :create)
|
|
|
- (format s "~A~A~%" current text)))
|
|
|
+ (format s "~A~A" current text)))
|
|
|
(legit:add repo repo-path)
|
|
|
(legit:commit repo message)
|
|
|
(handler-case
|
|
|
@@ -66,6 +67,13 @@
|
|
|
(legit:git-error ())))
|
|
|
(error "Tried 5 times to push ~A to ~A" path (legit:remote-url repo)))))
|
|
|
|
|
|
+(defun ledger/add-git (chat-id remote path text message)
|
|
|
+ (let* ((location (git-get-chat-location chat-id remote))
|
|
|
+ (repo (git-get-repo location remote)))
|
|
|
+ (git-append-latest-file repo path
|
|
|
+ (format nil "~%~A~%" text)
|
|
|
+ message)))
|
|
|
+
|
|
|
(defun ledger/get-hook-url (chat-id)
|
|
|
(get-webhook-url "ledger" chat-id (token-hmac (write-to-string chat-id))))
|
|
|
|
|
|
@@ -182,3 +190,53 @@
|
|
|
(setf (gethash chat-id *ledger/chat-journals*) nil)
|
|
|
(get-chat-journal-info chat-id info)
|
|
|
"OK"))))))))
|
|
|
+
|
|
|
+;; New entries
|
|
|
+(defun format-entry (entry)
|
|
|
+ (format nil "```~%~A```" (pta-ledger:render entry)))
|
|
|
+
|
|
|
+(defparameter +new-entry-actions+
|
|
|
+ `(("a" . "➕")
|
|
|
+ ("e" . "💲")
|
|
|
+ ("c" . "✖️")))
|
|
|
+
|
|
|
+(defun ledger/new-chat-entry (chat-id entry)
|
|
|
+ (bot-send-message chat-id (format-entry entry)
|
|
|
+ :parse-mode "markdown"
|
|
|
+ :reply-markup (telegram-inline-keyboard-markup
|
|
|
+ (list (loop for (a . l) in +new-entry-actions+
|
|
|
+ collect (list :text l
|
|
|
+ :callback-data
|
|
|
+ (encode-callback-data
|
|
|
+ chat-id :ln a 86400)))))))
|
|
|
+(defun ledger/format-add-entry-message (from)
|
|
|
+ (format nil "Ledger add from ~A ~A at ~A"
|
|
|
+ (aget "first_name" from) (aget "last_name" from)
|
|
|
+ (ledger/format-time (get-universal-time))))
|
|
|
+
|
|
|
+(defun ledger/process-add-entry (chat-id callback)
|
|
|
+ (with-secret (info (list :ledger chat-id))
|
|
|
+ (if info
|
|
|
+ (cond
|
|
|
+ ((consp info)
|
|
|
+ (handler-case (progn
|
|
|
+ (ledger/add-git
|
|
|
+ chat-id
|
|
|
+ (car info) (cadr info)
|
|
|
+ (agets callback "message" "text")
|
|
|
+ (ledger/format-add-entry-message (agets callback "from")))
|
|
|
+ "Добавил!")
|
|
|
+ (error (e)
|
|
|
+ (log:error "~A" e)
|
|
|
+ "Не смог :(")))
|
|
|
+ (:otherwise "Добавляю только в git журнал :("))
|
|
|
+ "Добавь git журнал.")))
|
|
|
+
|
|
|
+(def-callback-section-handler cb-handle-ln (:ln)
|
|
|
+ (case (keyify data)
|
|
|
+ (:a (telegram-answer-callback-query query-id :text "Добавляю...")
|
|
|
+ (telegram-send-message chat-id (ledger/process-add-entry
|
|
|
+ chat-id callback))
|
|
|
+ (telegram-edit-message-reply-markup nil :chat-id chat-id :message-id message-id))
|
|
|
+ (:e (telegram-answer-callback-query query-id :text "TBD"))
|
|
|
+ (:c (telegram-delete-message chat-id message-id))))
|