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