<commit_before>;;; Copyright 2012-2015 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 send-comment-notification-email (comment-id)
  (let* ((comment (db comment-id)) ; get the comment
         (on-id (getf comment :on))
         (on-item (db on-id))  ; get the conversation
         (on-type (getf on-item :type)) ; :converation or :transaction
        ;(participants (getf on-item :participants))
         (inventory-item (db (getf on-item :on))) ; when transaction, get inventory item
         (inventory-type (if (eq (getf inventory-item :type) :request)
                           "request" "offer"))
         (inventory-text (or (getf inventory-item :title)
                             (getf inventory-item :details)
                             (getf on-item :deleted-item-title)
                             (getf on-item :deleted-item-details)
                             (getf on-item :deleted-item-text)))
         (sender-id (car (getf comment :by)))
         (sender-group-id (cdr (getf comment :by)))
         (sender-group (db sender-group-id))
         (sender (db sender-id))
         (sender-name (getf sender :name))
         (inventory-poster (getf inventory-item :by))
         ;; get an a list of (person-id . group-id)
         (people-boxes (mapcar #'car (getf on-item :people)))
         (recipient-boxes (remove-if #'(lambda (box)
                                         (eql sender-id (car box)))
                                     people-boxes))
         ;;get a list of '(((person-id) . recipient-data) ...)
         ;;for boxes without group-ids as cdr
         (recipient-people (mapcar #'(lambda (box) (cons box (db (car box))))
                                   (remove-if #'cdr recipient-boxes)))
         ;;remove people who don't want notification emails
         (valid-recipient-people (remove-if-not #'(lambda (recipient)
                                                    (getf (cdr recipient)
                                                          :notify-message))
                                                recipient-people))
         (group-ids (remove-duplicates
                      (remove nil (mapcar #'cdr recipient-boxes))))
         (valid-recipient-group-admin-boxes
           (apply #'append
                  (mapcar #'(lambda (groupid)
                              (let ((group (db groupid))
                                    (admin-boxes))
                                ;; make sure it's actually a group
                                (when (eql (getf group :type) :group)
                                  (dolist (id (getf group :notify-message))
                                    (push (cons id groupid) admin-boxes))
                                  admin-boxes)))
                          group-ids)))
         (valid-recipient-group-admins
           (mapcar #'(lambda (box) (cons box (db (car box))))
                   ;; don't send mult. mess. to admins of more than 1 group
                   (remove-duplicates
                     ;; don't send 2 messages if person is a recipient
                     ;; and also an admin of a group that is a recipient
                     (remove-if #'(lambda (person-id)
                                    (or (eql person-id sender-id)
                                        (find person-id
                                              (mapcar #'caar
                                                  valid-recipient-people))))
                                valid-recipient-group-admin-boxes
                                :key #'car)
                     :key #'car)))
         (all-recipients (append valid-recipient-people
                                 valid-recipient-group-admins))
        ;(deleted-item-type (getf on-item :deleted-item-type))
        ;(text (if (and deleted-item-type
        ;               (eql comment-id (apply #'min (gethash on-id *comment-index*))))
        ;        (deleted-invalid-item-reply-text
        ;          (db (car (remove sender-id participants)) :name)
        ;          sender-name
        ;          deleted-item-type
        ;          (getf comment :text))
        ;        (getf comment :text)))
         )

    (flet ((subject-text (groupid)
             (if (eq on-type :transaction)
               (s+ sender-name
                   " has replied to "
                 (if (eql sender-id inventory-poster)
                   " your message about their "
                   (aif groupid (s+ (db it :name) "'s ") "your "))
                   inventory-type)
               (s+ "New message from " sender-name " on Kindista"))))

      (dolist (recipient all-recipients)
        (let* ((groupid (cdar recipient))
               (person (cdr recipient))
               (email (car (getf person :emails)))
               (unsub-key (getf person :unsubscribe-key))
              ;(people (name-list (remove (caar recipient)
              ;                           participants)
              ;                   :func #'person-name
              ;                   :maximum-links 5))
               (subject (subject-text groupid)))

          (when (or *productionp*
                    (getf person :test-user))
            (cl-smtp:send-email
            +mail-server+
            "Kindista <<EMAIL>>"<commit_msg>send test emails to admins<commit_after>;;; Copyright 2012-2015 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 send-comment-notification-email (comment-id)
  (let* ((comment (db comment-id)) ; get the comment
         (on-id (getf comment :on))
         (on-item (db on-id))  ; get the conversation
         (on-type (getf on-item :type)) ; :converation or :transaction
        ;(participants (getf on-item :participants))
         (inventory-item (db (getf on-item :on))) ; when transaction, get inventory item
         (inventory-type (if (eq (getf inventory-item :type) :request)
                           "request" "offer"))
         (inventory-text (or (getf inventory-item :title)
                             (getf inventory-item :details)
                             (getf on-item :deleted-item-title)
                             (getf on-item :deleted-item-details)
                             (getf on-item :deleted-item-text)))
         (sender-id (car (getf comment :by)))
         (sender-group-id (cdr (getf comment :by)))
         (sender-group (db sender-group-id))
         (sender (db sender-id))
         (sender-name (getf sender :name))
         (inventory-poster (getf inventory-item :by))
         ;; get an a list of (person-id . group-id)
         (people-boxes (mapcar #'car (getf on-item :people)))
         (recipient-boxes (remove-if #'(lambda (box)
                                         (eql sender-id (car box)))
                                     people-boxes))
         ;;get a list of '(((person-id) . recipient-data) ...)
         ;;for boxes without group-ids as cdr
         (recipient-people (mapcar #'(lambda (box) (cons box (db (car box))))
                                   (remove-if #'cdr recipient-boxes)))
         ;;remove people who don't want notification emails
         (valid-recipient-people (remove-if-not #'(lambda (recipient)
                                                    (getf (cdr recipient)
                                                          :notify-message))
                                                recipient-people))
         (group-ids (remove-duplicates
                      (remove nil (mapcar #'cdr recipient-boxes))))
         (valid-recipient-group-admin-boxes
           (apply #'append
                  (mapcar #'(lambda (groupid)
                              (let ((group (db groupid))
                                    (admin-boxes))
                                ;; make sure it's actually a group
                                (when (eql (getf group :type) :group)
                                  (dolist (id (getf group :notify-message))
                                    (push (cons id groupid) admin-boxes))
                                  admin-boxes)))
                          group-ids)))
         (valid-recipient-group-admins
           (mapcar #'(lambda (box) (cons box (db (car box))))
                   ;; don't send mult. mess. to admins of more than 1 group
                   (remove-duplicates
                     ;; don't send 2 messages if person is a recipient
                     ;; and also an admin of a group that is a recipient
                     (remove-if #'(lambda (person-id)
                                    (or (eql person-id sender-id)
                                        (find person-id
                                              (mapcar #'caar
                                                  valid-recipient-people))))
                                valid-recipient-group-admin-boxes
                                :key #'car)
                     :key #'car)))
         (all-recipients (append valid-recipient-people
                                 valid-recipient-group-admins))
        ;(deleted-item-type (getf on-item :deleted-item-type))
        ;(text (if (and deleted-item-type
        ;               (eql comment-id (apply #'min (gethash on-id *comment-index*))))
        ;        (deleted-invalid-item-reply-text
        ;          (db (car (remove sender-id participants)) :name)
        ;          sender-name
        ;          deleted-item-type
        ;          (getf comment :text))
        ;        (getf comment :text)))
         )

    (flet ((subject-text (groupid)
             (if (eq on-type :transaction)
               (s+ sender-name
                   " has replied to "
                 (if (eql sender-id inventory-poster)
                   " your message about their "
                   (aif groupid (s+ (db it :name) "'s ") "your "))
                   inventory-type)
               (s+ "New message from " sender-name " on Kindista"))))

      (dolist (recipient all-recipients)
        (let* ((groupid (cdar recipient))
               (person (cdr recipient))
               (email (car (getf person :emails)))
               (unsub-key (getf person :unsubscribe-key))
              ;(people (name-list (remove (caar recipient)
              ;                           participants)
              ;                   :func #'person-name
              ;                   :maximum-links 5))
               (subject (subject-text groupid)))

          (when (or *productionp*
                    (getf person :admin)
                    (getf person :test-user))
            (cl-smtp:send-email
            +mail-server+
            "Kindista <<EMAIL>>"
{'fraction_non_alphanumeric': 0.10989101717305153, 'fraction_numerical': 0.0019815059445178335, 'mean_word_length': 1.3384169884169883, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 56, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3728866', 'n_tokens_mistral': 3172, 'n_tokens_neox': 3044, 'n_words': 962}