<commit_before>;;; Copyright 2012-2013 CommonGoods Network, Inc.
;;;
;;; This file is part of Kindista.
;;;
;;; Kindista is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU Affero General Public License as published
;;; by the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Kindista is distributed in the hope that it will be useful, but WITHOUT
;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
;;; License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public License
;;; along with Kindista.  If not, see <http://www.gnu.org/licenses/>.

(in-package :kindista)

(defun result-gratitude-p (result)
  (eq (result-type result) :gratitude))

(defun users-with-new-mail ()
  (iter (for id in (hash-table-keys *db*))
        (let ((new-items (new-inbox-items id)))
          (when (and (eq (db id :type) :person)
                     (> new-items 0))
            (collect id)))))

(defun new-inbox-items (&optional (userid *userid*))
  (loop for item in (all-inbox-items :id userid)
        while (< (or (db userid :last-checked-mail) 0) (result-time item))
        unless (case (result-type item)
                 (:conversation
                   (eql (db (db (result-id item) :latest-comment) :by)
                        userid))
                 (:reply
                   (eql (db (db (result-id item) :latest-comment) :by)
                        userid))
                 (:gratitude
                   (eql (db (result-id item) :author) userid)))
        counting item into new-items
        finally (return new-items)))

(defun all-inbox-items (&key (id *userid*))
  (sort (append (gethash id *person-conversation-index*)
                (gethash id *person-notification-index*)
                (remove-if-not #'result-gratitude-p
                               (gethash id *activity-person-index*)))
    #'> :key #'result-time))

(defun inbox-items (&key (page 0) (count 20))
  (let ((start (* page count))
        (items (all-inbox-items)))
    (html
      (iter (for i from 0 to (+ start count))
            (cond
              ((< i start)
               (setf items (cdr items)))
              ((and (>= i start) items)
               (let* ((item (car items))
                      (item-data (db (result-id item))))
                 (case (result-type item)
                   (:conversation
                     (let* ((id (result-id item))
                            (latest (latest-comment id))
                            (latest-seen (cdr (assoc *userid* (getf item-data :people))))
                            (comment-data (db latest))
                            (comments (length (gethash id *comment-index*)))
                            (people (remove *userid*
                                            (cons (getf comment-data :by)
                                                  (remove (getf comment-data :by)
                                                          (mapcar #'car (getf item-data :people)))))))
                       (str
                         (card
                           (html
                             (str (h3-timestamp (result-time item)))
                             (:p :class "people"
                               (cond
                                 ((eql (getf comment-data :by) *userid*)
                                  (str "↪ "))
                                 ((not (eql latest latest-seen))
                                  (str "• ")))

                               (if people
                                 (str (name-list people))
                                 (htm (:span :class "nobody" "Empty conversation"))))

                             (:p :class "text"
                               (:span :class "title"
                                 (:a :href (strcat "/conversations/" id) (str (ellipsis (getf item-data :subject) 30)))
                                 (when (> comments 1)
                                   (htm
                                     " (" (str comments) ")")))
                               " - "
                               (:a :href (strcat "/conversations/" id)
                                (str (ellipsis (getf comment-data :text))))))))))
                   (:reply
                     (let* ((id (result-id item))
                            (latest (latest-comment id))
                            (latest-seen (cdr (assoc *userid* (getf item-data :people))))
                            (comment-data (db latest))
                            (original-item (db (getf item-data :on)))
                            (with (or (getf original-item :by)
                                      (first (remove *userid*
                                                     (mapcar #'car (db id :people))))))
                            (comments (length (gethash id *comment-index*))))
                       (str
                         (card
                           (html
                             (str (h3-timestamp (result-time item)))
                             (:p :class "people"
                               (cond
                                 ((eql (getf comment-data :by) *userid*)
                                  (str "↪ "))
                                 ((not (eql latest latest-seen))
                                  (str "• ")))

                               (if (eql (db id :by) *userid*)
                                 (htm
                                   "You replied to "
                                   (str (person-link with))
                                   "'s "
                                   (case (getf original-item :type)
                                     (:offer
                                      (htm (:a :href (strcat "/offers/" (getf item-data :on)) "offer")))
                                     (:request
                                      (htm (:a :href (strcat "/requests/" (getf item-data :on)) "request")))
                                     (t (htm (:span :class "none" "deleted offer or request")))))
                                 (htm
                                   (str (person-link (getf item-data :by)))
                                   " replied to your "
                                   (case (getf original-item :type)
                                     (:offer
                                      (htm (:a :href (strcat "/offers/" (getf item-data :on)) "offer")))
                                     (:request
                                      (htm (:a :href (strcat "/requests/" (getf item-data :on)) "request")))
                                     (t (htm (:span :class "none" "deleted offer or request")))))))

                             (:p :class "text"
                               (:span :class "title"
                                 (when (> comments 1)
                                   (htm
                                     " (" (str comments) ") - ")))
                               (:a :href (strcat "/conversations/" id)
                                (str (ellipsis (getf comment-data :text))))))))))
                   (:contact-n
                     (str
                      (card
                        (html
                          (str (h3-timestamp (result-time item)))
                          (:p (str (person-link (getf item-data :subject))) " added you as a contact.")))))

                   (:gratitude
                     (unless (eql (getf item-data :author) *userid*)
                       (str
                        (card
                          (html
                            (str (h3-timestamp (result-time item)))
                            (:p (str (person-link (getf item-data :author))) " shared " (:a :href (strcat "/gratitude/" (result-id item)) "gratitude") " for you."))))))))
               (setf items (cdr items)))

              ((and (eql i start)
                    (not items))
               (htm
                 (:div :class "small card"
                   (:em "No results")))
               (finish)))<commit_msg>added a bit of inventory text as a title (to address a user complaint of not knowing that the conversation was clickable)<commit_after>;;; Copyright 2012-2013 CommonGoods Network, Inc.
;;;
;;; This file is part of Kindista.
;;;
;;; Kindista is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU Affero General Public License as published
;;; by the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Kindista is distributed in the hope that it will be useful, but WITHOUT
;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
;;; License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public License
;;; along with Kindista.  If not, see <http://www.gnu.org/licenses/>.

(in-package :kindista)

(defun result-gratitude-p (result)
  (eq (result-type result) :gratitude))

(defun users-with-new-mail ()
  (iter (for id in (hash-table-keys *db*))
        (let ((new-items (new-inbox-items id)))
          (when (and (eq (db id :type) :person)
                     (> new-items 0))
            (collect id)))))

(defun new-inbox-items (&optional (userid *userid*))
  (loop for item in (all-inbox-items :id userid)
        while (< (or (db userid :last-checked-mail) 0) (result-time item))
        unless (case (result-type item)
                 (:conversation
                   (eql (db (db (result-id item) :latest-comment) :by)
                        userid))
                 (:reply
                   (eql (db (db (result-id item) :latest-comment) :by)
                        userid))
                 (:gratitude
                   (eql (db (result-id item) :author) userid)))
        counting item into new-items
        finally (return new-items)))

(defun all-inbox-items (&key (id *userid*))
  (sort (append (gethash id *person-conversation-index*)
                (gethash id *person-notification-index*)
                (remove-if-not #'result-gratitude-p
                               (gethash id *activity-person-index*)))
    #'> :key #'result-time))

(defun inbox-items (&key (page 0) (count 20))
  (let ((start (* page count))
        (items (all-inbox-items)))
    (html
      (iter (for i from 0 to (+ start count))
            (cond
              ((< i start)
               (setf items (cdr items)))
              ((and (>= i start) items)
               (let* ((item (car items))
                      (item-data (db (result-id item))))
                 (case (result-type item)
                   (:conversation
                     (let* ((id (result-id item))
                            (latest (latest-comment id))
                            (latest-seen (cdr (assoc *userid* (getf item-data :people))))
                            (comment-data (db latest))
                            (comments (length (gethash id *comment-index*)))
                            (people (remove *userid*
                                            (cons (getf comment-data :by)
                                                  (remove (getf comment-data :by)
                                                          (mapcar #'car (getf item-data :people)))))))
                       (str
                         (card
                           (html
                             (str (h3-timestamp (result-time item)))
                             (:p :class "people"
                               (cond
                                 ((eql (getf comment-data :by) *userid*)
                                  (str "↪ "))
                                 ((not (eql latest latest-seen))
                                  (str "• ")))

                               (if people
                                 (str (name-list people))
                                 (htm (:span :class "nobody" "Empty conversation"))))

                             (:p :class "text"
                               (:span :class "title"
                                 (:a :href (strcat "/conversations/" id) (str (ellipsis (getf item-data :subject) 30)))
                                 (when (> comments 1)
                                   (htm
                                     " (" (str comments) ")")))
                               " - "
                               (:a :href (strcat "/conversations/" id)
                                (str (ellipsis (getf comment-data :text))))))))))
                   (:reply
                     (let* ((id (result-id item))
                            (latest (latest-comment id))
                            (latest-seen (cdr (assoc *userid* (getf item-data :people))))
                            (comment-data (db latest))
                            (original-item (db (getf item-data :on)))
                            (with (or (getf original-item :by)
                                      (first (remove *userid*
                                                     (mapcar #'car (db id :people))))))
                            (comments (length (gethash id *comment-index*))))
                       (str
                         (card
                           (html
                             (str (h3-timestamp (result-time item)))
                             (:p :class "people"
                               (cond
                                 ((eql (getf comment-data :by) *userid*)
                                  (str "↪ "))
                                 ((not (eql latest latest-seen))
                                  (str "• ")))

                               (if (eql (db id :by) *userid*)
                                 (htm
                                   "You replied to "
                                   (str (person-link with))
                                   "'s "
                                   (case (getf original-item :type)
                                     (:offer
                                      (htm (:a :href (strcat "/offers/" (getf item-data :on)) "offer")))
                                     (:request
                                      (htm (:a :href (strcat "/requests/" (getf item-data :on)) "request")))
                                     (t (htm (:span :class "none" "deleted offer or request")))))
                                 (htm
                                   (str (person-link (getf item-data :by)))
                                   " replied to your "
                                   (case (getf original-item :type)
                                     (:offer
                                      (htm (:a :href (strcat "/offers/" (getf item-data :on)) "offer")))
                                     (:request
                                      (htm (:a :href (strcat "/requests/" (getf item-data :on)) "request")))
                                     (t (htm (:span :class "none" "deleted offer or request")))))))

                             (:p :class "text"
                               (:span :class "title"
                                 (:a :href (strcat "/conversations/" id)
                                   (str (ellipsis (getf original-item :text) 30)))
                                 (when (> comments 1)
                                   (htm
                                     " (" (str comments) ") "))
                                 " - ")
                               (:a :href (strcat "/conversations/" id)
                                (str (ellipsis (getf comment-data :text))))))))))
                   (:contact-n
                     (str
                      (card
                        (html
                          (str (h3-timestamp (result-time item)))
                          (:p (str (person-link (getf item-data :subject))) " added you as a contact.")))))

                   (:gratitude
                     (unless (eql (getf item-data :author) *userid*)
                       (str
                        (card
                          (html
                            (str (h3-timestamp (result-time item)))
                            (:p (str (person-link (getf item-data :author))) " shared " (:a :href (strcat "/gratitude/" (result-id item)) "gratitude") " for you."))))))))
               (setf items (cdr items)))

              ((and (eql i start)
                    (not items))
               (htm
                 (:div :class "small card"
                   (:em "No results")))
               (finish)))
{'fraction_non_alphanumeric': 0.11792592592592592, 'fraction_numerical': 0.0028444444444444446, 'mean_word_length': 0.9616412879228177, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 127, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1652884', 'n_tokens_mistral': 4233, 'n_tokens_neox': 3963, 'n_words': 1259}