/** * Convert a single URL, assumes $url has been verified to be * a real URL * * @param string $url */ public function convertUrl($url) { $oembed = Oembed::get_oembed_from_url($url, false, $this->oembedOptions); if ($oembed) { return array('Title' => '', 'Content' => $oembed->forTemplate()); } $graph = OpenGraph::fetch($url); if ($graph) { foreach ($graph as $key => $value) { $data[$key] = Varchar::create_field('Varchar', $value); } if (isset($data['url'])) { return array('Title' => $graph->Title, 'Content' => MicroPost::create()->customise($data)->renderWith('OpenGraphPost')); } } // get the post and take its <title> tag at the very least $service = new RestfulService($url); $response = $service->request(); if ($response && $response->getStatusCode() == 200) { if (preg_match('/<title>(.*?)<\\/title>/is', $response->getBody(), $matches)) { $title = Convert::raw2xml(trim($matches[1])); return array('Title' => $title, 'Content' => "<a href='{$url}'>{$title}</a>"); } } }
public function run($request) { $posts = MicroPost::get()->filter('ParentID', 0); foreach ($posts as $post) { $sql = 'UPDATE "MicroPost" SET "ThreadOwnerID" = ' . $post->OwnerID . ' WHERE "ThreadID" = ' . $post->ID; DB::query($sql); } }
/** * What tags are present in the request (ie that should filter and be applied to posts */ protected function afterPostCreated(MicroPost $post) { $tags = array(); if ($this->data()->SelfTagPosts) { $tags[] = $this->data()->selfTag(); } $add = $this->AddTags->getValues(); if (count($add)) { $tags = array_merge($tags, $add); } $post->tag($tags); }
public function testCreateTypedPost() { MicroPost::get()->removeAll(); $svc = singleton('MicroBlogService'); $svc->typeAge = array(); /* @var $svc MicroBlogService */ $member = $this->objFromFixture('Member', 'user1'); $post = $svc->createPost($member, "My test post"); $this->assertTrue($post->ID > 0); // get the list, and check that this post is in it $posts = $svc->getStatusUpdates(); $this->assertEquals($post->ID, $posts[0]->ID); $post2 = $svc->createPost($member, "Another test post", array('PostType' => 'mypost')); $posts = $svc->getStatusUpdates(); $this->assertEquals(2, count($posts)); $this->assertEquals($post2->ID, $posts[0]->ID); $svc->typeAge = array('mypost' => 2); sleep(3); $posts = $svc->getStatusUpdates(); $this->assertEquals(1, count($posts)); $this->assertEquals($post->ID, $posts[0]->ID); }
/** * Create a list of posts depending on a filter and time range * * @param array $filter * * @param int $since * The ID after which to get posts * @param int $before * The ID or pagination offset from which to get posts before. * @param type $topLevelOnly * Only retrieve the top level of posts. * @param array $tags * A set of tags to filter posts by * @param int $offset * Offset to start returning results by * @param int $number * How many results to return * * @return DataList */ public function microPostList($filter, $sortBy = 'ID', $since = 0, $before = false, $topLevelOnly = true, $tags = array(), $offset = 0, $number = 10) { if ($topLevelOnly) { $filter['ParentID'] = '0'; } $filter['Deleted'] = 0; if ($since) { $since = Convert::raw2sql($since); $filter['ID:GreaterThan'] = $since; } if ($before !== false) { $before = (int) $before; $filter['ID:LessThan'] = $before; } $sort = array(); if (is_string($sortBy)) { if (in_array($sortBy, $this->canSort)) { $sort[$sortBy] = 'DESC'; } // final sort as a tie breaker $sort['ID'] = 'DESC'; } else { if (is_array($sortBy)) { // $sort = $sortBy; foreach ($sortBy as $sortKey => $sortDir) { if (in_array($sortKey, $this->canSort)) { $sort[$sortKey] = $sortDir; } } } else { $sort = array('ID' => 'DESC'); } } $offset = (int) $offset; $limit = $number ? $offset . ', ' . (int) $number : ''; if (count($tags)) { $filter['Tags.Title'] = $tags; } $this->recordUserAction(); $list = MicroPost::get()->filter($filter)->sort($sort)->limit($limit); $list = $this->updatePostList($list); // if we're only allowing singe votes, we need to get _all_ the current user's votes and // mark the individual posts that have been voted on; this allows the toggling // of the vote options if ($this->singleVotes && $this->securityContext->getMember()) { $ids = $list->column('ID'); $votes = MicroPostVote::get()->filter(array('UserID' => $this->securityContext->getMember()->ID, 'PostID' => $ids)); $map = $votes->map('PostID', 'Direction')->toArray(); foreach ($list as $post) { if (isset($map[$post->ID])) { $post->UserVote = $map[$post->ID] > 0 ? 'upvote' : 'downvote'; } } } return $list->restrict(); }