1
0
mirror of https://github.com/ONLYOFFICE/onlyoffice-owncloud.git synced 2025-04-18 13:24:05 +03:00

feat: send notifications via email

This commit is contained in:
rivexe 2025-01-20 11:42:40 +03:00
parent f36d5c894b
commit 2f3cd5cc81
5 changed files with 329 additions and 2 deletions

View File

@ -228,7 +228,8 @@ class Application extends App {
$this->crypt,
$c->query("IManager"),
$c->query("Session"),
$c->query("ServerContainer")->getGroupManager()
$c->query("ServerContainer")->getGroupManager(),
$c->query("Mailer")
);
}
);

View File

@ -39,6 +39,7 @@ use OCP\IUser;
use OCP\IUserSession;
use OCP\IUserManager;
use OCP\IGroupManager;
use OCP\Mail\IMailer;
use OCP\Share\IManager;
use OCP\Share;
@ -47,6 +48,7 @@ use OCA\Files\Helper;
use OCA\Onlyoffice\AppConfig;
use OCA\Onlyoffice\Crypt;
use OCA\Onlyoffice\DocumentService;
use OCA\Onlyoffice\EmailManager;
use OCA\Onlyoffice\FileUtility;
use OCA\Onlyoffice\VersionManager;
use OCA\Onlyoffice\FileVersions;
@ -147,6 +149,20 @@ class EditorController extends Controller {
*/
private $avatarManager;
/**
* Mailer
*
* @var IMailer
*/
private $mailer;
/**
* Email manager
*
* @var EmailManager
*/
private $emailManager;
/**
* @param string $AppName - application name
* @param IRequest $request - request object
@ -161,6 +177,7 @@ class EditorController extends Controller {
* @param IManager $shareManager - Share manager
* @param ISession $session - Session
* @param IGroupManager $groupManager - Group manager
* @param IMailer $mailer - Mailer
*/
public function __construct(
$AppName,
@ -175,7 +192,8 @@ class EditorController extends Controller {
Crypt $crypt,
IManager $shareManager,
ISession $session,
IGroupManager $groupManager
IGroupManager $groupManager,
IMailer $mailer
) {
parent::__construct($AppName, $request);
@ -194,6 +212,7 @@ class EditorController extends Controller {
$this->fileUtility = new FileUtility($AppName, $trans, $logger, $config, $shareManager, $session);
$this->avatarManager = \OC::$server->getAvatarManager();
$this->emailManager = new EmailManager($AppName, $trans, $logger, $mailer, $userManager, $urlGenerator);
}
/**
@ -582,6 +601,9 @@ class EditorController extends Controller {
$notification->setUser($recipientId);
$notificationManager->notify($notification);
if ($this->config->getEmailNotifications()) {
$this->emailManager->notifyMentionEmail($userId, $recipientId, $file->getId(), $file->getName(), $anchor, $notification->getObjectId());
}
}
return ["message" => $this->trans->t("Notification sent successfully")];

View File

@ -28,10 +28,13 @@ use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\Mail\IMailer;
use OCP\IUserManager;
use OCA\Onlyoffice\AppConfig;
use OCA\Onlyoffice\Crypt;
use OCA\Onlyoffice\DocumentService;
use OCA\Onlyoffice\EmailManager;
/**
* Editors availability check background job
@ -87,6 +90,13 @@ class EditorsCheck extends TimedJob {
*/
private $groupManager;
/**
* Email manager
*
* @var EmailManager
*/
private $emailManager;
/**
* @param string $AppName - application name
* @param IURLGenerator $urlGenerator - url generator service
@ -114,6 +124,9 @@ class EditorsCheck extends TimedJob {
$this->crypt = $crypt;
$this->groupManager = $groupManager;
$this->setInterval($this->config->getEditorsCheckInterval());
$mailer = \OC::$server->getMailer();
$userManager = \OC::$server->getUserManager();
$this->emailManager = new EmailManager($AppName, $trans, $logger, $mailer, $userManager, $urlGenerator);
}
/**
@ -192,6 +205,9 @@ class EditorsCheck extends TimedJob {
foreach ($this->getUsersToNotify() as $uid) {
$notification->setUser($uid);
$notificationManager->notify($notification);
if ($this->config->getEmailNotifications()) {
$this->emailManager->notifyEditorsCheckEmail($uid);
}
}
}
}

238
lib/emailmanager.php Normal file
View File

@ -0,0 +1,238 @@
<?php
/**
* @author Ascensio System SIA <integration@onlyoffice.com>
*
* (c) Copyright Ascensio System SIA 2025
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace OCA\Onlyoffice;
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\IUserManager;
/**
* Email manager
*
* @package OCA\Onlyoffice
*/
class EmailManager {
/**
* Application name
*
* @var string
*/
private $appName;
/**
* l10n service
*
* @var IL10N
*/
private $trans;
/**
* Logger
*
* @var ILogger
*/
private $logger;
/**
* Mailer
*
* @var IMailer
*/
private $mailer;
/**
* User manager
*
* @var IUserManager
*/
private $userManager;
/**
* Url generator service
*
* @var IURLGenerator
*/
private $urlGenerator;
/**
* @param string $appName - application name
* @param IL10N $trans - l10n service
* @param ILogger $logger - logger
* @param IMailer $mailer - mailer
* @param IUserManager $userManager - user manager
* @param IURLGenerator $urlGenerator - URL generator
*/
public function __construct(
$appName,
IL10N $trans,
ILogger $logger,
IMailer $mailer,
IUserManager $userManager,
IURLGenerator $urlGenerator
) {
$this->appName = $appName;
$this->trans = $trans;
$this->logger = $logger;
$this->mailer = $mailer;
$this->userManager = $userManager;
$this->urlGenerator = $urlGenerator;
}
/**
* Send notification about mention via email
*
* @param string $notifierId - id of notifier user
* @param string $recipientId - id of recipient user
* @param string $fileId - file id
* @param string $fileName - file name
* @param string $anchor - anchor
* @param string $notificationObjectId - object of notification
* @return bool
*/
public function notifyMentionEmail(
string $notifierId,
string $recipientId,
string $fileId,
string $fileName,
string $anchor,
string $notificationObjectId
) {
$recipient = $this->userManager->get($recipientId);
if (empty($recipient)) {
$this->logger->error("recipient $recipientId is null", ["app" => $this->appName]);
return false;
}
$email = $recipient->getEMailAddress();
if (empty($email)) {
$this->logger->info("The mentioned recipient $recipientId does not have an email", ["app" => $this->appName]);
return false;
}
$recipientName = $recipient->getDisplayName();
$notifier = $this->userManager->get($notifierId);
if (empty($notifier)) {
$this->logger->error("notifier $notifierId is null", ["app" => $this->appName]);
return false;
}
$notifierName = $notifier->getDisplayName();
$editorLink = $this->urlGenerator->linkToRouteAbsolute($this->appName . ".editor.index", [
"fileId" => $fileId,
"anchor" => $anchor
]);
$subject = $this->trans->t("You were mentioned in the document");
$heading = $this->trans->t("%1\$s mentioned you in the document comment", [$notifierName]);
$bodyHtml = $this->trans->t(
"This is a mail message to notify that you have been mentioned by <b>%1\$s</b> in the comment to the <a href=\"%2\$s\">%3\$s</a>:<br>\"%4\$s\"",
[$notifierName, $editorLink, $fileName, $notificationObjectId]
);
$this->logger->debug($bodyHtml, ["app" => $this->appName]);
$button = [$this->trans->t("Open file"), $editorLink];
$template = $this->buildEmailTemplate($heading, $bodyHtml, $button);
$result = $this->sendEmailNotification($template, $email, $recipientName, $subject);
if ($result) {
$this->logger->info("Email to $recipientId was sent", ["app" => $this->appName]);
}
return $result;
}
/**
* Send notification about editors unsuccessfull check via email
*
* @param string $uid - user id
*
* @return bool
*/
public function notifyEditorsCheckEmail(string $uid) {
$user = $this->userManager->get($uid);
if (empty($user)) {
$this->logger->error("recipient $uid is null", ["app" => $this->appName]);
return false;
}
$email = $user->getEMailAddress();
if (empty($email)) {
$this->logger->info("The notification recipient $uid does not have an email", ["app" => $this->appName]);
return false;
}
$userName = $user->getDisplayName();
$subject = $this->trans->t("ONLYOFFICE Document Server is unavailable");
$bodyHtml = $this->trans->t("This is a mail message to notify that the connection with the ONLYOFFICE Document Server has been lost. Please check the connection settings:");
$appSettingsLink = $this->urlGenerator->getAbsoluteURL("/settings/admin?sectionid=additional");
$button = [$this->trans->t("Go to Settings"), $appSettingsLink];
$template = $this->buildEmailTemplate($subject, $bodyHtml, $button);
$result = $this->sendEmailNotification($template, $email, $userName, $subject);
if ($result) {
$this->logger->info("Email to $uid was sent", ["app" => $this->appName]);
}
return $result;
}
/**
* Build email template
*
* @param string $heading - e-mail heading text
* @param string $body - e-mail body html
* @param array $button - params for NC-button (0-text, 1-link)
*
* @return OC_Template
*/
private function buildEmailTemplate(string $heading, string $body, array $button = []) {
$template = new \OC_Template($this->appName, 'email/notify');
$template->assign('msgHeading', $heading);
$template->assign('msgBody', $body);
if (!empty($button) && isset($button[0]) && isset($button[1]) && is_string($button[0]) && is_string($button[1])) {
$template->assign('msgButtonText', $button[0]);
$template->assign('msgButtonLink', $button[1]);
}
return $template;
}
/**
* Send email
*
* @param OC_Template $template - e-mail template
* @param string $email - e-mail address
* @param string $recipientName - recipient name
*
* @return bool
*/
private function sendEmailNotification(\OC_Template $template, string $email, string $recipientName, string $subject) {
try {
$message = $this->mailer->createMessage();
$message->setTo([$email => $recipientName]);
$message->setSubject($subject);
$msgPage = $template->fetchPage();
$message->setHtmlBody($msgPage);
$message->setHtmlBody($msgPage);
$this->mailer->send($message);
} catch (\Exception $e) {
$this->logger->logException($e, ["message" => "Send email", "app" => $this->appName]);
return false;
}
return true;
}
}

View File

@ -0,0 +1,50 @@
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr><td>
<table cellspacing="0" cellpadding="0" border="0" width="600px">
<tr>
<td bgcolor="<?php p($theme->getMailHeaderColor());?>" width="20px">&nbsp;</td>
<td bgcolor="<?php p($theme->getMailHeaderColor());?>">
<img src="<?php p(\OC::$server->getURLGenerator()->getAbsoluteURL(image_path('', 'logo-mail.gif'))); ?>" alt="<?php p($theme->getName()); ?>"/>
</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
<td width="20px">&nbsp;</td>
<td style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">
<h2>
<?php
p($_['msgHeading']);
?>
</h2>
</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
<td width="20px">&nbsp;</td>
<td style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">
<?php
print_unescaped($_['msgBody']);
?>
</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
<td width="20px">&nbsp;</td>
<td style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">
<a href="<?php p($_['msgButtonLink']); ?>"><?php p($_['msgButtonText']); ?></a>
</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
<td width="20px">&nbsp;</td>
<td style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">
<?php print_unescaped($this->inc('html.mail.footer', ['app'=>'core'])); ?>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
</table>
</td></tr>
</table>