]> git.mxchange.org Git - friendica.git/blob - src/Model/Conversation.php
Merge pull request #9586 from annando/inbox-status
[friendica.git] / src / Model / Conversation.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
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;
29
30 class Conversation
31 {
32         /*
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.
35          */
36         const PARCEL_ACTIVITYPUB        = 0;
37         const PARCEL_DFRN               = 1;
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_TWITTER            = 67;
43         const PARCEL_UNKNOWN            = 255;
44
45         /**
46          * Unknown message direction
47          */
48         const UNKNOWN = 0;
49         /**
50          * The message had been pushed to this sytem
51          */
52         const PUSH    = 1;
53         /**
54          * The message had been fetched by our system
55          */
56         const PULL    = 2;
57
58         public static function getByItemUri($item_uri)
59         {
60                 return DBA::selectFirst('conversation', [], ['item-uri' => $item_uri]);
61         }
62
63         /**
64          * Store the conversation data
65          *
66          * @param array $arr Item array with conversation data
67          * @return array Item array with removed conversation data
68          * @throws \Exception
69          */
70         public static function insert(array $arr)
71         {
72                 if (in_array(($arr['network'] ?? '') ?: Protocol::PHANTOM,
73                         [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::TWITTER]) && !empty($arr['uri'])) {
74                         $conversation = ['item-uri' => $arr['uri'], 'received' => DateTimeFormat::utcNow()];
75
76                         if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
77                                 $conversation['reply-to-uri'] = $arr['parent-uri'];
78                         }
79
80                         if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
81                                 $conversation['reply-to-uri'] = $arr['thr-parent'];
82                         }
83
84                         if (isset($arr['conversation-uri'])) {
85                                 $conversation['conversation-uri'] = $arr['conversation-uri'];
86                         }
87
88                         if (isset($arr['conversation-href'])) {
89                                 $conversation['conversation-href'] = $arr['conversation-href'];
90                         }
91
92                         if (isset($arr['protocol'])) {
93                                 $conversation['protocol'] = $arr['protocol'];
94                         }
95
96                         if (isset($arr['direction'])) {
97                                 $conversation['direction'] = $arr['direction'];
98                         }
99
100                         if (isset($arr['source'])) {
101                                 $conversation['source'] = $arr['source'];
102                         }
103
104                         $fields = ['item-uri', 'reply-to-uri', 'conversation-uri', 'conversation-href', 'protocol', 'source'];
105                         $old_conv = DBA::selectFirst('conversation', $fields, ['item-uri' => $conversation['item-uri']]);
106                         if (DBA::isResult($old_conv)) {
107                                 // Don't update when only the source has changed.
108                                 // Only do this when there had been no source before.
109                                 if ($old_conv['source'] != '') {
110                                         unset($old_conv['source']);
111                                 }
112                                 // Update structure data all the time but the source only when its from a better protocol.
113                                 if (
114                                         empty($conversation['source'])
115                                         || (
116                                                 !empty($old_conv['source'])
117                                                 && ($old_conv['protocol'] < (($conversation['protocol'] ?? '') ?: self::PARCEL_UNKNOWN))
118                                         )
119                                 ) {
120                                         unset($conversation['protocol']);
121                                         unset($conversation['source']);
122                                 }
123                                 if (!DBA::update('conversation', $conversation, ['item-uri' => $conversation['item-uri']], $old_conv)) {
124                                         Logger::log('Conversation: update for ' . $conversation['item-uri'] . ' from ' . $old_conv['protocol'] . ' to ' . $conversation['protocol'] . ' failed',
125                                                 Logger::DEBUG);
126                                 }
127                         } else {
128                                 if (!DBA::insert('conversation', $conversation, Database::INSERT_UPDATE)) {
129                                         Logger::log('Conversation: insert for ' . $conversation['item-uri'] . ' (protocol ' . $conversation['protocol'] . ') failed',
130                                                 Logger::DEBUG);
131                                 }
132                         }
133                 }
134
135                 unset($arr['conversation-uri']);
136                 unset($arr['conversation-href']);
137                 unset($arr['protocol']);
138                 unset($arr['source']);
139                 unset($arr['direction']);
140
141                 return $arr;
142         }
143 }