Skip to content

Commit 0ff0d68

Browse files
authored
Merge pull request #203 from nextcloud/enh/app-suggestions
Notify admin on first login about suggested apps
2 parents 0c66132 + a31dd93 commit 0ff0d68

7 files changed

Lines changed: 165 additions & 2 deletions

File tree

img/apps/calendar.svg

Lines changed: 1 addition & 0 deletions
Loading

img/apps/contacts.svg

Lines changed: 1 addition & 0 deletions
Loading

img/apps/mail.svg

Lines changed: 4 additions & 0 deletions
Loading

img/apps/spreed.svg

Lines changed: 1 addition & 0 deletions
Loading

lib/AppInfo/Application.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace OCA\FirstRunWizard\AppInfo;
2525

26+
use OCA\FirstRunWizard\Notification\AppHint;
2627
use OCA\FirstRunWizard\Notification\Notifier;
2728
use OCP\AppFramework\App;
2829
use OCP\AppFramework\Http\TemplateResponse;
@@ -69,14 +70,15 @@ protected function registerScripts() {
6970

7071
/** @var IConfig $config */
7172
$config = $this->getContainer()->query(IConfig::class);
72-
73+
$appHint = $this->getContainer()->query(AppHint::class);
7374

7475
if ($config->getUserValue($user->getUID(), 'firstrunwizard', 'show', '1') !== '0') {
7576
\OC_Util::addScript('firstrunwizard', 'activate');
7677

7778
$jobList = $this->getContainer()->getServer()->getJobList();
7879
$jobList->add('OCA\FirstRunWizard\Notification\BackgroundJob', ['uid' => $userSession->getUser()->getUID()]);
7980
}
81+
$appHint->sendAppHintNotifications();
8082
});
8183
}
8284

@@ -90,5 +92,8 @@ protected function registerNotificationNotifier() {
9092
'name' => $l->t('First run wizard'),
9193
];
9294
});
95+
/** @var AppHint $appHint */
96+
$appHint = $this->getContainer()->query(AppHint::class);
97+
$appHint->registerAppListener();
9398
}
9499
}

lib/Notification/AppHint.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
4+
*
5+
* @author Julius Härtl <jus@bitgrid.net>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\FirstRunWizard\Notification;
25+
26+
use OCP\App\IAppManager;
27+
use OCP\App\ManagerEvent;
28+
use OCP\EventDispatcher\IEventDispatcher;
29+
use OCP\IConfig;
30+
use OCP\IGroupManager;
31+
use OCP\Notification\IManager as INotificationManager;
32+
33+
class AppHint {
34+
35+
/** @var INotificationManager */
36+
protected $notificationManager;
37+
38+
/** @var IGroupManager */
39+
protected $groupManager;
40+
41+
/** @var IAppManager */
42+
protected $appManager;
43+
44+
/** @var IConfig */
45+
protected $config;
46+
47+
/** @var IEventDispatcher */
48+
private $dispatcher;
49+
50+
/** @var string */
51+
private $userId;
52+
53+
public function __construct(INotificationManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IConfig $config, IEventDispatcher $eventDispatcher, $userId) {
54+
$this->notificationManager = $notificationManager;
55+
$this->groupManager = $groupManager;
56+
$this->appManager =$appManager;
57+
$this->config = $config;
58+
$this->dispatcher = $eventDispatcher;
59+
$this->userId = $userId;
60+
}
61+
62+
public function sendAppHintNotifications(): void {
63+
if ($this->userId !== null && $this->groupManager->isAdmin($this->userId) && $this->config->getUserValue($this->userId, 'firstrunwizard', 'apphint') !== 'yes') {
64+
$this->sendNotification('calendar', $this->userId);
65+
$this->sendNotification('contacts', $this->userId);
66+
$this->sendNotification('mail', $this->userId);
67+
$this->sendNotification('spreed', $this->userId);
68+
$this->config->setUserValue($this->userId, 'firstrunwizard', 'apphint', 'yes');
69+
}
70+
}
71+
72+
public function registerAppListener(): void {
73+
$this->dispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) {
74+
$this->dismissNotification($event->getAppID());
75+
});
76+
}
77+
78+
protected function sendNotification(string $app, string $user): void {
79+
$notification = $this->generateNotification($app, $user);
80+
if (
81+
$this->config->getUserValue($this->userId, 'firstrunwizard', 'apphint') !== 'yes'
82+
&& $this->notificationManager->getCount($notification) === 0
83+
&& !$this->appManager->isEnabledForUser($app)
84+
) {
85+
$notification->setDateTime(new \DateTime());
86+
$this->notificationManager->notify($notification);
87+
}
88+
if ($this->notificationManager->getCount($notification) === 0) {
89+
return;
90+
}
91+
}
92+
93+
protected function dismissNotification(string $app) {
94+
$notification = $this->notificationManager->createNotification();
95+
$notification->setApp('firstrunwizard')
96+
->setSubject('apphint-'. $app)
97+
->setObject('app', $app);
98+
$this->notificationManager->markProcessed($notification);
99+
}
100+
101+
protected function generateNotification(string $app, string $user) {
102+
$notification = $this->notificationManager->createNotification();
103+
$notification->setApp('firstrunwizard')
104+
->setSubject('apphint-'. $app)
105+
->setObject('app', $app)
106+
->setUser($user);
107+
return $notification;
108+
}
109+
}

