We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit 3ed726bb authored by Brion Vibber's avatar Brion Vibber

Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 0.9.x

parents 143897bf 23c45d6c
...@@ -258,10 +258,28 @@ EndShowExportData: just after showing the <div> with export data (feeds) ...@@ -258,10 +258,28 @@ EndShowExportData: just after showing the <div> with export data (feeds)
- $action: action object being shown - $action: action object being shown
StartShowNoticeItem: just before showing the notice item StartShowNoticeItem: just before showing the notice item
- $action: action object being shown - $item: The NoticeListItem object being shown
EndShowNoticeItem: just after showing the notice item EndShowNoticeItem: just after showing the notice item
- $action: action object being shown - $item: the NoticeListItem object being shown
StartShowNoticeInfo: just before showing notice info
- $item: The NoticeListItem object being shown
EndShowNoticeInfo: just after showing notice info
- $item: The NoticeListItem object being shown
StartShowNoticeOptions: just before showing notice options like fave, repeat, etc.
- $item: the NoticeListItem object being shown
EndShowNoticeOptions: just after showing notice options like fave, repeat, etc.
- $item: the NoticeListItem object being shown
StartShowFaveForm: just before showing the fave form
- $item: the NoticeListItem object being shown
EndShowFaveForm: just after showing the fave form
- $item: the NoticeListItem object being shown
StartShowPageNotice: just before showing the page notice (instructions or error) StartShowPageNotice: just before showing the page notice (instructions or error)
- $action: action object being shown - $action: action object being shown
...@@ -756,6 +774,22 @@ EndDisfavorNotice: After saving a notice as a favorite ...@@ -756,6 +774,22 @@ EndDisfavorNotice: After saving a notice as a favorite
- $profile: profile of the person faving (can be remote!) - $profile: profile of the person faving (can be remote!)
- $notice: notice being faved - $notice: notice being faved
StartFavorNoticeForm: starting the data in the form for favoring a notice
- $FavorForm: the favor form being shown
- $notice: notice being favored
EndFavorNoticeForm: Ending the data in the form for favoring a notice
- $FavorForm: the favor form being shown
- $notice: notice being favored
StartDisFavorNoticeForm: starting the data in the form for disfavoring a notice
- $DisfavorForm: the disfavor form being shown
- $notice: notice being difavored
EndDisFavorNoticeForm: Ending the data in the form for disfavoring a notice
- $DisfavorForm: the disfavor form being shown
- $notice: notice being disfavored
StartFindMentions: start finding mentions in a block of text StartFindMentions: start finding mentions in a block of text
- $sender: sender profile - $sender: sender profile
- $text: plain text version of the notice - $text: plain text version of the notice
......
...@@ -314,10 +314,14 @@ class SingleNoticeItem extends NoticeListItem ...@@ -314,10 +314,14 @@ class SingleNoticeItem extends NoticeListItem
function show() function show()
{ {
$this->showStart(); $this->showStart();
if (Event::handle('StartShowNoticeItem', array($this))) {
$this->showNotice(); $this->showNotice();
$this->showNoticeAttachments(); $this->showNoticeAttachments();
$this->showNoticeInfo(); $this->showNoticeInfo();
$this->showNoticeOptions(); $this->showNoticeOptions();
Event::handle('EndShowNoticeItem', array($this));
}
$this->showEnd(); $this->showEnd();
} }
......
...@@ -473,6 +473,41 @@ class Profile extends Memcached_DataObject ...@@ -473,6 +473,41 @@ class Profile extends Memcached_DataObject
return $cnt; return $cnt;
} }
function hasFave($notice)
{
$cache = common_memcache();
// XXX: Kind of a hack.
if (!empty($cache)) {
// This is the stream of favorite notices, in rev chron
// order. This forces it into cache.
$ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
// If it's in the list, then it's a fave
if (in_array($notice->id, $ids)) {
return true;
}
// If we're not past the end of the cache window,
// then the cache has all available faves, so this one
// is not a fave.
if (count($ids) < NOTICE_CACHE_WINDOW) {
return false;
}
// Otherwise, cache doesn't have all faves;
// fall through to the default
}
$fave = Fave::pkeyGet(array('user_id' => $this->id,
'notice_id' => $notice->id));
return ((is_null($fave)) ? false : true);
}
function faveCount() function faveCount()
{ {
$c = common_memcache(); $c = common_memcache();
...@@ -516,6 +551,20 @@ class Profile extends Memcached_DataObject ...@@ -516,6 +551,20 @@ class Profile extends Memcached_DataObject
return $cnt; return $cnt;
} }
function blowFavesCache()
{
$cache = common_memcache();
if ($cache) {
// Faves don't happen chronologically, so we need to blow
// ;last cache, too
$cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
$cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
$cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id));
$cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id.';last'));
}
$this->blowFaveCount();
}
function blowSubscriberCount() function blowSubscriberCount()
{ {
$c = common_memcache(); $c = common_memcache();
......
...@@ -412,37 +412,8 @@ class User extends Memcached_DataObject ...@@ -412,37 +412,8 @@ class User extends Memcached_DataObject
function hasFave($notice) function hasFave($notice)
{ {
$cache = common_memcache(); $profile = $this->getProfile();
return $profile->hasFave($notice);
// XXX: Kind of a hack.
if ($cache) {
// This is the stream of favorite notices, in rev chron
// order. This forces it into cache.
$ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
// If it's in the list, then it's a fave
if (in_array($notice->id, $ids)) {
return true;
}
// If we're not past the end of the cache window,
// then the cache has all available faves, so this one
// is not a fave.
if (count($ids) < NOTICE_CACHE_WINDOW) {
return false;
}
// Otherwise, cache doesn't have all faves;
// fall through to the default
}
$fave = Fave::pkeyGet(array('user_id' => $this->id,
'notice_id' => $notice->id));
return ((is_null($fave)) ? false : true);
} }
function mutuallySubscribed($other) function mutuallySubscribed($other)
...@@ -511,17 +482,8 @@ class User extends Memcached_DataObject ...@@ -511,17 +482,8 @@ class User extends Memcached_DataObject
function blowFavesCache() function blowFavesCache()
{ {
$cache = common_memcache();
if ($cache) {
// Faves don't happen chronologically, so we need to blow
// ;last cache, too
$cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
$cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
$cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id));
$cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id.';last'));
}
$profile = $this->getProfile(); $profile = $this->getProfile();
$profile->blowFaveCount(); $profile->blowFavesCache();
} }
function getSelfTags() function getSelfTags()
......
...@@ -123,9 +123,13 @@ class DisfavorForm extends Form ...@@ -123,9 +123,13 @@ class DisfavorForm extends Form
function formData() function formData()
{ {
if (Event::handle('StartDisFavorNoticeForm', array($this, $this->notice))) {
$this->out->hidden('notice-n'.$this->notice->id, $this->out->hidden('notice-n'.$this->notice->id,
$this->notice->id, $this->notice->id,
'notice'); 'notice');
Event::handle('EndDisFavorNoticeForm', array($this, $this->notice));
}
} }
/** /**
......
...@@ -123,9 +123,12 @@ class FavorForm extends Form ...@@ -123,9 +123,12 @@ class FavorForm extends Form
function formData() function formData()
{ {
if (Event::handle('StartFavorNoticeForm', array($this, $this->notice))) {
$this->out->hidden('notice-n'.$this->notice->id, $this->out->hidden('notice-n'.$this->notice->id,
$this->notice->id, $this->notice->id,
'notice'); 'notice');
Event::handle('EndFavorNoticeForm', array($this, $this->notice));
}
} }
/** /**
......
...@@ -226,16 +226,21 @@ class NoticeListItem extends Widget ...@@ -226,16 +226,21 @@ class NoticeListItem extends Widget
function showNoticeInfo() function showNoticeInfo()
{ {
$this->out->elementStart('div', 'entry-content'); $this->out->elementStart('div', 'entry-content');
if (Event::handle('StartShowNoticeInfo', array($this))) {
$this->showNoticeLink(); $this->showNoticeLink();
$this->showNoticeSource(); $this->showNoticeSource();
$this->showNoticeLocation(); $this->showNoticeLocation();
$this->showContext(); $this->showContext();
$this->showRepeat(); $this->showRepeat();
Event::handle('EndShowNoticeInfo', array($this));
}
$this->out->elementEnd('div'); $this->out->elementEnd('div');
} }
function showNoticeOptions() function showNoticeOptions()
{ {
if (Event::handle('StartShowNoticeOptions', array($this))) {
$user = common_current_user(); $user = common_current_user();
if ($user) { if ($user) {
$this->out->elementStart('div', 'notice-options'); $this->out->elementStart('div', 'notice-options');
...@@ -245,6 +250,8 @@ class NoticeListItem extends Widget ...@@ -245,6 +250,8 @@ class NoticeListItem extends Widget
$this->showDeleteLink(); $this->showDeleteLink();
$this->out->elementEnd('div'); $this->out->elementEnd('div');
} }
Event::handle('EndShowNoticeOptions', array($this));
}
} }
/** /**
...@@ -270,6 +277,7 @@ class NoticeListItem extends Widget ...@@ -270,6 +277,7 @@ class NoticeListItem extends Widget
function showFaveForm() function showFaveForm()
{ {
if (Event::handle('StartShowFaveForm', array($this))) {
$user = common_current_user(); $user = common_current_user();
if ($user) { if ($user) {
if ($user->hasFave($this->notice)) { if ($user->hasFave($this->notice)) {
...@@ -280,6 +288,8 @@ class NoticeListItem extends Widget ...@@ -280,6 +288,8 @@ class NoticeListItem extends Widget
$favor->show(); $favor->show();
} }
} }
Event::handle('EndShowFaveForm', array($this));
}
} }
/** /**
......
<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* A plugin to allow anonymous users to favorite notices
*
* 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
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
// 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');
/**
* Anonymous Fave plugin to allow anonymous (not logged in) users
* to favorite notices
*
* @category Plugin
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
class AnonymousFavePlugin extends Plugin {
function onArgsInitialize() {
// We always want a session because we're tracking anon users
common_ensure_session();
}
/**
* Hook for ensuring our tables are created
*
* Ensures the fave_tally table is there and has the right columns
*
* @return boolean hook return
*/
function onCheckSchema()
{
$schema = Schema::get();
// For storing total number of times a notice has been faved
$schema->ensureTable('fave_tally',
array(
new ColumnDef('notice_id', 'integer', null, false, 'PRI'),
new ColumnDef('count', 'integer', null, false),
new ColumnDef(
'modified',
'timestamp',
null,
false,
null,
'CURRENT_TIMESTAMP',
'on update CURRENT_TIMESTAMP'
)
)
);
return true;
}
function onEndShowHTML($action)
{
if (!common_logged_in()) {
// Set a place to return to when submitting forms
common_set_returnto($action->selfUrl());
}
}
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 onAutoload($cls)
{
$dir = dirname(__FILE__);
switch ($cls) {
case 'Fave_tally':
include_once $dir . '/' . $cls . '.php';
return false;
case 'AnonFavorAction':
include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
return false;
case 'AnonDisFavorAction':
include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
return false;
case 'AnonFavorForm':
include_once $dir . '/anonfavorform.php';
return false;
case 'AnonDisFavorForm':
include_once $dir . '/anondisfavorform.php';
return false;
default:
return true;
}
}
function onStartInitializeRouter($m) {
$m->connect('main/anonfavor', array('action' => 'AnonFavor'));
$m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
return true;
}
function onStartShowNoticeOptions($item) {
if (!common_logged_in()) {
$item->out->elementStart('div', 'notice-options');
$item->showFaveForm();
$item->out->elementEnd('div');
}
return true;
}
function onStartShowFaveForm($item) {
if (!common_logged_in()) {
$profile = AnonymousFavePlugin::getAnonProfile();
if (!empty($profile)) {
if ($profile->hasFave($item->notice)) {
$disfavor = new AnonDisFavorForm($item->out, $item->notice);
$disfavor->show();
} else {
$favor = new AnonFavorForm($item->out, $item->notice);
$favor->show();
}
}
}
return true;
}
function onEndFavorNoticeForm($form, $notice)
{
$this->showTally($form->out, $notice);
}
function onEndDisFavorNoticeForm($form, $notice)
{
$this->showTally($form->out, $notice);
}
function showTally($out, $notice)
{
$tally = Fave_tally::ensureTally($notice->id);
if (!empty($tally)) {
$out->elementStart(
'div',
array(
'id' => 'notice-' . $notice->id . '-tally',
'class' => 'notice-tally'
)
);
$out->raw(sprintf(_m("favored %d times"), $tally->count));
$out->elementEnd('div');
}
}
function onEndFavorNotice($profile, $notice)
{
$tally = Fave_tally::increment($notice->id);
}
function onEndDisfavorNotice($profile, $notice)
{
$tally = Fave_tally::decrement($notice->id);
}
static function createAnonProfile() {
// Get the anon user's IP, and turn it into a nickname
list($proxy, $ip) = common_client_ip();
// IP + time + random number should help to avoid collisions
$baseNickname = $ip . '-' . time() . '-' . common_good_rand(5);
$profile = new Profile();
$profile->nickname = $baseNickname;
$id = $profile->insert();
if (!$id) {
throw new ServerException(_m("Couldn't create anonymous user session."));
}
// Stick the Profile ID into the nickname
$orig = clone($profile);
$profile->nickname = 'anon-' . $id . '-' . $baseNickname;
$result = $profile->update($orig);
if (!$result) {
throw new ServerException(_m("Couldn't create anonymous user session."));
}
common_log(
LOG_INFO,
"AnonymousFavePlugin - created profile for anonymous user from IP: "
. $ip
. ', nickname = '
. $profile->nickname
);
return $profile;
}
static function getAnonProfile() {
$token = $_SESSION['anon_token'];
$anon = base64_decode($token);
$profile = null;
if (!empty($anon) && substr($anon, 0, 5) == 'anon-') {
$parts = explode('-', $anon);
$id = $parts[1];
// Do Profile lookup by ID instead of nickname for safety/performance
$profile = Profile::staticGet('id', $id);
} else {
$profile = AnonymousFavePlugin::createAnonProfile();
// Obfuscate so it's hard to figure out the Profile ID
$_SESSION['anon_token'] = base64_encode($profile->nickname);
}
return $profile;
}
/**
* Provide plugin version information.
*
* This data is used when showing the version page.
*
* @param array &$versions array of version data arrays; see EVENTS.txt
*
* @return boolean hook value
*/
function onPluginVersion(&$versions)
{
$url = 'http://status.net/wiki/Plugin:AnonymousFave';
$versions[] = array('name' => 'AnonymousFave',
'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
'author' => 'Zach Copley',
'homepage' => $url,
'rawdescription' =>
_m('Allow anonymous users to favorite notices.'));
return true;
}
}
<?php
/**
* Data class for favorites talley
*
* PHP version 5
*
* @category Data
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* 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