Пример #1
0
 /**
  * MI: Accept "fake_sample_url" option, passed only in
  * post#index. It will set a flag that will be read by PostFileMethods::sample_url()
  */
 public static function batch_api_data(array $posts, $options = array())
 {
     self::$create_fake_sample_url = !empty($options['fake_sample_url']);
     foreach ($posts as $post) {
         $result['posts'][] = $post->api_attributes();
     }
     if (empty($options['exclude_pools'])) {
         $result['pools'] = $result['pool_posts'] = [];
         $pool_posts = Pool::get_pool_posts_from_posts($posts);
         $pools = Pool::get_pools_from_pool_posts($pool_posts);
         foreach ($pools as $p) {
             $result['pools'][] = $p->api_attributes();
         }
         foreach ($pool_posts as $pp) {
             $result['pool_posts'][] = $pp->api_attributes();
         }
     }
     if (empty($options['exclude_tags'])) {
         $result['tags'] = Tag::batch_get_tag_types_for_posts($posts);
     }
     if (!empty($options['user'])) {
         $user = $options['user'];
     } else {
         $user = current_user();
     }
     # Allow loading votes along with the posts.
     #
     # The post data is cachable and vote data isn't, so keep this data separate from the
     # main post data to make it easier to cache API output later.
     if (empty($options['exclude_votes'])) {
         $vote_map = array();
         if ($posts) {
             $post_ids = array();
             foreach ($posts as $p) {
                 $post_ids[] = $p->id;
             }
             if ($post_ids) {
                 $post_ids = implode(',', $post_ids);
                 $sql = sprintf("SELECT v.* FROM post_votes v WHERE v.user_id = %d AND v.post_id IN (%s)", $user->id, $post_ids);
                 $votes = PostVote::findBySql($sql);
                 foreach ($votes as $v) {
                     $vote_map[$v->post_id] = $v->score;
                 }
             }
         }
         $result['votes'] = $vote_map;
     }
     self::$create_fake_sample_url = false;
     return $result;
 }