]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #48 from abinoam/thmail-5
authorSimon <simon@kisikew.org>
Sun, 26 Feb 2012 02:01:09 +0000 (18:01 -0800)
committerSimon <simon@kisikew.org>
Sun, 26 Feb 2012 02:01:09 +0000 (18:01 -0800)
Comment Notifications by Email - Threading Them

boot.php
database.sql
include/enotify.php
include/items.php
mod/item.php
update.php

index d9d361ee3b9c15068b9e30626963f70ffddcbd94..54f318e5e9f0284e9dd44f2a17c701074fca85b0 100755 (executable)
--- a/boot.php
+++ b/boot.php
@@ -11,7 +11,7 @@ require_once('include/cache.php');
 define ( 'FRIENDICA_PLATFORM',     'Friendica');
 define ( 'FRIENDICA_VERSION',      '2.3.1263' );
 define ( 'DFRN_PROTOCOL_VERSION',  '2.22'    );
-define ( 'DB_UPDATE_VERSION',      1125      );
+define ( 'DB_UPDATE_VERSION',      1126      );
 
 define ( 'EOL',                    "<br />\r\n"     );
 define ( 'ATOM_TIME',              'Y-m-d\TH:i:s\Z' );
index f4199cb55a1f553b6ff208379d7d3ba5b16acdc9..9ad96ca77dca3c541373a8b0891717f0cfaa19c3 100755 (executable)
@@ -810,5 +810,31 @@ INDEX ( `uid` )
 ) ENGINE = MyISAM DEFAULT CHARSET=utf8;
 
 
