<?php
 class vB_Systemevent_Item_Push_Privatemessage extends vB_Systemevent_Item { protected int $nodeid = 0; protected array $recipientids = []; protected array $serializefields = ['nodeid', 'recipientids',]; public static function create(int $pmnodeid, array $recipientids) : self { $item = new self(); $item->nodeid = $pmnodeid; $item->recipientids = $recipientids; return $item; } public function run() : void { $datastore = vB::getDatastore(); if (!$datastore->getOption('enable_push_notifications')) { return; } $pushLib = vB_Library::instance('pushnotification'); $nodeLib = vB_Library::instance('node'); $content = $nodeLib->getFullContentforNodes([$this->nodeid => $this->nodeid]); $content = $content[$this->nodeid] ?? []; if (!$content) { return; } $body = vB_String::stripBbcode($content['content']['rawtext']); $title = $pushLib->prepareStringForWebPush($content['content']['startertitle'] ?? ''); $author = $pushLib->prepareStringForWebPush($content['content']['authorname']); $body = $pushLib->prepareStringForWebPush($body); $filteredRecipients = $pushLib->filterUsersByOptIn($this->recipientids, 'privatemessage'); if (count($filteredRecipients)) { $message = [ 'title' => $title, 'author' => $author, 'body' => $author . ': ' . $body, 'url' => $this->getPMUrl($content, false), 'type' => vB_Library_FCMessaging::CLICK_ACTION_PRIVATEMESSAGE, 'avatarurl' => $this->getAvatarAbsoluteUrl($content), 'nodeid' => $content['nodeid'], 'starter' => $content['starter'], ]; $messageOverrides = []; foreach ($filteredRecipients AS $__userid) { if ($this->userCanUsePmChat($__userid)) { $messageOverrides[$__userid] = [ 'url' => $this->getPMUrl($content, true), ]; } } $pushLib->sendPushNotificationToUsers($filteredRecipients, $message, $messageOverrides); } } private function getAvatarAbsoluteUrl(array $content) : string { $url = $content['content']['avatar']['avatarpath'] ?? ''; if (!$url) { return ''; } $frontendurl = vB::getDatastore()->getOption('frontendurl'); if (preg_match('#^data|https?#', $url)) { return $url; } else { return $frontendurl . '/' . ltrim($url, '/'); } } private function getPMUrl(array $nodeContent, bool $pmchat) : string { if ($pmchat) { return vB5_Route::buildUrl('pmchat|fullurl', [], ['messageid' => $nodeContent['starter']]); } else { $urlInfo = [ 'action' => 'view', 'messageid' => $nodeContent['starter'], 'innerPost' => $nodeContent['nodeid'], ]; return vB5_Route::buildUrl('privatemessage|fullurl', $urlInfo); } } private function userCanUsePmChat(int $userid) : bool { if (!$userid) { return false; } $vboptions = vB::getDatastore()->getValue('options'); $systemOnline = ($vboptions['pmchat_enabled']); if (!$systemOnline) { return false; } $usercontext = vB::getUserContext($userid); $userGroupAuthorized = $usercontext->hasPermission('pmpermissions', 'canusepmchat'); if (!$userGroupAuthorized) { return false; } $userInfo = vB_User::fetchUserinfo($userid); if (!$userInfo['enable_pmchat']) { return false; } return true; } } 