Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ parameters:
env(APP_POWERED_BY_PHPLIST): '0'
app.preference_page_show_private_lists: '%%env(PREFERENCEPAGE_SHOW_PRIVATE_LISTS)%%'
env(PREFERENCEPAGE_SHOW_PRIVATE_LISTS): '0'
app.rest_api_domain: '%%env(REST_API_DOMAIN)%%'
env(REST_API_DOMAIN): 'example.com'
app.rest_api_base_url: '%%env(REST_API_BASE_URL)%%'
env(REST_API_BASE_URL): 'https://example.com/api/v2'

# Email configuration
app.mailer_from: '%%env(MAILER_FROM)%%'
Expand Down
4 changes: 4 additions & 0 deletions config/services/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ services:
autoconfigure: true
public: true

PhpList\Core\Domain\Analytics\Service\UserMessageService:
autowire: true
autoconfigure: true

PhpList\Core\Domain\Messaging\Service\SendRateLimiter:
autowire: true
autoconfigure: true
Expand Down
5 changes: 5 additions & 0 deletions src/Domain/Analytics/Model/UserMessageView.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public function setViewed(?DateTime $viewed): self
return $this;
}

public function setViewedNow(): self
{
return $this->setViewed(new DateTime());
}

public function setIp(?string $ip): self
{
$this->ip = $ip;
Expand Down
56 changes: 56 additions & 0 deletions src/Domain/Analytics/Service/UserMessageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace PhpList\Core\Domain\Analytics\Service;

use Doctrine\ORM\EntityManagerInterface;
use PhpList\Core\Domain\Analytics\Model\UserMessageView;
use PhpList\Core\Domain\Messaging\Repository\MessageRepository;
use PhpList\Core\Domain\Messaging\Repository\UserMessageRepository;
use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository;

class UserMessageService
{
public function __construct(
private readonly UserMessageRepository $userMessageRepository,
private readonly SubscriberRepository $subscriberRepository,
private readonly MessageRepository $messageRepository,
private readonly EntityManagerInterface $entityManager
) {
}

public function trackUserMessageView(string $uid, int $messageId, array $metadata): void
{
$subscriber = $this->subscriberRepository->findOneByUniqueId($uid);
$message = $this->messageRepository->findById($messageId);

if ($subscriber === null || $message === null) {
return;
}

$userMessage = $this->userMessageRepository->findByUserAndMessage($subscriber, $message);
if ($userMessage === null) {
return;
}

$userMessage->setViewedNow();
$message->getMetadata()->incrementViews();

$data = [];
foreach (['HTTP_USER_AGENT', 'HTTP_REFERER'] as $key) {
if (isset($metadata[$key])) {
$data[$key] = htmlspecialchars(strip_tags($metadata[$key]));
}
}

$userMessageView = new UserMessageView();
$userMessageView->setUserId($subscriber->getId());
$userMessageView->setMessageId($messageId);
$userMessageView->setViewedNow();
$userMessageView->setIp($metadata['client_ip'] ?? null);
$userMessageView->setData(serialize($data));

$this->entityManager->persist($userMessageView);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class UserTrackValueResolver implements PlaceholderValueResolverInterface
{
public function __construct(
private readonly ConfigProvider $config,
#[Autowire('%rest_api_domain%')] private readonly string $restApiDomain,
#[Autowire('%app.rest_api_base_url%')] private readonly string $restApiBaseUrl,
) {
}

Expand All @@ -24,7 +24,7 @@ public function name(): string

public function __invoke(PlaceholderContext $ctx): string
{
$base = $this->config->getValue(ConfigOption::Domain) ?? $this->restApiDomain;
$base = $this->config->getValue(ConfigOption::Domain) ?? $this->restApiBaseUrl;

if ($ctx->isText() || empty($base)) {
return '';
Expand All @@ -33,7 +33,7 @@ public function __invoke(PlaceholderContext $ctx): string

return '<img src="'
. $base
. '/ut.php?u='
. '/t/open.gif?u='
. $uid
. '&amp;m='
. $ctx->messageId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private function processSubscribersForCampaign(Message $campaign, array $subscri
break;
}

$existing = $this->userMessageRepository->findOneByUserAndMessage($subscriber, $campaign);
$existing = $this->userMessageRepository->findByUserAndMessage($subscriber, $campaign);
if ($existing && $existing->getStatus() !== UserMessageStatus::Todo) {
continue;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Domain/Messaging/Model/Message/MessageMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public function setViews(int $viewed): self
return $this;
}

public function incrementViews(): self
{
$this->viewed += 1;

return $this;
}

public function getViews(): int
{
return $this->viewed;
Expand Down
10 changes: 10 additions & 0 deletions src/Domain/Messaging/Model/UserMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,14 @@ public function setStatus(?UserMessageStatus $status): self
$this->status = $status->value;
return $this;
}

public function isViewed(): bool
{
return $this->viewed !== null;
}

public function setViewedNow(): self
{
return $this->setViewed(new DateTime());
}
}
10 changes: 10 additions & 0 deletions src/Domain/Messaging/Repository/MessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function findCampaignsWithoutUuid(): array
->getResult();
}

/** @return Message[] */
public function getByOwnerId(int $ownerId): array
{
return $this->createQueryBuilder('m')
Expand All @@ -36,6 +37,15 @@ public function getByOwnerId(int $ownerId): array
->getResult();
}

public function findById(int $id): ?Message
{
return $this->createQueryBuilder('m')
->where('m.id = :id')
->setParameter('id', $id)
->getQuery()
->getOneOrNullResult();
}

/** @return Message[] */
public function getFilteredAfterId(int $lastId, int $limit, ?FilterRequestInterface $filter = null): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Messaging/Repository/UserMessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class UserMessageRepository extends AbstractRepository
{
public function findOneByUserAndMessage(Subscriber $subscriber, Message $campaign): ?UserMessage
public function findByUserAndMessage(Subscriber $subscriber, Message $campaign): ?UserMessage
{
return $this->findOneBy(['user' => $subscriber, 'message' => $campaign]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HttpReceivedStampBuilder
{
public function __construct(
private readonly RequestStack $requestStack,
#[Autowire('%app.rest_api_domain%')] private readonly string $hostname,
#[Autowire('%app.rest_api_base_url%')] private readonly string $restApiBaseUrl,
) {
}

Expand Down Expand Up @@ -40,7 +40,7 @@ public function buildStamp(): ?string
$requestTime = $request->server->get('REQUEST_TIME') ?? time();
$date = (new DateTimeImmutable('@' . $requestTime))->format(\DATE_RFC2822);

return sprintf('from %s by %s with HTTP; %s', $from, $this->hostname, $date);
return sprintf('from %s by %s with HTTP; %s', $from, $this->restApiBaseUrl, $date);
}

private function getHostByAddr(string $ipAddress): ?string
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Messaging/Service/ForwardingGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function assertCanForward(string $uid, Message $campaign): Subscriber
if ($subscriber === null) {
throw new MessageNotReceivedException();
}
$receivedMessage = $this->userMessageRepository->findOneByUserAndMessage($subscriber, $campaign);
$receivedMessage = $this->userMessageRepository->findByUserAndMessage($subscriber, $campaign);

if ($receivedMessage === null) {
throw new MessageNotReceivedException();
Expand Down
Loading
Loading