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