-
Notifications
You must be signed in to change notification settings - Fork 50
/
Events.php
234 lines (198 loc) · 8.46 KB
/
Events.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\mail;
use humhub\modules\mail\helpers\Url;
use humhub\modules\mail\models\Config;
use humhub\modules\mail\models\MessageEntry;
use humhub\modules\mail\models\Message;
use humhub\modules\mail\models\UserMessageTag;
use humhub\modules\mail\models\UserMessage;
use humhub\modules\mail\permissions\SendMail;
use humhub\modules\mail\permissions\StartConversation;
use humhub\modules\mail\search\SearchProvider;
use humhub\modules\mail\widgets\NewMessageButton;
use humhub\modules\mail\widgets\NotificationInbox;
use humhub\widgets\MetaSearchWidget;
use humhub\modules\user\models\User;
use Yii;
/**
* Description of Events
*
* @author luke
*/
class Events
{
/**
* @param $event
*/
public static function onIntegrityCheck($event)
{
$integrityController = $event->sender;
$integrityController->showTestHeadline("Mail Module (" . Message::find()->count() . " conversations)");
try {
foreach (Message::find()->each() as $message) {
/* @var $message Message */
if (!$message->getAuthor()->count()) {
if ($integrityController->showFix("Deleting conversation id " . $message->id . " without existing author!")) {
$message->delete();
}
}
}
$integrityController->showTestHeadline("Mail Module (" . MessageEntry::find()->count() . " message entries)");
foreach (MessageEntry::find()->each() as $messageEntry) {
/* @var $messageEntry MessageEntry */
if (!$messageEntry->getUser()->count()) {
if ($integrityController->showFix("Deleting message entry id " . $messageEntry->id . " without existing user!")) {
$messageEntry->delete();
}
}
if (!$messageEntry->message) {
if ($integrityController->showFix("Deleting message entry id " . $messageEntry->id . " without existing conversation!")) {
$messageEntry->delete();
}
}
}
$integrityController->showTestHeadline("Mail Module (" . UserMessage::find()->count() . " user message entries)");
foreach (UserMessage::find()->each() as $userMessage) {
/* @var $userMessage UserMessage */
if (!$userMessage->getUser()->count()) {
if ($integrityController->showFix("Deleting user message id " . $userMessage->message_id . " without existing user!")) {
$userMessage->delete();
}
}
}
$integrityController->showTestHeadline("Mail Module (" . UserMessageTag::find()->count() . " user message tag entries)");
foreach (UserMessageTag::find()->each() as $messageTag) {
/* @var $messageTag UserMessageTag */
if (!$messageTag->getUser()->count()) {
if ($integrityController->showFix("Deleting user tag id " . $messageTag->id . " without existing user!")) {
$messageTag->delete();
}
}
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
/**
* On User delete, also delete all comments
*
* @param type $event
* @return bool
*/
public static function onUserDelete($event)
{
try {
foreach (Message::findAll(['created_by' => $event->sender->id]) as $message) {
$message->delete();
}
foreach (MessageEntry::findAll(['user_id' => $event->sender->id]) as $messageEntry) {
$messageEntry->delete();
}
foreach (UserMessage::findAll(['user_id' => $event->sender->id]) as $userMessage) {
if ($userMessage->message) {
$userMessage->message->leave($event->sender->id);
}
$userMessage->delete();
}
foreach (UserMessageTag::findAll(['user_id' => $event->sender->id]) as $userMessageTag) {
$userMessageTag->delete();
}
} catch (\Throwable $e) {
Yii::error($e);
}
return true;
}
/**
* On build of the TopMenu, check if module is enabled
* When enabled add a menu item
*
* @param type $event
*/
public static function onTopMenuInit($event)
{
try {
if (Yii::$app->user->isGuest) {
return;
}
$module = Config::getModule();
// See https://github.com/humhub/humhub-modules-mail/issues/201
if (method_exists($module, 'hideInTopNav') && !$module->hideInTopNav()) {
$event->sender->addItem([
'label' => Yii::t('MailModule.base', 'Messages'),
'url' => Url::toMessenger(),
'icon' => '<i class="fa fa-envelope"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'mail'),
'sortOrder' => 300,
]);
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onNotificationAddonInit($event)
{
try {
if (Yii::$app->user->isGuest) {
return;
}
$event->sender->addWidget(NotificationInbox::className(), [], ['sortOrder' => 90]);
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onProfileHeaderControlsInit($event)
{
try {
/** @var User $profileContainer */
$profileContainer = $event->sender->user;
if ($profileContainer->isCurrentUser() || !Yii::$app->user->can(StartConversation::class)) {
return;
}
// Is the current logged user allowed to send mails to profile user?
if (!Yii::$app->user->isAdmin() && !$profileContainer->can(SendMail::class)) {
return;
}
$event->sender->addWidget(NewMessageButton::class, ['guid' => $event->sender->user->guid, 'size' => null, 'icon' => null], ['sortOrder' => 90]);
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onRestApiAddRules()
{
/* @var \humhub\modules\rest\Module $restModule */
$restModule = Yii::$app->getModule('rest');
$restModule->addRules([
// Conversations
['pattern' => 'mail', 'route' => 'mail/rest/message/index', 'verb' => 'GET'],
['pattern' => 'mail/<id:\d+>', 'route' => 'mail/rest/message/view', 'verb' => 'GET'],
['pattern' => 'mail', 'route' => 'mail/rest/message/create', 'verb' => 'POST'],
// Participants
['pattern' => 'mail/<messageId:\d+>/users', 'route' => 'mail/rest/user/index', 'verb' => 'GET'],
['pattern' => 'mail/<messageId:\d+>/user/<userId:\d+>', 'route' => 'mail/rest/user/add', 'verb' => 'POST'],
['pattern' => 'mail/<messageId:\d+>/user/<userId:\d+>', 'route' => 'mail/rest/user/leave', 'verb' => 'DELETE'],
// Entries
['pattern' => 'mail/<messageId:\d+>/entries', 'route' => 'mail/rest/entry/index', 'verb' => 'GET'],
['pattern' => 'mail/<messageId:\d+>/entry', 'route' => 'mail/rest/entry/add', 'verb' => 'POST'],
['pattern' => 'mail/<messageId:\d+>/entry/<entryId:\d+>', 'route' => 'mail/rest/entry/view', 'verb' => 'GET'],
['pattern' => 'mail/<messageId:\d+>/entry/<entryId:\d+>', 'route' => 'mail/rest/entry/update', 'verb' => 'PUT'],
['pattern' => 'mail/<messageId:\d+>/entry/<entryId:\d+>', 'route' => 'mail/rest/entry/delete', 'verb' => 'DELETE'],
// Tags
['pattern' => 'mail/<messageId:\d+>/tags', 'route' => 'mail/rest/tag/index', 'verb' => 'GET'],
['pattern' => 'mail/<messageId:\d+>/tags', 'route' => 'mail/rest/tag/update', 'verb' => 'PUT'],
], 'mail');
}
public static function onMetaSearchWidgetInit($event)
{
if (Yii::$app->user->isGuest) {
return;
}
/* @var MetaSearchWidget $widget */
$widget = $event->sender;
$widget->addProvider(SearchProvider::class);
}
}