activity.php 9.5 KB

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