/** * Vote for a particular post * * @param DataObject $post */ public function vote(DataObject $post, $dir = 1) { $member = $this->securityContext->getMember(); if ($member->VotesToGive <= 0) { $post->RemainingVotes = 0; return $post; } // we allow multiple votes - as many as the user has to give! unless // configured not to... $currentVote = null; if ($this->singleVotes) { $votes = $post->currentVotesByUser(); if (count($votes)) { $currentVote = $votes[0]; } } if (!$currentVote) { $currentVote = MicroPostVote::create(); $currentVote->UserID = $member->ID; $currentVote->PostID = $post->ID; } $currentVote->Direction = $dir > 0 ? 1 : -1; $currentVote->write(); $list = DataList::create('MicroPostVote'); $upList = $list->filter(array('PostID' => $post->ID, 'Direction' => 1)); $post->Up = $upList->count(); $downList = $list->filter(array('PostID' => $post->ID, 'Direction' => -1)); $post->Down = $downList->count(); $owner = $post->Owner(); if (!$post->OwnerID || !$owner || !$owner->exists()) { $owner = Security::findAnAdministrator(); } // write the post as the owner, and calculate some changes for the author $this->transactionManager->run(function () use($post, $currentVote, $member) { $author = $post->Owner(); if ($author && $author->exists() && $author->ID != $member->ID) { if ($currentVote->Direction > 0) { $author->Up += 1; } else { $author->Down += 1; } $author->write(); } $post->write(); }, $owner); $this->rewardMember($member, -1); $post->RemainingVotes = $member->VotesToGive; return $post; }
/** * Retrieve the container permission source for all this user's posts * * @TODO This is currently not being actively used anywhere. Currently, posts for a * particular user must have permissions assigned individually. */ public function postPermissionSource() { if ($this->owner->MyPermSourceID) { return $this->owner->MyPermSource(); } $source = new PermissionParent(); $source->Title = 'Posts for ' . $this->owner->getTitle(); $owner = $this->owner; $this->transactionManager->run(function () use($source, $owner) { $source->write(); $owner->MyPermSourceID = $source->ID; $owner->write(); }, $owner); return $source; }
public function process() { $fromTime = $this->since ? $this->since : 0; $members = $this->members; $nextId = array_shift($members); $this->members = $members; $member = Member::get()->byID($nextId); $microBlogService = $this->microBlogService; // if we don't have a 'since' time, we need to only scan from 'now' onwards, to prevent _every_ // post from being collected if (!$fromTime) { $fromTime = time(); } $since = date('Y-m-d 00:00:00', $fromTime); if ($member && $member->ID) { $this->transactionManager->run(function () use($microBlogService, $since, $member) { $posts = $microBlogService->globalFeed(array('ParentID' => 0, 'ThreadOwnerID:not' => $member->ID, 'Created:GreaterThan' => $since), $orderBy = 'ID DESC', $since = null, $number = 10, $markViewed = false); if (!count($posts)) { return; } $content = SSViewer::execute_template('DigestEmail', ArrayData::create(array('Posts' => $posts, 'Member' => $member))); $content = HTTP::absoluteURLs($content); $config = SiteConfig::current_site_config(); $mail = new Email(); $mail->setTo($member->Email); $mail->setBody($content); $mail->setSubject($config->Title . ' digest'); if ($config->FromEmail) { $mail->setFrom($config->FromEmail); } $mail->send(); }, $member); } $this->currentStep++; if (count($members) == 0) { if (!$this->sendTime) { $this->sendTime = '23:55:00'; } $nextTime = $this->type == 'weekly' ? '+1 week' : '+1 day'; $nextDate = date('Y-m-d ' . $this->sendTime, strtotime($nextTime)); $nextJob = new MicroPostDigestJob(time(), $this->type, $this->groupId, $this->sendTime); singleton('QueuedJobService')->queueJob($nextJob, $nextDate); $this->isComplete = true; } }