Skip to content

Commit

Permalink
Merge branch 'enh/324-attach-files-to-message-entries' of github.com:…
Browse files Browse the repository at this point in the history
…cuzy-app/mail
  • Loading branch information
luke- committed Sep 15, 2023
2 parents 79031bb + b55d41d commit bfe1f8b
Show file tree
Hide file tree
Showing 14 changed files with 537 additions and 349 deletions.
8 changes: 1 addition & 7 deletions assets/MailMessengerAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

use humhub\components\assets\AssetBundle;

/**
* Created by PhpStorm.
* User: kingb
* Date: 29.07.2018
* Time: 08:19
*/
class MailMessengerAsset extends AssetBundle
{
public $sourcePath = '@mail/resources/js';
Expand All @@ -25,4 +19,4 @@ class MailMessengerAsset extends AssetBundle
public $depends = [
MailNotificationAsset::class
];
}
}
89 changes: 49 additions & 40 deletions controllers/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@
namespace humhub\modules\mail\controllers;

use humhub\components\access\ControllerAccess;
use humhub\components\Controller;
use humhub\modules\file\handler\FileHandlerCollection;
use humhub\modules\mail\helpers\Url;
use humhub\modules\mail\models\forms\CreateMessage;
use humhub\modules\mail\models\forms\InviteParticipantForm;
use humhub\modules\mail\models\forms\ReplyForm;
use humhub\modules\mail\models\Message;
use humhub\modules\mail\models\MessageEntry;
use humhub\modules\mail\models\UserMessage;
use humhub\modules\mail\Module;
use humhub\modules\mail\permissions\SendMail;
use humhub\modules\mail\permissions\StartConversation;
use humhub\modules\mail\widgets\ConversationEntry;
use humhub\modules\mail\widgets\ConversationHeader;
use humhub\modules\mail\widgets\Messages;
use humhub\modules\mail\widgets\ConversationEntry;
use humhub\modules\User\models\User;
use humhub\modules\user\models\UserFilter;
use humhub\modules\user\models\UserPicker;
use humhub\modules\user\widgets\UserListBox;
use Yii;
use humhub\modules\mail\permissions\StartConversation;
use yii\helpers\Html;
use humhub\modules\mail\helpers\Url;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use humhub\components\Controller;
use humhub\modules\mail\models\Message;
use humhub\modules\mail\models\MessageEntry;
use humhub\modules\mail\models\UserMessage;
use humhub\modules\User\models\User;
use humhub\modules\mail\models\forms\InviteParticipantForm;
use humhub\modules\mail\models\forms\ReplyForm;
use humhub\modules\mail\models\forms\CreateMessage;
use humhub\modules\mail\permissions\SendMail;
use humhub\modules\user\models\UserPicker;
use yii\web\NotFoundHttpException;

