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