From 46ac40d9818c52e4544aced7b44a2d47646a3a7e Mon Sep 17 00:00:00 2001 From: Alexei Sorokin Date: Sat, 8 Aug 2020 12:22:35 +0300 Subject: [PATCH] Clean Notice_prefs and Fave_tally when a notice is deleted --- classes/Notice.php | 11 ++ plugins/AnonymousFave/AnonymousFavePlugin.php | 105 ++++++++++-------- 2 files changed, 67 insertions(+), 49 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index 5bd291c371..bab09cee84 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -185,6 +185,7 @@ class Notice extends Managed_DataObject // Clear related records $this->clearReplies(); $this->clearLocation(); + $this->clearPrefs(); $this->clearRepeats(); $this->clearTags(); $this->clearGroupInboxes(); @@ -2482,6 +2483,16 @@ class Notice extends Managed_DataObject } } + private function clearPrefs(): void + { + $prefs = new Notice_prefs(); + $prefs->notice_id = $this->id; + + if ($prefs->find()) { + $prefs->delete(); + } + } + public function clearFiles() { $f2p = new File_to_post(); diff --git a/plugins/AnonymousFave/AnonymousFavePlugin.php b/plugins/AnonymousFave/AnonymousFavePlugin.php index 662e5da5e3..b0d7d2b657 100644 --- a/plugins/AnonymousFave/AnonymousFavePlugin.php +++ b/plugins/AnonymousFave/AnonymousFavePlugin.php @@ -1,8 +1,20 @@ . + /** - * StatusNet - the distributed open-source microblogging tool - * Copyright (C) 2010, StatusNet, Inc. - * * A plugin to allow anonymous users to favorite notices * * If you want to keep certain users from having anonymous faving for their @@ -10,38 +22,17 @@ * * addPlugin( * 'AnonymousFave', - * array('restricted' => array('spock', 'kirk', 'bones')) + * ['restricted' => ['spock', 'kirk', 'bones']] * ); * - * - * PHP version 5 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * * @category Plugin - * @package StatusNet + * @package GNUsocial * @author Zach Copley * @copyright 2010 StatusNet, Inc. - * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 - * @link http://status.net/ + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -if (!defined('STATUSNET')) { - // This check helps protect against security problems; - // your code file can't be executed directly from the web. - exit(1); -} +defined('GNUSOCIAL') || die(); define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1.0'); @@ -50,11 +41,10 @@ define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1.0'); * to favorite notices * * @category Plugin - * @package StatusNet + * @package GNUsocial * @author Zach Copley * @copyright 2010 StatusNet, Inc. - * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 - * @link http://status.net/ + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ class AnonymousFavePlugin extends Plugin { @@ -62,7 +52,8 @@ class AnonymousFavePlugin extends Plugin // that anonymous faving is allowed for all users. public $restricted = array(); - function onArgsInitialize() { + public function onArgsInitialize() + { // We always want a session because we're tracking anon users common_ensure_session(); } @@ -75,7 +66,7 @@ class AnonymousFavePlugin extends Plugin * @return boolean hook return */ - function onCheckSchema() + public function onCheckSchema() { $schema = Schema::get(); @@ -85,7 +76,7 @@ class AnonymousFavePlugin extends Plugin return true; } - function onEndShowHTML($action) + public function onEndShowHTML($action) { if (!common_logged_in()) { // Set a place to return to when submitting forms @@ -93,14 +84,14 @@ class AnonymousFavePlugin extends Plugin } } - function onEndShowScripts($action) + public function onEndShowScripts($action) { // Setup ajax calls for favoriting. Usually this is only done when // a user is logged in. $action->inlineScript('SN.U.NoticeFavor();'); } - function onStartInitializeRouter($m) + public function onStartInitializeRouter($m) { $m->connect('main/anonfavor', array('action' => 'AnonFavor')); $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor')); @@ -108,21 +99,22 @@ class AnonymousFavePlugin extends Plugin return true; } - function onStartShowNoticeOptions($item) + public function onStartShowNoticeOptionItems(NoticeListItem $item): bool { if (!common_logged_in()) { $item->out->elementStart('div', 'notice-options'); - $item->showFaveForm(); + if (Event::handle('StartShowFaveForm', [$item])) { + Event::handle('EndShowFaveForm', [$item]); + } $item->out->elementEnd('div'); } return true; } - function onStartShowFaveForm($item) + public function onStartShowFaveForm($item) { if (!common_logged_in() && $this->hasAnonFaving($item)) { - $profile = AnonymousFavePlugin::getAnonProfile(); if ($profile instanceof Profile) { if (Fave::existsForProfile($item->notice, $profile)) { @@ -138,17 +130,17 @@ class AnonymousFavePlugin extends Plugin return true; } - function onEndFavorNoticeForm($form, $notice) + public function onEndFavorNoticeForm($form, $notice) { $this->showTally($form->out, $notice); } - function onEndDisFavorNoticeForm($form, $notice) + public function onEndDisFavorNoticeForm($form, $notice) { $this->showTally($form->out, $notice); } - function showTally($out, $notice) + private function showTally($out, Notice $notice): void { $tally = Fave_tally::ensureTally($notice->id); @@ -171,17 +163,33 @@ class AnonymousFavePlugin extends Plugin } } - function onEndFavorNotice($profile, $notice) + public function onEndFavorNotice($profile, $notice) { $tally = Fave_tally::increment($notice->id); } - function onEndDisfavorNotice($profile, $notice) + public function onEndDisfavorNotice($profile, $notice) { $tally = Fave_tally::decrement($notice->id); } - static function createAnonProfile() + /** + * Remove tally when the notice is deleted + * + * @param Notice $notice Notice being deleted + * @return bool hook value + */ + public function onNoticeDeleteRelated(Notice $notice): bool + { + $ft = Fave_tally::getKV('notice_id', $notice->id); + + if (!empty($ft)) { + $ft->delete(); + } + return true; + } + + private static function createAnonProfile(): Profile { // Get the anon user's IP, and turn it into a nickname list($proxy, $ip) = common_client_ip(); @@ -220,9 +228,8 @@ class AnonymousFavePlugin extends Plugin return $profile; } - static function getAnonProfile() + public static function getAnonProfile() { - $token = $_SESSION['anon_token']; $anon = base64_decode($token); @@ -252,7 +259,7 @@ class AnonymousFavePlugin extends Plugin * in the list of restricted profiles, otherwise * return true */ - function hasAnonFaving($item) + private function hasAnonFaving($item): bool { $profile = Profile::getKV('id', $item->notice->profile_id); if (in_array($profile->nickname, $this->restricted)) {