Delete correctly checks status.
This commit is contained in:
parent
1dd9844a22
commit
051d39cdab
109
Message/Queue.pm
109
Message/Queue.pm
@ -168,9 +168,9 @@ sub delete_message {
|
|||||||
$self -> clear_error();
|
$self -> clear_error();
|
||||||
|
|
||||||
if($args -> {"id"}) {
|
if($args -> {"id"}) {
|
||||||
return $self -> _delete_by_field("id", $args -> {"id"}, $args -> {"userid"}, $now);
|
return $self -> _delete_by_id($args -> {"id"}, $args -> {"userid"}, $now);
|
||||||
} elsif($args -> {"ident"}) {
|
} elsif($args -> {"ident"}) {
|
||||||
return $self -> _delete_by_field("message_ident", $args -> {"ident"}, $args -> {"userid"}, $now);
|
return $self -> _delete_by_ident($args -> {"ident"}, $args -> {"userid"}, $now);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $self -> self_error("No id or ident passed to delete_message()");
|
return $self -> self_error("No id or ident passed to delete_message()");
|
||||||
@ -481,64 +481,107 @@ sub _add_recipient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
## @method private $ _delete_by_field($field, $value, $userid, $deleted)
|
## @method private $ _delete_by_ident($ident, $userid, $deleted);
|
||||||
# Attempt to delete messages where the specified field contains the value given.
|
# Delete messages with the specified message ident. This may delete zero or more
|
||||||
# Note that this *does not* remove the message from the table, it simply marks
|
# messages, and will not delete messages that have been sent or viewed.
|
||||||
# it as deleted so that get_message() will not normally return it. This will not
|
|
||||||
# delete messages that have been sent or viewed.
|
|
||||||
#
|
#
|
||||||
# @param field The database table field to search for messages on.
|
# @param ident Messages with this message ident will be deleted.
|
||||||
# @param value When a given message has this value in the specified field, it is
|
# @param userid The user performing the delete. May be undef.
|
||||||
# marked as deleted (aleady deleted messages are not changed)
|
# @param deleted The timestamp to place in the deleted field.
|
||||||
# @param userid The user performing the delete. May be undef.
|
# @return The number of messages deleted (which may be zero) on success, undef
|
||||||
# @param deleted The timestamp to place in the deleted field.
|
# on error.
|
||||||
# @return The number of rows deleted.
|
sub _delete_by_ident {
|
||||||
sub _delete_by_field {
|
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $field = shift;
|
my $ident = shift;
|
||||||
my $value = shift;
|
|
||||||
my $userid = shift;
|
my $userid = shift;
|
||||||
my $deleted = shift;
|
my $deleted = shift;
|
||||||
|
|
||||||
$self -> clear_error();
|
$self -> clear_error();
|
||||||
|
|
||||||
# Force valid field
|
$self -> {"logger"} -> log("messaging", $userid || 0, undef, "Attempting delete of messages with ident $ident");
|
||||||
$field = "id" unless($field && ($field eq "id" || $field eq "message_ident"));
|
|
||||||
|
|
||||||
$self -> {"logger"} -> log("messaging", $userid || 0, undef, "Attempting delete of message(s) with $field = '$value'");
|
my $messh = $self -> {"dbh"} -> prepare("SELECT id
|
||||||
|
FROM `".$self -> {"settings"} -> {"database"} -> {"message_queue"}."`
|
||||||
|
WHERE message_ident = ?");
|
||||||
|
$messh -> execute($ident)
|
||||||
|
or return $self -> self_error("Unable to perform message ident lookup: ". $self -> {"dbh"} -> errstr);
|
||||||
|
|
||||||
|
my $deleted = 0;
|
||||||
|
while(my $msgid = $messh -> fetchrow_arrayref()) {
|
||||||
|
my $result = $self -> _delete_by_id($msgid -> [0], $userid, $deleted);
|
||||||
|
return undef if(!defined($result));
|
||||||
|
|
||||||
|
$deleted += $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $deleted;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
## @method private $ _delete_by_id($messageid, $userid, $deleted)
|
||||||
|
# Attempt to delete the message with the specified message id.
|
||||||
|
# Note that this *does not* remove the message from the table, it simply marks
|
||||||
|
# it as deleted so that get_message() will not normally return it. This will not
|
||||||
|
# delete messages that have been sent or viewed.
|
||||||
|
#
|
||||||
|
# @param messageid The ID of the message to delete.
|
||||||
|
# @param userid The user performing the delete. May be undef.
|
||||||
|
# @param deleted The timestamp to place in the deleted field.
|
||||||
|
# @return 1 of the message is deleted, 0 if the message can not be deleted
|
||||||
|
# because it has been sent or viewed, undef on error.
|
||||||
|
sub _delete_by_id {
|
||||||
|
my $self = shift;
|
||||||
|
my $messageid = shift;
|
||||||
|
my $userid = shift;
|
||||||
|
my $deleted = shift;
|
||||||
|
|
||||||
|
$self -> clear_error();
|
||||||
|
|
||||||
|
$self -> {"logger"} -> log("messaging", $userid || 0, undef, "Attempting delete of message $messageid");
|
||||||
|
|
||||||
# Check that the message has no views
|
# Check that the message has no views
|
||||||
my $viewh = $self -> {"dbh"} -> prepare("SELECT COUNT(*)
|
my $viewh = $self -> {"dbh"} -> prepare("SELECT COUNT(*)
|
||||||
FROM `".$self -> {"settings"} -> {"database"} -> {"message_queue"}."` AS q,
|
FROM `".$self -> {"settings"} -> {"database"} -> {"message_recipients"}."`
|
||||||
`".$self -> {"settings"} -> {"database"} -> {"message_recipients"}."` AS r
|
WHERE message_id = ?
|
||||||
WHERE q.$field = ?
|
AND viewed IS NOT NULL");
|
||||||
AND r.message_id = q.id
|
$viewh -> execute($messageid)
|
||||||
AND r.viewed IS NOT NULL");
|
or return $self -> self_error("Unable to perform message $messageid view check: ". $self -> {"dbh"} -> errstr);
|
||||||
$viewh -> execute($value)
|
|
||||||
or return $self -> self_error("Unable to perform message view check: ". $self -> {"dbh"} -> errstr);
|
|
||||||
|
|
||||||
my $views = $viewh -> fetchrow_arrayref()
|
my $views = $viewh -> fetchrow_arrayref()
|
||||||
or return $self -> self_error("Unable to obtain message view count. This should not happen!");
|
or return $self -> self_error("Unable to obtain message $messageid view count. This should not happen!");
|
||||||
|
|
||||||
# If the view count is non-zero, the message or message group can not be deleted.
|
# If the view count is non-zero, the message can not be deleted.
|
||||||
return 0 if($views -> [0]);
|
return 0 if($views -> [0]);
|
||||||
|
|
||||||
$self -> {"logger"} -> log("messaging", $userid || 0, undef, "Delete of message(s) with $field = '$value' passed view check");
|
# Check that the message has not been sent
|
||||||
|
my $senth = $self -> {"dbh"} -> prepare("SELECT COUNT(*)
|
||||||
|
FROM `".$self -> {"settings"} -> {"database"} -> {"message_status"}."`
|
||||||
|
WHERE message_id = ?
|
||||||
|
AND status = 'sent'");
|
||||||
|
|
||||||
|
$senth -> execute($messageid)
|
||||||
|
or return $self -> self_error("Unable to perform message $messageid sent check: ". $self -> {"dbh"} -> errstr);
|
||||||
|
|
||||||
|
my $sent = $senth -> fetchrow_arrayref()
|
||||||
|
or return $self -> self_error("Unable to obtain message $messageid sent count. This should not happen!");
|
||||||
|
|
||||||
|
# If the sent count is non-zero, the message can not be deleted.
|
||||||
|
return 0 if($sent -> [0]);
|
||||||
|
|
||||||
|
$self -> {"logger"} -> log("messaging", $userid || 0, undef, "Delete of message $messageid passed view and sent check");
|
||||||
|
|
||||||
# Otherwise, nobody is marked as having seen the message, if it hasn't been sent, mark it as deleted
|
# Otherwise, nobody is marked as having seen the message, if it hasn't been sent, mark it as deleted
|
||||||
my $nukeh = $self -> {"dbh"} -> prepare("UPDATE `".$self -> {"settings"} -> {"database"} -> {"message_queue"}."`
|
my $nukeh = $self -> {"dbh"} -> prepare("UPDATE `".$self -> {"settings"} -> {"database"} -> {"message_queue"}."`
|
||||||
SET deleted = ?, deleted_id = ?
|
SET deleted = ?, deleted_id = ?
|
||||||
WHERE $field = ?
|
WHERE $field = ?
|
||||||
AND status != 'sent'
|
|
||||||
AND deleted IS NULL");
|
AND deleted IS NULL");
|
||||||
my $result = $nukeh -> execute($deleted, $userid, $value);
|
my $result = $nukeh -> execute($deleted, $userid, $value);
|
||||||
return $self -> self_error("Unable to perform message delete: ". $self -> {"dbh"} -> errstr) if(!$result);
|
return $self -> self_error("Unable to perform message delete: ". $self -> {"dbh"} -> errstr) if(!$result);
|
||||||
|
|
||||||
|
$self -> {"logger"} -> log("messaging", $userid || 0, undef, "Delete of message $messageid row updated count: $result");
|
||||||
|
|
||||||
# Result should contain the number of rows updated.
|
# Result should contain the number of rows updated.
|
||||||
return 0 if($result eq "0E0"); # need a special case for the zero rows, just in case...
|
return 0 if($result eq "0E0"); # need a special case for the zero rows, just in case...
|
||||||
|
|
||||||
$self -> {"logger"} -> log("messaging", $userid || 0, undef, "Delete of message(s) with $field = '$value' marked $result messages");
|
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user