123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- <?php
- defined('GNUSOCIAL') || die();
- class FavoriteModule extends ActivityVerbHandlerModule
- {
- const MODULE_VERSION = '2.0.0';
- protected $email_notify_fave = 1;
- public function tag()
- {
- return 'favorite';
- }
- public function types()
- {
- return array();
- }
- public function verbs()
- {
- return array(ActivityVerb::FAVORITE, ActivityVerb::LIKE,
- ActivityVerb::UNFAVORITE, ActivityVerb::UNLIKE);
- }
- public function onCheckSchema()
- {
- $schema = Schema::get();
- $schema->ensureTable('fave', Fave::schemaDef());
- return true;
- }
- public function initialize()
- {
- common_config_set('email', 'notify_fave', $this->email_notify_fave);
- }
- public function onStartUpgrade()
- {
-
-
-
-
- $user = new User();
- $user->whereAdd('emailnotifyfav IS NOT NULL');
- if ($user->find()) {
- printfnq("Detected old User table (emailnotifyfav IS NOT NULL). Moving 'emailnotifyfav' property to Profile_prefs...");
-
- $schema = Schema::get();
- $schema->ensureTable('profile_prefs', Profile_prefs::schemaDef());
-
- while ($user->fetch()) {
- $user->setPref('email', 'notify_fave', $user->emailnotifyfav);
- $orig = clone($user);
- $user->emailnotifyfav = $user->sqlValue('NULL');
- $user->update($orig);
- }
- 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 {
- $fave->decache();
- $fave->query(sprintf(
- "UPDATE fave SET uri = '%s', modified = TIMESTAMP '%s' " .
- 'WHERE user_id = %d AND notice_id = %d',
- Fave::newUri($fave->getActor(), $fave->getTarget(), $fave->modified),
- common_sql_date(strtotime($fave->modified)),
- $fave->user_id,
- $fave->notice_id
- ));
- } catch (Exception $e) {
- common_log(LOG_ERR, "Error updating fave URI: " . $e->getMessage());
- }
- }
- }
- printfnq("DONE.\n");
- }
- public function onRouterInitialized(URLMapper $m)
- {
-
- $m->connect(
- 'main/favor',
- ['action' => 'favor']
- );
- $m->connect(
- 'main/disfavor',
- ['action' => 'disfavor']
- );
- if (common_config('singleuser', 'enabled')) {
- $nickname = User::singleUserNickname();
- $m->connect(
- 'favorites',
- [
- 'action' => 'showfavorites',
- 'nickname' => $nickname,
- ]
- );
- $m->connect(
- 'favoritesrss',
- [
- 'action' => 'favoritesrss',
- 'nickname' => $nickname,
- ]
- );
- } else {
- $m->connect(
- 'favoritedrss',
- ['action' => 'favoritedrss']
- );
- $m->connect(
- 'favorited/',
- ['action' => 'favorited']
- );
- $m->connect(
- 'favorited',
- ['action' => 'favorited']
- );
- $m->connect(
- ':nickname/favorites',
- ['action' => 'showfavorites'],
- ['nickname' => Nickname::DISPLAY_FMT]
- );
- $m->connect(
- ':nickname/favorites/rss',
- ['action' => 'favoritesrss'],
- ['nickname' => Nickname::DISPLAY_FMT]
- );
- }
-
- $m->connect(
- 'api/favorites/create.:format',
- ['action' => 'ApiFavoriteCreate'],
- ['format' => '(xml|json)']
- );
- $m->connect(
- 'api/favorites/destroy.:format',
- ['action' => 'ApiFavoriteDestroy'],
- ['format' => '(xml|json)']
- );
- $m->connect(
- 'api/favorites/list.:format',
- ['action' => 'ApiTimelineFavorites'],
- ['format' => '(xml|json|rss|atom|as)']
- );
- $m->connect(
- 'api/favorites/:id.:format',
- ['action' => 'ApiTimelineFavorites'],
- [
- 'id' => Nickname::INPUT_FMT,
- 'format' => '(xml|json|rss|atom|as)',
- ]
- );
- $m->connect(
- 'api/favorites.:format',
- ['action' => 'ApiTimelineFavorites'],
- ['format' => '(xml|json|rss|atom|as)']
- );
- $m->connect(
- 'api/favorites/create/:id.:format',
- ['action' => 'ApiFavoriteCreate'],
- [
- 'id' => '[0-9]+',
- 'format' => '(xml|json)',
- ]
- );
- $m->connect(
- 'api/favorites/destroy/:id.:format',
- ['action' => 'ApiFavoriteDestroy'],
- [
- 'id' => '[0-9]+',
- 'format' => '(xml|json)',
- ]
- );
-
- $m->connect(
- 'api/statusnet/app/favorites/:profile/:notice.atom',
- ['action' => 'AtomPubShowFavorite'],
- [
- 'profile' => '[0-9]+',
- 'notice' => '[0-9]+',
- ]
- );
- $m->connect(
- 'api/statusnet/app/favorites/:profile.atom',
- ['action' => 'AtomPubFavoriteFeed'],
- ['profile' => '[0-9]+']
- );
-
- $m->connect(
- 'api/statuses/favs/:id.:format',
- ['action' => 'ApiStatusesFavs'],
- [
- 'id' => '[0-9]+',
- 'format' => '(xml|json)',
- ]
- );
- }
-
- protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
- {
- assert($this->isMyActivity($act));
-
- $actobj = $act->objects[0];
- $object = Fave::saveActivityObject($actobj, $stored);
- return $object;
- }
-
-
-
- public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
- {
- if (!$this->isMyNotice($stored)) {
- return true;
- }
- $this->extendActivity($stored, $act, $scoped);
- return false;
- }
- public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
- {
- Fave::extendActivity($stored, $act, $scoped);
- }
- public function activityObjectFromNotice(Notice $notice)
- {
- $fave = Fave::fromStored($notice);
- return $fave->asActivityObject();
- }
- public function deleteRelated(Notice $notice)
- {
- try {
- $fave = Fave::fromStored($notice);
- $fave->delete();
- } catch (NoResultException $e) {
-
- }
- }
-
-
- public function onNoticeSimpleStatusArray($notice, array &$status, Profile $scoped=null, array $args=array())
- {
- if ($scoped instanceof Profile) {
- $status['favorited'] = Fave::existsForProfile($notice, $scoped);
- } else {
- $status['favorited'] = false;
- }
- return true;
- }
- public function onTwitterUserArray(Profile $profile, array &$userdata, Profile $scoped=null, array $args=array())
- {
- $userdata['favourites_count'] = Fave::countByProfile($profile);
- }
-
- public function onStatusNetApiNoticeInfo(Notice $notice, array &$info, Profile $scoped=null, array $args=array())
- {
- if ($scoped instanceof Profile) {
- $info['favorite'] = Fave::existsForProfile($notice, $scoped) ? 'true' : 'false';
- }
- return true;
- }
- public function onNoticeDeleteRelated(Notice $notice)
- {
- parent::onNoticeDeleteRelated($notice);
-
-
-
- $fave = new Fave();
- $fave->notice_id = $notice->id;
- if ($fave->find()) {
- while ($fave->fetch()) {
- $fave->delete();
- }
- }
- $fave->free();
- }
- public function onProfileDeleteRelated(Profile $profile, array &$related)
- {
- $fave = new Fave();
- $fave->user_id = $profile->id;
- $fave->delete();
- $fave->free();
- Fave::blowCacheForProfileId($profile->id);
- return true;
- }
- public function onStartNoticeListPrefill(array &$notices, array $notice_ids, Profile $scoped=null)
- {
-
- Fave::fillFaves($notice_ids);
-
- if ($scoped instanceof Profile) {
- Fave::pivotGet('notice_id', $notice_ids, array('user_id' => $scoped->id));
- }
- }
-
- public function onStartShowNoticeOptionItems($nli)
- {
- if (Event::handle('StartShowFaveForm', array($nli))) {
- $scoped = Profile::current();
- if ($scoped instanceof Profile) {
- if (Fave::existsForProfile($nli->notice, $scoped)) {
- $disfavor = new DisfavorForm($nli->out, $nli->notice);
- $disfavor->show();
- } else {
- $favor = new FavorForm($nli->out, $nli->notice);
- $favor->show();
- }
- }
- Event::handle('EndShowFaveForm', array($nli));
- }
- }
- protected function showNoticeListItem(NoticeListItem $nli)
- {
-
- }
- public function openNoticeListItemElement(NoticeListItem $nli)
- {
-
- }
- public function closeNoticeListItemElement(NoticeListItem $nli)
- {
-
- }
- public function onAppendUserActivityStreamObjects(UserActivityStream $uas, array &$objs)
- {
- $fave = new Fave();
- $fave->user_id = $uas->getUser()->id;
- if (!empty($uas->after)) {
- $fave->whereAdd("modified > '" . common_sql_date($uas->after) . "'");
- }
- if ($fave->find()) {
- while ($fave->fetch()) {
- $objs[] = clone($fave);
- }
- }
- return true;
- }
- public function onEndShowThreadedNoticeTailItems(NoticeListItem $nli, Notice $notice, &$threadActive)
- {
- if ($nli instanceof ThreadedNoticeListSubItem) {
-
- $item = new ThreadedNoticeListInlineFavesItem($notice, $nli->out);
- } else {
- $item = new ThreadedNoticeListFavesItem($notice, $nli->out);
- }
- $threadActive = $item->show() || $threadActive;
- return true;
- }
- public function onEndFavorNotice(Profile $actor, Notice $target)
- {
- try {
- $notice_author = $target->getProfile();
-
-
- if ($notice_author->id == $actor->id || !$notice_author->isLocal()) {
- return true;
- }
- $local_user = $notice_author->getUser();
- mail_notify_fave($local_user, $actor, $target);
- } catch (Exception $e) {
-
- }
- }
-
- public function onStartInterpretCommand($cmd, $arg, $user, &$result)
- {
- if ($result === false && $cmd == 'fav') {
- if (empty($arg)) {
- $result = null;
- } else {
- list($other, $extra) = CommandInterpreter::split_arg($arg);
- if (!empty($extra)) {
- $result = null;
- } else {
- $result = new FavCommand($user, $other);
- }
- }
- return false;
- }
- return true;
- }
- public function onHelpCommandMessages(HelpCommand $help, array &$commands)
- {
-
- $commands['fav <nickname>'] = _m('COMMANDHELP', "add user's last notice as a 'fave'");
-
- $commands['fav #<notice_id>'] = _m('COMMANDHELP', "add notice with the given id as a 'fave'");
- }
-
- public function onCommandSupportedAPI(Command $cmd, &$supported)
- {
- $supported = $supported || $cmd instanceof FavCommand;
- }
-
- public function onEndEmailFormData(Action $action, Profile $scoped)
- {
- $emailfave = $scoped->getConfigPref('email', 'notify_fave') ? 1 : 0;
- $action->elementStart('li');
- $action->checkbox(
- 'email-notify_fave',
-
- _('Send me email when someone adds my notice as a favorite.'),
- $emailfave
- );
- $action->elementEnd('li');
- return true;
- }
- public function onStartEmailSaveForm(Action $action, Profile $scoped)
- {
- $emailfave = $action->boolean('email-notify_fave');
- try {
- if ($emailfave == (bool) $scoped->getPref('email', 'notify_fave')) {
-
- return true;
- }
- } catch (NoResultException $e) {
-
- }
- $scoped->setPref('email', 'notify_fave', $emailfave);
- return true;
- }
-
- public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
- {
- $menu->out->menuItem(
- common_local_url('showfavorites', ['nickname' => $target->getNickname()]),
-
- _m('MENU', 'Favorites'),
-
-
-
- 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')) {
-
- $menu->out->menuItem(
- common_local_url('favorited'),
- _m('MENU', 'Popular'),
-
- _('Popular notices'),
- ($menu->actionName === 'favorited'),
- 'nav_timeline_favorited'
- );
- }
- }
- public function onEndShowSections(Action $action)
- {
- if (!$action->isAction(array('all', 'public'))) {
- return true;
- }
- if (!common_config('performance', 'high')) {
- $section = new PopularNoticeSection($action, $action->getScoped());
- $section->show();
- }
- }
- protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
- {
- return Fave::existsForProfile($target, $scoped)
-
- ? _m('TITLE', 'Unmark notice as favorite')
-
- : _m('TITLE', 'Mark notice as favorite');
- }
- protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
- {
- if ($action->isPost()) {
-
-
- return false;
- }
- $exists = Fave::existsForProfile($target, $scoped);
- $expected_verb = $exists ? ActivityVerb::UNFAVORITE : ActivityVerb::FAVORITE;
- switch (true) {
- case $exists && ActivityUtils::compareVerbs($verb, array(ActivityVerb::FAVORITE, ActivityVerb::LIKE)):
- case !$exists && ActivityUtils::compareVerbs($verb, array(ActivityVerb::UNFAVORITE, ActivityVerb::UNLIKE)):
- common_redirect(common_local_url(
- 'activityverb',
- [
- 'id' => $target->getID(),
- 'verb' => ActivityUtils::resolveUri($expected_verb, true),
- ]
- ));
- break;
- default:
-
- }
- return false;
- }
- protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
- {
- switch (true) {
- case ActivityUtils::compareVerbs($verb, array(ActivityVerb::FAVORITE, ActivityVerb::LIKE)):
- Fave::addNew($scoped, $target);
- break;
- case ActivityUtils::compareVerbs($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 onModuleVersion(array &$versions): bool
- {
- $versions[] = array('name' => 'Favorite',
- 'version' => self::MODULE_VERSION,
- 'author' => 'Mikael Nordfeldth',
- 'homepage' => GNUSOCIAL_ENGINE_URL,
- 'rawdescription' =>
-
- _m('Favorites (likes) using ActivityStreams.'));
- return true;
- }
- }
- function mail_notify_fave(User $rcpt, Profile $sender, Notice $notice)
- {
- if (!$rcpt->receivesEmailNotifications() || !$rcpt->getConfigPref('email', 'notify_fave')) {
- return;
- }
-
- if (!$sender->hasRight(Right::EMAILONFAVE)) {
- return;
- }
- if ($rcpt->hasBlocked($sender)) {
-
- return;
- }
-
- require_once INSTALLDIR . '/lib/util/mail.php';
- $bestname = $sender->getBestName();
- common_switch_locale($rcpt->language);
-
-
- $subject = sprintf(_('%1$s (@%2$s) added your notice as a favorite'), $bestname, $sender->getNickname());
-
-
-
-
-
- $body = sprintf(
- _("%1\$s (@%7\$s) just added your notice from %2\$s".
- " as one of their favorites.\n\n" .
- "The URL of your notice is:\n\n" .
- "%3\$s\n\n" .
- "The text of your notice is:\n\n" .
- "%4\$s\n\n" .
- "You can see the list of %1\$s's favorites here:\n\n" .
- "%5\$s"),
- $bestname,
- common_exact_date($notice->created),
- common_local_url('shownotice', ['notice' => $notice->id]),
- $notice->content,
- common_local_url('showfavorites', ['nickname' => $sender->getNickname()]),
- common_config('site', 'name'),
- $sender->getNickname()
- ) .
- mail_footer_block();
- $headers = _mail_prepare_headers('fave', $rcpt->getNickname(), $sender->getNickname());
- common_switch_locale();
- mail_to_user($rcpt, $subject, $body, $headers);
- }
|