123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- abstract class ActivityHandlerModule extends Module
- {
-
-
- abstract function tag();
-
- abstract function types();
-
- public function verbs() {
- return array(ActivityVerb::POST);
- }
-
- function isMyActivity(Activity $act) {
- return (count($act->objects) == 1
- && ($act->objects[0] instanceof ActivityObject)
- && $this->isMyVerb($act->verb)
- && $this->isMyType($act->objects[0]->type));
- }
-
- function isMyNotice(Notice $notice) {
- return $this->isMyVerb($notice->verb) && $this->isMyType($notice->object_type);
- }
- function isMyVerb($verb) {
- $verb = $verb ?: ActivityVerb::POST;
- return ActivityUtils::compareVerbs($verb, $this->verbs());
- }
- function isMyType($type) {
-
- return count($this->types())===0 || ActivityUtils::compareTypes($type, $this->types());
- }
-
- public function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
- {
-
-
-
- if (isset($this->oldSaveNew)) {
- throw new ServerException('A function has been called for new saveActivity functionality, but is still set with an oldSaveNew configuration');
- }
- return Notice::saveActivity($activity, $actor, $options);
- }
-
- protected function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
- {
- throw new ServerException('This function should be abstract when all plugins have migrated to saveObjectFromActivity');
- }
-
- public function onStoreActivityObject(Activity $act, Notice $stored, array $options, &$object) {
-
-
- if (!$this->isMyActivity($act) || isset($this->oldSaveNew)) {
- return true;
- }
- $object = $this->saveObjectFromActivity($act, $stored, $options);
- return false;
- }
-
- abstract function activityObjectFromNotice(Notice $notice);
-
- abstract function deleteRelated(Notice $notice);
- protected function notifyMentioned(Notice $stored, array &$mentioned_ids)
- {
-
-
- return true;
- }
-
- function activityObjectOutputAtom(ActivityObject $obj, XMLOutputter $out)
- {
-
- }
-
- public function activityObjectOutputJson(ActivityObject $obj, array &$out)
- {
-
- }
-
- public function onNoticeDeleteRelated(Notice $notice)
- {
- if ($this->isMyNotice($notice)) {
- try {
- $this->deleteRelated($notice);
- } catch (NoProfileException $e) {
-
-
-
- } catch (AlreadyFulfilledException $e) {
-
- }
- }
-
- return true;
- }
-
- public function onStartNotifyMentioned(Notice $stored, array &$mentioned_ids)
- {
- if (!$this->isMyNotice($stored)) {
- return true;
- }
- return $this->notifyMentioned($stored, $mentioned_ids);
- }
-
- function onStartActivityObjectFromNotice(Notice $notice, &$object)
- {
- if (!$this->isMyNotice($notice)) {
- return true;
- }
- $object = $this->activityObjectFromNotice($notice);
- return false;
- }
-
- function onStartHandleFeedEntryWithProfile(Activity $activity, Profile $profile, &$notice)
- {
- if (!$this->isMyActivity($activity)) {
- return true;
- }
-
- $profile = ActivityUtils::checkAuthorship($activity, $profile);
- $object = $activity->objects[0];
- $options = array('uri' => $object->id,
- 'url' => $object->link,
- 'self' => $object->selfLink,
- 'is_local' => Notice::REMOTE,
- 'source' => 'ostatus');
- if (!isset($this->oldSaveNew)) {
- $notice = Notice::saveActivity($activity, $profile, $options);
- } else {
- $notice = $this->saveNoticeFromActivity($activity, $profile, $options);
- }
- return false;
- }
-
- function onStartHandleSalmonTarget(Activity $activity, $target)
- {
- if (!$this->isMyActivity($activity)) {
- return true;
- }
- if (!isset($this->oldSaveNew)) {
-
-
- return true;
- }
- $this->log(LOG_INFO, get_called_class()." checking {$activity->id} as a valid Salmon slap.");
- if ($target instanceof User_group || $target->isGroup()) {
- $uri = $target->getUri();
- if (!array_key_exists($uri, $activity->context->attention)) {
-
-
- throw new ClientException(_('Object not posted to this group.'));
- }
- } elseif ($target instanceof Profile && $target->isLocal()) {
- $original = null;
-
- if (!ActivityUtils::compareVerbs($activity->verb, array(ActivityVerb::POST)) && isset($activity->objects[0])) {
-
- if (!empty($activity->objects[0]->id)) {
- $activity->context->replyToID = $activity->objects[0]->id;
- }
- }
- if (!empty($activity->context->replyToID)) {
- $original = Notice::getKV('uri', $activity->context->replyToID);
- }
- if ((!$original instanceof Notice || $original->profile_id != $target->id)
- && !array_key_exists($target->getUri(), $activity->context->attention)) {
-
-
- throw new ClientException(_('Object not posted to this user.'));
- }
- } else {
-
- throw new ServerException(_('Do not know how to handle this kind of target.'));
- }
- $oactor = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
- $actor = $oactor->localProfile();
-
- if (ActivityUtils::compareVerbs($activity->verb, array(ActivityVerb::POST))) {
- $object = $activity->objects[0];
- } else {
- $object = $activity;
- }
- $options = array('uri' => $object->id,
- 'url' => $object->link,
- 'self' => $object->selfLink,
- 'is_local' => Notice::REMOTE,
- 'source' => 'ostatus');
- $notice = $this->saveNoticeFromActivity($activity, $actor, $options);
- return false;
- }
-
- public function onStartAtomPubNewActivity(Activity $activity, Profile $scoped, Notice &$notice=null)
- {
- if (!$this->isMyActivity($activity)) {
- return true;
- }
- $options = array('source' => 'atompub');
- $notice = $this->saveNoticeFromActivity($activity, $scoped, $options);
- return false;
- }
-
- function onStartImportActivity($user, $author, Activity $activity, $trusted, &$done)
- {
- if (!$this->isMyActivity($activity)) {
- return true;
- }
- $obj = $activity->objects[0];
- $options = array('uri' => $object->id,
- 'url' => $object->link,
- 'self' => $object->selfLink,
- 'source' => 'restore');
-
- $saved = $this->saveNoticeFromActivity($activity,
- $user->getProfile(),
- $options);
- if (!empty($saved)) {
- $done = true;
- }
- return false;
- }
-
- function onEndActivityObjectOutputAtom(ActivityObject $obj, XMLOutputter $out)
- {
- if (in_array($obj->type, $this->types())) {
- $this->activityObjectOutputAtom($obj, $out);
- }
- return true;
- }
-
- function onEndActivityObjectOutputJson(ActivityObject $obj, array &$out)
- {
- if (in_array($obj->type, $this->types())) {
- $this->activityObjectOutputJson($obj, $out);
- }
- return true;
- }
- public function onStartOpenNoticeListItemElement(NoticeListItem $nli)
- {
- if (!$this->isMyNotice($nli->notice)) {
- return true;
- }
- $this->openNoticeListItemElement($nli);
- Event::handle('EndOpenNoticeListItemElement', array($nli));
- return false;
- }
- public function onStartCloseNoticeListItemElement(NoticeListItem $nli)
- {
- if (!$this->isMyNotice($nli->notice)) {
- return true;
- }
- $this->closeNoticeListItemElement($nli);
- Event::handle('EndCloseNoticeListItemElement', array($nli));
- return false;
- }
- protected function openNoticeListItemElement(NoticeListItem $nli)
- {
- $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
- $class = 'h-entry notice ' . $this->tag();
- if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
- $class .= ' limited-scope';
- }
- try {
- $class .= ' notice-source-'.common_to_alphanumeric($nli->notice->source);
- } catch (Exception $e) {
-
- }
- $nli->out->elementStart('li', array('class' => $class,
- 'id' => 'notice-' . $id));
- }
- protected function closeNoticeListItemElement(NoticeListItem $nli)
- {
- $nli->out->elementEnd('li');
- }
-
- public function onStartShowNoticeItem(NoticeListItem $nli)
- {
- if (!$this->isMyNotice($nli->notice)) {
- return true;
- }
- try {
- $this->showNoticeListItem($nli);
- } catch (Exception $e) {
- common_log(LOG_ERR, 'Error showing notice '.$nli->getNotice()->getID().': ' . $e->getMessage());
- $nli->out->element('p', 'error', sprintf(_('Error showing notice: %s'), $e->getMessage()));
- }
- Event::handle('EndShowNoticeItem', array($nli));
- return false;
- }
- protected function showNoticeListItem(NoticeListItem $nli)
- {
- $nli->showNoticeHeaders();
- $nli->showContent();
- $nli->showNoticeFooter();
- }
- public function onStartShowNoticeItemNotice(NoticeListItem $nli)
- {
- if (!$this->isMyNotice($nli->notice)) {
- return true;
- }
- $this->showNoticeItemNotice($nli);
- Event::handle('EndShowNoticeItemNotice', array($nli));
- return false;
- }
- protected function showNoticeItemNotice(NoticeListItem $nli)
- {
- $nli->showNoticeTitle();
- $nli->showAuthor();
- $nli->showAddressees();
- $nli->showContent();
- }
- public function onStartShowNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
- {
- if (!$this->isMyNotice($stored)) {
- return true;
- }
- try {
- $this->showNoticeContent($stored, $out, $scoped);
- } catch (Exception $e) {
- $out->element('div', 'error', $e->getMessage());
- }
- return false;
- }
- protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
- {
- $out->text($stored->getContent());
- }
- }
|