]> git.mxchange.org Git - friendica.git/blob - src/Model/Conversation.php
Merge pull request #8939 from MrPetovan/task/8906-frio-viewas-redesign
[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\DBA;
27 use Friendica\Util\DateTimeFormat;
28
29 class Conversation
30 {
31         /*
32          * These constants represent the parcel format used to transport a conversation independently of the message protocol.
33          * It currently is stored in the "protocol" field for legacy reasons.
34          */
35         const PARCEL_ACTIVITYPUB        = 0;
36         const PARCEL_DFRN               = 1;
37         const PARCEL_DIASPORA           = 2;
38         const PARCEL_SALMON             = 3;
39         const PARCEL_FEED               = 4; // Deprecated
40         const PARCEL_SPLIT_CONVERSATION = 6;
41         const PARCEL_TWITTER            = 67;
42         const PARCEL_UNKNOWN            = 255;
43
44         /**
45          * Unknown message direction
46          */
47         const UNKNOWN = 0;
48         /**
49          * The message had been pushed to this sytem
50          */
51         const PUSH    = 1;
52         /**
53          * The message had been fetched by our system
54          */
55         const PULL    = 2;
56
57         public static function getByItemUri($item_uri)
58         {
59                 return DBA::selectFirst('conversation', [], ['item-uri' => $item_uri]);
60         }
61
62         /**
63          * Store the conversation data
64          *
65          * @param array $arr Item array with conversation data
66          * @return array Item array with removed conversation data
67          * @throws \Exception
68          */
69         public static function insert(array $arr)
70         {
71                 if (in_array(($arr['network'] ?? '') ?: Protocol::PHANTOM,
72                         [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::TWITTER]) && !empty($arr['uri'])) {
73                         $conversation = ['item-uri' => $arr['uri'], 'received' => DateTimeFormat::utcNow()];
74
75                         if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
76                                 $conversation['reply-to-uri'] = $arr['parent-uri'];
77                         }
78
79                         if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
80                                 $conversation['reply-to-uri'] = $arr['thr-parent'];
81                         }
82
83                         if (isset($arr['conversation-uri'])) {
84                                 $conversation['conversation-uri'] = $arr['conversation-uri'];
85                         }
86
87                         if (isset($arr['conversation-href'])) {
88                                 $conversation['conversation-href'] = $arr['conversation-href'];
89                         }
90
91                         if (isset($arr['protocol'])) {
92                                 $conversation['protocol'] = $arr['protocol'];
93                         }
94
95                         if (isset($arr['direction'])) {
96                                 $conversation['direction'] = $arr['direction'];
97                         }
98
99                         if (isset($arr['source'])) {
100                                 $conversation['source'] = $arr['source'];
101                         }
102
103                         $fields = ['item-uri', 'reply-to-uri', 'conversation-uri', 'conversation-href', 'protocol', 'source'];
104                         $old_conv = DBA::selectFirst('conversation', $fields, ['item-uri' => $conversation['item-uri']]);
105                         if (DBA::isResult($old_conv)) {
106                                 // Don't update when only the source has changed.
107                                 // Only do this when there had been no source before.
108                                 if ($old_conv['source'] != '') {
109                                         unset($old_conv['source']);
110                                 }
111                                 // Update structure data all the time but the source only when its from a better protocol.
112                                 if (
113                                         empty($conversation['source'])
114                                         || (
115                                                 !empty($old_conv['source'])
116                                                 && ($old_conv['protocol'] < (($conversation['protocol'] ?? '') ?: self::PARCEL_UNKNOWN))
117                                         )
118                                 ) {
119                                         unset($conversation['protocol']);
120                                         unset($conversation['source']);
121                                 }
122                                 if (!DBA::update('conversation', $conversation, ['item-uri' => $conversation['item-uri']], $old_conv)) {
123                                         Logger::log('Conversation: update for ' . $conversation['item-uri'] . ' from ' . $old_conv['protocol'] . ' to ' . $conversation['protocol'] . ' failed',
124                                                 Logger::DEBUG);
125                                 }
126                         } else {
127                                 if (!DBA::insert('conversation', $conversation, true)) {
128                                         Logger::log('Conversation: insert for ' . $conversation['item-uri'] . ' (protocol ' . $conversation['protocol'] . ') failed',
129                                                 Logger::DEBUG);
130                                 }
131                         }
132                 }
133
134                 unset($arr['conversation-uri']);
135                 unset($arr['conversation-href']);
136                 unset($arr['protocol']);
137                 unset($arr['source']);
138                 unset($arr['direction']);
139
140                 return $arr;
141         }
142 }