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 Dec 8, 2018
2 parents c73ff62 + f69fe6a commit 84006b6
Show file tree
Hide file tree
Showing 23 changed files with 425 additions and 87 deletions.
15 changes: 15 additions & 0 deletions app/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,4 +1047,19 @@ public function deleteThreads()
$thread->deleteThread();
});
}

/**
* Get waiting since time for the conversation.
* @param [type] $folder [description]
* @return [type] [description]
*/
public function getWaitingSince($folder)
{
$waiting_since_field = $folder->getWaitingSinceField();
if ($waiting_since_field) {
return \App\User::dateDiffForHumans($this->$waiting_since_field);
} else {
return '';
}
}
}
81 changes: 79 additions & 2 deletions app/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ public function conversations()

public function getTypeName()
{
return __(self::$types[$this->type]);
// To make name translatable.
switch ($this->type) {
case self::TYPE_UNASSIGNED:
return __('Unassigned');
case self::TYPE_MINE:
return __('Mine');
default:
return __(self::$types[$this->type]);
}
}

public function getTypeIcon()
Expand Down Expand Up @@ -190,9 +198,10 @@ public function updateCounters()
$this->total_count = $this->active_count;
} elseif ($this->isIndirect()) {
// Conversation are connected to folder via conversation_folder table.
// Drafts.
$this->active_count = ConversationFolder::where('conversation_folder.folder_id', $this->id)
->join('conversations', 'conversations.id', '=', 'conversation_folder.conversation_id')
->where('state', Conversation::STATE_PUBLISHED)
//->where('state', Conversation::STATE_PUBLISHED)
->count();
$this->total_count = $this->active_count;
} else {
Expand All @@ -207,6 +216,21 @@ public function updateCounters()
$this->save();
}

/**
* Get count to display in folders list.
*
* @param array $folders [description]
* @return [type] [description]
*/
public function getCount($folders = [])
{
if ($this->type == self::TYPE_STARRED || $this->type == self::TYPE_DRAFTS) {
return $this->total_count;
} else {
return $this->getActiveCount($folders);
}
}

/**
* Get calculated number of active conversations.
*/
Expand All @@ -230,4 +254,57 @@ public function getActiveCount($folders = [])

return $active_count;
}

public function getConversationsQuery()
{
if ($this->type == self::TYPE_MINE) {
// Assigned to user.
return Conversation::where('user_id', $this->user_id)
->where('mailbox_id', $this->mailbox_id);
} elseif ($this->isIndirect()) {
// Via intermediate table.
return Conversation::join('conversation_folder', 'conversations.id', '=', 'conversation_folder.conversation_id')
->where('conversation_folder.folder_id', $this->id);
} else {
// All other conversations.
return $this->conversations();
}
}

/**
* Works for main folder only for now.
*
* @return [type] [description]
*/
public function getWaitingSince()
{
// Get oldest active conversation.
$conversation = $this->getConversationsQuery()
->where('state', Conversation::STATE_PUBLISHED)
->where('status', Conversation::STATUS_ACTIVE)
->orderBy($this->getWaitingSinceField(), 'asc')
->first();
if ($conversation) {
return $conversation->getWaitingSince($this);
} else {
return '';
}
}

