]> git.mxchange.org Git - friendica.git/blob - src/Model/Conversation.php
e6888ca8f6cc01fa930a220939303bd2ef5adbe7
[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         /**
15          * @brief Store the conversation data
16          *
17          * @param array $arr Item array with conversation data
18          * @return array Item array with removed conversation data
19          */
20         public static function insert($arr) {
21                 if (in_array(defaults($arr, 'network', NETWORK_PHANTOM), [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]) && !empty($arr['uri'])) {
22                         $conversation = ['item-uri' => $arr['uri'], 'received' => DBM::date()];
23
24                         if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
25                                 $conversation['reply-to-uri'] = $arr['parent-uri'];
26                         }
27                         if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
28                                 $conversation['reply-to-uri'] = $arr['thr-parent'];
29                         }
30
31                         if (isset($arr['conversation-uri'])) {
32                                 $conversation['conversation-uri'] = $arr['conversation-uri'];
33                         }
34
35                         if (isset($arr['conversation-href'])) {
36                                 $conversation['conversation-href'] = $arr['conversation-href'];
37                         }
38
39                         if (isset($arr['protocol'])) {
40                                 $conversation['protocol'] = $arr['protocol'];
41                         }
42
43                         if (isset($arr['source'])) {
44                                 $conversation['source'] = $arr['source'];
45                         }
46
47                         $old_conv = dba::fetch_first("SELECT `item-uri`, `reply-to-uri`, `conversation-uri`, `conversation-href`, `protocol`, `source`
48                                         FROM `conversation` WHERE `item-uri` = ?", $conversation['item-uri']);
49                         if (DBM::is_result($old_conv)) {
50                                 // Don't update when only the source has changed.
51                                 // Only do this when there had been no source before.
52                                 if ($old_conv['source'] != '') {
53                                         unset($old_conv['source']);
54                                 }
55                                 // Update structure data all the time but the source only when its from a better protocol.
56                                 if (($old_conv['protocol'] < $conversation['protocol']) && ($old_conv['protocol'] != 0)) {
57                                         unset($conversation['protocol']);
58                                         unset($conversation['source']);
59                                 }
60                                 if (!dba::update('conversation', $conversation, ['item-uri' => $conversation['item-uri']], $old_conv)) {
61                                         logger('Conversation: update for '.$conversation['item-uri'].' from '.$conv['protocol'].' to '.$conversation['protocol'].' failed', LOGGER_DEBUG);
62                                 }
63                         } else {
64                                 if (!dba::insert('conversation', $conversation, true)) {
65                                         logger('Conversation: insert for '.$conversation['item-uri'].' (protocol '.$conversation['protocol'].') failed', LOGGER_DEBUG);
66                                 }
67                         }
68                 }
69
70                 unset($arr['conversation-uri']);
71                 unset($arr['conversation-href']);
72                 unset($arr['protocol']);
73                 unset($arr['source']);
74
75                 return $arr;
76         }
77 }