]> git.mxchange.org Git - friendica.git/blob - src/Model/Item.php
Transfer all item shadow functions to the item class
[friendica.git] / src / Model / Item.php
1 <?php
2
3 /**
4  * @file src/Model/Item.php
5  */
6
7 namespace Friendica\Model;
8
9 use Friendica\Core\Worker;
10 use Friendica\Model\Term;
11 use Friendica\Model\Contact;
12 use Friendica\Database\DBM;
13 use dba;
14
15 require_once 'include/tags.php';
16 require_once 'include/threads.php';
17
18 class Item
19 {
20         /**
21          * @brief Update existing item entries
22          *
23          * @param array $fields The fields that are to be changed
24          * @param array $condition The condition for finding the item entries
25          *
26          * In the future we may have to change permissions as well.
27          * Then we had to add the user id as third parameter.
28          *
29          * A return value of "0" doesn't mean an error - but that 0 rows had been changed.
30          *
31          * @return integer|boolean number of affected rows - or "false" if there was an error
32          */
33         public static function update(array $fields, array $condition)
34         {
35                 if (empty($condition) || empty($fields)) {
36                         return false;
37                 }
38
39                 $success = dba::update('item', $fields, $condition);
40
41                 if (!$success) {
42                         return false;
43                 }
44
45                 $rows = dba::affected_rows();
46
47                 // We cannot simply expand the condition to check for origin entries
48                 // The condition needn't to be a simple array but could be a complex condition.
49                 $items = dba::select('item', ['id', 'origin'], $condition);
50                 while ($item = dba::fetch($items)) {
51                         // We only need to notfiy others when it is an original entry from us
52                         if (!$item['origin']) {
53                                 continue;
54                         }
55
56                         create_tags_from_item($item['id']);
57                         Term::createFromItem($item['id']);
58                         update_thread($item['id']);
59
60                         Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $item['id']);
61                 }
62
63                 return $rows;
64         }
65
66         /**
67          * @brief Add a shadow entry for a given item id that is a thread starter
68          *
69          * We store every public item entry additionally with the user id "0".
70          * This is used for the community page and for the search.
71          * It is planned that in the future we will store public item entries only once.
72          *
73          * @param integer $itemid Item ID that should be added
74          */
75         public static function addShadow($itemid) {
76                 $fields = ['uid', 'wall', 'private', 'moderated', 'visible', 'contact-id', 'deleted', 'network', 'author-id', 'owner-id'];
77                 $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
78                 $item = dba::selectFirst('item', $fields, $condition);
79
80                 if (!DBM::is_result($item)) {
81                         return;
82                 }
83
84                 // is it already a copy?
85                 if (($itemid == 0) || ($item['uid'] == 0)) {
86                         return;
87                 }
88
89                 // Is it a visible public post?
90                 if (!$item["visible"] || $item["deleted"] || $item["moderated"] || $item["private"]) {
91                         return;
92                 }
93
94                 // is it an entry from a connector? Only add an entry for natively connected networks
95                 if (!in_array($item["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""])) {
96                         return;
97                 }
98
99                 // Is the public contact configured as hidden?
100                 if (Contact::isHidden($item["owner-id"]) || Contact::isHidden($item["author-id"])) {
101                         return;
102                 }
103
104                 // Only do these checks if the post isn't a wall post
105                 if (!$item["wall"]) {
106                         // Check, if hide-friends is activated - then don't do a shadow entry
107                         if (dba::exists('profile', ['is-default' => true, 'uid' => $item['uid'], 'hide-friends' => true])) {
108                                 return;
109                         }
110
111                         // Check if the contact is hidden or blocked
112                         if (!dba::exists('contact', ['hidden' => false, 'blocked' => false, 'id' => $item['contact-id']])) {
113                                 return;
114                         }
115                 }
116
117                 // Only add a shadow, if the profile isn't hidden
118                 if (dba::exists('user', ['uid' => $item['uid'], 'hidewall' => true])) {
119                         return;
120                 }
121
122                 $item = dba::selectFirst('item', [], ['id' => $itemid]);
123
124                 if (count($item) && ($item["allow_cid"] == '')  && ($item["allow_gid"] == '') &&
125                         ($item["deny_cid"] == '') && ($item["deny_gid"] == '')) {
126
127                         if (!dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
128                                 // Preparing public shadow (removing user specific data)
129                                 require_once("include/items.php");
130
131                                 unset($item['id']);
132                                 $item['uid'] = 0;
133                                 $item['origin'] = 0;
134                                 $item['wall'] = 0;
135                                 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
136
137                                 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
138                                         $item['type'] = 'remote-comment';
139                                 } elseif ($item['type'] == 'wall') {
140                                         $item['type'] = 'remote';
141                                 }
142
143                                 $public_shadow = item_store($item, false, false, true);
144
145                                 logger("Stored public shadow for thread ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
146                         }
147                 }
148         }
149
150         /**
151          * @brief Add a shadow entry for a given item id that is a comment
152          *
153          * This function does the same like the function above - but for comments
154          *
155          * @param integer $itemid Item ID that should be added
156          */
157         public static function addShadowPost($itemid) {
158                 $item = dba::selectFirst('item', [], ['id' => $itemid]);
159                 if (!DBM::is_result($item)) {
160                         return;
161                 }
162
163                 // Is it a toplevel post?
164                 if ($item['id'] == $item['parent']) {
165                         self::addShadow($itemid);
166                         return;
167                 }
168
169                 // Is this a shadow entry?
170                 if ($item['uid'] == 0)
171                         return;
172
173                 // Is there a shadow parent?
174                 if (!dba::exists('item', ['uri' => $item['parent-uri'], 'uid' => 0])) {
175                         return;
176                 }
177
178                 // Is there already a shadow entry?
179                 if (dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
180                         return;
181                 }
182
183                 // Preparing public shadow (removing user specific data)
184                 require_once("include/items.php");
185
186                 unset($item['id']);
187                 $item['uid'] = 0;
188                 $item['origin'] = 0;
189                 $item['wall'] = 0;
190                 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
191
192                 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
193                         $item['type'] = 'remote-comment';
194                 } elseif ($item['type'] == 'wall') {
195                         $item['type'] = 'remote';
196                 }
197
198                 $public_shadow = item_store($item, false, false, true);
199
200                 logger("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, LOGGER_DEBUG);
201         }
202 }