mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
After tracking a visit, set its location in the request as attribute
This commit is contained in:
@@ -15,6 +15,7 @@ use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
|||||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
|
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
|
||||||
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
||||||
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||||
|
|
||||||
abstract class AbstractTrackingAction implements MiddlewareInterface, RequestMethodInterface
|
abstract class AbstractTrackingAction implements MiddlewareInterface, RequestMethodInterface
|
||||||
{
|
{
|
||||||
@@ -30,9 +31,12 @@ abstract class AbstractTrackingAction implements MiddlewareInterface, RequestMet
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$shortUrl = $this->urlResolver->resolveEnabledShortUrl($identifier);
|
$shortUrl = $this->urlResolver->resolveEnabledShortUrl($identifier);
|
||||||
$this->requestTracker->trackIfApplicable($shortUrl, $request);
|
$visit = $this->requestTracker->trackIfApplicable($shortUrl, $request);
|
||||||
|
|
||||||
return $this->createSuccessResp($shortUrl, $request);
|
return $this->createSuccessResp(
|
||||||
|
$shortUrl,
|
||||||
|
$request->withAttribute(Location::class, $visit?->getVisitLocation()),
|
||||||
|
);
|
||||||
} catch (ShortUrlNotFoundException) {
|
} catch (ShortUrlNotFoundException) {
|
||||||
return $this->createErrorResp($request, $handler);
|
return $this->createErrorResp($request, $handler);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\Core\Action;
|
namespace Shlinkio\Shlink\Core\Action;
|
||||||
|
|
||||||
use Fig\Http\Message\StatusCodeInterface;
|
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||||
@@ -13,7 +12,7 @@ use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
|
|||||||
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
||||||
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
||||||
|
|
||||||
class RedirectAction extends AbstractTrackingAction implements StatusCodeInterface
|
class RedirectAction extends AbstractTrackingAction
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
ShortUrlResolverInterface $urlResolver,
|
ShortUrlResolverInterface $urlResolver,
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use Shlinkio\Shlink\Core\Model\DeviceType;
|
|||||||
use Shlinkio\Shlink\Core\RedirectRule\Model\RedirectConditionType;
|
use Shlinkio\Shlink\Core\RedirectRule\Model\RedirectConditionType;
|
||||||
use Shlinkio\Shlink\Core\RedirectRule\Model\Validation\RedirectRulesInputFilter;
|
use Shlinkio\Shlink\Core\RedirectRule\Model\Validation\RedirectRulesInputFilter;
|
||||||
use Shlinkio\Shlink\Core\Util\IpAddressUtils;
|
use Shlinkio\Shlink\Core\Util\IpAddressUtils;
|
||||||
|
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
|
||||||
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||||
|
|
||||||
use function Shlinkio\Shlink\Core\acceptLanguageToLocales;
|
use function Shlinkio\Shlink\Core\acceptLanguageToLocales;
|
||||||
@@ -128,7 +129,8 @@ class RedirectCondition extends AbstractEntity implements JsonSerializable
|
|||||||
private function matchesGeolocationCountryCode(ServerRequestInterface $request): bool
|
private function matchesGeolocationCountryCode(ServerRequestInterface $request): bool
|
||||||
{
|
{
|
||||||
$geolocation = $request->getAttribute(Location::class);
|
$geolocation = $request->getAttribute(Location::class);
|
||||||
if (!($geolocation instanceof Location)) {
|
// TODO We should eventually rely on `Location` type only
|
||||||
|
if (! ($geolocation instanceof Location) && ! ($geolocation instanceof VisitLocation)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
|||||||
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
|
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
|
||||||
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
||||||
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
||||||
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||||
|
|
||||||
use function array_slice;
|
use function array_slice;
|
||||||
use function count;
|
use function count;
|
||||||
@@ -73,9 +74,13 @@ readonly class ExtraPathRedirectMiddleware implements MiddlewareInterface
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$shortUrl = $this->resolver->resolveEnabledShortUrl($identifier);
|
$shortUrl = $this->resolver->resolveEnabledShortUrl($identifier);
|
||||||
$this->requestTracker->trackIfApplicable($shortUrl, $request);
|
$visit = $this->requestTracker->trackIfApplicable($shortUrl, $request);
|
||||||
|
|
||||||
$longUrl = $this->redirectionBuilder->buildShortUrlRedirect($shortUrl, $request, $extraPath);
|
$longUrl = $this->redirectionBuilder->buildShortUrlRedirect(
|
||||||
|
$shortUrl,
|
||||||
|
$request->withAttribute(Location::class, $visit?->getVisitLocation()),
|
||||||
|
$extraPath,
|
||||||
|
);
|
||||||
return $this->redirectResponseHelper->buildRedirectResponse($longUrl);
|
return $this->redirectResponseHelper->buildRedirectResponse($longUrl);
|
||||||
} catch (ShortUrlNotFoundException) {
|
} catch (ShortUrlNotFoundException) {
|
||||||
if ($extraPath === null || ! $this->urlShortenerOptions->multiSegmentSlugsEnabled) {
|
if ($extraPath === null || ! $this->urlShortenerOptions->multiSegmentSlugsEnabled) {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ use PHPUnit\Framework\TestCase;
|
|||||||
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
|
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
|
||||||
use Shlinkio\Shlink\Core\Model\DeviceType;
|
use Shlinkio\Shlink\Core\Model\DeviceType;
|
||||||
use Shlinkio\Shlink\Core\RedirectRule\Entity\RedirectCondition;
|
use Shlinkio\Shlink\Core\RedirectRule\Entity\RedirectCondition;
|
||||||
|
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
|
||||||
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||||
|
|
||||||
use const ShlinkioTest\Shlink\ANDROID_USER_AGENT;
|
use const ShlinkioTest\Shlink\ANDROID_USER_AGENT;
|
||||||
@@ -98,7 +99,7 @@ class RedirectConditionTest extends TestCase
|
|||||||
|
|
||||||
#[Test, DataProvider('provideVisits')]
|
#[Test, DataProvider('provideVisits')]
|
||||||
public function matchesGeolocationCountryCode(
|
public function matchesGeolocationCountryCode(
|
||||||
Location|null $location,
|
Location|VisitLocation|null $location,
|
||||||
string $countryCodeToMatch,
|
string $countryCodeToMatch,
|
||||||
bool $expected,
|
bool $expected,
|
||||||
): void {
|
): void {
|
||||||
@@ -113,5 +114,15 @@ class RedirectConditionTest extends TestCase
|
|||||||
yield 'non-matching location' => [new Location(countryCode: 'ES'), 'US', false];
|
yield 'non-matching location' => [new Location(countryCode: 'ES'), 'US', false];
|
||||||
yield 'matching location' => [new Location(countryCode: 'US'), 'US', true];
|
yield 'matching location' => [new Location(countryCode: 'US'), 'US', true];
|
||||||
yield 'matching case-insensitive' => [new Location(countryCode: 'US'), 'us', true];
|
yield 'matching case-insensitive' => [new Location(countryCode: 'US'), 'us', true];
|
||||||
|
yield 'matching visit location' => [
|
||||||
|
VisitLocation::fromGeolocation(new Location(countryCode: 'US')),
|
||||||
|
'US',
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
yield 'matching visit case-insensitive' => [
|
||||||
|
VisitLocation::fromGeolocation(new Location(countryCode: 'es')),
|
||||||
|
'ES',
|
||||||
|
true,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user