mirror of https://github.com/pixelfed/pixelfed
commit
6350b71cdd
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Profile;
|
||||
use App\Util\ActivityPub\Helpers;
|
||||
use App\Util\ActivityPub\HttpSignature;
|
||||
|
||||
class ActivityPubDeliveryService {
|
||||
|
||||
public $sender;
|
||||
public $to;
|
||||
public $payload;
|
||||
|
||||
public static function queue()
|
||||
{
|
||||
return new self;
|
||||
}
|
||||
|
||||
public function from($profile)
|
||||
{
|
||||
$this->sender = $profile;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function to(string $url)
|
||||
{
|
||||
$this->to = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function payload($payload)
|
||||
{
|
||||
$this->payload = $payload;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function send()
|
||||
{
|
||||
return $this->queueDelivery();
|
||||
}
|
||||
|
||||
protected function queueDelivery()
|
||||
{
|
||||
abort_if(!$this->sender || !$this->to || !$this->payload, 400);
|
||||
abort_if(!Helpers::validateUrl($this->to), 400);
|
||||
abort_if($this->sender->domain != null || $this->sender->status != null, 400);
|
||||
|
||||
$body = $this->payload;
|
||||
$payload = json_encode($body);
|
||||
$headers = HttpSignature::sign($this->sender, $this->to, $body);
|
||||
|
||||
$ch = curl_init($this->to);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||
curl_setopt($ch, CURLOPT_HEADER, true);
|
||||
curl_exec($ch);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Lexer;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use App\Util\Lexer\Autolink;
|
||||
use App\Util\Lexer\Extractor;
|
||||
|
||||
class UsernameTest extends TestCase
|
||||
{
|
||||
/** @test **/
|
||||
public function genericUsername()
|
||||
{
|
||||
$username = '@dansup';
|
||||
$entities = Extractor::create()->extract($username);
|
||||
$autolink = Autolink::create()->autolink($username);
|
||||
$expectedAutolink = '<a class="u-url mention" href="https://pixelfed.dev/dansup" rel="external nofollow noopener" target="_blank">@dansup</a>';
|
||||
$expectedEntity = [
|
||||
"hashtags" => [],
|
||||
"urls" => [],
|
||||
"mentions" => [
|
||||
"dansup",
|
||||
],
|
||||
"replyto" => "dansup",
|
||||
"hashtags_with_indices" => [],
|
||||
"urls_with_indices" => [],
|
||||
"mentions_with_indices" => [
|
||||
[
|
||||
"screen_name" => "dansup",
|
||||
"indices" => [
|
||||
0,
|
||||
7,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->assertEquals($expectedAutolink, $autolink);
|
||||
$this->assertEquals($expectedEntity, $entities);
|
||||
}
|
||||
|
||||
/** @test **/
|
||||
public function usernameWithPeriod()
|
||||
{
|
||||
$username = '@dansup.two';
|
||||
$autolink = Autolink::create()->autolink($username);
|
||||
$entities = Extractor::create()->extract($username);
|
||||
$expectedAutolink = '<a class="u-url mention" href="https://pixelfed.dev/dansup.two" rel="external nofollow noopener" target="_blank">@dansup.two</a>';
|
||||
$expectedEntity = [
|
||||
"hashtags" => [],
|
||||
"urls" => [],
|
||||
"mentions" => [
|
||||
"dansup.two",
|
||||
],
|
||||
"replyto" => "dansup.two",
|
||||
"hashtags_with_indices" => [],
|
||||
"urls_with_indices" => [],
|
||||
"mentions_with_indices" => [
|
||||
[
|
||||
"screen_name" => "dansup.two",
|
||||
"indices" => [
|
||||
0,
|
||||
11,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->assertEquals($expectedAutolink, $autolink);
|
||||
$this->assertEquals($expectedEntity, $entities);
|
||||
}
|
||||
|
||||
/** @test **/
|
||||
public function usernameWithDash()
|
||||
{
|
||||
$username = '@dansup-too';
|
||||
$autolink = Autolink::create()->autolink($username);
|
||||
$entities = Extractor::create()->extract($username);
|
||||
$expectedAutolink = '<a class="u-url mention" href="https://pixelfed.dev/dansup-too" rel="external nofollow noopener" target="_blank">@dansup-too</a>';
|
||||
$expectedEntity = [
|
||||
"hashtags" => [],
|
||||
"urls" => [],
|
||||
"mentions" => [
|
||||
"dansup-too",
|
||||
],
|
||||
"replyto" => "dansup-too",
|
||||
"hashtags_with_indices" => [],
|
||||
"urls_with_indices" => [],
|
||||
"mentions_with_indices" => [
|
||||
[
|
||||
"screen_name" => "dansup-too",
|
||||
"indices" => [
|
||||
0,
|
||||
11,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->assertEquals($expectedAutolink, $autolink);
|
||||
$this->assertEquals($expectedEntity, $entities);
|
||||
}
|
||||
|
||||
/** @test **/
|
||||
public function usernameWithUnderscore()
|
||||
{
|
||||
$username = '@dansup_too';
|
||||
$autolink = Autolink::create()->autolink($username);
|
||||
$entities = Extractor::create()->extract($username);
|
||||
$expectedAutolink = '<a class="u-url mention" href="https://pixelfed.dev/dansup_too" rel="external nofollow noopener" target="_blank">@dansup_too</a>';
|
||||
$expectedEntity = [
|
||||
"hashtags" => [],
|
||||
"urls" => [],
|
||||
"mentions" => [
|
||||
"dansup_too",
|
||||
],
|
||||
"replyto" => "dansup_too",
|
||||
"hashtags_with_indices" => [],
|
||||
"urls_with_indices" => [],
|
||||
"mentions_with_indices" => [
|
||||
[
|
||||
"screen_name" => "dansup_too",
|
||||
"indices" => [
|
||||
0,
|
||||
11,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->assertEquals($expectedAutolink, $autolink);
|
||||
$this->assertEquals($expectedEntity, $entities);
|
||||
}
|
||||
|
||||
/** @test **/
|
||||
public function multipleMentions()
|
||||
{
|
||||
$text = 'hello @dansup and @pixelfed.team from @username_underscore';
|
||||
$autolink = Autolink::create()->autolink($text);
|
||||
$entities = Extractor::create()->extract($text);
|
||||
$expectedAutolink = 'hello <a class="u-url mention" href="https://pixelfed.dev/dansup" rel="external nofollow noopener" target="_blank">@dansup</a> and <a class="u-url mention" href="https://pixelfed.dev/pixelfed.team" rel="external nofollow noopener" target="_blank">@pixelfed.team</a> from <a class="u-url mention" href="https://pixelfed.dev/username_underscore" rel="external nofollow noopener" target="_blank">@username_underscore</a>';
|
||||
$expectedEntity = [
|
||||
"hashtags" => [],
|
||||
"urls" => [],
|
||||
"mentions" => [
|
||||
"dansup",
|
||||
"pixelfed.team",
|
||||
"username_underscore",
|
||||
],
|
||||
"replyto" => null,
|
||||
"hashtags_with_indices" => [],
|
||||
"urls_with_indices" => [],
|
||||
"mentions_with_indices" => [
|
||||
[
|
||||
"screen_name" => "dansup",
|
||||
"indices" => [
|
||||
6,
|
||||
13,
|
||||
],
|
||||
],
|
||||
[
|
||||
"screen_name" => "pixelfed.team",
|
||||
"indices" => [
|
||||
18,
|
||||
32,
|
||||
],
|
||||
],
|
||||
[
|
||||
"screen_name" => "username_underscore",
|
||||
"indices" => [
|
||||
38,
|
||||
58,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertEquals($expectedAutolink, $autolink);
|
||||
$this->assertEquals($expectedEntity, $entities);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue