EventMerger.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Activity;
  7. use OCP\Activity\IEvent;
  8. use OCP\Activity\IEventMerger;
  9. use OCP\IL10N;
  10. class EventMerger implements IEventMerger {
  11. /** @var IL10N */
  12. protected $l10n;
  13. /**
  14. * @param IL10N $l10n
  15. */
  16. public function __construct(IL10N $l10n) {
  17. $this->l10n = $l10n;
  18. }
  19. /**
  20. * Combines two events when possible to have grouping:
  21. *
  22. * Example1: Two events with subject '{user} created {file}' and
  23. * $mergeParameter file with different file and same user will be merged
  24. * to '{user} created {file1} and {file2}' and the childEvent on the return
  25. * will be set, if the events have been merged.
  26. *
  27. * Example2: Two events with subject '{user} created {file}' and
  28. * $mergeParameter file with same file and same user will be merged to
  29. * '{user} created {file1}' and the childEvent on the return will be set, if
  30. * the events have been merged.
  31. *
  32. * The following requirements have to be met, in order to be merged:
  33. * - Both events need to have the same `getApp()`
  34. * - Both events must not have a message `getMessage()`
  35. * - Both events need to have the same subject `getSubject()`
  36. * - Both events need to have the same object type `getObjectType()`
  37. * - The time difference between both events must not be bigger then 3 hours
  38. * - Only up to 5 events can be merged.
  39. * - All parameters apart from such starting with $mergeParameter must be
  40. * the same for both events.
  41. *
  42. * @param string $mergeParameter
  43. * @param IEvent $event
  44. * @param IEvent|null $previousEvent
  45. * @return IEvent
  46. */
  47. public function mergeEvents($mergeParameter, IEvent $event, ?IEvent $previousEvent = null) {
  48. // No second event => can not combine
  49. if (!$previousEvent instanceof IEvent) {
  50. return $event;
  51. }
  52. // Different app => can not combine
  53. if ($event->getApp() !== $previousEvent->getApp()) {
  54. return $event;
  55. }
  56. // Message is set => can not combine
  57. if ($event->getMessage() !== '' || $previousEvent->getMessage() !== '') {
  58. return $event;
  59. }
  60. // Different subject => can not combine
  61. if ($event->getSubject() !== $previousEvent->getSubject()) {
  62. return $event;
  63. }
  64. // Different object type => can not combine
  65. if ($event->getObjectType() !== $previousEvent->getObjectType()) {
  66. return $event;
  67. }
  68. // More than 3 hours difference => can not combine
  69. if (abs($event->getTimestamp() - $previousEvent->getTimestamp()) > 3 * 60 * 60) {
  70. return $event;
  71. }
  72. // Other parameters are not the same => can not combine
  73. try {
  74. [$combined, $parameters] = $this->combineParameters($mergeParameter, $event, $previousEvent);
  75. } catch (\UnexpectedValueException $e) {
  76. return $event;
  77. }
  78. try {
  79. $newSubject = $this->getExtendedSubject($event->getRichSubject(), $mergeParameter, $combined);
  80. $parsedSubject = $this->generateParsedSubject($newSubject, $parameters);
  81. $event->setRichSubject($newSubject, $parameters)
  82. ->setParsedSubject($parsedSubject)
  83. ->setChildEvent($previousEvent)
  84. ->setTimestamp(max($event->getTimestamp(), $previousEvent->getTimestamp()));
  85. } catch (\UnexpectedValueException $e) {
  86. return $event;
  87. }
  88. return $event;
  89. }
  90. /**
  91. * @param string $mergeParameter
  92. * @param IEvent $event
  93. * @param IEvent $previousEvent
  94. * @return array
  95. * @throws \UnexpectedValueException
  96. */
  97. protected function combineParameters($mergeParameter, IEvent $event, IEvent $previousEvent) {
  98. $params1 = $event->getRichSubjectParameters();
  99. $params2 = $previousEvent->getRichSubjectParameters();
  100. $params = [];
  101. $combined = 0;
  102. // Check that all parameters from $event exist in $previousEvent
  103. foreach ($params1 as $key => $parameter) {
  104. if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) {
  105. if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) {
  106. $combined++;
  107. $params[$mergeParameter . $combined] = $parameter;
  108. }
  109. continue;
  110. }
  111. if (!isset($params2[$key]) || $params2[$key] !== $parameter) {
  112. // Parameter missing on $previousEvent or different => can not combine
  113. throw new \UnexpectedValueException();
  114. }
  115. $params[$key] = $parameter;
  116. }
  117. // Check that all parameters from $previousEvent exist in $event
  118. foreach ($params2 as $key => $parameter) {
  119. if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) {
  120. if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) {
  121. $combined++;
  122. $params[$mergeParameter . $combined] = $parameter;
  123. }
  124. continue;
  125. }
  126. if (!isset($params1[$key]) || $params1[$key] !== $parameter) {
  127. // Parameter missing on $event or different => can not combine
  128. throw new \UnexpectedValueException();
  129. }
  130. $params[$key] = $parameter;
  131. }
  132. return [$combined, $params];
  133. }
  134. /**
  135. * @param array[] $parameters
  136. * @param string $mergeParameter
  137. * @param array $parameter
  138. * @return bool
  139. */
  140. protected function checkParameterAlreadyExits($parameters, $mergeParameter, $parameter) {
  141. foreach ($parameters as $key => $param) {
  142. if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) {
  143. if ($param === $parameter) {
  144. return true;
  145. }
  146. }
  147. }
  148. return false;
  149. }
  150. /**
  151. * @param string $subject
  152. * @param string $parameter
  153. * @param int $counter
  154. * @return mixed
  155. */
  156. protected function getExtendedSubject($subject, $parameter, $counter) {
  157. switch ($counter) {
  158. case 1:
  159. $replacement = '{' . $parameter . '1}';
  160. break;
  161. case 2:
  162. $replacement = $this->l10n->t(
  163. '%1$s and %2$s',
  164. ['{' . $parameter . '2}', '{' . $parameter . '1}']
  165. );
  166. break;
  167. case 3:
  168. $replacement = $this->l10n->t(
  169. '%1$s, %2$s and %3$s',
  170. ['{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}']
  171. );
  172. break;
  173. case 4:
  174. $replacement = $this->l10n->t(
  175. '%1$s, %2$s, %3$s and %4$s',
  176. ['{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}']
  177. );
  178. break;
  179. case 5:
  180. $replacement = $this->l10n->t(
  181. '%1$s, %2$s, %3$s, %4$s and %5$s',
  182. ['{' . $parameter . '5}', '{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}']
  183. );
  184. break;
  185. default:
  186. throw new \UnexpectedValueException();
  187. }
  188. return str_replace(
  189. '{' . $parameter . '}',
  190. $replacement,
  191. $subject
  192. );
  193. }
  194. /**
  195. * @param string $subject
  196. * @param array[] $parameters
  197. * @return string
  198. */
  199. protected function generateParsedSubject($subject, $parameters) {
  200. $placeholders = $replacements = [];
  201. foreach ($parameters as $placeholder => $parameter) {
  202. $placeholders[] = '{' . $placeholder . '}';
  203. if ($parameter['type'] === 'file') {
  204. $replacements[] = trim($parameter['path'], '/');
  205. } elseif (isset($parameter['name'])) {
  206. $replacements[] = $parameter['name'];
  207. } else {
  208. $replacements[] = $parameter['id'];
  209. }
  210. }
  211. return str_replace($placeholders, $replacements, $subject);
  212. }
  213. }