Mail storing is now centralized

This commit is contained in:
Michael 2019-05-08 05:44:22 +00:00
parent 10b5ea825d
commit b8a23369b6
3 changed files with 72 additions and 97 deletions

View file

@ -9,6 +9,7 @@ use Friendica\Core\L10n;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Model\Item;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Network\Probe; use Friendica\Network\Probe;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -18,6 +19,70 @@ use Friendica\Util\DateTimeFormat;
*/ */
class Mail 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 * Send private message
* *
@ -48,7 +113,7 @@ class Mail
} }
$guid = System::createUUID(); $guid = System::createUUID();
$uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid; $uri = Item::newURI(local_user(), $guid);
$convid = 0; $convid = 0;
$reply = false; $reply = false;
@ -176,7 +241,7 @@ class Mail
} }
$guid = System::createUUID(); $guid = System::createUUID();
$uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid; $uri = Item::newURI(local_user(), $guid);
$me = Probe::uri($replyto); $me = Probe::uri($replyto);

View file

@ -25,6 +25,7 @@ use Friendica\Model\Conversation;
use Friendica\Model\Event; use Friendica\Model\Event;
use Friendica\Model\GContact; use Friendica\Model\GContact;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Mail;
use Friendica\Model\PermissionSet; use Friendica\Model\PermissionSet;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Model\User; use Friendica\Model\User;
@ -1865,7 +1866,6 @@ class DFRN
{ {
Logger::log("Processing mails"); Logger::log("Processing mails");
/// @TODO Rewrite this to one statement
$msg = []; $msg = [];
$msg["uid"] = $importer["importer_uid"]; $msg["uid"] = $importer["importer_uid"];
$msg["from-name"] = $xpath->query("dfrn:sender/dfrn:name/text()", $mail)->item(0)->nodeValue; $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["created"] = DateTimeFormat::utc($xpath->query("dfrn:sentdate/text()", $mail)->item(0)->nodeValue);
$msg["title"] = $xpath->query("dfrn:subject/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["body"] = $xpath->query("dfrn:content/text()", $mail)->item(0)->nodeValue;
$msg["seen"] = 0;
$msg["replied"] = 0;
DBA::insert('mail', $msg); Mail::insert($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.");
} }
/** /**

View file

@ -27,6 +27,7 @@ use Friendica\Model\Conversation;
use Friendica\Model\GContact; use Friendica\Model\GContact;
use Friendica\Model\Group; use Friendica\Model\Group;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Mail;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Network\Probe; use Friendica\Network\Probe;
@ -1841,14 +1842,7 @@ class Diaspora
$person = self::personByHandle($msg_author); $person = self::personByHandle($msg_author);
DBA::lock('mail'); return Mail::insert([
if (DBA::exists('mail', ['guid' => $msg_guid, 'uid' => $importer["uid"]])) {
Logger::log("duplicate message already delivered.", Logger::DEBUG);
return false;
}
DBA::insert('mail', [
'uid' => $importer['uid'], 'uid' => $importer['uid'],
'guid' => $msg_guid, 'guid' => $msg_guid,
'convid' => $conversation['id'], 'convid' => $conversation['id'],
@ -1858,36 +1852,10 @@ class Diaspora
'contact-id' => $contact['id'], 'contact-id' => $contact['id'],
'title' => $subject, 'title' => $subject,
'body' => $body, 'body' => $body,
'seen' => 0,
'reply' => 0,
'uri' => $message_uri, 'uri' => $message_uri,
'parent-uri' => $author . ':' . $guid, 'parent-uri' => $author . ':' . $guid,
'created' => $msg_created_at '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"]); $body = self::replacePeopleGuid($body, $person["url"]);
DBA::lock('mail'); return Mail::insert([
if (DBA::exists('mail', ['guid' => $guid, 'uid' => $importer["uid"]])) {
Logger::log("duplicate message already delivered.", Logger::DEBUG);
return false;
}
DBA::insert('mail', [
'uid' => $importer['uid'], 'uid' => $importer['uid'],
'guid' => $guid, 'guid' => $guid,
'convid' => $conversation['id'], 'convid' => $conversation['id'],
@ -2122,36 +2083,11 @@ class Diaspora
'contact-id' => $contact['id'], 'contact-id' => $contact['id'],
'title' => $conversation['subject'], 'title' => $conversation['subject'],
'body' => $body, 'body' => $body,
'seen' => 0,
'reply' => 1, 'reply' => 1,
'uri' => $message_uri, 'uri' => $message_uri,
'parent-uri' => $author.":".$conversation['guid'], 'parent-uri' => $author.":".$conversation['guid'],
'created' => $created_at '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;
} }
/** /**