]> git.mxchange.org Git - friendica.git/commitdiff
New function to delete items for users
authorMichael <heluecht@pirati.ca>
Tue, 29 May 2018 05:22:57 +0000 (05:22 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 29 May 2018 05:22:57 +0000 (05:22 +0000)
include/api.php
include/items.php
mod/admin.php
mod/events.php
mod/item.php
mod/photos.php
mod/videos.php
src/Model/Item.php
src/Protocol/DFRN.php
src/Protocol/Diaspora.php
src/Protocol/OStatus.php

index 383225778496ae971b5041e38e48873814468ecd..a70c18f3ca6a149417d50ea813c5d3eb2e2b12e2 100644 (file)
@@ -2256,7 +2256,7 @@ function api_statuses_destroy($type)
 
        $ret = api_statuses_show($type);
 
-       Item::deleteById($id, PRIORITY_HIGH, api_user());
+       Item::deleteForUser(['id' => $id], api_user());
 
        return $ret;
 }
@@ -4148,7 +4148,7 @@ function api_fr_photoalbum_delete($type)
                if (!DBM::is_result($photo_item)) {
                        throw new InternalServerErrorException("problem with deleting items occured");
                }
-               Item::deleteById($photo_item[0]['id'], PRIORITY_HIGH, api_user());
+               Item::deleteForUser(['id' => $photo_item[0]['id']], api_user());
        }
 
        // now let's delete all photos from the album
@@ -4441,7 +4441,7 @@ function api_fr_photo_delete($type)
                }
                // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore
                // to the user and the contacts of the users (drop_items() do all the necessary magic to avoid orphans in database and federate deletion)
-               Item::deleteById($photo_item[0]['id'], PRIORITY_HIGH, api_user());
+               Item::deleteForUser(['id' => $photo_item[0]['id']], api_user());
 
                $answer = ['result' => 'deleted', 'message' => 'photo with id `' . $photo_id . '` has been deleted from server.'];
                return api_format_data("photo_delete", $type, ['$result' => $answer]);
