Skip to content
This repository has been archived by the owner on Dec 22, 2018. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
Conflicts:
	CHANGELOG.md
  • Loading branch information
Sem Goedknegt committed Nov 6, 2015
2 parents 2f6942e + 28c54dd commit 33c6e63
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 98 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## Brimir unreleased.unreleased.unreleased (to be announced)
### Added
- Incoming email address selection on new ticket screen. Allowing rules to be applied for manually created tickets.
- To/cc address now becomes visible as notified users for all incoming mails.
- User preference to disable quoting of original message in the reply form.

### Changed
- Signatures are added to new tickets again.
- Realigned sign in view.
- Customers can only see replies they were notified of, allowing replies to be used as internal communication between agents.
- Reply notifications are now sent to the same users as the last reply by default.

### Deprecated

Expand Down
28 changes: 6 additions & 22 deletions app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ def show
.where(draft: true)
.first

@replies = @ticket.replies.chronologically.without_drafts.select do |reply|
can? :show, reply
end

if draft.present?
@reply = draft
else
@reply = @ticket.replies.new(user: current_user)
@reply.reply_to = @replies.last || @ticket
@reply.set_default_notifications!
end

Expand Down Expand Up @@ -139,28 +144,7 @@ def create
end

if !@ticket.nil? && @ticket.save

Rule.apply_all(@ticket) unless @ticket.is_a?(Reply)

# where user notifications added?
if @ticket.notified_users.count == 0
@ticket.set_default_notifications!
end

# @ticket might be a Reply when via json post
if @ticket.is_a?(Ticket)
if @ticket.assignee.nil?
@ticket.notified_users.each do |user|
mail = NotificationMailer.new_ticket(@ticket, user)
mail.deliver_now unless EmailAddress.pluck(:email).include?(user.email)
@ticket.message_id = mail.message_id
end

@ticket.save
else
NotificationMailer.assigned(@ticket).deliver_now
end
end
NotificationMailer.incoming_message(@ticket, params[:message])
end

respond_to do |format|
Expand Down
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def user_params
:locale,
:per_page,
:prefer_plain_text,
:include_quote_in_reply,
label_ids: []
)

Expand Down
61 changes: 61 additions & 0 deletions app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,67 @@ class NotificationMailer < ActionMailer::Base

add_template_helper HtmlTextHelper

def self.incoming_message(ticket_or_reply, original_message)
if ticket_or_reply.is_a? Reply
reply = ticket_or_reply
reply.set_default_notifications!

message_id = nil

reply.notified_users.each do |user|
message = NotificationMailer.new_reply(reply, user)
message.message_id = message_id
message.deliver_now

reply.message_id = message.message_id
message_id = message.message_id
end

reply.save
else
ticket = ticket_or_reply

Rule.apply_all ticket

# where user notifications added?
if ticket.notified_users.count == 0
ticket.set_default_notifications!
end

if ticket.assignee.nil?
message_id = nil

ticket.notified_users.each do |user|
message = NotificationMailer.new_ticket(ticket, user)
message.message_id = message_id
message.deliver_now unless EmailAddress.pluck(:email).include?(user.email)

ticket.message_id = message.message_id
message_id = message.message_id
end

ticket.save
else
NotificationMailer.assigned(ticket).deliver_now
end
end

original_message = Mail.new(original_message)

# store original cc/to users as well
(original_message.to.to_a + original_message.cc.to_a).each do |email|
next if EmailAddress.pluck(:email).include?(email)

user = User.find_first_by_auth_conditions(email: email)
if user.nil?
ticket_or_reply.notified_users << User.create(email: email)
else
next if ticket_or_reply.notified_users.include?(user)
ticket_or_reply.notified_users << user
end
end
end

def new_ticket(ticket, user)
unless user.locale.blank?
@locale = user.locale
Expand Down
17 changes: 3 additions & 14 deletions app/mailers/ticket_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def receive(email)
subject = email.subject.to_s.encode('UTF-8')
end


if email.in_reply_to
# is this a reply to a ticket or to another reply?
response_to = Ticket.find_by_message_id(email.in_reply_to)
Expand Down Expand Up @@ -100,7 +99,9 @@ def receive(email)
from: from_address,
message_id: email.message_id,
content_type: content_type,
raw_message: StringIO.new(email.to_s)
raw_message: StringIO.new(email.to_s),
reply_to_id: response_to.try(:id),
reply_to_type: response_to.try(:class).try(:name)
})

else
Expand Down Expand Up @@ -148,18 +149,6 @@ def receive(email)

end

if ticket != incoming
incoming.set_default_notifications!

incoming.notified_users.each do |user|
mail = NotificationMailer.new_reply(incoming, user)
mail.deliver_now
incoming.message_id = mail.message_id
end

incoming.save
end

if bounced?(email)
nil
else
Expand Down
21 changes: 14 additions & 7 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,20 @@ def initialize(user)
protected

def customer(user)
# customers can view their own tickets, its replies and attachments
can [:create, :read], Reply, ticket: { user_id: user.id }
# customers can view replies where they were notified of
can :read, Reply do |reply|
reply.notified_user_ids.include? user.id || reply.user_id == user.id
end
# customers can view their own replies
can :read, Reply, user_id: user.id
# customers can reply to their own tickets
can :create, Reply, ticket: { user_id: user.id }

# customers can reply when they have access to the label
can :create, Reply do |reply|
# at least one label_id overlap or ticket of user himself
(reply.ticket.label_ids & user.label_ids).size > 0
end

# customers can edit their own account
can :update, User, id: user.id
Expand All @@ -51,11 +63,6 @@ def customer(user)
# at least one label_id overlap
ticket.user == user || (ticket.label_ids & user.label_ids).size > 0
end

