Download only new messages

Write messages counter
This commit is contained in:
Denis Zheleztsov 2017-10-01 14:28:02 +03:00
parent e87191d004
commit bfb9c75619

38
idec.el
View File

@ -133,18 +133,27 @@ Not used if `idec-smart-fetching' is not nil."
"Make filename from CONTENT unixtime and ID." "Make filename from CONTENT unixtime and ID."
(concat (nth 2 (split-string content)) "-" id)) (concat (nth 2 (split-string content)) "-" id))
(defun get-message-file (echo id content) (defun get-message-file (echo id)
"Get ECHO message filename by ID and CONTENT unixtime." "Get ECHO message filename by ID."
(concat (get-echo-dir echo) "/" (filename-to-store content id))) (concat (get-echo-dir echo) "/" id))
(defun get-counter-file (echo)
"Get ECHO counter filename."
(concat (get-echo-dir echo) "/counter"))
(defun store-message (content echo id) (defun store-message (content echo id)
"Store CONTENT from ECHO message in `idec-mail-dir' with it ID." "Store CONTENT from ECHO message in `idec-mail-dir' with it ID."
(create-echo-mail-dir echo) (create-echo-mail-dir echo)
(write-region content nil (get-message-file echo id content) nil nil nil t)) (write-region content nil (get-message-file echo id)))
(defun store-echo-counter (echo)
"Store count messages in ECHO."
(create-echo-mail-dir echo)
(write-region (echo-messages-count echo) nil (get-counter-file echo)))
(defun check-message-in-echo (msg echo) (defun check-message-in-echo (msg echo)
"Check if exists message MSG in ECHO `idec-mail-dir'." "Check if exists message MSG in ECHO `idec-mail-dir'."
(file-exists-p (concat (get-echo-dir echo) (concat "/" msg)))) (file-exists-p (get-message-file echo msg)))
(defun get-url-content (url) (defun get-url-content (url)
"Get URL content and return it without headers." "Get URL content and return it without headers."
@ -165,9 +174,8 @@ Not implemented."
(dolist (line (split-string (download-subscriptions) "\n")) (dolist (line (split-string (download-subscriptions) "\n"))
(if (string-match "\\." line) (if (string-match "\\." line)
(and (setq current-echo line) (and (setq current-echo line)
(message current-echo)) (store-echo-counter line))
(if (not (check-message-in-echo current-echo line)) (download-message current-echo line))))
(download-message current-echo line)))))
(defun get-message-content (echo msg) (defun get-message-content (echo msg)
"Get ECHO MSG content from `idec-primary-node'." "Get ECHO MSG content from `idec-primary-node'."
@ -180,7 +188,9 @@ Not implemented."
"Download ECHO message MSG to `idec-mail-dir'." "Download ECHO message MSG to `idec-mail-dir'."
(message (concat "Download message " msg " to " echo)) (message (concat "Download message " msg " to " echo))
(if (not (string-match "^$" msg)) (if (not (string-match "^$" msg))
(store-message (get-message-content echo msg) echo msg))) (if (not
(check-message-in-echo msg echo))
(store-message (get-message-content echo msg) echo msg))))
(defun download-subscriptions () (defun download-subscriptions ()
"Download messages from echoes defined in `idec-echo-subscriptions' from `idec-primary-node'." "Download messages from echoes defined in `idec-echo-subscriptions' from `idec-primary-node'."
@ -209,6 +219,15 @@ with `idec-download-offset' and `idec-download-limit'."
(string-join messages "/"))) (string-join messages "/")))
(message (concat idec-primary-node "u/m/" messages)))) (message (concat idec-primary-node "u/m/" messages))))
(defun make-count-url (echo)
"Return messages count url in `idec-primary-node' from ECHO."
(concat idec-primary-node "/x/c/" echo))
(defun echo-messages-count (echo)
"Get messages count in ECHO."
(nth 1 (split-string
(get-url-content (make-count-url echo)) ":")))
(defun display-echo-messages (messages) (defun display-echo-messages (messages)
"Display downloaded MESSAGES from echo." "Display downloaded MESSAGES from echo."
(with-output-to-temp-buffer (get-buffer-create (concat "*IDEC: browse echo*")) (with-output-to-temp-buffer (get-buffer-create (concat "*IDEC: browse echo*"))
@ -217,6 +236,7 @@ with `idec-download-offset' and `idec-download-limit'."
(defun load-echo-messages (echo) (defun load-echo-messages (echo)
"Load messages from ECHO." "Load messages from ECHO."
(store-echo-counter echo)
(display-echo-messages (get-url-content (make-echo-url echo)))) (display-echo-messages (get-url-content (make-echo-url echo))))
(defun proccess-echo-message (msg echo) (defun proccess-echo-message (msg echo)