index b84ac701a614ba03a2a132fa648caa989ed76fd7..4533b6077e1b0d68574a944a35dbb4668635e93b 100644 (file)
@@ -321,7 +321,7 @@ function drop_items($items) {
 
        if (count($items)) {
                foreach ($items as $item) {
-                       $owner = Item::deleteById($item, PRIORITY_HIGH, local_user());
+                       $owner = Item::deleteForUser(['id' => $item], local_user());
                        if ($owner && !$uid)
                                $uid = $owner;
                }
@@ -393,7 +393,7 @@ function drop_item($id) {
                }
 
                // delete the item
-               Item::deleteById($item['id'], PRIORITY_HIGH, local_user());
+               Item::deleteForUser(['id' => $item['id']], local_user());
 
                goaway(System::baseUrl() . '/' . $_SESSION['return_url']);
                //NOTREACHED
index e374201fffa5e49a46f2838a168a941caaef2397..83dafcfd97f6e564ac1e2a1222df1ca7f17d6b12 100644 (file)
@@ -555,14 +555,9 @@ function admin_page_deleteitem_post(App $a)
                if (strpos($guid, '/')) {
                        $guid = substr($guid, strrpos($guid, '/') + 1);
                }
-               // Now that we have the GUID get all IDs of the associated entries in the
-               // item table of the DB and drop those items, which will also delete the
+               // Now that we have the GUID, drop those items, which will also delete the
                // associated threads.
-               $r = dba::select('item', ['id'], ['guid' => $guid]);
-               while ($row = dba::fetch($r)) {
-                       Item::deleteById($row['id']);
-               }
-               dba::close($r);
+               Item::delete(['guid' => $guid]);
        }
 
        info(L10n::t('Item marked for deletion.') . EOL);
index e40e69f5009ae7345418be79a19cc2a71c236011..87c358a99eed6c3e9559e86cf0d1cc2ed48d67bc 100644 (file)
@@ -545,7 +545,7 @@ function events_content(App $a) {
 
                // Delete only real events (no birthdays)
                if (DBM::is_result($ev) && $ev[0]['type'] == 'event') {
-                       $del = Item::deleteById($ev[0]['itemid'], PRIORITY_HIGH, local_user());
+                       $del = Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
                }
 
                if ($del == 0) {
index ca79a640287e938bbe3fa9ae73a6e6d5df718669..823350874b0f422c28aff708eb39917b31565790 100644 (file)
@@ -877,7 +877,7 @@ function item_content(App $a) {
        $o = '';
        if (($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
                if (is_ajax()) {
-                       $o = Item::deleteById($a->argv[2], PRIORITY_HIGH, local_user());
+                       $o = Item::deleteForUser(['id' => $a->argv[2]], local_user());
                } else {
                        $o = drop_item($a->argv[2]);
                }
index 3d38be5037cf26332e61ff887ce8e694ecd5a558..30d3728d36865db24d5d14441daec2752894d308 100644 (file)
@@ -284,14 +284,7 @@ function photos_post(App $a)
                        );
 
                        // find and delete the corresponding item with all the comments and likes/dislikes
-                       $r = q("SELECT `id` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
-                               intval($page_owner_uid)
-                       );
-                       if (DBM::is_result($r)) {
-                               foreach ($r as $rr) {
-                                       Item::deleteById($rr['id'], PRIORITY_HIGH, $page_owner_uid);
-                               }
-                       }
+                       Item::deleteForUser(['resource-id' => $res, 'uid' => $page_owner_uid], $page_owner_uid);
 
                        // Update the photo albums cache
                        Photo::clearAlbumCache($page_owner_uid);
@@ -344,16 +337,11 @@ function photos_post(App $a)
                                intval($page_owner_uid),
                                dbesc($r[0]['resource-id'])
                        );
-                       $i = q("SELECT `id` FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1",
-                               dbesc($r[0]['resource-id']),
-                               intval($page_owner_uid)
-                       );
-                       if (DBM::is_result($i)) {
-                               Item::deleteById($i[0]['id'], PRIORITY_HIGH, $page_owner_uid);
 
-                               // Update the photo albums cache
-                               Photo::clearAlbumCache($page_owner_uid);
-                       }
+                       Item::deleteForUser(['resource-id' => $r[0]['resource-id'], 'uid' => $page_owner_uid], $page_owner_uid);
+
+                       // Update the photo albums cache
+                       Photo::clearAlbumCache($page_owner_uid);
                }
 
                goaway('photos/' . $a->data['user']['nickname']);
index 6dee4586aa6f584428a036a0a198d5dd710fb8d1..0dd653e914d724e7dce19865cfe175f9da4ed038 100644 (file)
@@ -169,7 +169,7 @@ function videos_post(App $a) {
                        );
 
                        if (DBM::is_result($i)) {
-                               Item::deleteById($i[0]['id'], PRIORITY_HIGH, local_user());
+                               Item::deleteForUser(['id' => $i[0]['id']], local_user());
                        }
                }
 
index ea866152bb46126cc5f5b8a87da256b1ec7498bd..0bac3210d716beea256833cae90b616a614134f1 100644 (file)
@@ -92,13 +92,38 @@ class Item extends BaseObject
         *
         * @param array $condition The condition for finding the item entries
         * @param integer $priority Priority for the notification
-        * @param integer $uid User who wants to delete the item
         */
-       public static function delete($condition, $priority = PRIORITY_HIGH, $uid = 0)
+       public static function delete($condition, $priority = PRIORITY_HIGH)
        {
                $items = dba::select('item', ['id'], $condition);
                while ($item = dba::fetch($items)) {
-                       self::deleteById($item['id'], $priority, $uid);
+                       self::deleteById($item['id'], $priority);
+               }
+               dba::close($items);
+       }
+
+       /**
+        * @brief Delete an item for an user and notify others about it - if it was ours
+        *
+        * @param array $condition The condition for finding the item entries
+        * @param integer $uid User who wants to delete this item
+        */
+       public static function deleteForUser($condition, $uid)
+       {
+               if ($uid == 0) {
+                       return;
+               }
+
+               $items = dba::select('item', ['id', 'uid'], $condition);
+               while ($item = dba::fetch($items)) {
+                       // "Deleting" global items just means hiding them
+                       if ($item['uid'] == 0) {
+                               dba::update('user-item', ['hidden' => true], ['iid' => $item['id'], 'uid' => $uid], true);
+                       } elseif ($item['uid'] == $uid) {
+                               self::deleteById($item['id'], PRIORITY_HIGH);
+                       } else {
+                               logger('Wrong ownership. Not deleting item ' . $item['id']);
+                       }
                }
                dba::close($items);
        }
@@ -108,11 +133,10 @@ class Item extends BaseObject
         *
         * @param integer $item_id Item ID that should be delete
         * @param integer $priority Priority for the notification
-        * @param integer $uid User who wants to delete the item
         *
         * @return boolean success
         */
-       public static function deleteById($item_id, $priority = PRIORITY_HIGH, $uid = 0)
+       private static function deleteById($item_id, $priority = PRIORITY_HIGH)
        {
                // locate item to be deleted
                $fields = ['id', 'uri', 'uid', 'parent', 'parent-uri', 'origin',
@@ -134,12 +158,6 @@ class Item extends BaseObject
                        $parent = ['origin' => false];
                }
 
-               // "Deleting" global items just means hiding them
-               if (($item['uid'] == 0) && ($uid != 0)) {
-                       dba::update('user-item', ['hidden' => true], ['iid' => $item_id, 'uid' => $uid], true);
-                       return true;
-               }
-
                // clean up categories and tags so they don't end up as orphans
 
                $matches = false;
index 5efcfb80b5763365c905f9a973b8c00addacbf90..bf943f1a151def57fbb8c830a203409f32af2d4e 100644 (file)
@@ -2807,15 +2807,13 @@ class DFRN
                        }
                }
 
-               $entrytype = self::getEntryType($importer, $item);
-
-               if (!$item["deleted"]) {
-                       logger('deleting item '.$item["id"].' uri='.$uri, LOGGER_DEBUG);
-               } else {
+               if ($item["deleted"]) {
                        return;
                }
 
-               Item::deleteById($item["id"]);
+               logger('deleting item '.$item["id"].' uri='.$uri, LOGGER_DEBUG);
+
+               Item::delete(['id' => $item["id"]]);
        }
 
        /**
index d2a7995d867c4e510caf3eaf68752625dc3d7caa..f3aa672a215561435ec3c5ca10b8b49e5d6d37cd 100644 (file)
@@ -2785,7 +2785,7 @@ class Diaspora
 
                while ($item = dba::fetch($r)) {
                        // Fetch the parent item
-                       $parent = dba::selectFirst('item', ['author-link', 'origin'], ['id' => $item["parent"]]);
+                       $parent = dba::selectFirst('item', ['author-link'], ['id' => $item["parent"]]);
 
                        // Only delete it if the parent author really fits
                        if (!link_compare($parent["author-link"], $contact["url"]) && !link_compare($item["author-link"], $contact["url"])) {
@@ -2793,7 +2793,7 @@ class Diaspora
                                continue;
                        }
 
-                       Item::deleteById($item["id"]);
+                       Item::delete(['id' => $item["id"]]);
 
                        logger("Deleted target ".$target_guid." (".$item["id"].") from user ".$item["uid"]." parent: ".$item["parent"], LOGGER_DEBUG);
                }
index 8b6939214496aa61ff92b5598cc120ed397f4e0c..e91d81f58e212d7453ae9160dc80410a355e8db1 100644 (file)
@@ -537,13 +537,12 @@ class OStatus
        private static function deleteNotice($item)
        {
                $condition = ['uid' => $item['uid'], 'author-link' => $item['author-link'], 'uri' => $item['uri']];
-               $deleted = dba::selectFirst('item', ['id', 'parent-uri'], $condition);
-               if (!DBM::is_result($deleted)) {
-                       logger('Item from '.$item['author-link'].' with uri '.$item['uri'].' for user '.$item['uid']." wasn't found. We don't delete it. ");
+               if (!dba::exists('item', $condition)) {
+                       logger('Item from '.$item['author-link'].' with uri '.$item['uri'].' for user '.$item['uid']." wasn't found. We don't delete it.");
                        return;
                }
 
-               Item::deleteById($deleted["id"]);
+               Item::delete($condition);
 
                logger('Deleted item with uri '.$item['uri'].' for user '.$item['uid']);
        }