Skip to content

Commit

Permalink
Merge branch 'feat/ext-boost-views-2737' into 'master'
Browse files Browse the repository at this point in the history
Support for external boost views and returning posthog from the configs query (gql) #2737

See merge request minds/engine!1688
  • Loading branch information
markharding committed Jun 18, 2024
2 parents bfd4f35 + 5abb341 commit bed2fb2
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 10 deletions.
17 changes: 10 additions & 7 deletions Controllers/api/v2/analytics/views.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Minds\Controllers\api\v2\analytics;

use Minds\Api\Factory;
use Minds\Common\IpAddress;
use Minds\Common\Urn;
use Minds\Core;
use Minds\Core\Di\Di;
Expand All @@ -27,17 +28,16 @@ public function post($pages)

switch ($pages[0]) {
case 'boost':
if (!Session::getLoggedinUser()) {
throw new ForbiddenException();
}
$isLoggedIn = Session::getLoggedinUser();
$identifier = $isLoggedIn ? Session::getLoggedInUserGuid() : (new IpAddress)->get();

$keyValueLimiter = Di::_()->get('Security\RateLimits\KeyValueLimiter');
$config = Di::_()->get('Config');
$entityResolver = new Resolver();

$keyValueLimiter
->setKey('boost-view')
->setValue(md5(Session::getLoggedInUserGuid() . ":" . $pages[1]))
->setValue(md5($identifier . ":" . $pages[1]))
->setSeconds($config->get('boost_view_rate_limit') ?? 5)
->setMax(1)
->checkAndIncrement();
Expand All @@ -60,8 +60,10 @@ public function post($pages)
]);
}

Counters::increment($boost->getEntity()->guid, "impression");
Counters::increment($boost->getEntity()->owner_guid, "impression");
if ($isLoggedIn) {
Counters::increment($boost->getEntity()->guid, "impression");
Counters::increment($boost->getEntity()->owner_guid, "impression");
}

try {
if (!$boost->getEntity()) {
Expand All @@ -74,7 +76,8 @@ public function post($pages)
view: (new Core\Analytics\Views\View())
->setEntityUrn($boost->getEntity()->getUrn())
->setOwnerGuid((string) $boost->getEntity()->getOwnerGuid())
->setClientMeta($_POST['client_meta'] ?? []),
->setClientMeta($_POST['client_meta'] ?? [])
->setExternal($isLoggedIn === false || ($_GET['external'] ?? false)),
entity: $boost->getEntity()
);
} catch (\Exception $e) {
Expand Down
1 change: 1 addition & 0 deletions Core/Analytics/Views/Delegates/ViewsDelegate.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function onRecordView(View $view, EntityInterface $entity): void
$viewEvent->cmDelta = $view->getDelta();
$viewEvent->cmPosition = $view->getPosition();
$viewEvent->cmServedByGuid = $view->getServedByGuid();
$viewEvent->external = $view->isExternal();

$viewEvent->viewUUID = $view->getUuid();

Expand Down
8 changes: 5 additions & 3 deletions Core/Analytics/Views/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public function getList(array $opts = [])
->setMedium($row['medium'])
->setCampaign($row['campaign'])
->setDelta((int) $row['delta'])
->setTimestamp($row['uuid']->time());
->setTimestamp($row['uuid']->time())
->setExternal($row['external'] ?? false);

$response[] = $view;
}
Expand All @@ -139,8 +140,8 @@ public function add(View $view)
$timestamp = $view->getTimestamp() ?: time();
$date = new DateTime("@{$timestamp}", new DateTimeZone('utc'));

