* sb-mixi.el (shimbun-comment-article): Simplify.
[elisp/mixi.git] / sb-mixi.el
index 5ca6bcd..a11ea26 100644 (file)
 
 ;;; Commentary:
 
+;; If you have bug reports and/or suggestions for improvement, please
+;; send them via <URL:http://mixi.jp/view_community.pl?id=1596390>.
+
 ;;; Code:
 
 (require 'mixi)
 (require 'shimbun)
 
-(luna-define-class shimbun-mixi (shimbun) ())
+(eval-and-compile
+  (luna-define-class shimbun-mixi (shimbun) (comment-cache))
+  (luna-define-internal-accessors 'shimbun-mixi))
 
 (defcustom shimbun-mixi-group-alist '(("new-diaries" . mixi-get-new-diaries)
                                      ("new-comments" . mixi-get-new-comments)
-                                     ("new-topics" . mixi-get-new-topics)
+                                     ("new-bbses" . mixi-get-new-bbses)
                                      ("messages" . mixi-get-messages)
                                      ("my-diaries" . "/home.pl"))
   "*An alist of mixi shimbun group definition.
 Each element looks like (NAME . URL) or (NAME . FUNCTION).
 NAME is a shimbun group name.
 URL is the URL for mixi access point of the group.  When URL is friend's, get
-his/her diaries as article.  When community's, get its topics.  When diary's
-or topic's, get its comments.
+his/her diaries as article.  When community's, get its BBSes.  When diary's
+or BBS's, get its comments.
 FUNCTION is the function for getting articles."
   :group 'shimbun
   :type '(repeat (cons :fromat "%v"
@@ -49,28 +54,41 @@ FUNCTION is the function for getting articles."
                       (radio (string :tag "URL")
                              (const :tag "New diaries" mixi-get-new-diaries)
                              (const :tag "New comments" mixi-get-new-comments)
-                             (const :tag "New topics" mixi-get-new-topics)
+                             (const :tag "New BBSes" mixi-get-new-bbses)
                              (const :tag "Messages" mixi-get-messages)
                              (function :tag "Other function")))))
 
+;; FIXME: Don't use this user option.
 (defcustom shimbun-mixi-page-articles 10
   "*How many articles are there in one page."
   :group 'shimbun
   :type 'integer)
 
 (defcustom shimbun-mixi-get-comment-p t
-  "*If non-nil, get diaries or topics together with its comments."
+  "*If non-nil, get diaries or BBSes together with its comments."
   :group 'shimbun
   :type 'boolean)
 
+(defvar shimbun-mixi-x-face-alist
+  '(("default" . "X-Face: CY;j#FoBnpK^37`-IoJvN!J^u;GciiPmMQ@T)~RP1]t8iv?v)/bVI:I\"F!JfWJvhM5{zY!=
+ h.d+'g\\I{D>Ocy?Rc4uYUyOZj2%2Kl>,x-!MCSsyi3!L}psrrC1jlF,O?Ui>qf)X;sBz`/}\\066X%$
+ siG'|4K!2?==|oB&#E'5GGH\\#z[muyQ")))
+
+(luna-define-method initialize-instance :after ((shimbun shimbun-mixi)
+                                               &rest init-args)
+  (shimbun-mixi-set-comment-cache-internal shimbun
+                                          (make-hash-table :test 'equal))
+  shimbun)
+
 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
   (mapcar 'car shimbun-mixi-group-alist))
 
 (defun shimbun-mixi-make-subject (object)
   (let ((class (mixi-object-class object)))
-    (if (eq class 'mixi-comment)
-       (concat "Re: " (mixi-object-title (mixi-comment-parent object)))
-      (mixi-object-title object))))
+    (cond ((eq class 'mixi-comment)
+          (concat "Re: " (shimbun-mixi-make-subject
+                          (mixi-comment-parent object))))
+         (t (mixi-object-title object)))))
 
 (defun shimbun-mixi-make-from (object)
   (let ((owner (mixi-object-owner object)))
@@ -106,12 +124,17 @@ FUNCTION is the function for getting articles."
           (mixi-expand-url (mixi-diary-page object)))
          ((eq class 'mixi-topic)
           (mixi-expand-url (mixi-topic-page object)))
+         ((eq class 'mixi-event)
+          (mixi-expand-url (mixi-event-page object)))
          ((eq class 'mixi-comment)
           (concat (shimbun-mixi-make-xref (mixi-comment-parent object))
                   "#comment"))
          ((eq class 'mixi-message)
           (mixi-expand-url (mixi-message-page object))))))
 
+(defun shimbun-mixi-make-body (object)
+  (mixi-object-content object))
+
 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
   (when objects
     (let (headers)
@@ -120,9 +143,6 @@ FUNCTION is the function for getting articles."
                (when (mixi-object-p object)
                  (let ((class (mixi-object-class object))
                        (id (shimbun-mixi-make-message-id object)))
-                   (when (and (eq class 'mixi-comment)
-                              (shimbun-search-id shimbun id))
-                     (throw 'stop nil))
                    (push
                     (shimbun-create-header
                      0
@@ -139,12 +159,17 @@ FUNCTION is the function for getting articles."
                     headers)
                    (when (and shimbun-mixi-get-comment-p
                               (or (eq class 'mixi-diary)
-                                  (eq class 'mixi-topic)))
+                                  (eq class 'mixi-topic)
+                                  (eq class 'mixi-event)))
                      (let ((comments (mixi-get-comments object range)))
                        (mapc (lambda (header)
                                (push header headers))
                              (shimbun-mixi-get-headers shimbun
-                                                       comments)))))))
+                                                       comments))))
+                   (when (eq class 'mixi-comment)
+                     (puthash id (mixi-comment-content object)
+                              (shimbun-mixi-comment-cache-internal
+                               shimbun))))))
              objects))
       headers)))
 
@@ -160,30 +185,31 @@ FUNCTION is the function for getting articles."
          (cond ((eq class 'mixi-friend)
                 (setq objects (mixi-get-diaries object range)))
                ((eq class 'mixi-community)
-                (setq objects (mixi-get-topics object range)))
-               ((or (eq class 'mixi-diary) (eq class 'mixi-topic))
+                (setq objects (mixi-get-bbses object range)))
+               ((or (eq class 'mixi-diary)
+                    (eq class 'mixi-topic)
+                    (eq class 'mixi-event))
                 (setq objects (mixi-get-comments object range)))
                (t (error (concat (symbol-name class)
                                  " is not supported yet.")))))
-      (when (fboundp url-or-function)
-       (setq objects (funcall url-or-function range))))
+      (setq objects (funcall url-or-function range)))
     (shimbun-sort-headers (shimbun-mixi-get-headers shimbun objects range))))
 
-(defun shimbun-comment-article (url header)
-  (let ((parent (mixi-make-object-from-url url))
-       (date (shimbun-header-date header))
-       (message-id (shimbun-header-id header)))
-    (catch 'found
-      (mapc (lambda (comment)
-             (let ((id (mixi-friend-id (mixi-comment-owner comment)))
-                   (time (shimbun-mixi-make-date comment)))
-               (when (and (string= time date)
-                          (string-match (concat "^<[0-9]+\\." id "@")
-                                        message-id))
-                 ;; FIXME: Concat parent's information?
-                 (throw 'found (mixi-comment-content comment)))))
-           ;; FIXME: Limit range?
-           (mixi-get-comments parent)))))
+(defun shimbun-comment-article (url shimbun header)
+  (let* ((message-id (shimbun-header-id header))
+        (cache (shimbun-mixi-comment-cache-internal shimbun))
+        (article (gethash message-id cache)))
+    (unless (stringp article)
+      (let ((parent (mixi-make-object-from-url url)))
+       (mapc (lambda (comment)
+               (let ((id (shimbun-mixi-make-message-id comment))
+                     ;; FIXME: Concat parent's information?
+                     (content (mixi-comment-content comment)))
+                 (puthash id content cache)
+                 (when (string= id message-id)
+                   (setq article content))))
+             (mixi-get-comments parent))))
+    article))
 
 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
                                     header &optional outbuf)
@@ -193,9 +219,9 @@ FUNCTION is the function for getting articles."
        (or (with-temp-buffer
             (let* ((url (shimbun-article-url shimbun header))
                    (article (if (string-match "#comment$" url)
-                                (shimbun-comment-article url header)
+                                (shimbun-comment-article url shimbun header)
                               ;; FIXME: Concat community information?
-                              (mixi-object-content
+                              (shimbun-mixi-make-body
                                (mixi-make-object-from-url url)))))
               (when (stringp article)
                 (insert article)))