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;
*/
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
*
}
$guid = System::createUUID();
- $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
+ $uri = Item::newURI(local_user(), $guid);
$convid = 0;
$reply = false;
}
$guid = System::createUUID();
- $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
+ $uri = Item::newURI(local_user(), $guid);
$me = Probe::uri($replyto);
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;
{
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;
$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);
}
/**
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;
$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'],
'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;
}
/**
$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'],
'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;
}
/**