]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #10288 from annando/mail-uri-id
authorTobias Diekershoff <tobias.diekershoff@gmx.net>
Sat, 22 May 2021 14:38:42 +0000 (16:38 +0200)
committerGitHub <noreply@github.com>
Sat, 22 May 2021 14:38:42 +0000 (16:38 +0200)
API: improved mail handling

database.sql
src/Content/Text/BBCode.php
src/Factory/Api/Mastodon/Status.php
src/Model/Mail.php
src/Module/Api/Mastodon/Timelines/Direct.php
src/Protocol/ActivityPub/Transmitter.php
src/Worker/ExpirePosts.php
src/Worker/RemoveContact.php
static/dbstructure.config.php
update.php

index d080b41582998680538def5359567c6d18a3b0ad..3e6d398cf1b9b9fe5a960e41550a4cf972ea91b1 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
--- Friendica 2021.06-dev (Siberian Iris)
--- DB_UPDATE_VERSION 1418
+-- Friendica 2021.06-rc (Siberian Iris)
+-- DB_UPDATE_VERSION 1419
 -- ------------------------------------------
 
 
@@ -751,6 +751,7 @@ CREATE TABLE IF NOT EXISTS `mail` (
        `from-photo` varchar(255) NOT NULL DEFAULT '' COMMENT 'contact photo link of the sender',
        `from-url` varchar(255) NOT NULL DEFAULT '' COMMENT 'profile linke of the sender',
        `contact-id` varchar(255) COMMENT 'contact.id',
+       `author-id` int unsigned COMMENT 'Link to the contact table with uid=0 of the author of the mail',
        `convid` int unsigned COMMENT 'conv.id',
        `title` varchar(255) NOT NULL DEFAULT '' COMMENT '',
        `body` mediumtext COMMENT '',
@@ -759,7 +760,11 @@ CREATE TABLE IF NOT EXISTS `mail` (
        `replied` boolean NOT NULL DEFAULT '0' COMMENT '',
        `unknown` boolean NOT NULL DEFAULT '0' COMMENT 'if sender not in the contact table this is 1',
        `uri` varchar(255) NOT NULL DEFAULT '' COMMENT '',
+       `uri-id` int unsigned COMMENT 'Item-uri id of the related mail',
        `parent-uri` varchar(255) NOT NULL DEFAULT '' COMMENT '',
+       `parent-uri-id` int unsigned COMMENT 'Item-uri id of the parent of the related mail',
+       `thr-parent` varchar(255) COMMENT '',
+       `thr-parent-id` int unsigned COMMENT 'Id of the item-uri table that contains the thread parent uri',
        `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'creation time of the private message',
         PRIMARY KEY(`id`),
         INDEX `uid_seen` (`uid`,`seen`),
@@ -767,7 +772,15 @@ CREATE TABLE IF NOT EXISTS `mail` (
         INDEX `uri` (`uri`(64)),
         INDEX `parent-uri` (`parent-uri`(64)),
         INDEX `contactid` (`contact-id`(32)),
-       FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE
+        INDEX `author-id` (`author-id`),
+        INDEX `uri-id` (`uri-id`),
+        INDEX `parent-uri-id` (`parent-uri-id`),
+        INDEX `thr-parent-id` (`thr-parent-id`),
+       FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE,
+       FOREIGN KEY (`author-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT,
+       FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
+       FOREIGN KEY (`parent-uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
+       FOREIGN KEY (`thr-parent-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
 ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='private messages';
 
 --
index 8c1476fa35d2990bf13a91fd0e0c805c88475a3a..03ccd889a47ba43e6cbb1fb6008665cb0b0ca4f8 100644 (file)
@@ -1039,7 +1039,9 @@ class BBCode
 
                switch ($simplehtml) {
                        case self::API:
-                               $text = ($is_quote_share? '<br>' : '') . '<p>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ': </p>' . "\n" . $content;
+                               $text = ($is_quote_share? '<br>' : '') .
+                               '<p><b><a href="' . $attributes['link'] . '">' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . "</a>:</b> </p>\n" .
+                               '<blockquote class="shared_content">' . $content . '</blockquote>';
                                break;
                        case self::DIASPORA:
                                if (stripos(Strings::normaliseLink($attributes['link']), 'http://twitter.com/') === 0) {
index 3f16b3c380aa61e20838827be7b1523e5a264242..41f64daa163a0b59fc91f4bc865569766675343d 100644 (file)
@@ -136,7 +136,9 @@ class Status extends BaseFactory
 
                $account = DI::mstdnAccount()->createFromContactId($item['author-id']);
 
-               $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(0, 0, 0);
+               $replies = DBA::count('mail', ['thr-parent-id' => $item['uri-id'], 'reply' => true]);
+
+               $counts = new \Friendica\Object\Api\Mastodon\Status\Counts($replies, 0, 0);
 
                $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false);
 
index e7ed2c8454b9910b1f015cb43537a512eb1e2415..aa90586601393ece741f448b5efbf54344ba8628 100644 (file)
@@ -71,6 +71,20 @@ class Mail
                        return false;
                }
 
+               $msg['author-id']     = Contact::getIdForURL($msg['from-url'], 0, false);
+               $msg['uri-id']        = ItemURI::insert(['uri' => $msg['uri'], 'guid' => $msg['guid']]);
+               $msg['parent-uri-id'] = ItemURI::getIdByURI($msg['parent-uri']);
+
+               if ($msg['reply']) {
+                       $reply = DBA::selectFirst('mail', ['uri', 'uri-id'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
+
+                       $msg['thr-parent']    = $reply['uri'];
+                       $msg['thr-parent-id'] = $reply['uri-id'];
+               } else {
+                       $msg['thr-parent']    = $msg['uri'];
+                       $msg['thr-parent-id'] = $msg['uri-id'];
+               }
+
                DBA::insert('mail', $msg);
 
                $msg['id'] = DBA::lastInsertId();
index 104f88d1c2d1e219b2fc3517e51d15e8ba56a9f5..050bcbf478cab84336cfc44c6caa796fd19b6536 100644 (file)
@@ -48,22 +48,22 @@ class Direct extends BaseApi
                        'limit'    => 20, // Maximum number of results to return. Defaults to 20.
                ]);
 
-               $params = ['order' => ['id' => true], 'limit' => $request['limit']];
+               $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];
 
                $condition = ['uid' => $uid];
 
                if (!empty($request['max_id'])) {
-                       $condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]);
+                       $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
                }
 
                if (!empty($request['since_id'])) {
-                       $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]);
+                       $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
                }
 
                if (!empty($request['min_id'])) {
-                       $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]);
+                       $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
 
