3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
28 use Friendica\Util\DateTimeFormat;
33 * These constants represent the parcel format used to transport a conversation independently of the message protocol.
34 * It currently is stored in the "protocol" field for legacy reasons.
36 const PARCEL_ACTIVITYPUB = 0;
37 const PARCEL_DFRN = 1; // Deprecated
38 const PARCEL_DIASPORA = 2;
39 const PARCEL_SALMON = 3;
40 const PARCEL_FEED = 4; // Deprecated
41 const PARCEL_SPLIT_CONVERSATION = 6;
42 const PARCEL_LEGACY_DFRN = 7;
43 const PARCEL_DIASPORA_DFRN = 8;
44 const PARCEL_LOCAL_DFRN = 9;
45 const PARCEL_DIRECT = 10;
46 const PARCEL_TWITTER = 67;
47 const PARCEL_UNKNOWN = 255;
50 * Unknown message direction
54 * The message had been pushed to this sytem
58 * The message had been fetched by our system
62 public static function getByItemUri($item_uri)
64 return DBA::selectFirst('conversation', [], ['item-uri' => $item_uri]);
68 * Store the conversation data
70 * @param array $arr Item array with conversation data
71 * @return array Item array with removed conversation data
74 public static function insert(array $arr)
76 if (in_array(($arr['network'] ?? '') ?: Protocol::PHANTOM,
77 [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::TWITTER]) && !empty($arr['uri'])) {
78 $conversation = ['item-uri' => $arr['uri'], 'received' => DateTimeFormat::utcNow()];
80 if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
81 $conversation['reply-to-uri'] = $arr['parent-uri'];
84 if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
85 $conversation['reply-to-uri'] = $arr['thr-parent'];
88 if (isset($arr['conversation-uri'])) {
89 $conversation['conversation-uri'] = $arr['conversation-uri'];
92 if (isset($arr['conversation-href'])) {
93 $conversation['conversation-href'] = $arr['conversation-href'];
96 if (isset($arr['protocol'])) {
97 $conversation['protocol'] = $arr['protocol'];
100 if (isset($arr['direction'])) {
101 $conversation['direction'] = $arr['direction'];
104 if (isset($arr['source'])) {
105 $conversation['source'] = $arr['source'];
108 $fields = ['item-uri', 'reply-to-uri', 'conversation-uri', 'conversation-href', 'protocol', 'source'];
109 $old_conv = DBA::selectFirst('conversation', $fields, ['item-uri' => $conversation['item-uri']]);
110 if (DBA::isResult($old_conv)) {
111 // Don't update when only the source has changed.
112 // Only do this when there had been no source before.
113 if ($old_conv['source'] != '') {
114 unset($old_conv['source']);
116 // Update structure data all the time but the source only when its from a better protocol.
118 empty($conversation['source'])
120 !empty($old_conv['source'])
121 && ($old_conv['protocol'] < (($conversation['protocol'] ?? '') ?: self::PARCEL_UNKNOWN))
124 unset($conversation['protocol']);
125 unset($conversation['source']);
127 if (!DBA::update('conversation', $conversation, ['item-uri' => $conversation['item-uri']], $old_conv)) {
128 Logger::log('Conversation: update for ' . $conversation['item-uri'] . ' from ' . $old_conv['protocol'] . ' to ' . $conversation['protocol'] . ' failed',
132 if (!DBA::insert('conversation', $conversation, Database::INSERT_UPDATE)) {
133 Logger::log('Conversation: insert for ' . $conversation['item-uri'] . ' (protocol ' . $conversation['protocol'] . ') failed',
139 unset($arr['conversation-uri']);
140 unset($arr['conversation-href']);
141 unset($arr['source']);