lib/Notification/Notifier.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ public function prepare(INotification $notification, $languageCode) {
8181
$notification->setParsedSubject($subject)
8282
->setLink($this->url->linkToRouteAbsolute('settings.PersonalSettings.index'));
8383
return $notification;
84-
84+
case 'apphint-calendar':
85+
case 'apphint-contacts':
86+
case 'apphint-mail':
87+
case 'apphint-spreed':
88+
$app = $notification->getObjectId();
89+
return $this->setAppHintDetails($notification, $languageCode, $app);
8590
default:
8691
// Unknown subject => Unknown notification => throw
8792
throw new \InvalidArgumentException();
@@ -116,4 +121,41 @@ protected function getSubject(INotification $notification, $languageCode) {
116121
}
117122
}
118123
}
124+
125+
/**
126+
* @param INotification $notification
127+
* @param string $languageCode
128+
* @return INotification
129+
*/
130+
protected function setAppHintDetails(INotification $notification, $languageCode, $app) {
131+
$l = $this->factory->get('firstrunwizard', $languageCode);
132+
$appLink = '';
133+
switch ($app) {
134+
case 'calendar':
135+
$notification->setParsedSubject($l->t('App recommendation: Nextcloud Calendar'));
136+
$notification->setParsedMessage($l->t('Schedule work & meetings, synced with all your devices.'));
137+
$appLink = '/organization/calendar';
138+
break;
139+
case 'contacts':
140+
$notification->setParsedSubject($l->t('App recommendation: Nextcloud Contacts'));
141+
$notification->setParsedMessage($l->t('Keep your colleagues and friends in one place without leaking their private info.'));
142+
$appLink = '/organization/contacts';
143+
break;
144+
case 'mail':
145+
$notification->setParsedSubject($l->t('App recommendation: Nextcloud Mail'));
146+
$notification->setParsedMessage($l->t('Simple email app nicely integrated with Files, Contacts and Calendar.'));
147+
$appLink = '/social/mail';
148+
break;
149+
case 'spreed':
150+
$notification->setParsedSubject($l->t('App recommendation: Nextcloud Talk'));
151+
$notification->setParsedMessage($l->t('Screensharing, online meetings and web conferencing – on desktop and with mobile apps.'));
152+
$appLink = '/social/spreed';
153+
break;
154+
}
155+
$notification
156+
->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps') . $appLink, 'GET')
157+
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('firstrunwizard', 'apps/'. $app . '.svg')));
158+
return $notification;
159+
}
160+
119161
}

0 commit comments

Comments
 (0)