2017-01-31 23:10:26 +01:00
|
|
|
package Lemonldap::NG::Common::Notifications::JSON;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
|
|
|
use JSON qw(from_json to_json);
|
|
|
|
|
2019-02-12 18:21:38 +01:00
|
|
|
our $VERSION = '2.1.0';
|
2017-02-28 21:53:19 +01:00
|
|
|
|
2017-01-31 23:10:26 +01:00
|
|
|
sub newNotification {
|
2019-11-18 17:34:56 +01:00
|
|
|
my ( $self, $jsonString, $defaultCond ) = @_;
|
2017-02-01 07:16:33 +01:00
|
|
|
my $json;
|
2019-11-18 17:34:56 +01:00
|
|
|
$defaultCond ||= '';
|
2017-09-28 14:52:14 +02:00
|
|
|
eval { $json = from_json( $jsonString, { allow_nonref => 1 } ) };
|
2017-02-01 07:16:33 +01:00
|
|
|
if ( my $err = $@ ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
eval { $self->logger->error("Unable to decode JSON file: $err") };
|
2017-01-31 23:10:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-04-03 22:00:20 +02:00
|
|
|
|
2017-01-31 23:10:26 +01:00
|
|
|
my @notifs;
|
|
|
|
$json = [$json] unless ( ref($json) eq 'ARRAY' );
|
2020-04-03 22:00:20 +02:00
|
|
|
|
2017-01-31 23:10:26 +01:00
|
|
|
foreach my $notif (@$json) {
|
2018-07-05 22:56:16 +02:00
|
|
|
my @data;
|
2020-05-23 23:52:09 +02:00
|
|
|
$notif->{reference} =~ s/_/-/g; # Remove underscores (#2135)
|
2017-01-31 23:10:26 +01:00
|
|
|
|
|
|
|
# Mandatory information
|
|
|
|
foreach (qw(date uid reference)) {
|
|
|
|
my $tmp;
|
|
|
|
unless ( $tmp = $notif->{$_} ) {
|
2019-07-12 18:39:31 +02:00
|
|
|
my $err = "Attribute $_ is missing";
|
|
|
|
$self->logger->error("$err");
|
|
|
|
return ( 0, "$err" );
|
|
|
|
}
|
|
|
|
if ( $self->get( $notif->{uid}, $notif->{reference} ) ) {
|
|
|
|
my $err = "A notification already exists with reference "
|
|
|
|
. $notif->{reference};
|
|
|
|
$self->logger->error("$err");
|
|
|
|
return ( 0, "$err" );
|
2017-01-31 23:10:26 +01:00
|
|
|
}
|
2019-12-18 21:52:38 +01:00
|
|
|
|
|
|
|
# Prevent to store time. Keep date only
|
|
|
|
$tmp =~ s/^(\d{4}-\d{2}-\d{2}).*$/$1/;
|
2018-07-05 22:56:16 +02:00
|
|
|
push @data, $tmp;
|
2017-01-31 23:10:26 +01:00
|
|
|
}
|
2019-11-25 17:21:40 +01:00
|
|
|
|
|
|
|
unless ( exists $notif->{condition} ) {
|
|
|
|
$self->userLogger->info(
|
2020-02-20 23:34:02 +01:00
|
|
|
"Set defaultCondition ($defaultCond) for notification $notif->{reference}"
|
|
|
|
);
|
2019-11-25 17:21:40 +01:00
|
|
|
$notif->{condition} = $defaultCond;
|
|
|
|
}
|
2020-02-20 23:34:02 +01:00
|
|
|
|
2019-11-18 17:34:56 +01:00
|
|
|
push @data, ( $notif->{condition} );
|
2019-12-18 21:52:38 +01:00
|
|
|
$notif->{date} =~ s/^(\d{4}-\d{2}-\d{2}).*$/$1/;
|
2019-11-15 21:03:18 +01:00
|
|
|
my $body = to_json($notif);
|
|
|
|
push @notifs, [ @data, $body ];
|
2017-01-31 23:10:26 +01:00
|
|
|
}
|
|
|
|
my $count;
|
|
|
|
foreach (@notifs) {
|
|
|
|
$count++;
|
|
|
|
my ( $r, $err ) = $self->newNotif(@$_);
|
|
|
|
die "$err" unless ($r);
|
|
|
|
}
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub deleteNotification {
|
|
|
|
my ( $self, $uid, $myref ) = @_;
|
|
|
|
my @data;
|
|
|
|
|
|
|
|
# Check input parameters
|
|
|
|
unless ( $uid and $myref ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error(
|
2018-09-02 17:31:58 +02:00
|
|
|
'REST service "delete notification" called without all parameters');
|
2017-01-31 23:10:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"REST service deleteNotification called for uid $uid and reference $myref"
|
2017-01-31 23:10:26 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
# Get notifications
|
|
|
|
my $user = $self->get($uid);
|
|
|
|
|
|
|
|
# Return 0 if no files were found
|
|
|
|
return 0 unless ($user);
|
|
|
|
|
|
|
|
# Counting
|
|
|
|
my $count = 0;
|
|
|
|
|
|
|
|
foreach my $ref ( keys %$user ) {
|
2017-09-28 14:52:14 +02:00
|
|
|
my $json = from_json( $user->{$ref}, { allow_nonref => 1 } );
|
2019-07-20 22:25:03 +02:00
|
|
|
$json = [$json] unless ( ref($json) eq 'ARRAY' );
|
2019-09-16 18:08:03 +02:00
|
|
|
|
2017-01-31 23:10:26 +01:00
|
|
|
# Browse notification in file
|
|
|
|
foreach my $notif (@$json) {
|
|
|
|
|
|
|
|
# Get notification's data
|
|
|
|
if ( $notif->{reference} eq $myref ) {
|
|
|
|
push @data, $ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Delete the notification (really)
|
|
|
|
foreach (@data) {
|
|
|
|
if ( $self->purge( $_, 1 ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Notification $_ was removed.");
|
2017-01-31 23:10:26 +01:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|