public function validateUpdate($payload, sfWebRequest $request = null) { parent::validateUpdate($payload, $request); $params = $this->parsePayload($payload); $user = $this->getUser()->getGuardUser(); if (!$user) { throw new sfException('Action requires an auth token.', 401); } $primaryKey = $request->getParameter('id'); $episode = EpisodeTable::getInstance()->find($primaryKey); if (!$this->getUser()->isSuperAdmin()) { $admin = sfGuardUserSubredditMembershipTable::getInstance()->getFirstByUserSubredditAndMemberships($user->getIncremented(), $episode->getSubredditId(), array('admin')); $moderator = sfGuardUserSubredditMembershipTable::getInstance()->getFirstByUserSubredditAndMemberships($user->getIncremented(), $episode->getSubredditId(), array('moderator')); if (!$admin) { if (array_key_exists('episode_assignment_id', $params)) { $assignment = EpisodeAssignmentTable::getInstance()->find($params['episode_assignment_id']); if ($assignment->getSfGuardUserId() != $user->getIncremented()) { throw new sfException('You are not allowed to change the EpisodeAssignment of the Episode!', 403); } } if (array_key_exists('approved_by', $params) && !$moderator && $params['approved_by'] != $user->getIncremented()) { throw new sfException('You are not allowed to add approval for the Episode!', 403); } } } }
/** * Tests whether we can grab an Episode that is due to be released within a * specified length of time from now. */ public function testGetOneEpisodeReleasedWithinSeconds() { // Create Test Subreddit $subreddit = new Subreddit(); $subreddit->setName(rand(0, 1000)); $subreddit->setDomain(rand(0, 1000)); $subreddit->save(); // Create Test Episode $episode = new Episode(); $episode->setReleaseDate(date('Y-m-d H:i:s', time() + 100)); $episode->setSubreddit($subreddit); $episode->save(); $another_episode = new Episode(); $another_episode->setReleaseDate(date('Y-m-d H:i:s', time() + 500)); $another_episode->setSubreddit($subreddit); $another_episode->save(); $seconds_within = 200; // Run table query to grab episode without ID $random_episode = EpisodeTable::getInstance()->getOneEpisodeReleasedWithinSeconds($seconds_within); $this->assertTrue($random_episode instanceof Episode); // Run table query to grab episode WITH ID $random_episode = EpisodeTable::getInstance()->getOneEpisodeReleasedWithinSeconds($seconds_within, $episode->getIncremented()); $this->assertTrue($random_episode instanceof Episode); // Run table query to grab anotherepisode WITH ID $random_episode = EpisodeTable::getInstance()->getOneEpisodeReleasedWithinSeconds($seconds_within, $another_episode->getIncremented()); $this->assertFalse($random_episode instanceof Episode); // Delete episodes $another_episode->delete(); $episode->delete(); // Delete subreddit. $subreddit->delete(); }
public function validateCreate($payload, sfWebRequest $request = null) { $params = $this->parsePayload($payload); $user = $this->getUser()->getGuardUser(); if (!$user) { throw new sfException('Action requires an auth token.', 401); } $episode = EpisodeTable::getInstance()->find($params['episode_id']); $admin = sfGuardUserSubredditMembershipTable::getInstance()->getFirstByUserSubredditAndMemberships($user->getIncremented(), $episode->getSubredditId(), array('admin')); if (array_key_exists('sf_guard_user_id', $params) && !$this->getUser()->isSuperAdmin() && !$admin && $params['sf_guard_user_id'] != $user->getIncremented()) { throw new sfException('You are not allowed to create an EpisodeAssignment for someone else.', 403); } if (!array_key_exists('sf_guard_user_id', $params)) { $params[] = $user->getIncremented(); } $payload = $this->packagePayload($params); unset($this->_payload_array); parent::validateCreate($payload, $request); }
public function executeAssign(sfWebRequest $request) { $auth_key = $this->getUser()->getApiAuthKey(); $this->forward404Unless($request->getParameter('author_type_id') || $request->getParameter('episode_id')); $author_type_id = $request->getParameter('author_type_id'); $episode_id = $request->getParameter('episode_id'); $episode = EpisodeTable::getInstance()->find($episode_id); $user_id = $this->getUser()->getApiUserId(); $post_values = array('author_type_id' => $author_type_id, 'episode_id' => $episode_id, 'sf_guard_user_id' => $user_id); $result = Api::getInstance()->setUser($auth_key)->post('episodeassignment', $post_values, false); $success = $this->checkHttpCode($result, 'post', 'episodeassignment', json_encode($post_values)); if ($success) { $this->getUser()->setFlash('notice', 'Registered for Episode! You will be notified when it becomes available.'); } $this->redirect('subreddit/signup?domain=' . $episode->getSubreddit()->getDomain()); }
public function advanceEpisodeAssignments() { $subreddit_first_deadlines = array(); $subreddit_deadline_rules = array(); // We establish the pool of emails we'll be sending. // First to those who pass their deadline $passed_deadline_assignments = array(); // And to the new episode assignments that are reassigned $newly_assigned_assignments = array(); // Now we can start on the assignments that are misassigned $assignments = EpisodeAssignmentTable::getInstance()->getMisassignedEpisodes(); $episodes = new Doctrine_Collection('Episode'); $e = -1; for ($i = 0; $i < count($assignments); $i++) { $passed_deadline_assignments[] = $assignments[$i]; $assignments[$i]->setMissedDeadline(true); $episodes[++$e] = $assignments[$i]->getEpisode(); // Clean up the Episode for any new user to use. $episodes[$e]->setEpisodeAssignmentId(null); $audio_file = $episodes[$e]->getAudioFile(); $nice_filename = $episodes[$e]->getNiceFilename(); $graphic_file = $episodes[$e]->getGraphicFile(); $episodes[$e]->setAudioFile(null); $episodes[$e]->setNiceFilename(null); $episodes[$e]->setGraphicFile(null); $episodes[$e]->setIsNsfw(false); $episodes[$e]->setTitle(null); $episodes[$e]->setDescription(null); $episodes[$e]->setIsSubmitted(false); $episodes[$e]->setSubmittedAt(null); $episodes[$e]->setFileIsRemote(null); $episodes[$e]->setRemoteUrl(null); $episodes[$e]->setRedditPostUrl(null); } $episodes->save(); $assignments->save(); /* Now we make sure that all assignments past deadline are marked as * such. If the assignment is here, however, then it hasn't ever * actually BEEN assigned and isn't added to the list of emails to send * out. */ $assignments = EpisodeAssignmentTable::getInstance()->getUnmarkedEpisodesThatMissedDeadlines(); for ($i = 0; $i < count($assignments); $i++) { $assignments[$i]->setMissedDeadline(true); } $assignments->save(); /* Now all episodes are cleared and we need to see if they need to be * reassigned to an existing asignment. */ /* Returns assignments closest to the front for each unassigned episode, * in order of closeness. */ $assignments = EpisodeAssignmentTable::getInstance()->getEpisodesPossiblyNeedingAssignment(); $subreddit_ids = EpisodeAssignmentTable::getInstance()->getSubrbedditsOfEpisodesPossiblyNeedingAssignment(); $deadlines = DeadlineTable::getInstance()->getDeadlinesForGivenSubreddits($subreddit_ids); $subreddit_first_authortypes = array(); $subreddit_deadline_rules = array(); $begun = false; $prev_subreddit_id = null; foreach ($deadlines as $deadline) { $subreddit_id = $deadline['subreddit_id']; if ($prev_subreddit_id != $subreddit_id && $begun) { $begun = false; $prev_subreddit_id = $subreddit_id; } if (!$begun) { $subreddit_deadline_rules[$subreddit_id] = array($deadline['author_type_id'] => $deadline['seconds']); $subreddit_first_authortypes[$subreddit_id] = $deadline['author_type_id']; $begun = true; } else { $author_type_id = $deadline['author_type_id']; $subreddit_deadline_rules[$subreddit_id][$author_type_id] = $deadline['seconds']; } } $episodes_affected = array(); // Things used from assignment: episode_id, epsiode, author_type_id, id, sf_guard_user_id // Things used from episode: subreddit_id, surbeddit, release_date, // Things used from subreddit: getFirstDeadlineId(), getDeadlineRules() foreach ($assignments as $assignment) { if (!in_array($assignment['episode_id'], $episodes_affected)) { /* Ignore all subsequent assignments for an episode after the * first! We should only be dealing with assignments that have * not missed their deadlines! */ $episodes_affected[] = $assignment['episode_id']; $assign_to_episode = false; $subreddit_id = $assignment['subreddit_id']; $subreddit = SubredditTable::getInstance()->find($subreddit_id); /* If the *first* assignment is in the first spot, then assign * it. */ $deadline_rules = $subreddit_deadline_rules[$subreddit_id]; $author_type_id = $assignment['author_type_id']; if ($author_type_id == $subreddit_first_authortypes[$subreddit_id]) { $assign_to_episode = true; } else { /* Otherwise, check if we are past the deadline for the * previous deadline. */ /*$previous_author_type_id = DeadlineTable::getInstance() ->getFirstAuthorTypeIdBySubredditWhereDeadlineIsGreaterThan( $deadline_rules[$author_type_id], $subreddit_id);*/ $inverse_deadline_rules = array_reverse($deadline_rules, true); $previous_author_type_id = null; foreach ($inverse_deadline_rules as $author_type => $seconds) { if ($seconds > $deadline_rules[$author_type_id]) { $previous_author_type_id = $author_type; break; } } $past_deadline_for_previous = strtotime($assignment['release_date']) - $deadline_rules[$previous_author_type_id] <= time(); if ($past_deadline_for_previous) { $assign_to_episode = true; } } if ($assign_to_episode) { $saved_episode = EpisodeTable::getInstance()->find($assignment['episode_id']); $saved_episode->setEpisodeAssignmentId($assignment['id']); $saved_episode->save(); $newly_assigned_assignments[] = $assignment; } } } // We send the emails for the current deadline we're checking. foreach ($passed_deadline_assignments as $assignment) { $this->sendEmailAboutPassedDeadline($assignment['sf_guard_user_id'], $assignment['episode_id']); } foreach ($newly_assigned_assignments as $assignment) { $release_date = strtotime($assignment['release_date']); $author_type_id = $assignment['author_type_id']; $seconds = $deadline_rules[$author_type_id]; $deadline = $release_date - $seconds; $this->sendEmailAboutNewAssignment($assignment['sf_guard_user_id'], $assignment['episode_id'], $deadline); } }
protected function deriveReleaseDate($parameters) { $episode_id = $parameters['episode_id']; $episode = EpisodeTable::getInstance()->find($episode_id); if (!$episode) { throw new sfException('Cannot find Episode identified by ' . $episode_id); } return date('l, F j, Y \\a\\t g:ia', strtotime($episode->getReleaseDate())); }
/** * Verifies if the current time is still before the Subreddit Deadline for * the AuthorType of the EpisodeAssignment. * * If an AuthorType id is given, then the check is against the Subreddit * Deadline for that AuthorTyope. * * @param int $author_type_id The id for a given AuthorType (optional) * @return bool Whether the current time is within the * Subreddit Deadline. */ public function isBeforeDeadlineForAuthorType($author_type_id = null) { if (is_null($author_type_id)) { $author_type_id = $this->getAuthorTypeId(); } if ($this->getMissedDeadline()) { return false; } $deadline_seconds = Doctrine::getTable('Deadline')->getSecondsByAuthorAndSubreddit($author_type_id, $this->getEpisode()->getSubredditId()); $release_date = strtotime(EpisodeTable::getInstance()->getCurrentReleaseDate($this->getEpisodeId())); $now_and_deadline = time() + $deadline_seconds; if ($now_and_deadline > $release_date && !$this->getMissedDeadline()) { $this->setMissedDeadline(true); } return $now_and_deadline < $release_date; }
protected function validateEpisodeForImageUpload($id, $filename) { if (is_null($this->getUser()->getAttribute('valid_episode', null))) { // Base value is false $this->getUser()->setAttribute('valid_episode', false); $episode = EpisodeTable::getInstance()->find($id); if ($episode) { if ($this->getUser()->getApiUserId() == $episode->getEpisodeAssignment()->getSfGuardUserId() && !$episode->getIsApproved()) { $valid_episode = true; $this->getUser()->setAttribute('valid_episode', true); $this->getUser()->setAttribute('valid_episode_id', $id); $this->getUser()->setAttribute('valid_episode_user_id', $episode->getEpisodeAssignment()->getSfGuardUserId()); $this->getUser()->setAttribute('valid_episode_image_file_hash', $this->generateFilenameHashForImage($filename)); $episode->setGraphicFile($this->generateFilenameHashForImage($filename)); $episode->setSkipBackup(true); $episode->save(); } } } return $this->getUser()->getAttribute('valid_episode', false); }
protected function execute($arguments = array(), $options = array()) { // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $connection = $databaseManager->getDatabase($options['connection'])->getConnection(); $quiet = (bool) $options['quiet']; // add your code here $num_subreddits = 1000; $num_deadlines = 5; $num_users = 3000; $create_episodes_attempt = 3; echo " Creating AuthorTypes..."; $authortypes = new Doctrine_Collection('AuthorType'); for ($i = 0; $i < $num_deadlines; $i++) { $exists = AuthorTypeTable::getInstance()->findOneBy('type', $i); if ($exists) { $authortypes[$i] = $exists; } else { $authortypes[$i] = new AuthorType(); } $authortypes[$i]->setType($i); $authortypes[$i]->setDescription($i); if (!$quiet) { echo $authortypes[$i]->getType() . ' '; } } $authortypes->save(); /*echo "\n Creating Subreddits..."; $subreddits = new Doctrine_Collection('Subreddit'); $x = 0; for ($i = 0; $i < $num_subreddits; $i++) { $exists = SubredditTable::getInstance()->findOneBy('name', $i); if ($exists) { if (!$quiet) echo $exists->getName() . ' '; continue; } $subreddits[$x] = new Subreddit(); $subreddits[$x]->setName($i); $subreddits[$x]->setDomain($i); $subreddits[$x]->setEpisodeScheduleCronFormatted('0 0 * * *'); $subreddits[$x]->setCreateNewEpisodesCronFormatted('0 0 1 * *'); if (!$quiet) echo $subreddits[$x]->getName() . ' '; $x++; } if ($x) $subreddits->save(); echo "\n Creating Deadlines..."; $deadlines = new Doctrine_Collection('Deadline'); $x = 0; for ($i = 0; $i < $num_subreddits; $i++) { $subreddit = SubredditTable::getInstance()->findOneBy('name', $i); $deadlines = $subreddit->getDeadlines(); if (!$quiet) echo $subreddit->getName() . ' '; if (count($deadlines) == 0) { for ($j = 0; $j < $num_deadlines; $j++) { $deadlines[$x] = new Deadline(); $deadlines[$x]->setAuthorType($authortypes[$j]); $deadlines[$x]->setSubreddit($subreddit); $deadlines[$x]->setSeconds((24 / $num_deadlines) * $j); $x++; } } } if ($x) $deadlines->save(); unset($deadlines); echo "\n Creating fake users for signup..."; $users = new Doctrine_collection("sfGuardUser"); $x = 0; for ($i = 0; $i < $num_users; $i++) { $exists = sfGuardUserTable::getInstance()->findOneBy('username', $i); if ($exists) { $users[$x] = $exists; continue; } else { $users[$x] = new sfGuardUser(); } $users[$x]->setUsername($i); $users[$x]->setEmailAddress($i); $users[$x]->setIsValidated(1); if (!$quiet) echo $users[$x]->getUsername() . ' '; $x++; } $users->save(); echo "Joining to subreddits..."; $memberships = new Doctrine_Collection('sfGuardUserSubredditMembership'); $x = 0; $member = MembershipTable::getInstance()->findOneBy('type', 'member'); foreach($users as $user) { echo $user->getUsername() .' '; foreach($subreddits as $subreddit) { $exists = sfGuardUserSubredditMembershipTable::getInstance()->getFirstByUserSubredditAndMemberships($user->getIncremented(), $subreddit->getIncremented(), array('member')); if ($exists) { continue; } $memberships[$x] = new sfGuardUserSubredditMembership(); $memberships[$x]->setSfGuardUserId($user->getIncremented()); $memberships[$x]->setSubredditId($subreddit->getIncremented()); $memberships[$x]->setMembershipId($member->getIncremented()); } } if ($x) $memberships->save(); echo "\n Analyizing which need Episodes..."; $subreddits = Doctrine::getTable('Subreddit') ->getSubredditsNeedingEpisodeGeneration(); foreach ($subreddits as $subreddit) { if (!$quiet) echo $subreddit->getName() . ' '; $episodes = $subreddit->collectGeneratedEpisodes(); if (!$quiet) echo ' Generated...'; $episodes->save(); if (!$quiet) echo " Saved!\n"; }*/ echo "\n Randomly having users attempt to sign up for Episodes..."; $assignments = new Doctrine_Collection('EpisodeAssignment'); $x = 0; for ($i = 0; $i < $num_users; $i++) { $user = sfGuardUserTable::getInstance()->findOneBy('username', $i); echo $user->getUsername(); for ($j = 0; $j < $create_episodes_attempt; $j++) { $episode = EpisodeTable::getInstance()->getRandomUnassignedFutureEpisode(); if (!$episode) { continue; } $assignments[$x] = new EpisodeAssignment(); $assignments[$x]->setEpisodeId($episode->getIncremented()); $assignments[$x]->setSfGuardUserId($user->getIncremented()); $assignments[$x]->setAuthorType($authortypes[$j]); try { $assignments[$x]->save(); } catch (Exception $e) { unset($e); } } } }