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