From c133030704f8441256a7ce04e4acce433d85dc23 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Wed, 28 May 2014 14:35:25 +0200 Subject: [PATCH] Add a timestamp field to invitations And purge those older than 2 hours --- docs/schema.mysql | 1 + docs/upgrade.mysql | 1 + public/vroom.pl | 19 +++++++++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/schema.mysql b/docs/schema.mysql index f1c4c62..08b90ac 100644 --- a/docs/schema.mysql +++ b/docs/schema.mysql @@ -40,6 +40,7 @@ CREATE TABLE `invitations` ( `response` varchar(20) DEFAULT NULL, `message` text DEFAULT NULL, `processed` tinyint(1) DEFAULT '0', + `timestamp` int(20) DEFAULT NULL, PRIMARY KEY (`token`) ) DEFAULT CHARSET=utf8; #DROP TABLE IF EXISTS `turnusers_lt`; diff --git a/docs/upgrade.mysql b/docs/upgrade.mysql index da4bb30..8c93b27 100644 --- a/docs/upgrade.mysql +++ b/docs/upgrade.mysql @@ -18,5 +18,6 @@ CREATE TABLE IF NOT EXISTS `invitations` ( `response` varchar(20) DEFAULT NULL, `message` text DEFAULT NULL, `processed` tinyint(1) DEFAULT '0', + `timestamp` int(20) DEFAULT NULL, PRIMARY KEY (`token`) ) DEFAULT CHARSET=utf8; diff --git a/public/vroom.pl b/public/vroom.pl index d560788..34754b1 100755 --- a/public/vroom.pl +++ b/public/vroom.pl @@ -474,8 +474,8 @@ helper add_invitation => sub { my $data = $self->get_room($room); my $id = $self->get_random(50); return undef unless ($data); - my $sth = eval { $self->db->prepare("INSERT INTO invitations (`id`,`from`,`token`,`email`) VALUES (?,?,?,?)") } || return undef; - $sth->execute($data->{id},$from,$id,$email) || return undef; + my $sth = eval { $self->db->prepare("INSERT INTO invitations (`id`,`from`,`token`,`email`,`timestamp`) VALUES (?,?,?,?,?)") } || return undef; + $sth->execute($data->{id},$from,$id,$email,time()) || return undef; return $id; }; @@ -518,6 +518,17 @@ helper processed_invitation => sub { return 1; }; +# Purge expired invitation links +helper delete_invitations => sub { + my $self = shift; + $self->app->log->debug('Removing expired invitations'); + # Invitation older than 2 hours doesn't make much sense + my $timeout = time()-7200; + my $sth = eval { $self->db->prepare("DELETE FROM `invitations` WHERE `timestamp` < $timeout;") } || return undef; + $sth->execute() || return undef; + return 1; +}; + # Route / to the index page any '/' => 'index'; @@ -873,6 +884,10 @@ post '/action' => sub { if ((int (rand 100)) <= 10){ $self->delete_rooms; } + # And same for expired invitation links + if ((int (rand 100)) <= 10){ + $self->delete_invitations; + } if ($res){ $status = 'success'; $msg = '';