/**
Expand Down Expand Up @@ -79,14 +80,15 @@ public function actionShow($id)
'message' => $message,
'messageCount' => UserMessage::getNewMessageCount(),
'replyForm' => new ReplyForm(['model' => $message]),
'fileHandlers' => FileHandlerCollection::getByType([FileHandlerCollection::TYPE_IMPORT, FileHandlerCollection::TYPE_CREATE]),
]);
}

public function actionSeen()
{
$id = Yii::$app->request->post('id');

if($id) {
if ($id) {
$message = ($id instanceof Message) ? $id : $this->getMessage($id);
$this->checkMessagePermissions($message);
$message->seen(Yii::$app->user->id);
Expand Down Expand Up @@ -158,7 +160,7 @@ public function actionUserList($id)
{
return $this->renderAjaxContent(UserListBox::widget([
'query' => $this->getMessage($id, true)->getUsers(),
'title' => '<strong>'.Yii::t('MailModule.base', 'Participants').'</strong>'
'title' => '<strong>' . Yii::t('MailModule.base', 'Participants') . '</strong>'
]));
}

Expand All @@ -177,7 +179,7 @@ public function actionAddUser($id)
$inviteForm = new InviteParticipantForm(['message' => $message]);

if ($inviteForm->load(Yii::$app->request->post())) {
if($inviteForm->save()) {
if ($inviteForm->save()) {
return $this->asJson([
'result' => ConversationHeader::widget(['message' => $message])
]);
Expand Down Expand Up @@ -218,7 +220,7 @@ public function actionSearchUser($keyword, $id = null)
{
$message = $this->getMessage($id);

if($message) {
if ($message) {
$this->checkMessagePermissions($message);
}

Expand All @@ -227,16 +229,16 @@ public function actionSearchUser($keyword, $id = null)
'keyword' => $keyword,
'permission' => (!Yii::$app->user->isAdmin()) ? new SendMail() : null,
'disableFillUser' => true,
'disabledText' => Yii::t('MailModule.base','You are not allowed to start a conversation with this user.')
'disabledText' => Yii::t('MailModule.base', 'You are not allowed to start a conversation with this user.')
]);

// Disable already participating users
if($message) {
foreach($result as $i=>$user) {
if($this->isParticipant($message, $user)) {
if ($message) {
foreach ($result as $i => $user) {
if ($this->isParticipant($message, $user)) {
$index = $i++;
$result[$index]['disabled'] = true;
$result[$index]['disabledText'] = Yii::t('MailModule.base','This user is already participating in this conversation.');
$result[$index]['disabledText'] = Yii::t('MailModule.base', 'This user is already participating in this conversation.');
}
}
}
Expand All @@ -250,22 +252,23 @@ private function checkMessagePermissions($message)
throw new HttpException(404, 'Could not find message!');
}

if(!$message->isParticipant(Yii::$app->user->getIdentity())) {
if (!$message->isParticipant(Yii::$app->user->getIdentity())) {
throw new HttpException(403, 'Access denied!');
}
}

/**
* Checks if a user (user json representation) is participant of a given
* message.
*
*
* @param type $message
* @param type $user
* @return boolean
*/
private function isParticipant($message, $user) {
foreach($message->users as $participant) {
if($participant->guid === $user['guid']) {
private function isParticipant($message, $user)
{
foreach ($message->users as $participant) {
if ($participant->guid === $user['guid']) {
return true;
}
}
Expand Down Expand Up @@ -310,26 +313,29 @@ private function findUserByFilter($keyword, $maxResult)
public function actionCreate($userGuid = null)
{
$model = new CreateMessage(['recipient' => [$userGuid]]);

// Preselect user if userGuid is given
if ($userGuid) {
/* @var User $user */
$user = User::find()->where(['guid' => $userGuid])->available()->one();

if(!$user) {
if (!$user) {
throw new NotFoundHttpException();
}

if(!$user->getPermissionManager()->can(SendMail::class) && !Yii::$app->user->isAdmin()) {
if (!$user->getPermissionManager()->can(SendMail::class) && !Yii::$app->user->isAdmin()) {
throw new ForbiddenHttpException();
}
}

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->htmlRedirect(['index', 'id' => $model->messageInstance->id]);
}

return $this->renderAjax('create', ['model' => $model]);

return $this->renderAjax('create', [
'model' => $model,
'fileHandlers' => FileHandlerCollection::getByType([FileHandlerCollection::TYPE_IMPORT, FileHandlerCollection::TYPE_CREATE]),
]);
}

/**
Expand Down Expand Up @@ -364,7 +370,7 @@ public function actionEditEntry($id)
{
$entry = MessageEntry::findOne(['id' => $id]);

if(!$entry) {
if (!$entry) {
throw new HttpException(404);
}

Expand All @@ -373,14 +379,17 @@ public function actionEditEntry($id)
}

if ($entry->load(Yii::$app->request->post()) && $entry->save()) {
$entry->fileManager->attach(Yii::$app->request->post('fileList'));
$entry->fileManager->attach(Yii::$app->request->post('MessageEntry')['files'] ?? null);
return $this->asJson([
'success' => true,
'content' => ConversationEntry::widget(['entry' => $entry])
]);
}

return $this->renderAjax('editEntry', ['entry' => $entry]);
return $this->renderAjax('editEntry', [
'entry' => $entry,
'fileHandlers' => FileHandlerCollection::getByType([FileHandlerCollection::TYPE_IMPORT, FileHandlerCollection::TYPE_CREATE]),
]);
}

/**
Expand All @@ -393,7 +402,7 @@ public function actionDeleteEntry($id)
$this->forcePostRequest();
$entry = MessageEntry::findOne(['id' => $id]);

if(!$entry) {
if (!$entry) {
throw new HttpException(404);
}

Expand Down Expand Up @@ -440,10 +449,10 @@ private function getMessage($id, $throw = false)
}
}

if($throw) {
if ($throw) {
throw new HttpException(404, 'Could not find message!');
}

return null;
}
}
}
9 changes: 5 additions & 4 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Changelog
=========

Unreleased
-------------------------
3.1.0 (Unreleased)
-------------------
- Fix #348: Remove new lines in notificationInbox view
- Fix #349: Assets now extending `humhub\components\assets\AssetBundle` instead of `yii\web\AssetBundle`
- Enh #324: Possibility to attach files to a message entry

3.0.2 (August 17, 2023)
-------------------------
Expand All @@ -29,7 +30,7 @@ Unreleased
- Fix #283: Add markdown-render class to Markdown text for Translator module to work
- Fix #272: Exclude invisible users from recipients
- Fix #280: Update styles of message block
- Enh #274: Browser Tab Indicator on New Unread Message
- Enh #274: Browser Tab Indicator on New Unread Message

2.1.0 (December 7, 2021)
-------------------------
Expand All @@ -47,7 +48,7 @@ Unreleased
2.0.6 (April 8, 2021)
----------------------
- Enh: Use controller config for not intercepted actions
- Enh #217: RESTFul API Module Support
- Enh #217: RESTFul API Module Support

2.0.5 - January 21, 2021
------------------------
Expand Down
16 changes: 12 additions & 4 deletions models/forms/CreateMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class CreateMessage extends Model
public $message;
public $title;

/**
* @var string[] attached files
*/
public $files;


/**
* @var Message new message
Expand All @@ -49,7 +54,7 @@ public function rules()
{
return [
[['message', 'recipient', 'title'], 'required'],
[['tags'], 'safe'],
[['files', 'tags'], 'safe'],
['recipient', 'checkRecipient']
];
}
Expand Down Expand Up @@ -91,7 +96,7 @@ public function checkRecipient($attribute, $params)

if ($user->isCurrentUser()) {
$this->addError($attribute, Yii::t('MailModule.base', 'You cannot send a message to yourself!'));
} else if(!$this->canSendToUser($user)) {
} else if (!$this->canSendToUser($user)) {
$this->addError($attribute, Yii::t('MailModule.base', 'You are not allowed to start a conversation with {userName}!', [
'userName' => Html::encode($user->getDisplayName())
]));
Expand Down Expand Up @@ -143,7 +148,7 @@ public function save()
return false;
}

if(!$this->saveOriginatorUserMessage()) {
if (!$this->saveOriginatorUserMessage()) {
$transaction->rollBack();
return false;
}
Expand Down Expand Up @@ -185,7 +190,7 @@ private function saveMessage()
'title' => $this->title
]);

if(!(new Config())->canCreateConversation(Yii::$app->user->getIdentity())) {
if (!(new Config())->canCreateConversation(Yii::$app->user->getIdentity())) {
$this->addError('message', Yii::t('MailModule.base', 'You\'ve exceeded your daily amount of new conversations.'));
return false;
}
Expand All @@ -205,6 +210,9 @@ private function saveMessageEntry()
{
$entry = MessageEntry::createForMessage($this->messageInstance, Yii::$app->user->getIdentity(), $this->message);
$result = $entry->save();
if ($result) {
$entry->fileManager->attach($this->files);
}
$entry->notify(true);
return $result;
}
Expand Down
8 changes: 4 additions & 4 deletions models/forms/ReplyForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace humhub\modules\mail\models\forms;

use humhub\modules\mail\helpers\Url;
use humhub\modules\mail\models\Message;
use humhub\modules\mail\models\MessageEntry;
use humhub\modules\mail\helpers\Url;
use Yii;
use yii\base\Model;

Expand Down Expand Up @@ -69,7 +69,7 @@ public function getUrl()

public function save()
{
if(!$this->validate()) {
if (!$this->validate()) {
return false;
}

Expand All @@ -79,10 +79,10 @@ public function save()
'content' => $this->message
]);

if($this->reply->save()) {
if ($this->reply->save()) {
$this->reply->refresh(); // Update created_by date, otherwise db expression is set...
$this->reply->notify();
$this->reply->fileManager->attach(Yii::$app->request->post('fileUploaderHiddenGuidField'));
$this->reply->fileManager->attach(Yii::$app->request->post('fileList'));
return true;
}

Expand Down
8 changes: 1 addition & 7 deletions resources/css/humhub.mail.css
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
.conversation-entry-content .markdown-render {
float: left;
width: 100%;
min-width: 230px;
}
.conversation-blocked-recipient {
-webkit-filter: grayscale(100%);
Expand Down Expand Up @@ -210,13 +211,6 @@
padding-right: 45px;
border-radius: 10px;
}
.mail-message-form .reply-button {
position: absolute;
right: 15px;
bottom: 15px;
z-index: 500;
border-radius: 4px;
}
.mail-message-form .help-block {
margin: 0;
}
Expand Down
Loading

0 comments on commit bfe1f8b

Please sign in to comment.