ActivityModerationModule.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * @package Activity
  4. * @maintainer Mikael Nordfeldth <mmn@hethane.se>
  5. */
  6. class ActivityModerationModule extends ActivityVerbHandlerModule
  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->deleteAs($scoped);
  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 object with the 'delete' verb we will trigger
  79. // $this->saveObjectFromActivity that will do the actual ->delete()
  80. if (false === Deleted_notice::addNew($stored, $actor)) {
  81. // false is returned if we did not have an error, but did not create the object
  82. // (i.e. the author is currently being deleted)
  83. return true;
  84. }
  85. // We return false (to stop the event) if the deleted_notice entry was
  86. // added, which means we have already run $this->saveObjectFromActivity
  87. // which in turn has called the delete function of the notice.
  88. return false;
  89. }
  90. /**
  91. * This is run when a 'delete' verb activity comes in.
  92. *
  93. * @return boolean hook flag
  94. */
  95. protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
  96. {
  97. // Everything is done in the StartNoticeSave event
  98. return true;
  99. }
  100. // FIXME: Put this in lib/modules/ActivityHandlerPlugin.php when we're ready
  101. // with the other microapps/activityhandlers as well.
  102. // Also it should be StartNoticeAsActivity (with a prepped Activity, including ->context etc.)
  103. public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
  104. {
  105. if (!$this->isMyNotice($stored)) {
  106. return true;
  107. }
  108. common_debug('Extending activity '.$stored->id.' with '.get_called_class());
  109. $this->extendActivity($stored, $act, $scoped);
  110. return false;
  111. }
  112. /**
  113. * This is run before ->insert, so our task in this function is just to
  114. * delete if it is the delete verb.
  115. */
  116. public function onStartNoticeSave(Notice $stored)
  117. {
  118. // DELETE is a bit special, we have to remove the existing entry and then
  119. // add a new one with the same URI in order to trigger the distribution.
  120. // (that's why we don't use $this->isMyNotice(...))
  121. if (!ActivityUtils::compareVerbs($stored->verb, array(ActivityVerb::DELETE))) {
  122. return true;
  123. }
  124. try {
  125. $target = Notice::getByUri($stored->uri);
  126. } catch (NoResultException $e) {
  127. throw new AlreadyFulfilledException('Notice URI not found, so we have nothing to delete.');
  128. }
  129. $actor = $stored->getProfile();
  130. $owner = $target->getProfile();
  131. if ($owner->hasRole(Profile_role::DELETED)) {
  132. // Don't bother with replacing notices if its author is being deleted.
  133. // The later "StoreActivityObject" will pick this up and execute
  134. // the deletion then.
  135. // (the "delete verb notice" is too new to ever pass through Notice::saveNew
  136. // which otherwise wouldn't execute the StoreActivityObject event)
  137. return true;
  138. }
  139. // Since the user deleting may not be the same as the notice's owner,
  140. // double-check this and also set the "re-stored" notice profile_id.
  141. if (!$actor->sameAs($owner) && !$actor->hasRight(Right::DELETEOTHERSNOTICE)) {
  142. throw new AuthorizationException(_('You are not allowed to delete another user\'s notice.'));
  143. }
  144. // We copy the identifying fields and replace the sensitive ones.
  145. //$stored->id = $target->id; // We can't copy this since DB_DataObject won't inject it anyway
  146. $props = array('uri', 'profile_id', 'conversation', 'reply_to', 'created', 'repeat_of', 'object_type', 'is_local', 'scope');
  147. foreach($props as $prop) {
  148. $stored->$prop = $target->$prop;
  149. }
  150. // Let's see if this has been deleted already.
  151. try {
  152. $deleted = Deleted_notice::getByKeys( ['uri' => $stored->getUri()] );
  153. return $deleted;
  154. } catch (NoResultException $e) {
  155. $deleted = new Deleted_notice();
  156. $deleted->id = $target->getID();
  157. $deleted->profile_id = $actor->getID();
  158. $deleted->uri = $stored->getUri();
  159. $deleted->act_created = $stored->created;
  160. $deleted->created = common_sql_now();
  161. // throws exception on error
  162. $result = $deleted->insert();
  163. }
  164. // Now we delete the original notice, leaving the id and uri free.
  165. $target->delete();
  166. return true;
  167. }
  168. public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
  169. {
  170. Deleted_notice::extendActivity($stored, $act, $scoped);
  171. }
  172. public function activityObjectFromNotice(Notice $notice)
  173. {
  174. $object = Deleted_notice::fromStored($notice);
  175. return $object->asActivityObject();
  176. }
  177. protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  178. {
  179. if (!$scoped instanceof Profile || !($scoped->sameAs($target->getProfile()) || $scoped->hasRight(Right::DELETEOTHERSNOTICE))) {
  180. throw new AuthorizationException(_('You are not allowed to delete other user\'s notices'));
  181. }
  182. return DeletenoticeForm($action, array('notice'=>$target));
  183. }
  184. }