-                       $params['order'] = ['id'];
+                       $params['order'] = ['uri-id'];
                }
 
                $mails = DBA::select('mail', ['id'], $condition, $params);
index 20c1e49b883de4cbd6dffce6e8e34284b0cbdf54..4e009d61d456622d98b8c6bd11add84850a78103 100644 (file)
@@ -868,24 +868,19 @@ class Transmitter
                        return [];
                }
 
-               $mail['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
-
-               $reply = DBA::selectFirst('mail', ['uri', 'from-url', 'guid'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
+               $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'from-url'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
 
                // Making the post more compatible for Mastodon by:
                // - Making it a note and not an article (no title)
                // - Moving the title into the "summary" field that is used as a "content warning"
 
-               if ($use_title) {
-                       $mail['body']         = $mail['body'];
-                       $mail['title']        = $mail['title'];
-               } else {
+               if (!$use_title) {
                        $mail['body']         = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
                        $mail['title']        = '';
                }
 
                $mail['author-link']      = $mail['owner-link'] = $mail['from-url'];
-               $mail['author-id']        = Contact::getIdForURL($mail['author-link'], 0, false); 
+               $mail['owner-id']         = $mail['author-id'];
                $mail['allow_cid']        = '<'.$mail['contact-id'].'>';
                $mail['allow_gid']        = '';
                $mail['deny_cid']         = '';
@@ -893,9 +888,9 @@ class Transmitter
                $mail['private']          = Item::PRIVATE;
                $mail['deleted']          = false;
                $mail['edited']           = $mail['created'];
-               $mail['plink']            = $mail['uri'];
-               $mail['thr-parent']       = $reply['uri'];
-               $mail['thr-parent-id']    = ItemURI::insert(['uri' => $reply['uri'], 'guid' => $reply['guid']]);
+               $mail['plink']            = DI::baseUrl() . '/message/' . $mail['id'];
+               $mail['parent-uri']       = $reply['uri'];
+               $mail['parent-uri-id']    = $reply['uri-id'];
                $mail['parent-author-id'] = Contact::getIdForURL($reply['from-url'], 0, false);
                $mail['gravity']          = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
                $mail['event-type']       = '';
index 2c3f6f68a16d30e2a13dc203975f0f511cdb7fbe..3f1075db5db68bc678696f9f88ea17fc55e3697b 100644 (file)
@@ -181,7 +181,10 @@ class ExpirePosts
                        AND NOT EXISTS(SELECT `uri-id` FROM `post-user` WHERE `uri-id` = `item-uri`.`id`)
                        AND NOT EXISTS(SELECT `parent-uri-id` FROM `post-user` WHERE `parent-uri-id` = `item-uri`.`id`)
                        AND NOT EXISTS(SELECT `thr-parent-id` FROM `post-user` WHERE `thr-parent-id` = `item-uri`.`id`)
-                       AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`)", $item['uri-id']]);
+                       AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`)
+                       AND NOT EXISTS(SELECT `uri-id` FROM `mail` WHERE `uri-id` = `item-uri`.`id`)
+                       AND NOT EXISTS(SELECT `parent-uri-id` FROM `mail` WHERE `parent-uri-id` = `item-uri`.`id`)
+                       AND NOT EXISTS(SELECT `thr-parent-id` FROM `mail` WHERE `thr-parent-id` = `item-uri`.`id`)", $item['uri-id']]);
 
                Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]);
                $affected_count = 0;
index 13cbbec8ba18cb4add35ed2eb97406b88aaca5a4..f64d4d02d537fd74c7a0c1cf3ceaa89d2c2daca4 100644 (file)
@@ -55,6 +55,7 @@ class RemoveContact {
                }
 
                DBA::delete('mail', ['contact-id' => $id]);
+               DBA::delete('mail', ['author-id' => $id]);
 
                Post\ThreadUser::delete(['author-id' => $id]);
                Post\ThreadUser::delete(['owner-id' => $id]);
index 835cb3ff37b5d784c3726efc7fd808dea6b1277e..14b7814c0923df0c068dc646460ba19d3de0f700 100644 (file)
@@ -55,7 +55,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1418);
+       define('DB_UPDATE_VERSION', 1419);
 }
 
 return [
@@ -818,6 +818,7 @@ return [
                        "from-photo" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "contact photo link of the sender"],
                        "from-url" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "profile linke of the sender"],
                        "contact-id" => ["type" => "varchar(255)", "relation" => ["contact" => "id"], "comment" => "contact.id"],
