<commit_before><?php
/**
 * Facebook integration
 */

namespace Minds\Core\ThirdPartyNetworks;

use Minds\Core;
use Minds\Core\Data;
use Facebook\Facebook as FacebookSDK;

class Facebook implements NetworkInterface
{

    private $db;
    private $fb;
    private $credentials = [];

    private $data = [];

    public function __construct($db = NULL, $fb = NULL)
    {
        $this->db = $db ?: new Data\Call('entities_by_time');
        $this->fb = $fb ?: new FacebookSDK([
          'app_id' => Core\Config::_()->facebook['app_id'],
          'app_secret' => Core\Config::_()->facebook['app_secret'],
          'default_graph_version' => 'v2.2',
        ]);
    }

    /**
     * Set and save the api credentials
     * @param array $credentials
     * @return $this
     */
    public function setApiCredentials($credentials = [])
    {
        $data = [
          'facebook:uuid' => $credentials['uuid'],
          'facebook:access_token' => $credentials['access_token'],
        ];
        $this->db->insert(Core\Session::getLoggedInUser()->guid . ":thirdpartynetworks:credentials", $data);
        return $this;
    }

    /**
     * Drop facebook api credentials
     * @return $this
     */
    public function dropApiCredentials()
    {
        $this->db->removeAttributes(Core\Session::getLoggedInUser()->guid . ":thirdpartynetworks:credentials", ['facebook:uuid', 'facebook:access_token']);
        return $this;
    }

    /**
     * Return api credentials
     * @return array
     */
    public function getApiCredentials()
    {
        $data = $this->db->getRow(Core\Session::getLoggedInUser()->guid . ":thirdpartynetworks:credentials");
        if(isset($data['facebook:uuid']))
            $this->credentials['uuid'] = $data['facebook:uuid'];
        if(isset($data['facebook:access_token']))
            $this->credentials['access_token'] = $data['facebook:access_token'];
        return $this;
    }

    public function getFb()
    {
        return $this->fb;
    }

    /**
     * Create a post
     * @param object $entity
     * @return $this
     */
    public function post($entity)
    {
        $this->data = array_merge($this->data, [
          'message' => $entity->message,
          'link' => $entity->getUrl(),
          'name' => $entity->title,
          'description' => "blurb"
        ]);

        if($entity->thumbnail_src){
            $this->data['picture'] = $entity->thumbnail_src;
        }

        $this->fb->post("/{$this->credentials['uuid']}/feed", $this->data, $this->credentials['access_token']);
    }

    /**
     * Schedule a post
     * @param int $timestamp
     * @return $this<commit_msg>(fix): respect the post type and construct for facebook<commit_after><?php
/**
 * Facebook integration
 */

namespace Minds\Core\ThirdPartyNetworks;

use Minds\Core;
use Minds\Core\Data;
use Facebook\Facebook as FacebookSDK;

class Facebook implements NetworkInterface
{

    private $db;
    private $fb;
    private $credentials = [];

    private $data = [];

    public function __construct($db = NULL, $fb = NULL)
    {
        $this->db = $db ?: new Data\Call('entities_by_time');
        $this->fb = $fb ?: new FacebookSDK([
          'app_id' => Core\Config::_()->facebook['app_id'],
          'app_secret' => Core\Config::_()->facebook['app_secret'],
          'default_graph_version' => 'v2.2',
        ]);
    }

    /**
     * Set and save the api credentials
     * @param array $credentials
     * @return $this
     */
    public function setApiCredentials($credentials = [])
    {
        $data = [
          'facebook:uuid' => $credentials['uuid'],
          'facebook:access_token' => $credentials['access_token'],
        ];
        $this->db->insert(Core\Session::getLoggedInUser()->guid . ":thirdpartynetworks:credentials", $data);
        return $this;
    }

    /**
     * Drop facebook api credentials
     * @return $this
     */
    public function dropApiCredentials()
    {
        $this->db->removeAttributes(Core\Session::getLoggedInUser()->guid . ":thirdpartynetworks:credentials", ['facebook:uuid', 'facebook:access_token']);
        return $this;
    }

    /**
     * Return api credentials
     * @return array
     */
    public function getApiCredentials()
    {
        $data = $this->db->getRow(Core\Session::getLoggedInUser()->guid . ":thirdpartynetworks:credentials");
        if(isset($data['facebook:uuid']))
            $this->credentials['uuid'] = $data['facebook:uuid'];
        if(isset($data['facebook:access_token']))
            $this->credentials['access_token'] = $data['facebook:access_token'];
        return $this;
    }

    public function getFb()
    {
        return $this->fb;
    }

    /**
     * Create a post
     * @param object $entity
     * @return $this
     */
    public function post($entity)
    {
        $this->data['message'] = $entity->message;

        if($entity->perma_url){
            $this->data = array_merge($this->data, [
              'link' => $entity->perma_url,
              'name' => $entity->title,
              'description' => "blurb",
              'picture' => $entity->thumbnail_src
            ]);
        }

        //Custom image posts
        if(($entity->thumbnail_src && !$entity->perma_url) || $entity->custom_type == 'batch'){
            $this->data['url'] = $entity->thumbnail_src;

            if($entity->custom_type == 'batch'){
                $this->data['url'] = $entity->custom_data[0]['src'];
            }

            $this->fb->post("/{$this->credentials['uuid']}/photos", $this->data, $this->credentials['access_token']);
            return true;
        }

        //Custom video posts
        if($entity->custom_type == 'video'){
            $this->title = $entity->title;
            $this->data['file_url'] = Core\Config::_()->site_url "/api/v1/archive/{$entity->custom_data['guid']}/play";
            $this->fb->post("/{$this->credentials['uuid']}/videos", $this->data, $this->credentials['access_token']);
            return true;
        }

        $this->fb->post("/{$this->credentials['uuid']}/feed", $this->data, $this->credentials['access_token']);
        exit;
    }

    /**
     * Schedule a post
     * @param int $timestamp
     * @return $this
{'fraction_non_alphanumeric': 0.17292268206608918, 'fraction_numerical': 0.0009624639076034649, 'mean_word_length': 2.4277075316107752, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1867015', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1845, 'n_words': 327}