<commit_before><?php

/*  phpADNSite
 Copyright (C) 2014-2015 <NAME>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace PhpADNSite\Core;

use Guzzle\Http\Client;

class GuzzleAPIClient implements APIClient {

	const DEFAULT_STREAM_FETCH_VARS = 'include_deleted=0&include_directed=0&include_annotations=1&include_html=0';

	private $client;
	private $username;

	private $userId = null;

	private function getIdFromUsername() {
		if (!$this->userId) {
			$userRequest = $this->client->get('users/@'.$this->username);
			$userResponse = $userRequest->send()->json();
			$this->userId = $userResponse['data']['id'];
		}
		return $this->userId;
	}

	public function configure(array $globalConfiguration, array $userConfiguration) {
		$this->client = new Client($globalConfiguration['uri']);
		$this->username = $userConfiguration['username'];
		$this->client->setDefaultHeaders(array('Authorization' => 'Bearer '.$userConfiguration['access_token']));
	}

	public function retrieveRecentPosts($count = 20) {
		return new PostPage($this->client
				->get('users/@'.$this->username.'/posts?count='.$count.'&'.self::DEFAULT_STREAM_FETCH_VARS)
				->send()->json());
	}

	public function retrieveSinglePost($id) {
		$request = $this->client->get('posts/'.$id.'?include_annotations=1&include_html=0&include_starred_by=1&include_reposters=1');
		$response = $request->send()->json();
		$post = new Post($response['data']);
		if ($response['data']['user']['username']!=$this->username) $post->setVisible(false);
		return $post;
	}

	public function retrievePostThread($id) {
		$response = $this->client
				->get('posts/'.$id.'/replies?include_deleted=0&include_annotations=1&include_html=0&include_starred_by=1&include_reposters=1')
				->send()->json();
		if ($response['data'][0]['user']['username']!=$this->username) return null;
		return new PostPage($response);
	}<commit_msg>Bugfix for bugfix - permalinks shouldn't work for foreign posts<commit_after><?php

/*  phpADNSite
 Copyright (C) 2014-2015 <NAME>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace PhpADNSite\Core;

use Guzzle\Http\Client;

class GuzzleAPIClient implements APIClient {

	const DEFAULT_STREAM_FETCH_VARS = 'include_deleted=0&include_directed=0&include_annotations=1&include_html=0';

	private $client;
	private $username;

	private $userId = null;

	private function getIdFromUsername() {
		if (!$this->userId) {
			$userRequest = $this->client->get('users/@'.$this->username);
			$userResponse = $userRequest->send()->json();
			$this->userId = $userResponse['data']['id'];
		}
		return $this->userId;
	}

	public function configure(array $globalConfiguration, array $userConfiguration) {
		$this->client = new Client($globalConfiguration['uri']);
		$this->username = $userConfiguration['username'];
		$this->client->setDefaultHeaders(array('Authorization' => 'Bearer '.$userConfiguration['access_token']));
	}

	public function retrieveRecentPosts($count = 20) {
		return new PostPage($this->client
				->get('users/@'.$this->username.'/posts?count='.$count.'&'.self::DEFAULT_STREAM_FETCH_VARS)
				->send()->json());
	}

	public function retrieveSinglePost($id) {
		$request = $this->client->get('posts/'.$id.'?include_annotations=1&include_html=0&include_starred_by=1&include_reposters=1');
		$response = $request->send()->json();
		$post = new Post($response['data']);
		if ($response['data']['user']['username']!=$this->username) $post->setVisible(false);
		return $post;
	}

	public function retrievePostThread($id) {
		$response = $this->client
				->get('posts/'.$id.'/replies?include_deleted=0&include_annotations=1&include_html=0&include_starred_by=1&include_reposters=1')
				->send()->json();
		foreach ($response['data'] as $post) {
			if ($post['id']==$id && $post['user']['username']!=$this->username) return null;
		}
		return new PostPage($response);
	}
{'fraction_non_alphanumeric': 0.14975173783515391, 'fraction_numerical': 0.009731876861966237, 'mean_word_length': 5.608923884514436, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1448805', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1509, 'n_words': 395}