]> git.mxchange.org Git - friendica.git/blob - src/Model/Conversation.php
Rename PROTOCOL_* constants to Model\Conversation::PARCEL_*
[friendica.git] / src / Model / Conversation.php
1 <?php
2 /**
3  * @file src/Model/Conversation
4  */
5 namespace Friendica\Model;
6
7 use Friendica\Database\DBA;
8 use Friendica\Util\DateTimeFormat;
9
10 require_once "include/dba.php";
11
12 class Conversation
13 {
14         /*
15          * These constants represent the parcel format used to transport a conversation independently of the message protocol.
16          * It currently is stored in the "protocol" field for legacy reasons.
17          */
18         const PARCEL_UNKNOWN            = 0;
19         const PARCEL_DFRN               = 1;
20         const PARCEL_DIASPORA           = 2;
21         const PARCEL_SALMON             = 3;
22         const PARCEL_FEED               = 4; // Deprecated
23         const PARCEL_SPLIT_CONVERSATION = 6;
24
25         /**
26          * @brief Store the conversation data
27          *
28          * @param array $arr Item array with conversation data
29          * @return array Item array with removed conversation data
30          */
31         public static function insert($arr) {
32                 if (in_array(defaults($arr, 'network', NETWORK_PHANTOM), [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]) && !empty($arr['uri'])) {
33                         $conversation = ['item-uri' => $arr['uri'], 'received' => DateTimeFormat::utcNow()];
34
35                         if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
36                                 $conversation['reply-to-uri'] = $arr['parent-uri'];
37                         }
38                         if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
39                                 $conversation['reply-to-uri'] = $arr['thr-parent'];
40                         }
41
42                         if (isset($arr['conversation-uri'])) {
43                                 $conversation['conversation-uri'] = $arr['conversation-uri'];
44                         }
45
46                         if (isset($arr['conversation-href'])) {
47                                 $conversation['conversation-href'] = $arr['conversation-href'];
48                         }
49
50                         if (isset($arr['protocol'])) {
51                                 $conversation['protocol'] = $arr['protocol'];
52                         }
53
54                         if (isset($arr['source'])) {
55                                 $conversation['source'] = $arr['source'];
56                         }
57
58                         $fields = ['item-uri', 'reply-to-uri', 'conversation-uri', 'conversation-href', 'protocol', 'source'];
59                         $old_conv = DBA::selectFirst('conversation', $fields, ['item-uri' => $conversation['item-uri']]);
60                         if (DBA::isResult($old_conv)) {
61                                 // Don't update when only the source has changed.
62                                 // Only do this when there had been no source before.
63                                 if ($old_conv['source'] != '') {
64                                         unset($old_conv['source']);
65                                 }
66                                 // Update structure data all the time but the source only when its from a better protocol.
67                                 if (isset($conversation['protocol']) && isset($conversation['source']) && ($old_conv['protocol'] < $conversation['protocol']) && ($old_conv['protocol'] != 0)) {
68                                         unset($conversation['protocol']);
69                                         unset($conversation['source']);
70                                 }
71                                 if (!DBA::update('conversation', $conversation, ['item-uri' => $conversation['item-uri']], $old_conv)) {
72                                         logger('Conversation: update for '.$conversation['item-uri'].' from '.$old_conv['protocol'].' to '.$conversation['protocol'].' failed', LOGGER_DEBUG);
73                                 }
74                         } else {
75                                 if (!DBA::insert('conversation', $conversation, true)) {
76                                         logger('Conversation: insert for '.$conversation['item-uri'].' (protocol '.$conversation['protocol'].') failed', LOGGER_DEBUG);
77                                 }
78                         }
79                 }
80
81                 unset($arr['conversation-uri']);
82                 unset($arr['conversation-href']);
83                 unset($arr['protocol']);
84                 unset($arr['source']);
85
86                 return $arr;
87         }
88 }