<commit_before><?php

namespace App\Jobs;

use App\Note;
use GuzzleHttp\Client;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendWebMentions extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $note;
    protected $guzzle;

    /**
     * Create the job instance, inject dependencies.
     *
     * @param  User $user
     * @param  Note $note
     * @return void
     */
    public function __construct(Note $note, Client $guzzle)
    {
        $this->note = $note;
        $this->guzzle = $guzzle ?? new Client();
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Note $note)
    {
        //grab the URLs
        $urlsInReplyTo = explode(' ', $this->note->in_reply_to);
        $urlsNote = $this->getLinks($this->note->note);
        $urls = array_filter(array_merge($urlsInReplyTo, $urlsNote)); //filter out none URLs
        foreach ($urls as $url) {
            $endpoint = $this->discoverWebmentionEndpoint($url);
            if ($endpoint) {
                $this->guzzle->post($endpoint, [
                    'form_params' => [
                        'source' => $this->note->longurl,
                        'target' => $url
                    ]
                ])
            }
        }
    }

    /**
     * Discover if a URL has a webmention endpoint.
     *
     * @param  string  The URL
     * @param  \GuzzleHttp\Client $client
     * @return string  The webmention endpoint URL
     */
    private function discoverWebmentionEndpoint($url)
    {
        $endpoint = null;

        $response = $this->guzzle->get($url);
        //check HTTP Headers for webmention endpoint
        $links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link'));
        foreach ($links as $link) {
            if ($link['rel'] == 'webmention') {
                return trim($link[0], '<>');
            }
        }

        //failed to find a header so parse HTML
        $html = (string) $response->getBody();

        $mf2 = new \Mf2\Parser($html, $url);
        $rels = $mf2->parseRelsAndAlternates();
        if (array_key_exists('webmention', $rels[0])) {<commit_msg>Don’t send webmentions to onseself<commit_after><?php

namespace App\Jobs;

use App\Note;
use GuzzleHttp\Client;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendWebMentions extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $note;
    protected $guzzle;

    /**
     * Create the job instance, inject dependencies.
     *
     * @param  User $user
     * @param  Note $note
     * @return void
     */
    public function __construct(Note $note, Client $guzzle)
    {
        $this->note = $note;
        $this->guzzle = $guzzle ?? new Client();
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Note $note)
    {
        //grab the URLs
        $urlsInReplyTo = explode(' ', $this->note->in_reply_to);
        $urlsNote = $this->getLinks($this->note->note);
        $urls = array_filter(array_merge($urlsInReplyTo, $urlsNote)); //filter out none URLs
        foreach ($urls as $url) {
            $endpoint = $this->discoverWebmentionEndpoint($url);
            if ($endpoint) {
                $this->guzzle->post($endpoint, [
                    'form_params' => [
                        'source' => $this->note->longurl,
                        'target' => $url
                    ]
                ])
            }
        }
    }

    /**
     * Discover if a URL has a webmention endpoint.
     *
     * @param  string  The URL
     * @param  \GuzzleHttp\Client $client
     * @return string  The webmention endpoint URL
     */
    private function discoverWebmentionEndpoint($url)
    {
        //let’s not send webmentions to myself
        if (parse_url($url, PHP_URL_HOST) == env('LONG_URL', 'localhost')) {
            return false;
        }

        $endpoint = null;

        $response = $this->guzzle->get($url);
        //check HTTP Headers for webmention endpoint
        $links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link'));
        foreach ($links as $link) {
            if ($link['rel'] == 'webmention') {
                return trim($link[0], '<>');
            }
        }

        //failed to find a header so parse HTML
        $html = (string) $response->getBody();

        $mf2 = new \Mf2\Parser($html, $url);
        $rels = $mf2->parseRelsAndAlternates();
        if (array_key_exists('webmention', $rels[0])) {
{'fraction_non_alphanumeric': 0.13115449915110355, 'fraction_numerical': 0.0025466893039049238, 'mean_word_length': 1.9753787878787878, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '490639', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1396, 'n_words': 327}