can [:create, :read], Reply do |reply|
# at least one label_id overlap
(reply.ticket.label_ids & user.label_ids).size > 0
end
end

def limited_agent(user)
Expand Down
41 changes: 23 additions & 18 deletions app/models/reply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Reply < ActiveRecord::Base
include CreateFromUser
include EmailMessage

attr_accessor :reply_to_id
attr_accessor :reply_to_type

has_many :notifications, as: :notifiable, dependent: :destroy
has_many :notified_users, source: :user, through: :notifications

Expand All @@ -45,29 +48,31 @@ class Reply < ActiveRecord::Base
}

def set_default_notifications!
users = users_to_notify.select do |user|
Ability.new(user).can? :show, self
unless reply_to_type.nil?
self.notified_users = [reply_to.user] + reply_to.notified_users - [user]
else
if ticket.assignee.present?
self.notified_users << ticket.assignee
else
self.notified_users = User.agents_to_notify
end

ticket.labels.each do |label|
self.notified_users += label.users
end
end
self.notified_user_ids = users.map(&:id)
end

def other_replies
ticket.replies.where.not(id: id)
def reply_to
reply_to_type.constantize.where(id: self.reply_to_id).first
end

def users_to_notify
to = [ticket.user] + other_replies.map(&:user)

if ticket.assignee.present?
to << ticket.assignee
else
to += User.agents_to_notify
end

ticket.labels.each do |label|
to += label.users
end
def reply_to=(value)
self.reply_to_id = value.id
self.reply_to_type = value.class.name
end

to.uniq - [user]
def other_replies
ticket.replies.where.not(id: id)
end
end
20 changes: 8 additions & 12 deletions app/views/replies/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,19 @@
else
content = "<p></p>#{current_user.signature}<br />"
end
reply_to = @reply.other_replies.where.not(draft: true).order(:id).last

if reply_to.nil?
reply_to = @reply.ticket
end

content += t(:on_date_author_wrote, author: reply_to.user.email,
date: l(reply_to.created_at.in_time_zone(current_user.time_zone), format: :long))
if current_user.include_quote_in_reply?
content += t(:on_date_author_wrote, author: @reply.reply_to.user.email,
date: l(@reply.reply_to.created_at.in_time_zone(current_user.time_zone), format: :long))

if current_user.prefer_plain_text?
content += "\n#{wrap_and_quote(reply_to.content)}"
else
content += "<br />#{text_to_html(wrap_and_quote(reply_to.content))}"
if current_user.prefer_plain_text?
content += "\n#{wrap_and_quote(@reply.reply_to.content)}"
else
content += "<br />#{text_to_html(wrap_and_quote(@reply.reply_to.content))}"
end
end
%>


<p>
<%= r.text_area :content, label: false, class: ('tinymce' unless current_user.prefer_plain_text?),
value: (@reply.content.nil? ? content : @reply.content) %>
Expand Down
13 changes: 3 additions & 10 deletions app/views/tickets/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,20 @@

<% if @ticket.replies.size > 1 %>
<div class="row replies">
<% count = 0 %>
<% @ticket.replies.chronologically.without_drafts.each do |reply| %>
<%# minus 2, because @reply is a new ticket that is counted as well... %>
<% if @ticket.replies.size - 2 == count %>
<% @replies.each_with_index do |reply, index| %>
<% if @replies.count - 1 == index %>
<% div_class = 'active' %>
<% else %>
<% div_class = '' %>
<% end %>
<%= render reply, div_class: div_class %>
<% count += 1 %>
<% end %>
</div>
<% end %>
<% if can? :create, @reply %>
<%= render 'replies/form' %>
<%= render 'replies/form', reply_to: @reply_to %>
<% end %>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<h5><%= t(:email_settings) %></h5>

<%= f.check_box :prefer_plain_text %>
<%= f.check_box :include_quote_in_reply %>
<% if @user.agent? %>
Expand All @@ -40,6 +41,7 @@
<%= f.text_area :signature, rows: 5, cols: 20, class: 'tinymce mbl' %>
<% end %>
<% if can? :create, Labeling.new(labelable: @user) %>
<h5><%= t(:access_control) %></h5>

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20151106141707_add_quote_preference_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddQuotePreferenceToUser < ActiveRecord::Migration
def change
add_column :users, :include_quote_in_reply, :boolean, default: true, null: false
end
end
19 changes: 12 additions & 7 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150915063710) do
ActiveRecord::Schema.define(version: 20151106141707) do

create_table "attachments", force: :cascade do |t|
t.integer "attachable_id"
Expand Down Expand Up @@ -116,12 +116,16 @@
create_table "tenants", force: :cascade do |t|
t.string "domain"
t.string "from"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "default_time_zone", default: "Amsterdam"
t.boolean "ignore_user_agent_locale", default: false, null: false
t.string "default_locale", default: "en"
t.boolean "share_drafts", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "default_time_zone", default: "Amsterdam"
t.boolean "ignore_user_agent_locale", default: false, null: false
t.string "default_locale", default: "en"
t.boolean "share_drafts", default: false
t.boolean "ticket_creation_requires_authenticated_user", default: false, null: false
t.string "ticket_creation_whitelisted_ips"
t.boolean "require_authenticated", default: false, null: false
t.string "require_authenticated_ip_whitelist"
end

create_table "tickets", force: :cascade do |t|
Expand Down Expand Up @@ -173,6 +177,7 @@
t.integer "per_page", default: 30, null: false
t.string "locale"
t.boolean "prefer_plain_text", default: false, null: false
t.boolean "include_quote_in_reply", default: true, null: false
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
Expand Down
Loading

0 comments on commit 33c6e63

Please sign in to comment.