You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

activity.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Files;
  23. use OC\L10N\Factory;
  24. use OCP\Activity\IExtension;
  25. use OCP\IL10N;
  26. use OCP\IURLGenerator;
  27. class Activity implements IExtension {
  28. const FILTER_FILES = 'files';
  29. const TYPE_SHARE_CREATED = 'file_created';
  30. const TYPE_SHARE_CHANGED = 'file_changed';
  31. const TYPE_SHARE_DELETED = 'file_deleted';
  32. const TYPE_SHARE_RESTORED = 'file_restored';
  33. /** @var IL10N */
  34. protected $l;
  35. /** @var Factory */
  36. protected $languageFactory;
  37. /** @var IURLGenerator */
  38. protected $URLGenerator;
  39. /**
  40. * @param Factory $languageFactory
  41. * @param IURLGenerator $URLGenerator
  42. */
  43. public function __construct(Factory $languageFactory, IURLGenerator $URLGenerator) {
  44. $this->languageFactory = $languageFactory;
  45. $this->URLGenerator = $URLGenerator;
  46. $this->l = $this->getL10N();
  47. }
  48. /**
  49. * @param string|null $languageCode
  50. * @return IL10N
  51. */
  52. protected function getL10N($languageCode = null) {
  53. return $this->languageFactory->get('files', $languageCode);
  54. }
  55. /**
  56. * The extension can return an array of additional notification types.
  57. * If no additional types are to be added false is to be returned
  58. *
  59. * @param string $languageCode
  60. * @return array|false
  61. */
  62. public function getNotificationTypes($languageCode) {
  63. $l = $this->getL10N($languageCode);
  64. return [
  65. self::TYPE_SHARE_CREATED => (string) $l->t('A new file or folder has been <strong>created</strong>'),
  66. self::TYPE_SHARE_CHANGED => (string) $l->t('A file or folder has been <strong>changed</strong>'),
  67. self::TYPE_SHARE_DELETED => (string) $l->t('A file or folder has been <strong>deleted</strong>'),
  68. self::TYPE_SHARE_RESTORED => (string) $l->t('A file or folder has been <strong>restored</strong>'),
  69. ];
  70. }
  71. /**
  72. * For a given method additional types to be displayed in the settings can be returned.
  73. * In case no additional types are to be added false is to be returned.
  74. *
  75. * @param string $method
  76. * @return array|false
  77. */
  78. public function getDefaultTypes($method) {
  79. if ($method === 'stream') {
  80. $settings = array();
  81. $settings[] = self::TYPE_SHARE_CREATED;
  82. $settings[] = self::TYPE_SHARE_CHANGED;
  83. $settings[] = self::TYPE_SHARE_DELETED;
  84. $settings[] = self::TYPE_SHARE_RESTORED;
  85. return $settings;
  86. }
  87. return false;
  88. }
  89. /**
  90. * The extension can translate a given message to the requested languages.
  91. * If no translation is available false is to be returned.
  92. *
  93. * @param string $app
  94. * @param string $text
  95. * @param array $params
  96. * @param boolean $stripPath
  97. * @param boolean $highlightParams
  98. * @param string $languageCode
  99. * @return string|false
  100. */
  101. public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  102. if ($app !== 'files') {
  103. return false;
  104. }
  105. switch ($text) {
  106. case 'created_self':
  107. return (string) $this->l->t('You created %1$s', $params);
  108. case 'created_by':
  109. return (string) $this->l->t('%2$s created %1$s', $params);
  110. case 'created_public':
  111. return (string) $this->l->t('%1$s was created in a public folder', $params);
  112. case 'changed_self':
  113. return (string) $this->l->t('You changed %1$s', $params);
  114. case 'changed_by':
  115. return (string) $this->l->t('%2$s changed %1$s', $params);
  116. case 'deleted_self':
  117. return (string) $this->l->t('You deleted %1$s', $params);
  118. case 'deleted_by':
  119. return (string) $this->l->t('%2$s deleted %1$s', $params);
  120. case 'restored_self':
  121. return (string) $this->l->t('You restored %1$s', $params);
  122. case 'restored_by':
  123. return (string) $this->l->t('%2$s restored %1$s', $params);
  124. default:
  125. return false;
  126. }
  127. }
  128. /**
  129. * The extension can define the type of parameters for translation
  130. *
  131. * Currently known types are:
  132. * * file => will strip away the path of the file and add a tooltip with it
  133. * * username => will add the avatar of the user
  134. *
  135. * @param string $app
  136. * @param string $text
  137. * @return array|false
  138. */
  139. function getSpecialParameterList($app, $text) {
  140. if ($app === 'files') {
  141. switch ($text) {
  142. case 'created_self':
  143. case 'created_by':
  144. case 'created_public':
  145. case 'changed_self':
  146. case 'changed_by':
  147. case 'deleted_self':
  148. case 'deleted_by':
  149. case 'restored_self':
  150. case 'restored_by':
  151. return [
  152. 0 => 'file',
  153. 1 => 'username',
  154. ];
  155. }
  156. }
  157. return false;
  158. }
  159. /**
  160. * A string naming the css class for the icon to be used can be returned.
  161. * If no icon is known for the given type false is to be returned.
  162. *
  163. * @param string $type
  164. * @return string|false
  165. */
  166. public function getTypeIcon($type) {
  167. switch ($type) {
  168. case self::TYPE_SHARE_CHANGED:
  169. return 'icon-change';
  170. case self::TYPE_SHARE_CREATED:
  171. return 'icon-add-color';
  172. case self::TYPE_SHARE_DELETED:
  173. return 'icon-delete-color';
  174. default:
  175. return false;
  176. }
  177. }
  178. /**
  179. * The extension can define the parameter grouping by returning the index as integer.
  180. * In case no grouping is required false is to be returned.
  181. *
  182. * @param array $activity
  183. * @return integer|false
  184. */
  185. public function getGroupParameter($activity) {
  186. if ($activity['app'] === 'files') {
  187. switch ($activity['subject']) {
  188. case 'created_self':
  189. case 'created_by':
  190. case 'changed_self':
  191. case 'changed_by':
  192. case 'deleted_self':
  193. case 'deleted_by':
  194. case 'restored_self':
  195. case 'restored_by':
  196. return 0;
  197. }
  198. }
  199. return false;
  200. }
  201. /**
  202. * The extension can define additional navigation entries. The array returned has to contain two keys 'top'
  203. * and 'apps' which hold arrays with the relevant entries.
  204. * If no further entries are to be added false is no be returned.
  205. *
  206. * @return array|false
  207. */
  208. public function getNavigation() {
  209. return [
  210. 'apps' => [
  211. self::FILTER_FILES => [
  212. 'id' => self::FILTER_FILES,
  213. 'name' => (string) $this->l->t('Files'),
  214. 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_FILES]),
  215. ],
  216. ],
  217. 'top' => [],
  218. ];
  219. }
  220. /**
  221. * The extension can check if a customer filter (given by a query string like filter=abc) is valid or not.
  222. *
  223. * @param string $filterValue
  224. * @return boolean
  225. */
  226. public function isFilterValid($filterValue) {
  227. return $filterValue === self::FILTER_FILES;
  228. }
  229. /**
  230. * The extension can filter the types based on the filter if required.
  231. * In case no filter is to be applied false is to be returned unchanged.
  232. *
  233. * @param array $types
  234. * @param string $filter
  235. * @return array|false
  236. */
  237. public function filterNotificationTypes($types, $filter) {
  238. if ($filter === self::FILTER_FILES) {
  239. return array_intersect([
  240. self::TYPE_SHARE_CREATED,
  241. self::TYPE_SHARE_CHANGED,
  242. self::TYPE_SHARE_DELETED,
  243. self::TYPE_SHARE_RESTORED,
  244. ], $types);
  245. }
  246. return false;
  247. }
  248. /**
  249. * For a given filter the extension can specify the sql query conditions including parameters for that query.
  250. * In case the extension does not know the filter false is to be returned.
  251. * The query condition and the parameters are to be returned as array with two elements.
  252. * E.g. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%'));
  253. *
  254. * @param string $filter
  255. * @return array|false
  256. */
  257. public function getQueryForFilter($filter) {
  258. if ($filter === self::FILTER_FILES) {
  259. return ['`app` = ?', ['files']];
  260. }
  261. return false;
  262. }
  263. }