+                       "author-id" => ["type" => "int unsigned", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the author of the mail"],
                        "convid" => ["type" => "int unsigned", "relation" => ["conv" => "id"], "comment" => "conv.id"],
                        "title" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
                        "body" => ["type" => "mediumtext", "comment" => ""],
@@ -826,7 +827,11 @@ return [
                        "replied" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
                        "unknown" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "if sender not in the contact table this is 1"],
                        "uri" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
+                       "uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Item-uri id of the related mail"],
                        "parent-uri" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
+                       "parent-uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Item-uri id of the parent of the related mail"],
+                       "thr-parent" => ["type" => "varchar(255)", "comment" => ""],
+                       "thr-parent-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table that contains the thread parent uri"],
                        "created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "creation time of the private message"],
                ],
                "indexes" => [
@@ -836,6 +841,10 @@ return [
                        "uri" => ["uri(64)"],
                        "parent-uri" => ["parent-uri(64)"],
                        "contactid" => ["contact-id(32)"],
+                       "author-id" => ["author-id"],
+                       "uri-id" => ["uri-id"],
+                       "parent-uri-id" => ["parent-uri-id"],
+                       "thr-parent-id" => ["thr-parent-id"],
                ]
        ],
        "mailacct" => [
index a34a5dbb0b92893b9a74e7937d46c161c82dc0b5..0a97b4e10cf01891ba033cc4dad6787303e575f5 100644 (file)
@@ -49,6 +49,7 @@ use Friendica\Database\DBStructure;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Item;
+use Friendica\Model\ItemURI;
 use Friendica\Model\Notification;
 use Friendica\Model\Photo;
 use Friendica\Model\Post;
@@ -912,3 +913,31 @@ function update_1413()
                return Update::FAILED;
        }
 }
+
+function update_1419()
+{
+       $mails = DBA::select('mail', ['id', 'from-url', 'uri', 'parent-uri', 'guid'], [], ['order' => ['id']]);
+       while ($mail = DBA::fetch($mails)) {
+               $fields = [];
+               $fields['author-id'] = Contact::getIdForURL($mail['from-url'], 0, false);
+               if (empty($fields['author-id'])) {
+                       continue;
+               }
+
+               $fields['uri-id']        = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
+               $fields['parent-uri-id'] = ItemURI::getIdByURI($mail['parent-uri']);
+
+               $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'guid'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
+               if (!empty($reply)) {
+                       $fields['thr-parent'] = $reply['uri'];
+                       if (!empty($reply['uri-id'])) {
+                               $fields['thr-parent-id'] = $reply['uri-id'];
+                       } else {
+                               $fields['thr-parent-id'] = ItemURI::insert(['uri' => $reply['uri'], 'guid' => $reply['guid']]);
+                       }
+               }
+
+               DBA::update('mail', $fields, ['id' => $mail['id']]);
+       }
+       return Update::SUCCESS;
+}