Skip to content

Commit

Permalink
add cleaningNotifs commandList
Browse files Browse the repository at this point in the history
  • Loading branch information
Sekiro-kost committed Dec 3, 2024
1 parent 107650a commit 083a101
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
10 changes: 5 additions & 5 deletions core/api/mobile.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ function saveMenuFromAppV2($menu, $mobile)
log::add('mobile', 'debug', '| [NOTICE] Token vide ');
}
}
if (isset($notification['retentionTime'])) {
$retentionTime = intval($notification['retentionTime']);
if ($mobile->getConfiguration('retentionTime', 30) != $retentionTime) {
$mobile->setConfiguration('retentionTime', $retentionTime);
log::add('mobile', 'debug', '| [INFO] New retentionTime > ' . intval($notification['retentionTime']));
if (isset($notification['notifsTime'])) {
$notifsTime = intval($notification['notifsTime']);
if ($mobile->getConfiguration('notifsTime', 30) != $notifsTime) {
$mobile->setConfiguration('notifsTime', $notifsTime);
log::add('mobile', 'debug', '| [INFO] New notifsTime > ' . intval($notification['notifsTime']));
$mobile->cleaningNotifications();
}
}
Expand Down
59 changes: 45 additions & 14 deletions core/class/mobile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,11 +860,11 @@ public function cmdForSpecificChannel($params = array(), $_trigger = 'location')
*/
public function cleaningNotifications()
{
$retentionTime = $this->getConfiguration('retentionTime', 30);
$notifsTime = $this->getConfiguration('notifsTime', 30);
log::add('mobile', 'debug', '┌──────────▶︎ :fg-warning: Nettoyage des Notifications et Images :/fg: ──────────');
log::add('mobile', 'debug', '| Durée de retention actuelle : ' . $retentionTime . ' jours');
log::add('mobile', 'debug', '| Durée de retention actuelle : ' . $notifsTime . ' jours');
// Images
$retentionSeconds = intVal($retentionTime) * 24 * 60 * 60;
$retentionSeconds = intVal($notifsTime) * 24 * 60 * 60;
$currentTime = time();
$pathImages = dirname(__FILE__) . '/../../data/images/';
if (is_dir($pathImages)) {
Expand All @@ -873,8 +873,11 @@ public function cleaningNotifications()
if (strpos($image, $this->getLogicalId()) !== false) {
$fileCreationTime = filemtime($image);
if ($fileCreationTime < ($currentTime - $retentionSeconds)) {
unlink($image);
log::add('mobile', 'debug', '| -> suppression image > ' . $image);
if (!unlink($image)) {
log::add('mobile', 'error', 'Erreur lors de la suppression de: ' . $image);
} else {
log::add('mobile', 'debug', '| -> suppression image > ' . $image);
}
}
}
}
Expand All @@ -886,6 +889,10 @@ public function cleaningNotifications()
$notifications = file_get_contents($filePath);
if ($notifications) {
$notifications = json_decode($notifications, true);
if (json_last_error() !== JSON_ERROR_NONE) {
log::add('mobile', 'error', 'Erreur decodage du JSON : ' . json_last_error_msg());
return;
}
$notificationsModified = false;

foreach ($notifications as $id => $value) {
Expand Down Expand Up @@ -924,7 +931,6 @@ public function postInsert()
public function postSave()
{
if ($this->getConfiguration('appVersion', 1) == 2) {

// Commande notification
$cmd = $this->getCmd(null, 'notif');
if (!is_object($cmd)) {
Expand Down Expand Up @@ -986,8 +992,8 @@ public function postSave()
$cmd = $this->getCmd(null, 'removeNotifs');
if (!is_object($cmd)) {
$cmd = new mobileCmd();
$cmd->setIsVisible(0);
$cmd->setName(__('Supprimer les notifications', __FILE__));
$cmd->setIsVisible(1);
$cmd->setName(__('Supprimer les Notifications', __FILE__));
$cmd->setLogicalId('removeNotifs');
$cmd->setGeneric_type('GENERIC_ACTION');
$cmd->setDisplay('icon', '<i class="icon fas fa-trash icon_red"></i>');
Expand All @@ -998,10 +1004,14 @@ public function postSave()
}
$cmd->setEqLogic_id($this->getId());
$cmd->setType('action');
$cmd->setSubType('other');
$cmd->setSubType('select');
$listValue = "1|Supprimer tous les Notifications;2|Supprimer les Asks expirés;3|Supprimer les Asks répondus";
$cmd->setConfiguration('listValue', $listValue);
if ($cmd->getChanged() === true) $cmd->save();
}



$cmdaskText = $this->getCmd(null, 'ask_Text');
if (is_object($cmdaskText)) {
$cmdaskText->remove();
Expand Down Expand Up @@ -1108,12 +1118,33 @@ public function execute($_options = array())

if ($this->getLogicalId() == 'removeNotifs') {
$filePath = dirname(__FILE__) . '/../data/notifications/' . $Iq . '.json';
if (file_exists($filePath)) {
file_put_contents($filePath, '');
log::add('mobile', 'info', '| Suppression des notifications effectuée');
} else {
log::add('mobile', 'info', '| Fichier de notifications non trouvé : ' . $filePath);
if (!file_exists($filePath)) log::add('mobile', 'info', '| Fichier de notifications non trouvé : ' . $filePath);
$valueUser = $_options['select'];
switch($valueUser){
case 1: file_put_contents($filePath, '');
log::add('mobile', 'info', '| Suppression des notifications effectuée');
break;
case 2:
$notifs = json_decode(file_get_contents($filePath), true);
$notifs = array_filter($notifs, function($notif) {
$askParams = json_decode($notif['data']['askParams'], true);
$notifTime = strtotime($notif['data']['date']);
$currentTime = time();
$timeout = $askParams['timeout'] / 1000;
return $notif['data']['askVariable'] == 'rien' || ($currentTime - $notifTime) < $timeout;
});
file_put_contents($filePath, json_encode($notifs));
log::add('mobile', 'info', '| Suppression des asks expirés effectuée');
break;
case 3: $notifs = json_decode(file_get_contents($filePath), true);
$notifs = array_filter($notifs, function($notif) {
return $notif['data']['choiceAsk'] == '';
});
file_put_contents($filePath, json_encode($notifs));
log::add('mobile', 'info', '| Suppression des asks répondus effectuée');
break;
}

log::add('mobile', 'debug', '└────────────────────');
}

Expand Down

0 comments on commit 083a101

Please sign in to comment.