]> git.mxchange.org Git - friendica.git/blob - src/Model/Conversation.php
Merge pull request #10800 from MrPetovan/task/10739-block
[friendica.git] / src / Model / Conversation.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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\Protocol;
25 use Friendica\Database\Database;
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; // Deprecated
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_LEGACY_DFRN        = 7; // @deprecated since version 2021.09
42         const PARCEL_DIASPORA_DFRN      = 8;
43         const PARCEL_LOCAL_DFRN         = 9;
44         const PARCEL_DIRECT             = 10;
45         const PARCEL_TWITTER            = 67;
46         const PARCEL_UNKNOWN            = 255;
47
48         /**
49          * Unknown message direction
50          */
51         const UNKNOWN = 0;
52         /**
53          * The message had been pushed to this sytem
54          */
55         const PUSH    = 1;
56         /**
57          * The message had been fetched by our system
58          */
59         const PULL    = 2;
60         /**
61          * The message had been pushed to this system via a relay server
62          */
63         const RELAY   = 3;
64
65         public static function getByItemUri($item_uri)
66         {
67                 return DBA::selectFirst('conversation', [], ['item-uri' => $item_uri]);
68         }
69
70         /**
71          * Store the conversation data
72          *
73          * @param array $arr Item array with conversation data
74          * @return array Item array with removed conversation data
75          * @throws \Exception
76          */
77         public static function insert(array $arr)
78         {
79                 if (in_array(($arr['network'] ?? '') ?: Protocol::PHANTOM,
80                         [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::TWITTER]) && !empty($arr['uri'])) {
81                         $conversation = ['item-uri' => $arr['uri'], 'received' => DateTimeFormat::utcNow()];
82
83                         if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
84                                 $conversation['reply-to-uri'] = $arr['parent-uri'];
85                         }
86
87                         if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
88                                 $conversation['reply-to-uri'] = $arr['thr-parent'];
89                         }
90
91                         if (isset($arr['conversation-uri'])) {
92                                 $conversation['conversation-uri'] = $arr['conversation-uri'];
93                         }
94
95                         if (isset($arr['conversation-href'])) {
96                                 $conversation['conversation-href'] = $arr['conversation-href'];
97                         }
98
99                         if (isset($arr['protocol'])) {
100                                 $conversation['protocol'] = $arr['protocol'];
101                         }
102
103                         if (isset($arr['direction'])) {
104                                 $conversation['direction'] = $arr['direction'];
105                         }
106
107                         if (isset($arr['source'])) {
108                                 $conversation['source'] = $arr['source'];
109                         }
110
111                         if (!DBA::exists('conversation', ['item-uri' => $conversation['item-uri']])) {
112                                 DBA::insert('conversation', $conversation, Database::INSERT_IGNORE);
113                         }
114                 }
115
116                 unset($arr['conversation-uri']);
117                 unset($arr['conversation-href']);
118                 unset($arr['source']);
119
120                 return $arr;
121         }
122 }