activity.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 === self::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. if (sizeof($params) === 2) {
  134. // New activity ownCloud 8.2+
  135. return (string) $l->t('You received a new remote share %2$s from %1$s', $params);
  136. }
  137. return (string) $l->t('You received a new remote share from %s', $params);
  138. case self::SUBJECT_REMOTE_SHARE_ACCEPTED:
  139. return (string) $l->t('%1$s accepted remote share %2$s', $params);
  140. case self::SUBJECT_REMOTE_SHARE_DECLINED:
  141. return (string) $l->t('%1$s declined remote share %2$s', $params);
  142. case self::SUBJECT_REMOTE_SHARE_UNSHARED:
  143. return (string) $l->t('%1$s unshared %2$s from you', $params);
  144. case self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED:
  145. return (string) $l->t('Public shared folder %1$s was downloaded', $params);
  146. case self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED:
  147. return (string) $l->t('Public shared file %1$s was downloaded', $params);
  148. case self::SUBJECT_SHARED_USER_SELF:
  149. return (string) $l->t('You shared %1$s with %2$s', $params);
  150. case self::SUBJECT_SHARED_GROUP_SELF:
  151. return (string) $l->t('You shared %1$s with group %2$s', $params);
  152. case self::SUBJECT_SHARED_WITH_BY:
  153. return (string) $l->t('%2$s shared %1$s with you', $params);
  154. case self::SUBJECT_SHARED_LINK_SELF:
  155. return (string) $l->t('You shared %1$s via link', $params);
  156. }
  157. }
  158. return false;
  159. }
  160. /**
  161. * The extension can define the type of parameters for translation
  162. *
  163. * Currently known types are:
  164. * * file => will strip away the path of the file and add a tooltip with it
  165. * * username => will add the avatar of the user
  166. *
  167. * @param string $app
  168. * @param string $text
  169. * @return array|false
  170. */
  171. public function getSpecialParameterList($app, $text) {
  172. if ($app === self::FILES_SHARING_APP) {
  173. switch ($text) {
  174. case self::SUBJECT_REMOTE_SHARE_RECEIVED:
  175. case self::SUBJECT_REMOTE_SHARE_UNSHARED:
  176. return array(
  177. 0 => 'federated_cloud_id',
  178. //1 => 'file', in theory its a file, but it does not exist yet/anymore
  179. );
  180. case self::SUBJECT_REMOTE_SHARE_ACCEPTED:
  181. case self::SUBJECT_REMOTE_SHARE_DECLINED:
  182. return array(
  183. 0 => 'federated_cloud_id',
  184. 1 => 'file',
  185. );
  186. case self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED:
  187. case self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED:
  188. return array(
  189. 0 => 'file',
  190. );
  191. case self::SUBJECT_SHARED_LINK_SELF:
  192. return [0 => 'file'];
  193. case self::SUBJECT_SHARED_USER_SELF:
  194. case self::SUBJECT_SHARED_WITH_BY:
  195. return [0 => 'file', 1 => 'username'];
  196. case self::SUBJECT_SHARED_GROUP_SELF:
  197. return [
  198. 0 => 'file',
  199. 1 => 'group',
  200. ];
  201. }
  202. }
  203. return false;
  204. }
  205. /**
  206. * The extension can define the parameter grouping by returning the index as integer.
  207. * In case no grouping is required false is to be returned.
  208. *
  209. * @param array $activity
  210. * @return integer|false
  211. */
  212. public function getGroupParameter($activity) {
  213. if ($activity['app'] === self::FILES_SHARING_APP) {
  214. switch ($activity['subject']) {
  215. case self::SUBJECT_SHARED_LINK_SELF:
  216. case self::SUBJECT_SHARED_WITH_BY:
  217. // Group by file name
  218. return 0;
  219. case self::SUBJECT_SHARED_USER_SELF:
  220. case self::SUBJECT_SHARED_GROUP_SELF:
  221. // Group by user/group
  222. return 1;
  223. }
  224. }
  225. return false;
  226. }
  227. /**
  228. * The extension can define additional navigation entries. The array returned has to contain two keys 'top'
  229. * and 'apps' which hold arrays with the relevant entries.
  230. * If no further entries are to be added false is no be returned.
  231. *
  232. * @return array|false
  233. */
  234. public function getNavigation() {
  235. $l = $this->getL10N();
  236. return [
  237. 'apps' => [],
  238. 'top' => [
  239. self::FILTER_SHARES => [
  240. 'id' => self::FILTER_SHARES,
  241. 'name' => (string) $l->t('Shares'),
  242. 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_SHARES]),
  243. ],
  244. ],
  245. ];
  246. }
  247. /**
  248. * The extension can check if a custom filter (given by a query string like filter=abc) is valid or not.
  249. *
  250. * @param string $filterValue
  251. * @return boolean
  252. */
  253. public function isFilterValid($filterValue) {
  254. return $filterValue === self::FILTER_SHARES;
  255. }
  256. /**
  257. * The extension can filter the types based on the filter if required.
  258. * In case no filter is to be applied false is to be returned unchanged.
  259. *
  260. * @param array $types
  261. * @param string $filter
  262. * @return array|false
  263. */
  264. public function filterNotificationTypes($types, $filter) {
  265. switch ($filter) {
  266. case self::FILTER_SHARES:
  267. return array_intersect([self::TYPE_SHARED, self::TYPE_REMOTE_SHARE], $types);
  268. }
  269. return false;
  270. }
  271. /**
  272. * For a given filter the extension can specify the sql query conditions including parameters for that query.
  273. * In case the extension does not know the filter false is to be returned.
  274. * The query condition and the parameters are to be returned as array with two elements.
  275. * E.g. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%'));
  276. *
  277. * @param string $filter
  278. * @return array|false
  279. */
  280. public function getQueryForFilter($filter) {
  281. if ($filter === self::FILTER_SHARES) {
  282. return [
  283. '`app` = ?',
  284. [self::FILES_SHARING_APP,],
  285. ];
  286. }
  287. return false;
  288. }
  289. }