ActivityModerationPlugin.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @package Activity
  4. * @maintainer Mikael Nordfeldth <mmn@hethane.se>
  5. */
  6. class ActivityModerationPlugin extends ActivityVerbHandlerPlugin
  7. {
  8. public function tag()
  9. {
  10. return 'actmod';
  11. }
  12. public function types()
  13. {
  14. return array();
  15. }
  16. public function verbs()
  17. {
  18. return array(ActivityVerb::DELETE);
  19. }
  20. public function onBeforePluginCheckSchema()
  21. {
  22. Deleted_notice::beforeSchemaUpdate();
  23. return true;
  24. }
  25. public function onCheckSchema()
  26. {
  27. $schema = Schema::get();
  28. $schema->ensureTable('deleted_notice', Deleted_notice::schemaDef());
  29. return true;
  30. }
  31. public function onGetNoticeSqlTimestamp($id, &$timestamp)
  32. {
  33. try {
  34. $deleted = Deleted_notice::getByID($id);
  35. $timestamp = $deleted->act_created;
  36. } catch (NoResultException $e) {
  37. return true;
  38. }
  39. // we're done for the event, so return false to stop it
  40. return false;
  41. }
  42. public function onIsNoticeDeleted($id, &$deleted)
  43. {
  44. try {
  45. $found = Deleted_notice::getByID($id);
  46. $deleted = ($found instanceof Deleted_notice);
  47. } catch (NoResultException $e) {
  48. $deleted = false;
  49. }
  50. // return true (continue event) if $deleted is false, return false (stop event) if deleted notice was found
  51. return !$deleted;
  52. }
  53. protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  54. {
  55. // FIXME: switch based on action type
  56. return _m('TITLE', 'Notice moderation');
  57. }
  58. protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  59. {
  60. // pass
  61. }
  62. protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  63. {
  64. switch (true) {
  65. case ActivityUtils::compareVerbs($verb, array(ActivityVerb::DELETE)):
  66. // do whatever preparation is necessary to delete a verb
  67. $target->delete();
  68. break;
  69. default:
  70. throw new ServerException('ActivityVerb POST not handled by plugin that was supposed to do it.');
  71. }
  72. }
  73. public function deleteRelated(Notice $notice)
  74. {
  75. // pass
  76. }
  77. public function onDeleteNoticeAsProfile(Notice $stored, Profile $actor, &$result) {
  78. // By adding a new 'delete' verb we will eventually trigger $this->saveObjectFromActivity
  79. if (false === Deleted_notice::addNew($stored, $actor)) {
  80. // false is returned if we did not have an error, but did not create the object
  81. // (i.e. the author is currently being deleted)
  82. return true;
  83. }
  84. // We return false (to stop the event) if the deleted_notice entry was
  85. // added, which means we have run $this->saveObjectFromActivity which
  86. // in turn has called the delete function of the notice.
  87. return false;
  88. }
  89. /**
  90. * This is run when a 'delete' verb activity comes in.
  91. *
  92. * @return boolean hook flag
  93. */
  94. protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
  95. {
  96. // Everything is done in the StartNoticeSave event
  97. return true;
  98. }
  99. // FIXME: Put this in lib/activityhandlerplugin.php when we're ready
  100. // with the other microapps/activityhandlers as well.
  101. // Also it should be StartNoticeAsActivity (with a prepped Activity, including ->context etc.)
  102. public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
  103. {
  104. if (!$this->isMyNotice($stored)) {
  105. return true;
  106. }
  107. common_debug('Extending activity '.$stored->id.' with '.get_called_class());
  108. $this->extendActivity($stored, $act, $scoped);
  109. return false;
  110. }
  111. /**
  112. * This is run before ->insert, so our task in this function is just to
  113. * delete if it is the delete verb.
  114. */
  115. public function onStartNoticeSave(Notice $stored)
  116. {
  117. // DELETE is a bit special, we have to remove the existing entry and then
  118. // add a new one with the same URI in order to trigger the distribution.
  119. // (that's why we don't use $this->isMyNotice(...))
  120. if (!ActivityUtils::compareVerbs($stored->verb, array(ActivityVerb::DELETE))) {
  121. return true;
  122. }
  123. try {
  124. $target = Notice::getByUri($stored->uri);
  125. } catch (NoResultException $e) {
  126. throw new AlreadyFulfilledException('Notice URI not found, so we have nothing to delete.');
  127. }
  128. $actor = $stored->getProfile();
  129. $owner = $target->getProfile();
  130. if ($owner->hasRole(Profile_role::DELETED)) {
  131. // Don't bother with replacing notices if its author is being deleted.
  132. // The later "StoreActivityObject" will pick this up and execute
  133. // the deletion then.
  134. // (the "delete verb notice" is too new to ever pass through Notice::saveNew
  135. // which otherwise wouldn't execute the StoreActivityObject event)
  136. return true;
  137. }
  138. // Since the user deleting may not be the same as the notice's owner,
  139. // double-check this and also set the "re-stored" notice profile_id.
  140. if (!$actor->sameAs($owner) && !$actor->hasRight(Right::DELETEOTHERSNOTICE)) {
  141. throw new AuthorizationException(_('You are not allowed to delete another user\'s notice.'));
  142. }
  143. // We copy the identifying fields and replace the sensitive ones.
  144. //$stored->id = $target->id; // We can't copy this since DB_DataObject won't inject it anyway
  145. $props = array('uri', 'profile_id', 'conversation', 'reply_to', 'created', 'repeat_of', 'object_type', 'is_local', 'scope');
  146. foreach($props as $prop) {
  147. $stored->$prop = $target->$prop;
  148. }
  149. // Let's see if this has been deleted already.
  150. try {
  151. $deleted = Deleted_notice::getByKeys( ['uri' => $stored->getUri()] );
  152. return $deleted;
  153. } catch (NoResultException $e) {
  154. $deleted = new Deleted_notice();
  155. $deleted->id = $target->getID();
  156. $deleted->profile_id = $actor->getID();
  157. $deleted->uri = $stored->getUri();
  158. $deleted->act_created = $stored->created;
  159. $deleted->created = common_sql_now();
  160. // throws exception on error
  161. $result = $deleted->insert();
  162. }
  163. // Now we delete the original notice, leaving the id and uri free.
  164. $target->delete();
  165. return true;
  166. }
  167. public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
  168. {
  169. Deleted_notice::extendActivity($stored, $act, $scoped);
  170. }
  171. public function activityObjectFromNotice(Notice $notice)
  172. {
  173. $object = Deleted_notice::fromStored($notice);
  174. return $object->asActivityObject();
  175. }
  176. protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  177. {
  178. if (!$scoped instanceof Profile || !($scoped->sameAs($target->getProfile()) || $scoped->hasRight(Right::DELETEOTHERSNOTICE))) {
  179. throw new AuthorizationException(_('You are not allowed to delete other user\'s notices'));
  180. }
  181. return DeletenoticeForm($action, array('notice'=>$target));
  182. }
  183. }