forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutable.rb
80 lines (69 loc) · 2.59 KB
/
mutable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module Mutable
attr_accessor :recently_unmuted
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def declare_mutable_broadcast_policy(options)
policy = options[:policy]
participants = options[:participants]
policy.dispatch :assignment_unmuted
policy.to { participants }
policy.whenever do |record|
@recently_unmuted
end
end
end
def mute!
self.update_attribute(:muted, true)
clear_sent_messages
hide_stream_items
end
def unmute!
self.update_attribute(:muted, false)
broadcast_unmute_event
show_stream_items
end
def broadcast_unmute_event
@recently_unmuted = true
self.save!
@recently_unmuted = false
end
protected
def clear_sent_messages
self.clear_broadcast_messages if self.respond_to? :clear_broadcast_messages
end
def hide_stream_items
if self.respond_to? :submissions
stream_items = StreamItem.select([:id, :context_type, :context_id]).
where(:asset_type => 'Submission', :asset_id => submissions).
includes(:context).to_a
stream_item_contexts = stream_items.map { |si| [si.context_type, si.context_id] }
associated_shards = stream_items.inject([]) { |result, si| result | si.associated_shards }
Shard.with_each_shard(associated_shards) do
StreamItemInstance.where(:stream_item_id => stream_items).
update_all_with_invalidation(stream_item_contexts, :hidden => true)
end
end
end
def show_stream_items
if self.respond_to? :submissions
submissions = submissions(:include => {:hidden_submission_comments => :author})
stream_items = StreamItem.select([:id, :context_type, :context_id]).
where(:asset_type => 'Submission', :asset_id => submissions).
includes(:context).to_a
stream_item_contexts = stream_items.map { |si| [si.context_type, si.context_id] }
associated_shards = stream_items.inject([]) { |result, si| result | si.associated_shards }
Shard.with_each_shard(associated_shards) do
StreamItemInstance.where(:hidden => true, :stream_item_id => stream_items).
update_all_with_invalidation(stream_item_contexts, :hidden => false)
end
outstanding = submissions.map{ |submission|
comments = submission.hidden_submission_comments.all
next if comments.empty?
[submission, comments.map(&:author_id).uniq.size == 1 ? [comments.last.author] : []]
}.compact
SubmissionComment.where(:hidden => true, :submission_id => submissions).update_all(:hidden => false)
end
end
end