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