Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Facebook plugin: Send invalid token reminder email at most once a week

  • Loading branch information...
commit a0e086dc9311aed62aa1d14bbeb1baa4741ca06f 1 parent 90ee331
@ginatrapani ginatrapani authored
View
40 webapp/plugins/facebook/model/class.FacebookPlugin.php
@@ -105,21 +105,43 @@ public function crawl() {
}
/**
- * Send user email alert about invalid OAuth tokens. In test mode, this will only write the message body to a file
- * in the application data directory.
+ * Send user email alert about invalid OAuth tokens, at most one message per week.
+ * In test mode, this will only write the message body to a file in the application data directory.
* @param str $email
* @param str $username
*/
private function sendInvalidOAuthEmailAlert($email, $username) {
- $mailer_view_mgr = new ViewManager();
- $mailer_view_mgr->caching=false;
+ //Determine whether or not an email about invalid tokens was sent in the past 7 days
+ $should_send_email = true;
+ $option_dao = DAOFactory::getDAO('OptionDAO');
+ $plugin_dao = DAOFactory::getDAO('PluginDAO');
+
+ $plugin_id = $plugin_dao->getPluginId('facebook');
+ $last_email_timestamp = $option_dao->getOptionByName(OptionDAO::PLUGIN_OPTIONS.'-'.$plugin_id,
+ 'invalid_oauth_email_sent_timestamp');
+ if (isset($last_email_timestamp)) { //option exists, a message was sent
+ //a message was sent in the past week
+ if ($last_email_timestamp->option_value > strtotime('-1 week') ) {
+ $should_send_email = false;
+ } else {
+ $option_dao->updateOption($last_email_timestamp->option_id, time());
+ }
+ } else {
+ $option_dao->insertOption(OptionDAO::PLUGIN_OPTIONS.'-'.$plugin_id,
+ 'invalid_oauth_email_sent_timestamp', time());
+ }
- $mailer_view_mgr->assign('thinkup_site_url', Utils::getApplicationURL());
- $mailer_view_mgr->assign('email', $email );
- $mailer_view_mgr->assign('faceboook_user_name', $username);
- $message = $mailer_view_mgr->fetch(Utils::getPluginViewDirectory('facebook').'_email.invalidtoken.tpl');
+ if ($should_send_email) {
+ $mailer_view_mgr = new ViewManager();
+ $mailer_view_mgr->caching=false;
- Mailer::mail($email, "Please re-authorize ThinkUp to access ". $username. " on Facebook", $message);
+ $mailer_view_mgr->assign('thinkup_site_url', Utils::getApplicationURL());
+ $mailer_view_mgr->assign('email', $email );
+ $mailer_view_mgr->assign('faceboook_user_name', $username);
+ $message = $mailer_view_mgr->fetch(Utils::getPluginViewDirectory('facebook').'_email.invalidtoken.tpl');
+
+ Mailer::mail($email, "Please re-authorize ThinkUp to access ". $username. " on Facebook", $message);
+ }
}
public function renderConfiguration($owner) {
View
28 webapp/plugins/facebook/tests/TestOfFacebookPlugin.php
@@ -168,6 +168,14 @@ public function testCrawlWithAuthError() {
$builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>5,
'auth_error'=>''));
+ //assert invalid_oauth_email_sent_timestamp option is not set
+ $option_dao = DAOFactory::getDAO('OptionDAO');
+ $plugin_dao = DAOFactory::getDAO('PluginDAO');
+ $plugin_id = $plugin_dao->getPluginId('facebook');
+ $last_email_timestamp = $option_dao->getOptionByName(OptionDAO::PLUGIN_OPTIONS.'-'.$plugin_id,
+ 'invalid_oauth_email_sent_timestamp');
+ $this->assertNull($last_email_timestamp);
+
//log in as that owner
$this->simulateLogin('me@example.com');
@@ -191,5 +199,25 @@ public function testCrawlWithAuthError() {
$actual_reg_email = Mailer::getLastMail();
$this->debug($actual_reg_email);
$this->assertPattern($expected_reg_email_pattern, $actual_reg_email);
+
+ //assert invalid_oauth_email_sent_timestamp option has been set
+ $last_email_timestamp = $option_dao->getOptionByName(OptionDAO::PLUGIN_OPTIONS.'-'.$plugin_id,
+ 'invalid_oauth_email_sent_timestamp');
+ $this->assertNotNull($last_email_timestamp);
+
+ //Delete last mail file
+ $test_email_file = FileDataManager::getDataPath(Mailer::EMAIL);
+ unlink($test_email_file);
+ $actual_reg_email = Mailer::getLastMail();
+ //Assert it's been deleted
+ $this->assertEqual($actual_reg_email, '');
+
+ //Crawl again
+ $fb_plugin->crawl();
+
+ //Assert email has not been resent
+ $actual_reg_email = Mailer::getLastMail();
+ $this->debug($actual_reg_email);
+ $this->assertEqual($actual_reg_email, '');
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.