public static function run(Application $app, $verbose = false) { global $CONFIG; if ($verbose) { print "Starting " . date("c") . "\n"; } $userRepo = new UserAccountRepository(); $siteRepo = new SiteRepository(); $groupRepo = new GroupRepository(); $eventRepo = new EventRepository(); $userWatchesGroupRepository = new UserWatchesGroupRepository(); $userWatchesGroupStopRepository = new UserWatchesGroupStopRepository(); $userAccountGeneralSecurityKeyRepository = new UserAccountGeneralSecurityKeyRepository(); $userNotificationRepo = new UserNotificationRepository(); $userHasNoEditorPermissionsInSiteRepo = new UserHasNoEditorPermissionsInSiteRepository(); $userPermissionsRepo = new UserPermissionsRepository($app['extensions']); /** @var usernotifications/UserWatchesGroupPromptNotificationType **/ $userNotificationType = $app['extensions']->getCoreExtension()->getUserNotificationType('UserWatchesGroupPrompt'); $b = new UserWatchesGroupRepositoryBuilder(); foreach ($b->fetchAll() as $userWatchesGroup) { $user = $userRepo->loadByID($userWatchesGroup->getUserAccountId()); $group = $groupRepo->loadById($userWatchesGroup->getGroupId()); $site = $siteRepo->loadById($group->getSiteID()); // This is not the most efficient as it involves DB access and the results might not be used. But it'll do for now. $userPermissions = $userPermissionsRepo->getPermissionsForUserInSite($user, $site, false, true); if ($verbose) { print date("c") . " User " . $user->getEmail() . " Site " . $site->getTitle() . " Group " . $group->getTitle() . "\n"; } // UserWatchesGroupRepositoryBuilder() should only return instances where site is not also watched if ($site->getIsClosedBySysAdmin()) { if ($verbose) { print " ... site is closed\n"; } } else { if ($group->getIsDeleted()) { if ($verbose) { print " ... group is deleted\n"; } } else { if ($userHasNoEditorPermissionsInSiteRepo->isUserInSite($user, $site)) { if ($verbose) { print " ... user does not have edit permissions allowed in site\n"; } } else { if (!$userPermissions->hasPermission("org.openacalendar", "CALENDAR_CHANGE")) { if ($verbose) { print " ... user does not have org.openacalendar/CALENDAR_CHANGE permission in site\n"; } // Technically UserWatchesSiteRepositoryBuilder() should only return getIsWatching() == true but lets double check } else { if ($userWatchesGroup->getIsWatching()) { if ($verbose) { print " ... searching for data\n"; } $lastEvent = $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId()); $data = $userWatchesGroup->getPromptEmailData($site, $lastEvent); if ($data['moreEventsNeeded']) { if ($verbose) { print " ... found data\n"; } ///// Notification Class $userNotification = $userNotificationType->getNewNotification($user, $site); $userNotification->setGroup($group); ////// Save Notification Class $userNotificationRepo->create($userNotification); ////// Send Email if ($userNotification->getIsEmail()) { $userWatchesGroupStop = $userWatchesGroupStopRepository->getForUserAndGroup($user, $group); configureAppForSite($site); configureAppForUser($user); $userAccountGeneralSecurityKey = $userAccountGeneralSecurityKeyRepository->getForUser($user); $unsubscribeURL = $CONFIG->getWebIndexDomainSecure() . '/you/emails/' . $user->getId() . '/' . $userAccountGeneralSecurityKey->getAccessKey(); $lastEventsBuilder = new EventRepositoryBuilder(); $lastEventsBuilder->setSite($site); $lastEventsBuilder->setGroup($group); $lastEventsBuilder->setOrderByStartAt(true); $lastEventsBuilder->setIncludeDeleted(false); $lastEventsBuilder->setIncludeImported(false); $lastEventsBuilder->setLimit($CONFIG->userWatchesGroupPromptEmailShowEvents); $lastEvents = $lastEventsBuilder->fetchAll(); $message = \Swift_Message::newInstance(); $message->setSubject("Any news about " . $group->getTitle() . "?"); $message->setFrom(array($CONFIG->emailFrom => $CONFIG->emailFromName)); $message->setTo($user->getEmail()); $messageText = $app['twig']->render('email/userWatchesGroupPromptEmail.txt.twig', array('group' => $group, 'user' => $user, 'lastEvents' => $lastEvents, 'stopCode' => $userWatchesGroupStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'unsubscribeURL' => $unsubscribeURL)); if ($CONFIG->isDebug) { file_put_contents('/tmp/userWatchesGroupPromptEmail.txt', $messageText); } $message->setBody($messageText); $messageHTML = $app['twig']->render('email/userWatchesGroupPromptEmail.html.twig', array('group' => $group, 'user' => $user, 'lastEvents' => $lastEvents, 'stopCode' => $userWatchesGroupStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'unsubscribeURL' => $unsubscribeURL)); if ($CONFIG->isDebug) { file_put_contents('/tmp/userWatchesGroupPromptEmail.html', $messageHTML); } $message->addPart($messageHTML, 'text/html'); $headers = $message->getHeaders(); $headers->addTextHeader('List-Unsubscribe', $unsubscribeURL); if ($verbose) { print " ... sending\n"; } if (!$CONFIG->isDebug) { $app['mailer']->send($message); } $userNotificationRepo->markEmailed($userNotification); } $userWatchesGroupRepository->markPromptEmailSent($userWatchesGroup, $data['checkTime']); } } } } } } } if ($verbose) { print "Finished " . date("c") . "\n"; } }
function stopWatchingFromEmail($slug, $userid, $code, Request $request, Application $app) { if (!$this->build($slug, $request, $app)) { $app->abort(404, "Group does not exist."); } $userRepo = new UserAccountRepository(); $user = $userRepo->loadByID($userid); if (!$user) { $app['monolog']->addError("Failed stop watching group from email - no user "); die("NO"); // TODO } $userWatchesGroupStopRepo = new UserWatchesGroupStopRepository(); $userWatchesGroupStop = $userWatchesGroupStopRepo->loadByUserAccountIDAndGroupIDAndAccessKey($user->getId(), $this->parameters['group']->getId(), $code); if (!$userWatchesGroupStop) { $app['monolog']->addError("Failed stop watching group from email - user " . $user->getId() . " - code wrong"); die("NO"); // TODO } $userWatchesGroupRepo = new UserWatchesGroupRepository(); $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $this->parameters['group']); if (!$userWatchesGroup || !$userWatchesGroup->getIsWatching()) { $app['monolog']->addError("Failed stop watching group from email - user " . $user->getId() . " - not watching"); die("You don't watch this group"); // TODO } if ($request->request->get('action') == 'unwatch' && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) { $userWatchesGroupRepo->stopUserWatchingGroup($user, $this->parameters['group']); // redirect here because if we didn't the twig global and $app vars would be wrong (the old state) // this is an easy way to get round that. $app['flashmessages']->addMessage("You have stopped watching this group."); return $app->redirect('/group/' . $this->parameters['group']->getSlugForURL()); } $this->parameters['user'] = $user; return $app['twig']->render('site/group/stopWatchingFromEmail.html.twig', $this->parameters); }
public static function run(Application $app, $verbose = false) { global $CONFIG; if ($verbose) { print "Starting " . date("c") . "\n"; } $siteRepo = new SiteRepository(); $groupRepo = new GroupRepository(); $importURLRepo = new ImportURLRepository(); $userRepo = new UserAccountRepository(); $userWatchesSiteStopRepository = new UserWatchesSiteStopRepository(); $userWatchesGroupStopRepository = new UserWatchesGroupStopRepository(); $userAccountGeneralSecurityKeyRepository = new UserAccountGeneralSecurityKeyRepository(); $userNotificationRepo = new UserNotificationRepository(); /** @var usernotifications/UpcomingEventsUserNotificationType **/ $userNotificationType = $app['extensions']->getCoreExtension()->getUserNotificationType('ImportURLExpired'); $iurlBuilder = new ImportURLRepositoryBuilder(); foreach ($iurlBuilder->fetchAll() as $importURL) { $site = $siteRepo->loadById($importURL->getSiteID()); $group = $groupRepo->loadById($importURL->getGroupId()); if ($verbose) { print date("c") . " ImportURL " . $importURL->getId() . " " . $importURL->getTitle() . " Site " . $site->getTitle() . "\n"; } if ($site->getIsClosedBySysAdmin()) { if ($verbose) { print " - site closed by sys admin\n"; } } else { if (!$site->getIsFeatureImporter()) { if ($verbose) { print " - site feature disabled\n"; } } else { if (!$group) { if ($verbose) { print " - no group - this should be impossible\n"; } } else { if ($group->getIsDeleted()) { if ($verbose) { print " - group deleted\n"; } } else { if ($importURL->getExpiredAt()) { if ($verbose) { print " - expired\n"; } } else { if (!$importURL->getIsEnabled()) { if ($verbose) { print " - not enabled\n"; } } else { if ($importURL->isShouldExpireNow()) { if ($verbose) { print " - expiring\n"; } $importURLRepo->expire($importURL); configureAppForSite($site); $uwsb = new UserWatchesSiteRepositoryBuilder(); $uwsb->setSite($site); foreach ($uwsb->fetchAll() as $userWatchesSite) { $user = $userRepo->loadByID($userWatchesSite->getUserAccountId()); if ($userWatchesSite->getIsWatching()) { /// Notification Class $userNotification = $userNotificationType->getNewNotification($user, $site); $userNotification->setImportURL($importURL); $userNotification->setGroup($group); ////// Save Notification Class $userNotificationRepo->create($userNotification); ////// Send Email if ($userNotification->getIsEmail()) { configureAppForUser($user); $userAccountGeneralSecurityKey = $userAccountGeneralSecurityKeyRepository->getForUser($user); $userWatchesSiteStop = $userWatchesSiteStopRepository->getForUserAndSite($user, $site); $message = \Swift_Message::newInstance(); $message->setSubject("Please confirm this is still valid: " . $importURL->getTitle()); $message->setFrom(array($CONFIG->emailFrom => $CONFIG->emailFromName)); $message->setTo($user->getEmail()); $messageText = $app['twig']->render('email/importURLExpired.watchesSite.txt.twig', array('user' => $user, 'importurl' => $importURL, 'stopCode' => $userWatchesSiteStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey())); if ($CONFIG->isDebug) { file_put_contents('/tmp/importURLExpired.watchesSite.txt', $messageText); } $message->setBody($messageText); $messageHTML = $app['twig']->render('email/importURLExpired.watchesSite.html.twig', array('user' => $user, 'importurl' => $importURL, 'stopCode' => $userWatchesSiteStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey())); if ($CONFIG->isDebug) { file_put_contents('/tmp/importURLExpired.watchesSite.html', $messageHTML); } $message->addPart($messageHTML, 'text/html'); if (!$CONFIG->isDebug) { $app['mailer']->send($message); } $userNotificationRepo->markEmailed($userNotification); } } } $uwgb = new UserWatchesGroupRepositoryBuilder(); $uwgb->setGroup($group); foreach ($uwgb->fetchAll() as $userWatchesGroup) { $user = $userRepo->loadByID($userWatchesGroup->getUserAccountId()); if ($userWatchesGroup->getIsWatching()) { /// Notification Class $userNotification = $userNotificationType->getNewNotification($user, $site); $userNotification->setImportURL($importURL); $userNotification->setGroup($group); ////// Save Notification Class $userNotificationRepo->create($userNotification); ////// Send Email if ($userNotification->getIsEmail()) { $userAccountGeneralSecurityKey = $userAccountGeneralSecurityKeyRepository->getForUser($user); $userWatchesGroupStop = $userWatchesGroupStopRepository->getForUserAndGroup($user, $group); $message = \Swift_Message::newInstance(); $message->setSubject("Please confirm this is still valid: " . $importURL->getTitle()); $message->setFrom(array($CONFIG->emailFrom => $CONFIG->emailFromName)); $message->setTo($user->getEmail()); $messageText = $app['twig']->render('email/importURLExpired.watchesGroup.txt.twig', array('user' => $user, 'importurl' => $importURL, 'stopCode' => $userWatchesGroupStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'group' => $group)); if ($CONFIG->isDebug) { file_put_contents('/tmp/importURLExpired.watchesGroup.txt', $messageText); } $message->setBody($messageText); $messageHTML = $app['twig']->render('email/importURLExpired.watchesGroup.html.twig', array('user' => $user, 'importurl' => $importURL, 'stopCode' => $userWatchesGroupStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'group' => $group)); if ($CONFIG->isDebug) { file_put_contents('/tmp/importURLExpired.watchesGroup.html', $messageHTML); } $message->addPart($messageHTML, 'text/html'); if (!$CONFIG->isDebug) { $app['mailer']->send($message); } $userNotificationRepo->markEmailed($userNotification); } } } } else { $lastRunDate = $importURLRepo->getLastRunDateForImportURL($importURL); $nowDate = \TimeSource::getDateTime(); if (!$lastRunDate || $lastRunDate->getTimestamp() < $nowDate->getTimestamp() - $CONFIG->importURLSecondsBetweenImports) { if ($verbose) { print " - importing\n"; } $runner = new ImportURLRunner(); $runner->go($importURL); } else { if ($verbose) { print " - already done on " . $lastRunDate->format("c") . "\n"; } } } } } } } } } } if ($verbose) { print "Finished " . date("c") . "\n"; } }
/** * @return array */ public function getUserNotifyContentForSiteAndUser(\models\SiteModel $siteModel, UserAccountModel $userAccountModel) { global $CONFIG; if (!$siteModel->getIsFeatureGroup()) { return array(); } $out = array(); $grb = new GroupRepositoryBuilder(); $grb->setSite($siteModel); $grb->setLimit(0); // all! No limit // TODO don't we still want to do this? How will user A get a notification if user B deletes group? but then so far most group deletetions are by admins. $grb->setIncludeDeleted(false); foreach ($grb->fetchAll() as $group) { $uwg = $this->loadByUserAndGroup($userAccountModel, $group); if ($uwg && $uwg->getIsWatching()) { $dateSince = $uwg->getSinceDateForNotifyChecking(); $historyRepositoryBuilder = new HistoryRepositoryBuilder(); $historyRepositoryBuilder->setGroup($group); $historyRepositoryBuilder->setSince($dateSince); $historyRepositoryBuilder->setNotUser($userAccountModel); // Only admins can change tags at the moment so don't include $historyRepositoryBuilder->setIncludeTagHistory(false); $histories = $historyRepositoryBuilder->fetchAll(); if ($histories) { $content = new UserWatchesGroupNotifyContent(); $content->setHistories($histories); $userWatchesGroupStopRepository = new UserWatchesGroupStopRepository(); $userWatchesGroupStop = $userWatchesGroupStopRepository->getForUserAndGroup($userAccountModel, $group); $content->setUnwatchURL($CONFIG->getWebSiteDomainSecure($siteModel->getSlug()) . '/group/' . $group->getSlugForURL() . '/stopWatchingFromEmail/' . $userAccountModel->getId() . '/' . $userWatchesGroupStop->getAccessKey()); $content->setUserAccount($userAccountModel); $content->setSite($siteModel); $content->setGroup($group); $content->setWatchedThingTitle($group->getTitle()); $content->setWatchedThingURL($CONFIG->getWebSiteDomainSecure($siteModel->getSlug()) . '/group/' . $group->getSlugForURL() . '/history'); $out[] = $content; } } } return $out; }