/**
* Get conversation field used to detect waiting since time.
* @return [type] [description]
*/
public function getWaitingSinceField()
{
if ($this->type == \App\Folder::TYPE_CLOSED) {
return 'closed_at';
} elseif ($this->type == \App\Folder::TYPE_DRAFTS) {
return 'updated_at';
} elseif ($this->type == \App\Folder::TYPE_DELETED) {
return 'user_updated_at';
} else {
return'last_reply_at';
}
}
}
27 changes: 26 additions & 1 deletion app/Http/Controllers/ConversationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function view(Request $request, $id)
/**
* New conversation.
*/
public function create($mailbox_id)
public function create(Request $request, $mailbox_id)
{
$mailbox = Mailbox::findOrFail($mailbox_id);
$this->authorize('view', $mailbox);
Expand All @@ -186,8 +186,33 @@ public function create($mailbox_id)

$after_send = $mailbox->getUserSettings(auth()->user()->id)->after_send;

// Create conversation from thread
$thread = null;
if (!empty($request->from_thread_id)) {
$orig_thread = Thread::find($request->from_thread_id);
if ($orig_thread) {
$conversation->subject = $orig_thread->conversation->subject;
$conversation->subject = preg_replace("/^Fwd:/i", 'Re: ', $conversation->subject);

$thread = new \App\Thread();
$thread->body = $orig_thread->body;
// If this is a forwarded message, try to fetch From
preg_match_all("/From:[^<\n]+<([^<\n]+)>/m", html_entity_decode(strip_tags($thread->body)), $m);

if (!empty($m[1])) {
foreach ($m[1] as $value) {
if (\MailHelper::validateEmail($value)) {
$thread->to = json_encode([$value]);
break;
}
}
}
}
}

return view('conversations/create', [
'conversation' => $conversation,
'thread' => $thread,
'mailbox' => $mailbox,
'folder' => $folder,
'folders' => $mailbox->getAssesibleFolders(),
Expand Down
4 changes: 4 additions & 0 deletions app/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ public function getMainFolders()
->orWhere(function ($query2) {
$query2->where(['type' => Folder::TYPE_MINE]);
$query2->where(['user_id' => auth()->user()->id]);
})
->orWhere(function ($query3) {
$query3->where(['type' => Folder::TYPE_STARRED]);
$query3->where(['user_id' => auth()->user()->id]);
});
})
->orderBy('type')
Expand Down
11 changes: 11 additions & 0 deletions app/Misc/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@ public static function sanitizeEmails($emails)
return $emails_array;
}

/**
* Check if email format is valid.
*
* @param [type] $email [description]
* @return [type] [description]
*/
public static function validateEmail($email)
{
return filter_var($email, FILTER_VALIDATE_EMAIL);
}

/**
* Send system alert to super admin.
*/
Expand Down
7 changes: 7 additions & 0 deletions app/Observers/ThreadObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public function created(Thread $thread)
if ($conversation->source_via == Conversation::PERSON_CUSTOMER) {
$conversation->read_by_user = false;
}

// Update preview.
if (in_array($thread->type, [Thread::TYPE_CUSTOMER, Thread::TYPE_MESSAGE, Thread::TYPE_NOTE]) && $thread->state == Thread::STATE_PUBLISHED && $conversation->threads_count > 1)
{
$conversation->setPreview($thread->body);
}

$conversation->save();
}
}
7 changes: 6 additions & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
| or any other location as required by the application or its packages.
*/

'version' => '1.1.0',
'version' => '1.1.1',

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -208,6 +208,11 @@
'main_light' => '#0078d7',
'main_dark' => '#005a9e',
'note' => '#ffc646',
'text_note' => '#e6b216',
'text_customer' => '#8d959b',
'text_user' => '#3197d6',
'bg_user_reply' => '#f1f7fc',
'bg_note' => '#fffbf1',
],

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function up()
// For incoming messages values are stored as is
$table->text('cc')->nullable(); // JSON
$table->text('bcc')->nullable(); // JSON
// Preview stores the body of the latest reply or note.
$table->string('preview', Conversation::PREVIEW_MAXLENGTH);
// The imported field enables conversation to be created for historical purposes
// (i.e. if moving from a different platform, you can import your history).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up()
{
if (version_compare(config('app.version'), '1.0.6', '<')) {
Schema::table('mailboxes', function (Blueprint $table) {
$table->string('in_password', 512)->change();
$table->string('in_password', 512)->nullable()->change();
});

foreach (\App\Mailbox::whereNotNull('in_password')->get() as $Mailbox) {
Expand All @@ -41,7 +41,7 @@ public function down()
}

Schema::table('mailboxes', function (Blueprint $table) {
$table->string('in_password')->change();
$table->string('in_password', 255)->nullable()->change();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddLocaleColumnToUsersTable extends Migration
{
Expand Down
Loading

0 comments on commit 84006b6

Please sign in to comment.