lemonldap-ng/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/JSON.pm

115 lines
3.1 KiB
Perl
Raw Normal View History

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;
}
2017-01-31 23:10:26 +01:00
my @notifs;
$json = [$json] unless ( ref($json) eq 'ARRAY' );
2017-01-31 23:10:26 +01:00
foreach my $notif (@$json) {
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->{$_} ) {
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
}
# Prevent to store time. Keep date only
$tmp =~ s/^(\d{4}-\d{2}-\d{2}).*$/$1/;
push @data, $tmp;
2017-01-31 23:10:26 +01:00
}
unless ( exists $notif->{condition} ) {
$self->userLogger->info(
2020-02-20 23:34:02 +01:00
"Set defaultCondition ($defaultCond) for notification $notif->{reference}"
);
$notif->{condition} = $defaultCond;
}
2020-02-20 23:34:02 +01:00
2019-11-18 17:34:56 +01:00
push @data, ( $notif->{condition} );
$notif->{date} =~ s/^(\d{4}-\d{2}-\d{2}).*$/$1/;
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 } );
$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;