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