More Favorite pluginification (favecount, cache, menus(favecount, cache, menus))

This commit is contained in:
Mikael Nordfeldth 2014-06-28 13:11:03 +02:00
parent c137ead909
commit 7e597ea7cc
29 changed files with 136 additions and 169 deletions

View File

@ -216,7 +216,9 @@ EndShowBody: called after showing the <body> element (and </body>)
- $action: action object being shown
StartPersonalGroupNav: beginning of personal group nav menu
- $action: action object being shown
- $menu: Menu list object being shown
- $target: Profile for whom it is shown
- $scoped: Profile of currently logged in user (or null)
EndPersonalGroupNav: end of personal group nav menu (good place to add a menu item)
- $action: action object being shown

View File

@ -74,8 +74,6 @@ class BlockAction extends ProfileFormAction
/**
* Handle request
*
* Shows a page with list of favorite notices
*
* @param array $args $_REQUEST args; handled in prepare()
*
* @return void

View File

@ -760,39 +760,6 @@ class Profile extends Managed_DataObject
$other->isSubscribed($this);
}
function hasFave($notice)
{
$fave = Fave::pkeyGet(array('user_id' => $this->id,
'notice_id' => $notice->id));
return ((is_null($fave)) ? false : true);
}
function faveCount()
{
$c = Cache::instance();
if (!empty($c)) {
$cnt = $c->get(Cache::key('profile:fave_count:'.$this->id));
if (is_integer($cnt)) {
return (int) $cnt;
}
}
$faves = new Fave();
$faves->user_id = $this->id;
$cnt = (int) $faves->count('notice_id');
if (!empty($c)) {
$c->set(Cache::key('profile:fave_count:'.$this->id), $cnt);
}
return $cnt;
}
function favoriteNotices($own=false, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
{
return Fave::stream($this->id, $offset, $limit, $own, $since_id, $max_id);
}
function noticeCount()
{
$c = Cache::instance();
@ -815,20 +782,6 @@ class Profile extends Managed_DataObject
return $cnt;
}
function blowFavesCache()
{
$cache = Cache::instance();
if ($cache) {
// Faves don't happen chronologically, so we need to blow
// ;last cache, too
$cache->delete(Cache::key('fave:ids_by_user:'.$this->id));
$cache->delete(Cache::key('fave:ids_by_user:'.$this->id.';last'));
$cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id));
$cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id.';last'));
}
$this->blowFaveCount();
}
function blowSubscriberCount()
{
$c = Cache::instance();
@ -845,14 +798,6 @@ class Profile extends Managed_DataObject
}
}
function blowFaveCount()
{
$c = Cache::instance();
if (!empty($c)) {
$c->delete(Cache::key('profile:fave_count:'.$this->id));
}
}
function blowNoticeCount()
{
$c = Cache::instance();

View File

@ -440,11 +440,6 @@ class User extends Managed_DataObject
}
}
function hasFave($notice)
{
return $this->getProfile()->hasFave($notice);
}
function mutuallySubscribed(Profile $other)
{
return $this->getProfile()->mutuallySubscribed($other);
@ -479,16 +474,6 @@ class User extends Managed_DataObject
return $this->getProfile()->getNotices($offset, $limit, $since_id, $before_id);
}
function favoriteNotices($own=false, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
{
return $this->getProfile()->favoriteNotices($own, $offset, $limit, $since_id, $max_id);
}
function blowFavesCache()
{
$this->getProfile()->blowFavesCache();
}
function getSelfTags()
{
return Profile_tag::getTagsArray($this->id, $this->id, $this->id);
@ -641,7 +626,7 @@ class User extends Managed_DataObject
common_log(LOG_INFO, "User {$this->nickname} has no profile; continuing deletion.");
}
$related = array('Fave',
$related = array(
'Confirm_address',
'Remember_me',
'Foreign_link',

View File

@ -243,8 +243,6 @@ class ApiAction extends Action
$twitter_user['created_at'] = $this->dateTwitter($profile->created);
$twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
$timezone = 'UTC';
if (!empty($user) && $user->timezone) {
@ -290,6 +288,9 @@ class ApiAction extends Action
$twitter_user['statusnet_profile_url'] = $profile->profileurl;
// The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array
Event::handle('TwitterUserArray', array($profile, &$twitter_user, $this->scoped, array()));
return $twitter_user;
}

View File

@ -50,24 +50,27 @@ class PersonalGroupNav extends Menu
*
* @return void
*/
function show()
public function show()
{
$user = common_current_user();
// FIXME: Legacy StatusNet behaviour was to do this, but really it should be the GroupNav
// of the targeted user! ($this->action->arg('nickname')
$target = Profile::current();
if (empty($user)) {
if (!$target instanceof Profile) {
throw new ServerException('Cannot show personal group navigation without a current user.');
}
$user_profile = $user->getProfile();
$nickname = $user->nickname;
$name = $user_profile->getBestName();
$nickname = $target->getNickname();
$name = $target->getBestName();
$scoped = Profile::current();
$action = $this->actionName;
$mine = ($this->action->arg('nickname') == $nickname); // @fixme kinda vague
$this->out->elementStart('ul', array('class' => 'nav'));
if (Event::handle('StartPersonalGroupNav', array($this))) {
if (Event::handle('StartPersonalGroupNav', array($this, $target, $scoped))) {
$this->out->menuItem(common_local_url('all', array('nickname' =>
$nickname)),
// TRANS: Menu item in personal group navigation menu.
@ -92,21 +95,9 @@ class PersonalGroupNav extends Menu
// TRANS: %s is a username.
sprintf(_('Replies to %s'), $name),
$mine && $action =='replies', 'nav_timeline_replies');
$this->out->menuItem(common_local_url('showfavorites', array('nickname' =>
$nickname)),
// TRANS: Menu item in personal group navigation menu.
_m('MENU','Favorites'),
// @todo i18n FIXME: Need to make this two messages.
// TRANS: Menu item title in personal group navigation menu.
// TRANS: %s is a username.
sprintf(_('%s\'s favorite notices'),
// TRANS: Replaces %s in '%s\'s favorite notices'. (Yes, we know we need to fix this.)
($user_profile) ? $name : _m('FIXME','User')),
$mine && $action =='showfavorites', 'nav_timeline_favorites');
$cur = common_current_user();
if ($cur && $cur->id == $user->id &&
if ($scoped instanceof Profile && $scoped->id == $target->id &&
!common_config('singleuser', 'enabled')) {
$this->out->menuItem(common_local_url('inbox', array('nickname' =>
@ -118,7 +109,7 @@ class PersonalGroupNav extends Menu
$mine && $action =='inbox');
}
Event::handle('EndPersonalGroupNav', array($this));
Event::handle('EndPersonalGroupNav', array($this, $target, $scoped));
}
$this->out->elementEnd('ul');
}

View File

@ -92,8 +92,6 @@ class ProfileFormAction extends RedirectingAction
/**
* Handle request
*
* Shows a page with list of favorite notices
*
* @param array $args $_REQUEST args; handled in prepare()
*
* @return void

View File

@ -88,13 +88,6 @@ class PublicGroupNav extends Menu
_('Featured users'), $this->actionName == 'featured', 'nav_featured');
}
if (!common_config('singleuser', 'enabled')) {
// TRANS: Menu item in search group navigation panel.
$this->out->menuItem(common_local_url('favorited'), _m('MENU','Popular'),
// TRANS: Menu item title in search group navigation panel.
_('Popular notices'), $this->actionName == 'favorited', 'nav_timeline_favorited');
}
Event::handle('EndPublicGroupNav', array($this));
}
$this->action->elementEnd('ul');

View File

@ -467,9 +467,9 @@ class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
$count - 3),
$count - 3);
} else {
// TRANS: List message for favoured notices.
// TRANS: %%s is a list of users liking a notice.
// TRANS: Plural is based on the number of of users that have favoured a notice.
// TRANS: List message for repeated notices.
// TRANS: %%s is a list of users who have repeated a notice.
// TRANS: Plural is based on the number of of users that have repeated a notice.
return sprintf(_m('%%s repeated this.',
'%%s repeated this.',
$count),

View File

@ -141,8 +141,8 @@ class TrainAction extends Action
if ($this->boolean('ajax')) {
$this->startHTML('text/xml;charset=utf-8');
$this->elementStart('head');
// TRANS: Page title for page on which favorite notices can be unfavourited.
$this->element('title', null, _('Disfavor favorite.'));
// TRANS: Page title for page on which we train the spam filter for ham or spam
$this->element('title', null, _('Train spam filter'));
$this->elementEnd('head');
$this->elementStart('body');
$form->show();

View File

@ -80,7 +80,7 @@ class AnonDisfavorAction extends RedirectingAction
$this->serverError(_m('Could not delete favorite.'));
}
$profile->blowFavesCache();
Fave::blowCacheForProfileId($profile->id);
if ($this->boolean('ajax')) {
$this->startHTML('text/xml;charset=utf-8');

View File

@ -68,12 +68,12 @@ class AnonFavorAction extends RedirectingAction
}
$fave = Fave::addNew($profile, $notice);
if (!$fave) {
if (!$fave instanceof Fave) {
// TRANS: Server error.
$this->serverError(_m('Could not create favorite.'));
}
$profile->blowFavesCache();
Fave::blowCacheForProfileId($profile->id);
if ($this->boolean('ajax')) {
$this->startHTML('text/xml;charset=utf-8');

View File

@ -287,16 +287,9 @@ class BookmarkPlugin extends MicroAppPlugin
*
* @see Action
*/
function onEndPersonalGroupNav($action)
function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
{
$this->user = common_current_user();
if (!$this->user) {
// TRANS: Client error displayed when trying to display bookmarks for a non-existing user.
$this->clientError(_('No such user.'));
}
$action->menuItem(common_local_url('bookmarks', array('nickname' => $this->user->nickname)),
$menu->menuItem(common_local_url('bookmarks', array('nickname' => $target->getNickname())),
// TRANS: Menu item in sample plugin.
_m('Bookmarks'),
// TRANS: Menu item title in sample plugin.

View File

@ -162,6 +162,11 @@ class FavoritePlugin extends ActivityHandlerPlugin
return true;
}
public function onTwitterUserArray(Profile $profile, array &$userdata, Profile $scoped=null, array $args=array())
{
$userdata['favourites_count'] = Fave::countByProfile($profile);
}
/**
* Typically just used to fill out StatusNet specific data in API calls in the referenced $info array.
*/
@ -180,10 +185,7 @@ class FavoritePlugin extends ActivityHandlerPlugin
if ($fave->find()) {
while ($fave->fetch()) {
Memcached_DataObject::blow('fave:ids_by_user_own:%d', $fave->user_id);
Memcached_DataObject::blow('fave:ids_by_user_own:%d;last', $fave->user_id);
Memcached_DataObject::blow('fave:ids_by_user:%d', $fave->user_id);
Memcached_DataObject::blow('fave:ids_by_user:%d;last', $fave->user_id);
Fave::blowCacheForProfileId($fave->user_id);
$fave->delete();
}
}
@ -191,6 +193,16 @@ class FavoritePlugin extends ActivityHandlerPlugin
$fave->free();
}
public function onUserDeleteRelated(User $user, array &$related)
{
$fave = new Fave();
$fave->user_id = $user->id;
$fave->delete(); // Will perform a DELETE matching "user_id = {$user->id}"
Fave::blowCacheForProfileId($user->id);
return true;
}
public function onStartNoticeListPrefill(array &$notices, array $notice_ids, Profile $scoped=null)
{
// prefill array of objects, before pluginfication it was Notice::fillFaves($notices)
@ -301,6 +313,31 @@ class FavoritePlugin extends ActivityHandlerPlugin
$supported = $supported || $cmd instanceof FavCommand;
}
// Layout stuff
public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
{
$menu->out->menuItem(common_local_url('showfavorites', array('nickname' => $target->getNickname())),
// TRANS: Menu item in personal group navigation menu.
_m('MENU','Favorites'),
// @todo i18n FIXME: Need to make this two messages.
// TRANS: Menu item title in personal group navigation menu.
// TRANS: %s is a username.
sprintf(_('%s\'s favorite notices'), $target->getBestName()),
$scoped instanceof Profile && $target->id === $scoped->id && $menu->actionName =='showfavorites',
'nav_timeline_favorites');
}
public function onEndPublicGroupNav(Menu $menu)
{
if (!common_config('singleuser', 'enabled')) {
// TRANS: Menu item in search group navigation panel.
$menu->out->menuItem(common_local_url('favorited'), _m('MENU','Popular'),
// TRANS: Menu item title in search group navigation panel.
_('Popular notices'), $menu->actionName == 'favorited', 'nav_timeline_favorited');
}
}
public function onPluginVersion(array &$versions)
{
$versions[] = array('name' => 'Favorite',

View File

@ -141,7 +141,7 @@ class ApiFavoriteCreateAction extends ApiAuthAction
}
$this->notify($fave, $this->notice, $this->user);
$this->user->blowFavesCache();
Fave::blowCacheForProfileId($this->user->id);
if ($this->format == 'xml') {
$this->showSingleXmlStatus($this->notice);

View File

@ -143,7 +143,7 @@ class ApiFavoriteDestroyAction extends ApiAuthAction
return;
}
$this->user->blowFavesCache();
Fave::blowCacheForProfileId($this->user->id);
if ($this->format == 'xml') {
$this->showSingleXmlStatus($this->notice);

View File

@ -181,23 +181,13 @@ class ApiTimelineFavoritesAction extends ApiBareAuthAction
common_debug("since id = " . $this->since_id . " max id = " . $this->max_id);
if (!empty($this->auth_user) && $this->auth_user->id == $this->target->id) {
$notice = $this->target->favoriteNotices(
true,
($this->page-1) * $this->count,
$this->count,
$this->since_id,
$this->max_id
);
} else {
$notice = $this->target->favoriteNotices(
false,
($this->page-1) * $this->count,
$this->count,
$this->since_id,
$this->max_id
);
}
$notice = Fave::stream($this->target->id,
($this->page-1) * $this->count, // offset
$this->count, // limit
(!empty($this->auth_user) && $this->auth_user->id == $this->target->id), // own feed?
$this->since_id,
$this->max_id
);
while ($notice->fetch()) {
$notices[] = clone($notice);

View File

@ -269,8 +269,8 @@ class AtompubfavoritefeedAction extends ApiAuthAction
$fave = Fave::addNew($profile, $notice);
if (!empty($fave)) {
$this->_profile->blowFavesCache();
if ($fave instanceof Fave) {
Fave::blowCacheForProfileId($this->_profile->id);
$this->notify($fave, $notice, $this->auth_user);
}

View File

@ -73,7 +73,7 @@ class DisfavorAction extends FormAction
// TRANS: Server error displayed when removing a favorite from the database fails.
$this->serverError(_('Could not delete favorite.'));
}
$this->scoped->blowFavesCache();
Fave::blowCacheForProfileId($this->scoped->id);
if (StatusNet::isAjax()) {
$this->startHTML('text/xml;charset=utf-8');
$this->elementStart('head');

View File

@ -65,7 +65,7 @@ class FavorAction extends FormAction
$this->serverError(_('Could not create favorite.'));
}
$this->notify($notice, $this->scoped->getUser());
$this->scoped->blowFavesCache();
Fave::blowCacheForProfileId($this->scoped->id);
if (StatusNet::isAjax()) {
$this->startHTML('text/xml;charset=utf-8');
$this->elementStart('head');

View File

@ -85,8 +85,7 @@ class FavoritesrssAction extends Rss10Action
*/
function getNotices($limit=0)
{
$user = $this->user;
$notice = $user->favoriteNotices(false, 0, $limit);
$notice = Fave::stream($this->user->id, 0, $limit, $false);
$notices = array();
while ($notice->fetch()) {
$notices[] = clone($notice);

View File

@ -116,17 +116,13 @@ class ShowfavoritesAction extends Action
$cur = common_current_user();
if (!empty($cur) && $cur->id == $this->user->id) {
// Show imported/gateway notices as well as local if
// the user is looking at their own favorites
$this->notice = $this->user->favoriteNotices(true, ($this->page-1)*NOTICES_PER_PAGE,
NOTICES_PER_PAGE + 1);
} else {
$this->notice = $this->user->favoriteNotices(false, ($this->page-1)*NOTICES_PER_PAGE,
NOTICES_PER_PAGE + 1);
}
// Show imported/gateway notices as well as local if
// the user is looking at their own favorites, otherwise not.
$this->notice = Fave::stream($this->user->id,
($this->page-1)*NOTICES_PER_PAGE, // offset
NOTICES_PER_PAGE + 1, // limit
(!empty($cur) && $cur->id == $this->user->id) // own feed?
);
if (empty($this->notice)) {
// TRANS: Server error displayed when favourite notices could not be retrieved from the database.

View File

@ -191,6 +191,27 @@ class Fave extends Managed_DataObject
return $fav;
}
static function countByProfile(Profile $profile)
{
$c = Cache::instance();
if (!empty($c)) {
$cnt = $c->get(Cache::key('fave:count_by_profile:'.$profile->id));
if (is_integer($cnt)) {
return $cnt;
}
}
$faves = new Fave();
$faves->user_id = $profile->id;
$cnt = (int) $faves->count('notice_id');
if (!empty($c)) {
$c->set(Cache::key('fave:count_by_profile:'.$profile->id), $cnt);
}
return $cnt;
}
function getURI()
{
if (!empty($this->uri)) {
@ -231,4 +252,18 @@ class Fave extends Managed_DataObject
$faveMap = Fave::listGet('notice_id', $notice_ids);
self::$_faves = array_replace(self::$_faves, $faveMap);
}
static public function blowCacheForProfileId($profile_id)
{
$cache = Cache::instance();
if ($cache) {
// Faves don't happen chronologically, so we need to blow
// ;last cache, too
$cache->delete(Cache::key('fave:ids_by_user:'.$profile_id));
$cache->delete(Cache::key('fave:ids_by_user:'.$profile_id.';last'));
$cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id));
$cache->delete(Cache::key('fave:ids_by_user_own:'.$profile_id.';last'));
$cache->delete(Cache::key('fave:count_by_profile:'.$profile_id));
}
}
}

View File

@ -44,7 +44,7 @@ class FavCommand extends Command
}
}
$this->user->blowFavesCache();
Fave::blowCacheForProfileId($this->user->id);
// TRANS: Text shown when a notice has been marked as favourite successfully.
$channel->output($this->user, _('Notice marked as fave.'));

View File

@ -149,7 +149,7 @@ class GNUsocialPhotosPlugin extends Plugin
'title' => _('Upload a photo')));
}
*/
function onEndPersonalGroupNav($nav)
function onEndPersonalGroupNav(Menu $nav, Profile $target, Profile $scoped=null)
{
$nav->out->menuItem(common_local_url('photos',

View File

@ -131,7 +131,7 @@ class GNUsocialProfileExtensionsPlugin extends Plugin
return true;
}
function onStartPersonalGroupNav($nav)
function onStartPersonalGroupNav(Menu $nav, Profile $target, Profile $scoped=null)
{
$nav->out->menuItem(common_local_url('bio',
array('nickname' => $nav->action->trimmed('nickname'))), _('Bio'),

View File

@ -37,6 +37,8 @@ require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
*
* This class allows users to link their Twitter accounts
*
* Depends on Favorite plugin.
*
* @category Plugin
* @package StatusNet
* @author Zach Copley <zach@status.net>

View File

@ -20,6 +20,8 @@
/**
* Basic client class for Yammer's OAuth/JSON API.
*
* Depends on Favorite plugin
*
* @package YammerImportPlugin
* @author Brion Vibber <brion@status.net>
*/

View File

@ -443,7 +443,7 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase
$this->assertEquals($notice->source, $noticeInfo->getAttribute('source'));
$this->assertEquals('', $noticeInfo->getAttribute('repeat_of'));
$this->assertEquals('', $noticeInfo->getAttribute('repeated'));
$this->assertEquals('', $noticeInfo->getAttribute('favorite'));
// $this->assertEquals('', $noticeInfo->getAttribute('favorite'));
$this->assertEquals('', $noticeInfo->getAttribute('source_link'));
}
@ -485,7 +485,7 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase
$this->assertEquals('false', $noticeInfo->getAttribute('repeated'));
}
public function testNoticeInfoFave()
/* public function testNoticeInfoFave()
{
$notice = $this->_fakeNotice();
@ -510,7 +510,7 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase
$noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
$this->assertEquals('false', $noticeInfo->getAttribute('favorite'));
}
}*/
public function testConversationLink()
{