From b8a23369b614811d18c48c4616e8080596ab7853 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 8 May 2019 05:44:22 +0000 Subject: [PATCH] Mail storing is now centralized --- src/Model/Mail.php | 69 ++++++++++++++++++++++++++++++++++++-- src/Protocol/DFRN.php | 32 ++---------------- src/Protocol/Diaspora.php | 70 ++------------------------------------- 3 files changed, 73 insertions(+), 98 deletions(-) diff --git a/src/Model/Mail.php b/src/Model/Mail.php index 2d304e55f8..4d5c1946b3 100644 --- a/src/Model/Mail.php +++ b/src/Model/Mail.php @@ -9,6 +9,7 @@ use Friendica\Core\L10n; use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\Core\Worker; +use Friendica\Model\Item; use Friendica\Database\DBA; use Friendica\Network\Probe; use Friendica\Util\DateTimeFormat; @@ -18,6 +19,70 @@ use Friendica\Util\DateTimeFormat; */ class Mail { + /** + * Insert received private message + * + * @param array $msg + * @return int|boolean Message ID or false on error + */ + public static function insert($msg) + { + $user = User::getById($msg['uid']); + + if (empty($msg['convid'])) { + $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]); + if (DBA::isResult($mail)) { + $msg['convid'] = $mail['convid']; + } + } + + if (empty($msg['guid'])) { + $host = parse_url($msg['from-url'], PHP_URL_HOST); + $msg['guid'] = Item::guidFromUri($msg['uri'], $host); + } + + DBA::lock('mail'); + + if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) { + DBA::unlock(); + Logger::info('duplicate message already delivered.'); + return false; + } + + DBA::insert('mail', $msg); + + $msg['id'] = DBA::lastInsertId(); + + DBA::unlock(); + + if (!empty($msg['convid'])) { + DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]); + } + + // send notifications. + $notif_params = [ + 'type' => NOTIFY_MAIL, + 'notify_flags' => $user['notify-flags'], + 'language' => $user['language'], + 'to_name' => $user['username'], + 'to_email' => $user['email'], + 'uid' => $user['uid'], + 'item' => $msg, + 'parent' => $msg['parent-uri'], + 'source_name' => $msg['from-name'], + 'source_link' => $msg['from-url'], + 'source_photo' => $msg['from-photo'], + 'verb' => ACTIVITY_POST, + 'otype' => 'mail' + ]; + + notification($notif_params); + + Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]); + + return $msg['id']; + } + /** * Send private message * @@ -48,7 +113,7 @@ class Mail } $guid = System::createUUID(); - $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid; + $uri = Item::newURI(local_user(), $guid); $convid = 0; $reply = false; @@ -176,7 +241,7 @@ class Mail } $guid = System::createUUID(); - $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid; + $uri = Item::newURI(local_user(), $guid); $me = Probe::uri($replyto); diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 1ceab79f65..e83732ffe2 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -25,6 +25,7 @@ use Friendica\Model\Conversation; use Friendica\Model\Event; use Friendica\Model\GContact; use Friendica\Model\Item; +use Friendica\Model\Mail; use Friendica\Model\PermissionSet; use Friendica\Model\Profile; use Friendica\Model\User; @@ -1865,7 +1866,6 @@ class DFRN { Logger::log("Processing mails"); - /// @TODO Rewrite this to one statement $msg = []; $msg["uid"] = $importer["importer_uid"]; $msg["from-name"] = $xpath->query("dfrn:sender/dfrn:name/text()", $mail)->item(0)->nodeValue; @@ -1877,34 +1877,8 @@ class DFRN $msg["created"] = DateTimeFormat::utc($xpath->query("dfrn:sentdate/text()", $mail)->item(0)->nodeValue); $msg["title"] = $xpath->query("dfrn:subject/text()", $mail)->item(0)->nodeValue; $msg["body"] = $xpath->query("dfrn:content/text()", $mail)->item(0)->nodeValue; - $msg["seen"] = 0; - $msg["replied"] = 0; - - DBA::insert('mail', $msg); - - $msg["id"] = DBA::lastInsertId(); - - // send notifications. - /// @TODO Arange this mess - $notif_params = [ - "type" => NOTIFY_MAIL, - "notify_flags" => $importer["notify-flags"], - "language" => $importer["language"], - "to_name" => $importer["username"], - "to_email" => $importer["email"], - "uid" => $importer["importer_uid"], - "item" => $msg, - "parent" => $msg["parent-uri"], - "source_name" => $msg["from-name"], - "source_link" => $importer["url"], - "source_photo" => $importer["thumb"], - "verb" => ACTIVITY_POST, - "otype" => "mail" - ]; - - notification($notif_params); - - Logger::log("Mail is processed, notification was sent."); + + Mail::insert($msg); } /** diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 3a7e73cee5..e7857cf8e1 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -27,6 +27,7 @@ use Friendica\Model\Conversation; use Friendica\Model\GContact; use Friendica\Model\Group; use Friendica\Model\Item; +use Friendica\Model\Mail; use Friendica\Model\Profile; use Friendica\Model\User; use Friendica\Network\Probe; @@ -1841,14 +1842,7 @@ class Diaspora $person = self::personByHandle($msg_author); - DBA::lock('mail'); - - if (DBA::exists('mail', ['guid' => $msg_guid, 'uid' => $importer["uid"]])) { - Logger::log("duplicate message already delivered.", Logger::DEBUG); - return false; - } - - DBA::insert('mail', [ + return Mail::insert([ 'uid' => $importer['uid'], 'guid' => $msg_guid, 'convid' => $conversation['id'], @@ -1858,36 +1852,10 @@ class Diaspora 'contact-id' => $contact['id'], 'title' => $subject, 'body' => $body, - 'seen' => 0, - 'reply' => 0, 'uri' => $message_uri, 'parent-uri' => $author . ':' . $guid, 'created' => $msg_created_at ]); - - $message_id = DBA::lastInsertId(); - - DBA::unlock(); - - DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]); - - notification([ - "type" => NOTIFY_MAIL, - "notify_flags" => $importer["notify-flags"], - "language" => $importer["language"], - "to_name" => $importer["username"], - "to_email" => $importer["email"], - "uid" => $importer["uid"], - "item" => ["id" => $message_id, "title" => $subject, "subject" => $subject, "body" => $body], - "parent" => $conversation["id"], - "source_name" => $person["name"], - "source_link" => $person["url"], - "source_photo" => $person["photo"], - "verb" => ACTIVITY_POST, - "otype" => "mail" - ]); - - return true; } /** @@ -2105,14 +2073,7 @@ class Diaspora $body = self::replacePeopleGuid($body, $person["url"]); - DBA::lock('mail'); - - if (DBA::exists('mail', ['guid' => $guid, 'uid' => $importer["uid"]])) { - Logger::log("duplicate message already delivered.", Logger::DEBUG); - return false; - } - - DBA::insert('mail', [ + return Mail::insert([ 'uid' => $importer['uid'], 'guid' => $guid, 'convid' => $conversation['id'], @@ -2122,36 +2083,11 @@ class Diaspora 'contact-id' => $contact['id'], 'title' => $conversation['subject'], 'body' => $body, - 'seen' => 0, 'reply' => 1, 'uri' => $message_uri, 'parent-uri' => $author.":".$conversation['guid'], 'created' => $created_at ]); - - $message_id = DBA::lastInsertId(); - - DBA::unlock(); - - DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]); - - notification([ - "type" => NOTIFY_MAIL, - "notify_flags" => $importer["notify-flags"], - "language" => $importer["language"], - "to_name" => $importer["username"], - "to_email" => $importer["email"], - "uid" => $importer["uid"], - "item" => ["id" => $message_id, "title" => $conversation["subject"], "subject" => $conversation["subject"], "body" => $body], - "parent" => $conversation["id"], - "source_name" => $person["name"], - "source_link" => $person["url"], - "source_photo" => $person["photo"], - "verb" => ACTIVITY_POST, - "otype" => "mail" - ]); - - return true; } /** -- 2.39.5