* @package Activity
* @maintainer Mikael Nordfeldth <mmn@hethane.se>
*/
-class FavoritePlugin extends ActivityHandlerPlugin
+class FavoritePlugin extends ActivityVerbHandlerPlugin
{
protected $email_notify_fave = 1;
public function verbs()
{
- return array(ActivityVerb::FAVORITE);
+ return array(ActivityVerb::FAVORITE, ActivityVerb::LIKE,
+ ActivityVerb::UNFAVORITE, ActivityVerb::UNLIKE);
}
-
+
public function onCheckSchema()
{
$schema = Schema::get();
printfnq("DONE.\n");
}
}
-
+
public function onEndUpgrade()
{
printfnq("Ensuring all faves have a URI...");
-
+
$fave = new Fave();
$fave->whereAdd('uri IS NULL');
-
+
if ($fave->find()) {
while ($fave->fetch()) {
try {
}
}
}
-
+
printfnq("DONE.\n");
}
}
// FIXME: Set this to abstract public in lib/activityhandlerplugin.php ddwhen all plugins have migrated!
- protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
+ protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
{
assert($this->isMyActivity($act));
}
return true;
}
-
+
public function onNoticeDeleteRelated(Notice $notice)
{
parent::onNoticeDeleteRelated($notice);
}
}
+ protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
+ {
+ return Fave::existsForProfile($target, $scoped)
+ // TRANS: Page/dialog box title when a notice is marked as favorite already
+ ? _m('TITLE', 'Unmark notice as favorite')
+ // TRANS: Page/dialog box title when a notice is not marked as favorite
+ : _m('TITLE', 'Mark notice as favorite');
+ }
+
+ protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
+ {
+ if ($action->isPost()) {
+ // The below tests are only for presenting to the user. POSTs which inflict
+ // duplicate favorite entries are handled with AlreadyFulfilledException.
+ return false;
+ }
+
+ $exists = Fave::existsForProfile($target, $scoped);
+ $expected_verb = $exists ? ActivityVerb::UNFAVORITE : ActivityVerb::FAVORITE;
+
+ switch (true) {
+ case $exists && ActivityUtils::compareTypes($verb, array(ActivityVerb::FAVORITE, ActivityVerb::LIKE)):
+ case !$exists && ActivityUtils::compareTypes($verb, array(ActivityVerb::UNFAVORITE, ActivityVerb::UNLIKE)):
+ common_redirect(common_local_url('activityverb',
+ array('id' => $target->getID(),
+ 'verb' => ActivityUtils::resolveUri($expected_verb, true))));
+ break;
+ default:
+ // No need to redirect as we are on the correct action already.
+ }
+
+ return false;
+ }
+
+ protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
+ {
+ switch (true) {
+ case ActivityUtils::compareTypes($verb, array(ActivityVerb::FAVORITE, ActivityVerb::LIKE)):
+ Fave::addNew($scoped, $target);
+ break;
+ case ActivityUtils::compareTypes($verb, array(ActivityVerb::UNFAVORITE, ActivityVerb::UNLIKE)):
+ Fave::removeEntry($scoped, $target);
+ break;
+ default:
+ throw new ServerException('ActivityVerb POST not handled by plugin that was supposed to do it.');
+ }
+ return false;
+ }
+
+ protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
+ {
+ return Fave::existsForProfile($target, $scoped)
+ ? new DisfavorForm($action, $target)
+ : new FavorForm($action, $target);
+ }
+
public function onPluginVersion(array &$versions)
{
$versions[] = array('name' => 'Favorite',