| 1 | package Lemonldap::NG::Common::Notifications::JSON; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use JSON qw(from_json to_json); | ||
| 6 | |||
| 7 | our $VERSION = '2.19.0'; | ||
| 8 | |||
| 9 | sub newNotification { | ||
| 10 | my ( $self, $jsonString, $defaultCond ) = @_; | ||
| 11 | my $json; | ||
| 12 | $defaultCond ||= ''; | ||
| 13 | eval { $json = from_json( $jsonString, { allow_nonref => 1 } ) }; | ||
| 14 | if ( my $err = $@ ) { | ||
| 15 | eval { $self->logger->error("Unable to decode JSON file: $err") }; | ||
| 16 | return 0; | ||
| 17 | } | ||
| 18 | |||
| 19 | my @notifs; | ||
| 20 | $json = [$json] unless ( ref($json) eq 'ARRAY' ); | ||
| 21 | |||
| 22 | foreach my $notif (@$json) { | ||
| 23 | my @data; | ||
| 24 | $notif->{reference} =~ s/_/-/g; # Remove underscores (#2135) | ||
| 25 | |||
| 26 | # Mandatory information | ||
| 27 | foreach (qw(date uid reference)) { | ||
| 28 | my $tmp; | ||
| 29 | unless ( $tmp = $notif->{$_} ) { | ||
| 30 | my $err = "Attribute $_ is missing"; | ||
| 31 | $self->logger->error("$err"); | ||
| 32 | return ( 0, "$err" ); | ||
| 33 | } | ||
| 34 | if ( $self->get( $notif->{uid}, $notif->{reference} ) ) { | ||
| 35 | my $err = "A notification already exists with reference " | ||
| 36 | . $notif->{reference}; | ||
| 37 | $self->logger->error("$err"); | ||
| 38 | return ( 0, "$err" ); | ||
| 39 | } | ||
| 40 | |||
| 41 | # Prevent to store time. Keep date only | ||
| 42 | $tmp =~ s/^(\d{4}-\d{2}-\d{2}).*$/$1/; | ||
| 43 | push @data, $tmp; | ||
| 44 | } | ||
| 45 | |||
| 46 | unless ( exists $notif->{condition} ) { | ||
| 47 | $self->logger->info( | ||
| 48 | "Set defaultCondition ($defaultCond) for notification $notif->{reference}" | ||
| 49 | ); | ||
| 50 | $notif->{condition} = $defaultCond; | ||
| 51 | } | ||
| 52 | |||
| 53 | push @data, ( $notif->{condition} ); | ||
| 54 | $notif->{date} =~ s/^(\d{4}-\d{2}-\d{2}).*$/$1/; | ||
| 55 | my $body = to_json($notif); | ||
| 56 | push @notifs, [ @data, $body ]; | ||
| 57 | } | ||
| 58 | my $count; | ||
| 59 | foreach (@notifs) { | ||
| 60 | $count++; | ||
| 61 | my ( $r, $err ) = $self->newNotif(@$_); | ||
| 62 | die "$err" unless ($r); | ||
| 63 | } | ||
| 64 | return $count; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub deleteNotification { | ||
| 68 | my ( $self, $uid, $myref ) = @_; | ||
| 69 | my @data; | ||
| 70 | |||
| 71 | # Check input parameters | ||
| 72 | unless ( $uid and $myref ) { | ||
| 73 | $self->userLogger->error( | ||
| 74 | 'REST service "delete notification" called without all parameters'); | ||
| 75 | return 0; | ||
| 76 | } | ||
| 77 | $self->logger->debug( | ||
| 78 | "REST service deleteNotification called for uid $uid and reference $myref" | ||
| 79 | ); | ||
| 80 | |||
| 81 | # Get notifications | ||
| 82 | my $user = $self->get($uid); | ||
| 83 | |||
| 84 | # Return 0 if no files were found | ||
| 85 | return 0 unless ($user); | ||
| 86 | |||
| 87 | # Counting | ||
| 88 | my $count = 0; | ||
| 89 | |||
| 90 | foreach my $ref ( keys %$user ) { | ||
| 91 | my $json = from_json( $user->{$ref}, { allow_nonref => 1 } ); | ||
| 92 | $json = [$json] unless ( ref($json) eq 'ARRAY' ); | ||
| 93 | |||
| 94 | # Browse notification in file | ||
| 95 | foreach my $notif (@$json) { | ||
| 96 | |||
| 97 | # Get notification's data | ||
| 98 | if ( $notif->{reference} eq $myref ) { | ||
| 99 | push @data, $ref; | ||
| 100 | } | ||
| 101 | |||
| 102 | # Delete the notification (really) | ||
| 103 | foreach (@data) { | ||
| 104 | if ( $self->purge( $_, 1 ) ) { | ||
| 105 | $self->logger->debug("Notification $_ was removed."); | ||
| 106 | $count++; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | return $count; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; |