[vhffs-dev] [2295] mailinglist: properly remove mail localpart entry if mailing list is the last child left in localpart

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 2295
Author:   gradator
Date:     2016-02-07 01:08:41 +0100 (Sun, 07 Feb 2016)
Log Message:
-----------
mailinglist: properly remove mail localpart entry if mailing list is the last child left in localpart

Removing mailing list left localpart entry in the database with no children
(box, redirect, mailing list), try to remove the mail localpart when deleting
a mailing list to prevent leaving empty localpart.

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Services/MailingList.pm

Modified: trunk/vhffs-api/src/Vhffs/Services/MailingList.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Services/MailingList.pm	2016-02-06 22:02:46 UTC (rev 2294)
+++ trunk/vhffs-api/src/Vhffs/Services/MailingList.pm	2016-02-07 00:08:41 UTC (rev 2295)
@@ -267,6 +267,42 @@
 	return $self->SUPER::commit;
 }
 
+=pod
+
+=head2 delete
+
+	my $ret = $ml->delete;
+
+Delete a mailinglist from the database. Should be called after mailing list have been cleaned up from the filesystem.
+
+=cut
+sub delete {
+	my $self = shift;
+	my $ret;
+
+	# Fetch the mailing list mail domain and only the localpart we need
+	#
+	# We need to fetch the localpart entry before deleting the mailing list because the delete localpart function
+	# remove the localpart if the refcount is <= 1 and not 0
+	#
+	# If we can't fetch the localpart entry we don't really care, the worst it can do is leaving
+	# a localpart entry with no children
+	my $mail = Vhffs::Services::Mail::get_by_mxdomain( $self->get_vhffs, $self->get_domain );
+	my $lp = $mail->fetch_localpart( $self->get_localpart ) if defined $mail;
+
+	# mailing list references corresponding object with an ON DELETE cascade foreign key
+	# so we don't even need to delete the mailing list
+	# rows that reference this mailing list will be deleted by foreign keys constraints
+	$ret = $self->SUPER::delete;
+	return $ret unless $ret;
+
+	# We don't care of the return value, if it fails it might means the localpart is still used (a box or at least one redirect)
+	# and in case of a real failure it only leaves a localpart entry with no children
+	$lp->delete if defined $lp;
+
+	return 1;
+}
+
 sub change_right_for_sub {
 	my ($self, $subscriber, $right) = @_;
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/