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

Commit

Permalink
Merge pull request #316 from git-jls/unread_messages_for_user
Browse files Browse the repository at this point in the history
Unread ticket status per user.
  • Loading branch information
frenkel authored Nov 30, 2016
2 parents 5a51247 + 9de1dbb commit ccd50cb
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Rule management interface has been improved, making it understandable for more users.
- Tabindex for a better user experience when filling out ticket forms. Constributed by @git-jls.
- Allows adding rules to filter on cc and actual to addresses. Constributed by @git-jls.
- Unread ticket status, so the user can differentiate read/unread tickets in the inbox. Constributed by @git-jls.


### Changed
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ Some users have made requests for the following features. If you would like to c
- Integration with OpsWeekly
- Social media integration such as FreshDesk and Zoho have (reply to requests via social media)
- Ticket creation api (and improving existing api)
- Unread ticket status per user.
- Ticket search that also searches in from field and replies.
- Mark tickets as duplicate, linking it to the duplicated ticket.
- Ability to rename tickets (change their subject).
Expand Down
6 changes: 5 additions & 1 deletion app/assets/stylesheets/_inbox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@
color: $body-font-color;
}
}
}
}

.unread {
background: #f4f4f4;
}
12 changes: 12 additions & 0 deletions app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class TicketsController < ApplicationController
skip_before_action :verify_authenticity_token, only: :create, if: 'request.format.json?'

def show
# first time seeing this ticket?
@ticket.mark_read current_user if @ticket.is_unread? current_user

@agents = User.agents

draft = @ticket.replies
Expand Down Expand Up @@ -232,6 +235,15 @@ def can_create_a_ticket(using_hook)
end
end

def send_notification_email
if !@ticket.nil? && @ticket.save
# we set ticket as unread for every user
@ticket.unread_users << User.all
# signed in we notify
notify_incoming @ticket
end
end

def current_tenant
@tenant = Tenant.current_tenant
end
Expand Down
3 changes: 2 additions & 1 deletion app/mailers/ticket_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def receive(email)
message_id: email.message_id,
content_type: content_type,
to_email_address: to_email_address,
raw_message: StringIO.new(email.to_s)
raw_message: StringIO.new(email.to_s),
unread_users: User.all # none of the users could have read this
})

incoming = ticket
Expand Down
10 changes: 10 additions & 0 deletions app/models/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Ticket < ActiveRecord::Base

has_many :status_changes, dependent: :destroy

has_and_belongs_to_many :unread_users, class_name: 'User'

enum status: [:open, :closed, :deleted, :waiting, :merged]
enum priority: [:unknown, :low, :medium, :high]

Expand Down Expand Up @@ -124,6 +126,14 @@ def set_default_notifications!
self.notified_user_ids = users.map(&:id)
end

def is_unread?(user)
unread_users.include? user
end

def mark_read(user)
unread_users.delete user
end

def status_times
total = {}

Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class User < ActiveRecord::Base
# identities for omniauth
has_many :identities

has_and_belongs_to_many :unread_tickets, class_name: 'Ticket'

after_initialize :default_localization
before_validation :generate_password

Expand Down
2 changes: 1 addition & 1 deletion app/views/tickets/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
</li>
</ul>
<% @tickets.each do |ticket| %>
<div class="priority-<%= ticket.priority %> row ticket bb" style="height: 4em;" data-ticket-url="<%= ticket_url(ticket) %>">
<div class="priority-<%= ticket.priority %> <%= ticket.is_unread?(current_user) ? 'unread' : '' %> row ticket bb" style="height: 4em;" data-ticket-url="<%= ticket_url(ticket) %>">
<div class="medium-6 plxxl v-middle columns">
<input type="checkbox" value="<%= ticket.id %>" name="id[]" class="no-mb" data-toggle-check />
<%= user_avatar ticket.user, class: 'avatar left mrl mts mlm', size: 30 %>
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20161125141914_create_join_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateJoinTable < ActiveRecord::Migration
def change
create_join_table :tickets, :users do |t|
# t.index [:ticket_id, :user_id]
# t.index [:user_id, :ticket_id]
end
end
end
57 changes: 57 additions & 0 deletions test/controllers/tickets_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,61 @@ class TicketsControllerTest < ActionController::TestCase
assert_response :success
assert_match replies(:solution).content, @response.body
end

test 'should mark new ticket from MTA as unread for all users' do
assert_difference 'Ticket.count' do

post :create, message: @simple_email, format: :json

assert_response :success

ticket = Ticket.last
assert_not_nil ticket.unread_users.nil?
end
end

test 'should mark new ticket as unread for all users' do
assert_difference 'Ticket.count' do
post :create, ticket: {
from: 'test@test.nl',
content: @ticket.content,
subject: @ticket.subject,
}

assert_response :success

ticket = Ticket.last

assert_not_nil ticket.unread_users.nil?
end
end

test 'should mark new ticket as unread for all users when posted from MTA' do
assert_difference 'Ticket.count' do

post :create, message: @simple_email, format: :json

assert_response :success

ticket = Ticket.last
assert_not_nil ticket.unread_users
end
end

test 'should mark ticket as read when clicked' do
user = users(:alice)
sign_in user
ticket = Ticket.last
ticket.unread_users << User.all
assert_difference 'Ticket.last.unread_users.count', -1 do

assert_not_nil ticket.unread_users

get :show, id: ticket.id

assert_response :success

assert_not ticket.unread_users.include?(user)
end
end
end

0 comments on commit ccd50cb

Please sign in to comment.