Blame view
sources/apps/activity/lib/grouphelper.php
4.92 KB
|
6d9380f96
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
<?php
/**
* ownCloud - Activity App
*
* @author Joas Schilling
* @copyright 2014 Joas Schilling nickvergessen@owncloud.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Activity;
use \OCP\Activity\IManager;
class GroupHelper
{
/** @var array */
protected $activities = array();
/** @var array */
protected $openGroup = array();
/** @var string */
protected $groupKey = '';
/** @var int */
protected $groupTime = 0;
/** @var bool */
protected $allowGrouping;
/** @var \OCP\Activity\IManager */
protected $activityManager;
/** @var \OCA\Activity\DataHelper */
protected $dataHelper;
/**
* @param \OCP\Activity\IManager $activityManager
* @param \OCA\Activity\DataHelper $dataHelper
* @param bool $allowGrouping
*/
public function __construct(IManager $activityManager, DataHelper $dataHelper, $allowGrouping) {
$this->allowGrouping = $allowGrouping;
$this->activityManager = $activityManager;
$this->dataHelper = $dataHelper;
}
/**
* Add an activity to the internal array
*
* @param array $activity
*/
public function addActivity($activity) {
$activity['subjectparams_array'] = unserialize($activity['subjectparams']);
if (!is_array($activity['subjectparams_array'])) {
$activity['subjectparams_array'] = array($activity['subjectparams_array']);
}
$activity['messageparams_array'] = unserialize($activity['messageparams']);
if (!is_array($activity['messageparams_array'])) {
$activity['messageparams_array'] = array($activity['messageparams_array']);
}
if (!$this->getGroupKey($activity)) {
if (!empty($this->openGroup)) {
$this->activities[] = $this->openGroup;
$this->openGroup = array();
$this->groupKey = '';
$this->groupTime = 0;
}
$this->activities[] = $activity;
return;
}
// Only group when the event has the same group key
// and the time difference is not bigger than 3 days.
if ($this->getGroupKey($activity) === $this->groupKey &&
abs($activity['timestamp'] - $this->groupTime) < (3 * 24 * 60 * 60)
) {
$parameter = $this->getGroupParameter($activity);
if ($parameter !== false) {
if (!is_array($this->openGroup['subjectparams_array'][$parameter])) {
$this->openGroup['subjectparams_array'][$parameter] = array($this->openGroup['subjectparams_array'][$parameter]);
}
if (!isset($this->openGroup['activity_ids'])) {
$this->openGroup['activity_ids'] = array((int) $this->openGroup['activity_id']);
}
$this->openGroup['subjectparams_array'][$parameter][] = $activity['subjectparams_array'][$parameter];
$this->openGroup['subjectparams_array'][$parameter] = array_unique($this->openGroup['subjectparams_array'][$parameter]);
$this->openGroup['activity_ids'][] = (int) $activity['activity_id'];
}
} else {
if (!empty($this->openGroup)) {
$this->activities[] = $this->openGroup;
}
$this->groupKey = $this->getGroupKey($activity);
$this->groupTime = $activity['timestamp'];
$this->openGroup = $activity;
}
}
/**
* Get grouping key for an activity
*
* @param array $activity
* @return false|string False, if grouping is not allowed, grouping key otherwise
*/
protected function getGroupKey($activity) {
if ($this->getGroupParameter($activity) === false) {
return false;
}
return $activity['app'] . '|' . $activity['user'] . '|' . $activity['subject'];
}
protected function getGroupParameter($activity) {
if (!$this->allowGrouping) {
return false;
}
if ($activity['app'] === 'files') {
switch ($activity['subject']) {
case 'created_self':
case 'created_by':
case 'changed_self':
case 'changed_by':
case 'deleted_self':
case 'deleted_by':
return 0;
}
}
// Allow other apps to group their notifications
return $this->activityManager->getGroupParameter($activity);
}
/**
* Get the prepared activities
*
* @return array translated activities ready for use
*/
public function getActivities() {
if (!empty($this->openGroup)) {
$this->activities[] = $this->openGroup;
}
$return = array();
foreach ($this->activities as $activity) {
$activity = $this->dataHelper->formatStrings($activity, 'subject');
$activity = $this->dataHelper->formatStrings($activity, 'message');
$activity['typeicon'] = $this->dataHelper->getTypeIcon($activity['type']);
$return[] = $activity;
}
return $return;
}
}
|