Skip to content

Commit

Permalink
Enh #424: Allow sending a message with attached files only, such as a…
Browse files Browse the repository at this point in the history
…n image (#425)
  • Loading branch information
marc-farre authored Oct 15, 2024
1 parent 92364ce commit a63e528
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 14 deletions.
5 changes: 5 additions & 0 deletions controllers/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public function actionReply($id)

// Reply Form
$replyForm = new ReplyForm(['model' => $message]);

if (!empty(Yii::$app->request->post('fileList'))) {
$replyForm->scenario = ReplyForm::SCENARIO_HAS_FILES;
}

if ($replyForm->load(Yii::$app->request->post()) && $replyForm->save()) {
return $this->asJson([
'success' => true,
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Changelog
- Enh #418: Update Active Form for Bootstrap 5
- Enh #422: Changed deprecated `DOMSubtreeModified` to `MutationObserver`
- Fix #423: Fix rendering of attached files on new reply by PushService
- Enh #424: Allow sending a message with attached files only, such as an image

3.2.2 (July 9, 2024)
--------------------
Expand Down
10 changes: 1 addition & 9 deletions models/AbstractMessageEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ abstract class AbstractMessageEntry extends ActiveRecord
public const TYPE_USER_JOINED = 1;
public const TYPE_USER_LEFT = 2;

protected bool $requiredContent = true;

/**
* Get type of the message entry
*
Expand Down Expand Up @@ -78,14 +76,8 @@ public static function tableName()
*/
public function rules()
{
$requiredColumns = ['message_id', 'user_id'];

if ($this->requiredContent) {
$requiredColumns[] = 'content';
}

return [
[$requiredColumns, 'required'],
[['message_id', 'user_id'], 'required'],
[['message_id', 'user_id', 'created_by', 'updated_by'], 'integer'],
[['created_at', 'updated_at'], 'safe'],
];
Expand Down
15 changes: 15 additions & 0 deletions models/MessageEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
class MessageEntry extends AbstractMessageEntry implements ViewableInterface
{
/**
* Scenario - when related content has attached files
*/
public const SCENARIO_HAS_FILES = 'hasFiles';

/**
* @inheritdoc
*/
Expand All @@ -23,6 +28,16 @@ public static function type(): int
return self::TYPE_MESSAGE;
}

/**
* @inerhitdoc
*/
public function rules()
{
return array_merge(parent::rules(), [
[['content'], 'required', 'except' => [self::SCENARIO_HAS_FILES]],
]);
}

/**
* @inheritdoc
*/
Expand Down
10 changes: 9 additions & 1 deletion models/forms/ReplyForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
class ReplyForm extends Model
{
/**
* Scenario - when related content has attached files
*/
public const SCENARIO_HAS_FILES = 'hasFiles';

/**
* @var Message
*/
Expand All @@ -35,7 +40,7 @@ class ReplyForm extends Model
public function rules()
{
return [
['message', 'required'],
['message', 'required', 'except' => [self::SCENARIO_HAS_FILES]],
['message', 'validateRecipients'],
];
}
Expand Down Expand Up @@ -77,6 +82,9 @@ public function save()
'user_id' => Yii::$app->user->id,
'content' => $this->message,
]);
if ($this->scenario === self::SCENARIO_HAS_FILES) {
$this->reply->scenario = MessageEntry::SCENARIO_HAS_FILES;
}

if ($this->reply->save()) {
$this->reply->refresh(); // Update created_by date, otherwise db expression is set...
Expand Down
2 changes: 0 additions & 2 deletions models/states/AbstractMessageState.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
*/
abstract class AbstractMessageState extends AbstractMessageEntry
{
protected bool $requiredContent = false;

/**
* @inheritdoc
*/
Expand Down
8 changes: 8 additions & 0 deletions resources/js/humhub.mail.ConversationView.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ humhub.module('mail.ConversationView', function (module, require, $) {
filePreview.hide();
filePreview.children('ul.files').html('');
}
var fileListInputs = that.getReplyFileListInputs();
if (fileListInputs.length) {
fileListInputs.remove();
}
that.scrollToBottom();
if (!view.isSmall()) { // prevent autofocus on mobile
that.focus();
Expand Down Expand Up @@ -109,6 +113,10 @@ humhub.module('mail.ConversationView', function (module, require, $) {
return this.$.find('.post-file-list');
};

ConversationView.prototype.getReplyFileListInputs = function () {
return this.$.find('input[name="fileList[]"]');
};


ConversationView.prototype.focus = function (evt) {
var replyRichtext = this.getReplyRichtext();
Expand Down
8 changes: 8 additions & 0 deletions resources/js/humhub.mail.messenger.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ humhub.module('mail.ConversationView', function (module, require, $) {
filePreview.hide();
filePreview.children('ul.files').html('');
}
var fileListInputs = that.getReplyFileListInputs();
if (fileListInputs.length) {
fileListInputs.remove();
}
that.scrollToBottom();
if (!view.isSmall()) { // prevent autofocus on mobile
that.focus();
Expand Down Expand Up @@ -109,6 +113,10 @@ humhub.module('mail.ConversationView', function (module, require, $) {
return this.$.find('.post-file-list');
};

ConversationView.prototype.getReplyFileListInputs = function () {
return this.$.find('input[name="fileList[]"]');
};


ConversationView.prototype.focus = function (evt) {
var replyRichtext = this.getReplyRichtext();
Expand Down
2 changes: 1 addition & 1 deletion resources/js/humhub.mail.messenger.bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion resources/js/humhub.mail.notification.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a63e528

Please sign in to comment.