Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,28 @@ def connect
protected

def find_verified_user
if verified_user = env['warden'].user
if verified_user = env['warden'].user || user_from_token
verified_user
else
reject_unauthorized_connection
end
end

def user_from_token
username = request.params[:username]
auth_token = request.params[:authToken] || request.params[:auth_token] || request.params[:Auth_Token]
return if username.blank? || auth_token.blank?

user = User.eager_load(:role, :auth_tokens).find_by(username: username)
token = user&.token_for_text?(auth_token, :general)
return if token.blank?

if token.auth_token_expiry > Time.zone.now
user
else
token.destroy!
nil
end
end
end
end
13 changes: 13 additions & 0 deletions app/channels/task_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class TaskChannel < ApplicationCable::Channel
include AuthorisationHelpers

def subscribed
project = Project.find_by(id: params[:project_id])
task_definition = project&.unit&.task_definitions&.find_by(id: params[:task_definition_id])

return reject unless project && task_definition && authorise?(current_user, project, :get)
return reject unless project.has_task_for_task_definition?(task_definition)

stream_for project.task_for_task_definition(task_definition)
end
end
6 changes: 6 additions & 0 deletions app/models/comments/task_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ class TaskComment < ApplicationRecord
mark_as_read(self.user)
end

after_commit :broadcast_comment_created, on: :create

# Delete action - before dependent association
before_destroy :delete_associated_files

def broadcast_comment_created
TaskChannel.broadcast_to(task, event: 'comment_created')
end

def valid_reply_to?
if reply_to_id.present?
originalTaskComment = TaskComment.find(reply_to_id)
Expand Down
5 changes: 5 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def specific_permission_hash(role, perm_hash, _other)
delegate :update_task_stats, to: :project

after_update :update_task_stats, if: :saved_change_to_task_status_id? # TODO: consider moving to async task
after_commit :broadcast_status_change, on: :update, if: :saved_change_to_task_status_id?

validates :task_definition_id, uniqueness: { scope: :project,
message: 'must be unique within the project' }
Expand Down Expand Up @@ -897,6 +898,10 @@ def add_status_comment(current_user, status)
comment
end

def broadcast_status_change
TaskChannel.broadcast_to(self, event: 'status_changed')
end

def add_discussed_comment(current_user)
comment = 'Discussed in class'

Expand Down
10 changes: 10 additions & 0 deletions config/cable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
development:
adapter: async

test:
adapter: test

production:
adapter: redis
url: <%= ENV.fetch("DF_REDIS_CABLE_URL", ENV.fetch("DF_REDIS_SIDEKIQ_URL", "redis://localhost:6379/1")) %>
channel_prefix: doubtfire_production
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
get 'api/units/:id/all_resources', to: 'lecture_resource_downloads#index'

mount ApiRoot => '/'
mount ActionCable.server => '/cable'
mount GrapeSwaggerRails::Engine => '/api/docs'
mount Sidekiq::Web => "/sidekiq" # mount Sidekiq::Web in your Rails app

Expand Down
Loading