Clean Notice_prefs and Fave_tally when a notice is deleted

This commit is contained in:
Alexei Sorokin 2020-08-08 12:22:35 +03:00 committed by Diogo Peralta Cordeiro
parent d7ec199793
commit 46ac40d981
2 changed files with 67 additions and 49 deletions

View File

@ -185,6 +185,7 @@ class Notice extends Managed_DataObject
// Clear related records // Clear related records
$this->clearReplies(); $this->clearReplies();
$this->clearLocation(); $this->clearLocation();
$this->clearPrefs();
$this->clearRepeats(); $this->clearRepeats();
$this->clearTags(); $this->clearTags();
$this->clearGroupInboxes(); $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() public function clearFiles()
{ {
$f2p = new File_to_post(); $f2p = new File_to_post();

View File

@ -1,8 +1,20 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social 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.
//
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* A plugin to allow anonymous users to favorite notices * A plugin to allow anonymous users to favorite notices
* *
* If you want to keep certain users from having anonymous faving for their * If you want to keep certain users from having anonymous faving for their
@ -10,38 +22,17 @@
* *
* addPlugin( * addPlugin(
* 'AnonymousFave', * '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 <http://www.gnu.org/licenses/>.
*
* @category Plugin * @category Plugin
* @package StatusNet * @package GNUsocial
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @copyright 2010 StatusNet, Inc. * @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
if (!defined('STATUSNET')) { defined('GNUSOCIAL') || die();
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1.0'); define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1.0');
@ -50,11 +41,10 @@ define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1.0');
* to favorite notices * to favorite notices
* *
* @category Plugin * @category Plugin
* @package StatusNet * @package GNUsocial
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @copyright 2010 StatusNet, Inc. * @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
class AnonymousFavePlugin extends Plugin class AnonymousFavePlugin extends Plugin
{ {
@ -62,7 +52,8 @@ class AnonymousFavePlugin extends Plugin
// that anonymous faving is allowed for all users. // that anonymous faving is allowed for all users.
public $restricted = array(); public $restricted = array();
function onArgsInitialize() { public function onArgsInitialize()
{
// We always want a session because we're tracking anon users // We always want a session because we're tracking anon users
common_ensure_session(); common_ensure_session();
} }
@ -75,7 +66,7 @@ class AnonymousFavePlugin extends Plugin
* @return boolean hook return * @return boolean hook return
*/ */
function onCheckSchema() public function onCheckSchema()
{ {
$schema = Schema::get(); $schema = Schema::get();
@ -85,7 +76,7 @@ class AnonymousFavePlugin extends Plugin
return true; return true;
} }
function onEndShowHTML($action) public function onEndShowHTML($action)
{ {
if (!common_logged_in()) { if (!common_logged_in()) {
// Set a place to return to when submitting forms // 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 // Setup ajax calls for favoriting. Usually this is only done when
// a user is logged in. // a user is logged in.
$action->inlineScript('SN.U.NoticeFavor();'); $action->inlineScript('SN.U.NoticeFavor();');
} }
function onStartInitializeRouter($m) public function onStartInitializeRouter($m)
{ {
$m->connect('main/anonfavor', array('action' => 'AnonFavor')); $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
$m->connect('main/anondisfavor', array('action' => 'AnonDisFavor')); $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
@ -108,21 +99,22 @@ class AnonymousFavePlugin extends Plugin
return true; return true;
} }
function onStartShowNoticeOptions($item) public function onStartShowNoticeOptionItems(NoticeListItem $item): bool
{ {
if (!common_logged_in()) { if (!common_logged_in()) {
$item->out->elementStart('div', 'notice-options'); $item->out->elementStart('div', 'notice-options');
$item->showFaveForm(); if (Event::handle('StartShowFaveForm', [$item])) {
Event::handle('EndShowFaveForm', [$item]);
}
$item->out->elementEnd('div'); $item->out->elementEnd('div');
} }
return true; return true;
} }
function onStartShowFaveForm($item) public function onStartShowFaveForm($item)
{ {
if (!common_logged_in() && $this->hasAnonFaving($item)) { if (!common_logged_in() && $this->hasAnonFaving($item)) {
$profile = AnonymousFavePlugin::getAnonProfile(); $profile = AnonymousFavePlugin::getAnonProfile();
if ($profile instanceof Profile) { if ($profile instanceof Profile) {
if (Fave::existsForProfile($item->notice, $profile)) { if (Fave::existsForProfile($item->notice, $profile)) {
@ -138,17 +130,17 @@ class AnonymousFavePlugin extends Plugin
return true; return true;
} }
function onEndFavorNoticeForm($form, $notice) public function onEndFavorNoticeForm($form, $notice)
{ {
$this->showTally($form->out, $notice); $this->showTally($form->out, $notice);
} }
function onEndDisFavorNoticeForm($form, $notice) public function onEndDisFavorNoticeForm($form, $notice)
{ {
$this->showTally($form->out, $notice); $this->showTally($form->out, $notice);
} }
function showTally($out, $notice) private function showTally($out, Notice $notice): void
{ {
$tally = Fave_tally::ensureTally($notice->id); $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); $tally = Fave_tally::increment($notice->id);
} }
function onEndDisfavorNotice($profile, $notice) public function onEndDisfavorNotice($profile, $notice)
{ {
$tally = Fave_tally::decrement($notice->id); $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 // Get the anon user's IP, and turn it into a nickname
list($proxy, $ip) = common_client_ip(); list($proxy, $ip) = common_client_ip();
@ -220,9 +228,8 @@ class AnonymousFavePlugin extends Plugin
return $profile; return $profile;
} }
static function getAnonProfile() public static function getAnonProfile()
{ {
$token = $_SESSION['anon_token']; $token = $_SESSION['anon_token'];
$anon = base64_decode($token); $anon = base64_decode($token);
@ -252,7 +259,7 @@ class AnonymousFavePlugin extends Plugin
* in the list of restricted profiles, otherwise * in the list of restricted profiles, otherwise
* return true * return true
*/ */
function hasAnonFaving($item) private function hasAnonFaving($item): bool
{ {
$profile = Profile::getKV('id', $item->notice->profile_id); $profile = Profile::getKV('id', $item->notice->profile_id);
if (in_array($profile->nickname, $this->restricted)) { if (in_array($profile->nickname, $this->restricted)) {