$cql = "INSERT INTO views (year, month, day, uuid, entity_urn, owner_guid, page_token, position, platform, source, medium, campaign, delta, tenant_id)
VALUES (?, ?, ?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$cql = "INSERT INTO views (year, month, day, uuid, entity_urn, owner_guid, page_token, position, platform, source, medium, campaign, delta, tenant_id, external)
VALUES (?, ?, ?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$values = [
(int) ($view->getYear() ?? $date->format('Y')),
new Tinyint((int) ($view->getMonth() ?? $date->format('m'))),
Expand All @@ -155,6 +156,7 @@ public function add(View $view)
$view->getCampaign() ?: '',
(int) ($view->getDelta() ?? 0),
$this->config->get('tenant_id') ?: -1,
$view->isExternal(),
];

$prepared = new Custom();
Expand Down
4 changes: 4 additions & 0 deletions Core/Analytics/Views/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
* @method string|null getServedByGuid()
* @method View setTimestamp(int $timestamp)
* @method int getTimestamp()
* @method View setExternal(bool $external)
* @method bool isExternal()
*/
class View
{
Expand Down Expand Up @@ -93,6 +95,8 @@ class View
private ?string $salt = null;
private ?string $servedByGuid = null;

protected bool $external = false;

/**
* @param array $clientMeta
* @return $this
Expand Down
3 changes: 3 additions & 0 deletions Core/Config/GraphQL/Controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function getConfig(string $key): ?string
'site_url',
'theme_override.color_scheme',
'theme_override.primary_color',
'posthog.project_id',
'posthog.api_key',
'posthog.host',
];

if (!in_array($key, $allowedKeys, true)) {
Expand Down
1 change: 1 addition & 0 deletions Core/EventStreams/Events/ViewEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ class ViewEvent implements EventInterface
use ClientMetaEventTrait;

public string $viewUUID;
public bool $external = false;
}
5 changes: 5 additions & 0 deletions Core/EventStreams/Topics/ViewsTopic.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private function createMessage(ViewEvent $event): Message
'cm_position' => $event->cmPosition,
'cm_served_by_guid' => $event->cmServedByGuid,
'view_uuid' => $event->viewUUID,
'external' => $event->external,
'tenant_id' => $this->config->get('tenant_id') ?: -1,
])
)
Expand Down Expand Up @@ -201,6 +202,10 @@ private function getSchema(): string
'name' => 'view_uuid',
'type' => 'string'
],
[
'name' => 'external',
'type' => 'boolean'
],
[
'name' => 'tenant_id',
'type' => 'int'
Expand Down
2 changes: 2 additions & 0 deletions Core/MultiTenant/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Minds\Core\MultiTenant\Controllers\TenantPsrController;
use Minds\Core\Router\Enums\ApiScopeEnum;
use Minds\Core\Router\Middleware\AdminMiddleware;
use Minds\Core\Router\Middleware\LoggedInMiddleware;
use Minds\Core\Router\Middleware\NotMultiTenantMiddleware;
use Minds\Core\Router\ModuleRoutes;
use Minds\Core\Router\Route;
Expand All @@ -25,6 +26,7 @@ public function register(): void
$route
->withMiddleware([
NotMultiTenantMiddleware::class,
LoggedInMiddleware::class,
])
->withScope(ApiScopeEnum::TENANT_CREATE_TRIAL)
->post(
Expand Down
2 changes: 2 additions & 0 deletions Spec/Core/Analytics/Views/RepositorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function it_should_add(View $view)
$view->getMedium()->shouldBeCalled()->willReturn('test');
$view->getCampaign()->shouldBeCalled()->willReturn('urn:phpspec:234234');
$view->getDelta()->shouldBeCalled()->willReturn(100);
$view->isExternal()->shouldBeCalled()->willReturn(false);

$this->db->request(Argument::that(function (Custom $prepared) {
$statement = $prepared->build();
Expand Down Expand Up @@ -88,6 +89,7 @@ public function it_should_add_with_a_timestamp(View $view)
$view->getMedium()->shouldBeCalled()->willReturn('test');
$view->getCampaign()->shouldBeCalled()->willReturn('urn:phpspec:234234');
$view->getDelta()->shouldBeCalled()->willReturn(100);
$view->isExternal()->shouldBeCalled()->willReturn(false);

$this->db->request(Argument::that(function (Custom $prepared) use ($now) {
$statement = $prepared->build();
Expand Down

0 comments on commit bed2fb2

Please sign in to comment.