+--
+-- Table structure for table `notify-threads`
+--
+-- notify-id:          notify.id of the first notification of this thread
+-- master-parent-item: item.id of the parent item
+-- parent-item:        item.id of the imediate parent (only for multi-thread)
+--                     not used yet.
+-- receiver-uid: user.uid of the receiver of this notification.
+--
+-- If we query for a master-parent-item and receiver-uid...
+--   * Returns 1 item: this is not the parent notification, 
+--     so just "follow" the thread (references to this notification)
+--   * Returns no item: this is the first notification related to
+--     this parent item. So, create the record and use the message-id 
+--     header.
+
+
+CREATE TABLE IF NOT EXISTS `notify-threads` (
+`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
+`notify-id` INT NOT NULL,
+`master-parent-item` INT( 10 ) unsigned NOT NULL DEFAULT '0',
+`parent-item` INT( 10 ) unsigned NOT NULL DEFAULT '0',
+`receiver-uid` INT NOT NULL,
+INDEX ( `master-parent-item` ),
+INDEX ( `receiver-uid` )
+) ENGINE = MyISAM DEFAULT CHARSET=utf8;
 
 
index 1eb3b54762a5d9a04127173525d200bc5940c799..33e083b5efefcae063b26297680c71a016c773ec 100755 (executable)
@@ -13,7 +13,9 @@ function notification($params) {
        $site_admin = sprintf( t('%s Administrator'), $sitename);
 
        $sender_name = $product;
-       $sender_email = t('noreply') . '@' . $a->get_hostname();
+       $hostname = $a->get_hostname();
+       $sender_email = t('noreply') . '@' . $hostname;
+       $additional_mail_header = "";
 
        if(array_key_exists('item',$params)) {
                $title = $params['item']['title'];
@@ -36,8 +38,15 @@ function notification($params) {
        }
 
        if($params['type'] == NOTIFY_COMMENT) {
+               logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
 
-               $subject = sprintf( t('%s commented on an item at %s'), $params['source_name'], $sitename);
+               $parent_id = $params['parent'];
+               
+               // Some mail softwares relies on subject field for threading.
+               // So, we cannot have different subjects for notifications of the same thread.
+               // Before this we have the name of the replier on the subject rendering 
+               // differents subjects for messages on the same thread.
+               $subject = sprintf( t('Someone commented on item #%d at %s'), $parent_id, $sitename);
                $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']); 
                $epreamble = sprintf( t('%s commented in %s.'), '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a watched conversation') . '[/url]'); 
 
@@ -126,8 +135,6 @@ function notification($params) {
        } while($dups == true);
 
 
-
-
        // create notification entry in DB
 
        $r = q("insert into notify (hash,name,url,photo,date,uid,link,type,verb,otype)
@@ -170,6 +177,40 @@ function notification($params) {
 
                logger('notification: sending notification email');
 
+               $id_for_parent = "${params['parent']}@${hostname}";
+
+               // Is this the first email notification for this parent item and user?
+               
+               $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1", 
+                       intval($params['parent']),
+                       intval($params['uid']) );
+
+               // If so, create the record of it and use a message-id smtp header.
+
+               if(!$r) {
+                       logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " . 
+intval($params['uid']), LOGGER_DEBUG);
+                       $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
+                               values(%d,%d,%d,%d)",
+                               intval($notify_id),
+                               intval($params['parent']),
+                               intval($params['uid']), 
+                               0 );
+
+                       $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
+                       $log_msg = "include/enotify: No previous notification found for this parent:\n" . 
+                                       "  parent: ${params['parent']}\n" . "  uid   : ${params['uid']}\n";
+                       logger($log_msg, LOGGER_DEBUG);
+               }
+
+               // If not, just "follow" the thread.
+
+               else {
+                       $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
+                       logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
+               }
+
+
 
                $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
                        $body))),ENT_QUOTES,'UTF-8'));
@@ -227,7 +268,8 @@ function notification($params) {
                        'toEmail' => $params['to_email'],
                        'messageSubject' => $subject,
                        'htmlVersion' => $email_html_body,
-                       'textVersion' => $email_text_body
+                       'textVersion' => $email_text_body,
+                       'additionalMailHeader' => $additional_mail_header,
                ));
        }
 
@@ -248,6 +290,7 @@ class enotify {
         * @param messageSubject        subject of the message
         * @param htmlVersion           html version of the message
         * @param textVersion           text only version of the message
+        * @param additionalMailHeader  additions to the smtp mail header
         */
        static public function send($params) {
 
@@ -262,6 +305,7 @@ class enotify {
 
                // generate a multipart/alternative message header
                $messageHeader =
+                       $params['additionalMailHeader'] .
                        "From: {$params['fromName']} <{$params['fromEmail']}>\n" . 
                        "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
                        "MIME-Version: 1.0\n" .
@@ -291,4 +335,4 @@ class enotify {
                logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);
        }
 }
-?>
\ No newline at end of file
+?>
index de3ade6b4e517aa8246418f049f9c1720c2b8e93..c461b83b49da65fae7779ea8cd00a08432f25ac3 100755 (executable)
@@ -2223,7 +2223,8 @@ function local_delivery($importer,$data) {
                                                                'source_photo' => ((link_compare($datarray['author-link'],$importer['url'])) 
                                                                        ? $importer['thumb'] : $datarray['author-avatar']),
                                                                'verb'         => ACTIVITY_POST,
-                                                               'otype'        => 'item'
+                                                               'otype'        => 'item',
+                                                               'parent'       => $parent,
 
                                                        ));
 
@@ -2317,7 +2318,7 @@ function local_delivery($importer,$data) {
                        
                                if($datarray['type'] != 'activity') {
 
-                                       $myconv = q("SELECT `author-link`, `author-avatar` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
+                                       $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
                                                dbesc($parent_uri),
                                                intval($importer['importer_uid'])
                                        );
@@ -2330,6 +2331,8 @@ function local_delivery($importer,$data) {
                                                                continue;
 
                                                        require_once('include/enotify.php');
+                                                       
+                                                       $conv_parent = $conv['parent'];
 
                                                        notification(array(
                                                                'type'         => NOTIFY_COMMENT,
@@ -2345,7 +2348,8 @@ function local_delivery($importer,$data) {
                                                                'source_photo' => ((link_compare($datarray['author-link'],$importer['url'])) 
                                                                        ? $importer['thumb'] : $datarray['author-avatar']),
                                                                'verb'         => ACTIVITY_POST,
-                                                               'otype'        => 'item'
+                                                               'otype'        => 'item',
+                                                               'parent'       => $conv_parent,
 
                                                        ));
 
index 9e0768a6582180815550fa40127f8ed00b8fbd7f..81d7c753b42484e1fe787b3829803a9159d2449d 100755 (executable)
@@ -755,7 +755,8 @@ function item_post(&$a) {
                                        'source_link'  => $datarray['author-link'],
                                        'source_photo' => $datarray['author-avatar'],
                                        'verb'         => ACTIVITY_POST,
-                                       'otype'        => 'item'
+                                       'otype'        => 'item',
+                                       'parent'       => $parent,
                                ));
                        
                        }
index 0096754d20afda75b19230946c5ee64e24788f9f..18dc90cce0b40218eee7f804197ab15234e53cc4 100755 (executable)
@@ -1,6 +1,6 @@
 <?php
 
-define( 'UPDATE_VERSION' , 1125 );
+define( 'UPDATE_VERSION' , 1126 );
 
 /**
  *
@@ -1076,4 +1076,17 @@ set_config('system','allowed_themes','dispy,quattro,testbubble,vier,darkbubble,d
 
 function update_1124() {
 q("alter table item add index (`author-name`) ");
-}
\ No newline at end of file
+}
+
+function update_1125() {
+  q("CREATE TABLE IF NOT EXISTS `notify-threads` (
+  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
+  `notify-id` INT NOT NULL,
+  `master-parent-item` INT( 10 ) unsigned NOT NULL DEFAULT '0',
+  `parent-item` INT( 10 ) unsigned NOT NULL DEFAULT '0',
+  `receiver-uid` INT NOT NULL,
+  INDEX ( `master-parent-item` ),
+  INDEX ( `receiver-uid` )
+  ) ENGINE = MyISAM DEFAULT CHARSET=utf8");
+}
+