<commit_before><?php
/**
 * @copyright Copyright (C) 2010-2021, the Friendica project
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Friendica\Model;

use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Protocol\Activity;
use Friendica\Util\DateTimeFormat;
use Friendica\Worker\Delivery;

/**
 * Class to handle private messages
 */
class Mail
{
	/**
	 * Insert private message
	 *
	 * @param array $msg
	 * @param bool  $notifiction
	 * @return int|boolean Message ID or false on error
	 */
	public static function insert($msg, $notifiction = true)
	{
		if (!isset($msg['reply'])) {
			$msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
		}

		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);
		}

		$msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow());

		$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']);

		DBA::lock('mail');

		if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
			DBA::unlock();
			Logger::info('duplicate message already delivered.');
			return false;
		}

		if ($msg['reply']) {
			$reply = DBA::selectFirst('mail', ['uri', 'uri-id'], ['parent-uri' => $msg['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();

		DBA::unlock();

		if (!empty($msg['convid'])) {
			DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
		}

		if ($notifiction) {<commit_msg>Check for reply existence before using its values in Model\Mail<commit_after><?php
/**
 * @copyright Copyright (C) 2010-2021, the Friendica project
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Friendica\Model;

use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Protocol\Activity;
use Friendica\Util\DateTimeFormat;
use Friendica\Worker\Delivery;

/**
 * Class to handle private messages
 */
class Mail
{
	/**
	 * Insert private message
	 *
	 * @param array $msg
	 * @param bool  $notifiction
	 * @return int|boolean Message ID or false on error
	 */
	public static function insert($msg, $notifiction = true)
	{
		if (!isset($msg['reply'])) {
			$msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
		}

		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);
		}

		$msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow());

		$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']);

		DBA::lock('mail');

		if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
			DBA::unlock();
			Logger::info('duplicate message already delivered.');
			return false;
		}

		if ($msg['reply'] && DBA::isResult($reply = DBA::selectFirst('mail', ['uri', 'uri-id'], ['parent-uri' => $msg['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();

		DBA::unlock();

		if (!empty($msg['convid'])) {
			DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
		}

		if ($notifiction) {
{'fraction_non_alphanumeric': 0.20030400270224624, 'fraction_numerical': 0.004053369363283229, 'mean_word_length': 4.2875, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5587299', 'n_tokens_mistral': 2100, 'n_tokens_neox': 1940, 'n_words': 523}