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