Skip to content

Commit

Permalink
Merge branch 'master' of github.com:freescout-helpdesk/freescout into…
Browse files Browse the repository at this point in the history
… dist
  • Loading branch information
freescout-help-desk committed Oct 4, 2024
2 parents 5c9ab33 + c80ce3d commit 30b07a8
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 101 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/FetchEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function ($mailboxId) {
// we increase connection sleep time and retry after sleep.
// https://github.com/freescout-help-desk/freescout/issues/4227
if (trim($e->getMessage()) == 'connection setup failed') {
$sleep += 200000;
$sleep += 500000;

usleep(self::MAX_SLEEP);

Expand Down
98 changes: 9 additions & 89 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers;

use App\Events\UserDeleted;
use App\Folder;
use App\Mailbox;
use App\Subscription;
Expand Down Expand Up @@ -259,6 +258,10 @@ public function permissions($id)

$user = User::findOrFail($id);

if ($user->isDeleted()) {
abort(404);
}

$mailboxes = Mailbox::all();

$users = $this->getUsersForSidebar($id);
Expand Down Expand Up @@ -317,6 +320,10 @@ public function notifications($id)
$user = User::findOrFail($id);
$this->authorize('update', $user);

if ($user->isDeleted()) {
abort(404);
}

$subscriptions = $user->subscriptions()->select('medium', 'event')->get();

$person = '';
Expand Down Expand Up @@ -491,94 +498,7 @@ public function ajax(Request $request)

if (!$response['msg']) {

// We have to process conversations one by one to move them to Unassigned folder,
// as conversations may be in different mailboxes
// $user->conversations()->update(['user_id' => null, 'folder_id' => ]);
$mailbox_unassigned_folders = [];

$user->conversations->each(function ($conversation) use ($auth_user, $request) {
// We don't fire ConversationUserChanged event to avoid sending notifications to users
if (!empty($request->assign_user)
&& !empty($request->assign_user[$conversation->mailbox_id])
&& (int) $request->assign_user[$conversation->mailbox_id] != -1
) {
// Set assignee.
// In this case conversation stays assigned, just assignee changes.
$conversation->user_id = $request->assign_user[$conversation->mailbox_id];

} else {

// Make convesation Unassigned.

// Unset assignee.
// Maybe use changeUser() here.
$conversation->user_id = null;

if ($conversation->isPublished()
&& ($conversation->isActive() || $conversation->isPending())
) {
// Change conversation folder to UNASSIGNED.
$folder_id = null;
if (!empty($mailbox_unassigned_folders[$conversation->mailbox_id])) {
$folder_id = $mailbox_unassigned_folders[$conversation->mailbox_id];
} else {
$folder = $conversation->mailbox->folders()
->where('type', Folder::TYPE_UNASSIGNED)
->first();

if ($folder) {
$folder_id = $folder->id;
$mailbox_unassigned_folders[$conversation->mailbox_id] = $folder_id;
}
}
if ($folder_id) {
$conversation->folder_id = $folder_id;
}
}
}

$conversation->save();

// Create lineitem thread
$thread = new Thread();
$thread->conversation_id = $conversation->id;
$thread->user_id = $conversation->user_id;
$thread->type = Thread::TYPE_LINEITEM;
$thread->state = Thread::STATE_PUBLISHED;
$thread->status = Thread::STATUS_NOCHANGE;
$thread->action_type = Thread::ACTION_TYPE_USER_CHANGED;
$thread->source_via = Thread::PERSON_USER;
$thread->source_type = Thread::SOURCE_TYPE_WEB;
$thread->customer_id = $conversation->customer_id;
$thread->created_by_user_id = $auth_user->id;
$thread->save();
});

// Recalculate counters for folders
//if ($user->isAdmin()) {
// Admin has access to all mailboxes
Mailbox::all()->each(function ($mailbox) {
$mailbox->updateFoldersCounters();
});
// } else {
// $user->mailboxes->each(function ($mailbox) {
// $mailbox->updateFoldersCounters();
// });
// }

// Disconnect user from mailboxes.
$user->mailboxes()->sync([]);
$user->folders()->delete();

$user->status = \App\User::STATUS_DELETED;
// Update email.
$email_suffix = User::EMAIL_DELETED_SUFFIX.date('YmdHis');
// We have to truncate email to avoid "Data too long" error.
$user->email = mb_substr($user->email, 0, User::EMAIL_MAX_LENGTH - mb_strlen($email_suffix)).$email_suffix;

$user->save();

event(new UserDeleted($user, $auth_user));
$user->deleteUser($auth_user, $request->assign_user);

\Session::flash('flash_success_floating', __('User deleted').': '.$user->getFullName());

Expand Down
7 changes: 5 additions & 2 deletions app/Misc/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,10 @@ public static function fetchMessage($mailbox, $message_id, $message_date = null)
continue;
}
// Message-ID: <[email protected]>
$search_message_id = addcslashes($message_id, '\"');
$query = $folder->query()
->text('<'.$message_id.'>')
//->text('<'.$message_id.'>')
->whereMessageId('"<'.$search_message_id.'>"')
->leaveUnread()
->limit(1);

Expand All @@ -826,7 +828,8 @@ public static function fetchMessage($mailbox, $message_id, $message_date = null)
if ($last_error && stristr($last_error, 'The specified charset is not supported')) {
// Solution for MS mailboxes.
// https://github.com/freescout-helpdesk/freescout/issues/176
$query = $folder->query()->text('<'.$message_id.'>')->leaveUnread()->limit(1)->setCharset(null);
//$query = $folder->query()->text('<'.$message_id.'>')->leaveUnread()->limit(1)->setCharset(null);
$query = $folder->query()->whereMessageId('"<'.$search_message_id.'>"')->leaveUnread()->limit(1)->setCharset(null);
if ($message_date) {
$query->since($message_date->subDays(7));
$query->before($message_date->addDays(14));
Expand Down
93 changes: 93 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace App;

use App\Email;
use App\Events\UserDeleted;
use App\Follower;
use App\Mail\PasswordChanged;
use App\Mail\UserInvite;
Expand Down Expand Up @@ -1234,4 +1235,96 @@ public function canSeeOnlyAssignedConversations()
{
return $this->hasManageMailboxPermission(0, Mailbox::ACCESS_PERM_ASSIGNED);
}

public function deleteUser($auth_user, $assign_user)
{
// We have to process conversations one by one to move them to Unassigned folder,
// as conversations may be in different mailboxes
// $this->conversations()->update(['user_id' => null, 'folder_id' => ]);
$mailbox_unassigned_folders = [];

$this->conversations->each(function ($conversation) use ($auth_user, $assign_user) {
// We don't fire ConversationUserChanged event to avoid sending notifications to users
if (!empty($assign_user)
&& !empty($assign_user[$conversation->mailbox_id])
&& (int) $assign_user[$conversation->mailbox_id] != -1
) {
// Set assignee.
// In this case conversation stays assigned, just assignee changes.
$conversation->user_id = $assign_user[$conversation->mailbox_id];

} else {

// Make convesation Unassigned.

// Unset assignee.
// Maybe use changeUser() here.
$conversation->user_id = null;

if ($conversation->isPublished()
&& ($conversation->isActive() || $conversation->isPending())
) {
// Change conversation folder to UNASSIGNED.
$folder_id = null;
if (!empty($mailbox_unassigned_folders[$conversation->mailbox_id])) {
$folder_id = $mailbox_unassigned_folders[$conversation->mailbox_id];
} else {
$folder = $conversation->mailbox->folders()
->where('type', Folder::TYPE_UNASSIGNED)
->first();

if ($folder) {
$folder_id = $folder->id;
$mailbox_unassigned_folders[$conversation->mailbox_id] = $folder_id;
}
}
if ($folder_id) {
$conversation->folder_id = $folder_id;
}
}
}

$conversation->save();

// Create lineitem thread
$thread = new Thread();
$thread->conversation_id = $conversation->id;
$thread->user_id = $conversation->user_id;
$thread->type = Thread::TYPE_LINEITEM;
$thread->state = Thread::STATE_PUBLISHED;
$thread->status = Thread::STATUS_NOCHANGE;
$thread->action_type = Thread::ACTION_TYPE_USER_CHANGED;
$thread->source_via = Thread::PERSON_USER;
$thread->source_type = Thread::SOURCE_TYPE_WEB;
$thread->customer_id = $conversation->customer_id;
$thread->created_by_user_id = $auth_user->id;
$thread->save();
});

// Recalculate counters for folders
//if ($this->isAdmin()) {
// Admin has access to all mailboxes
Mailbox::all()->each(function ($mailbox) {
$mailbox->updateFoldersCounters();
});
// } else {
// $this->mailboxes->each(function ($mailbox) {
// $mailbox->updateFoldersCounters();
// });
// }

// Disconnect user from mailboxes.
$this->mailboxes()->sync([]);
$this->folders()->delete();

$this->status = \App\User::STATUS_DELETED;
// Update email.
$email_suffix = User::EMAIL_DELETED_SUFFIX.date('YmdHis');
// We have to truncate email to avoid "Data too long" error.
$this->email = mb_substr($this->email, 0, User::EMAIL_MAX_LENGTH - mb_strlen($email_suffix)).$email_suffix;

$this->save();

event(new UserDeleted($this, $auth_user));
}
}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
| or any other location as required by the application or its packages.
*/

'version' => '1.8.153',
'version' => '1.8.154',

/*
|--------------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions config/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@
| Auth mode
|--------------------------------------------------------------------------
|
| 'login' or 'XOAUTH2'
| This option have an effect when set to 'XOAUTH2' only.
| In other cases Swiftmailer chooses auth mode automatically.
|
*/

'auth_mode' => env('MAIL_AUTH_MODE', 'login'),
'auth_mode' => env('MAIL_AUTH_MODE', ''),

/*
|--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Swift_SmtpTransport extends Swift_Transport_EsmtpTransport
* @param int $port
* @param string $encryption
*/
public function __construct($host = 'localhost', $port = 25, $encryption = null, $auth_mode = 'login')
public function __construct($host = 'localhost', $port = 25, $encryption = null, $auth_mode = '')
{
parent::__construct(...Swift_DependencyContainer::getInstance()->createDependenciesFor('transport.smtp'));
// call_user_func_array(
Expand All @@ -39,6 +39,8 @@ public function __construct($host = 'localhost', $port = 25, $encryption = null,
$this->setHost($host);
$this->setPort($port);
$this->setEncryption($encryption);
$this->setAuthMode($auth_mode);
if ($auth_mode == 'XOAUTH2') {
$this->setAuthMode($auth_mode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -790,13 +790,15 @@ public function getUid($id = null) {
*/
public function getMessageNumber(string $id): int {
$ids = $this->getUid();
foreach ($ids as $k => $v) {
if ($v == $id) {
return (int)$k;
if ($ids) {
foreach ($ids as $k => $v) {
if ($v == $id) {
return (int)$k;
}
}
}

throw new MessageNotFoundException('message number not found');
throw new MessageNotFoundException('message number not found: ' . $id);
}

/**
Expand Down

0 comments on commit 30b07a8

Please sign in to comment.