Skip to content

Commit 2a1b1cc

Browse files
committed
canvadocs annotations
closes CNVS-20189 closes CNVS-19902 Test plan: * enable canvadocs and crocodoc * enable annotations for canvadocs * homework submissions should be submitted to canvadocs (not crocodoc) (you can tell the difference by the unicycling panda) * disable crocodoc * annotations should still work (through canvadocs) * annotations should work the same as they did through crocodoc (but now more doc types are supported) Change-Id: I041b40d2545545c7766b57f98ea194b777fdef30 Reviewed-on: https://gerrit.instructure.com/59697 Tested-by: Jenkins Reviewed-by: Josh Simpson <jsimpson@instructure.com> QA-Review: Jason Carter <jcarter@instructure.com> Product-Review: Cameron Matheson <cameron@instructure.com>
1 parent fc90918 commit 2a1b1cc

7 files changed

Lines changed: 87 additions & 16 deletions

File tree

app/controllers/canvadoc_sessions_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def show
3131

3232
if attachment.canvadocable?
3333
attachment.submit_to_canvadocs unless attachment.canvadoc_available?
34-
url = attachment.canvadoc.session_url
34+
url = attachment.canvadoc.session_url(user: @current_user)
3535
redirect_to url
3636
else
3737
render :text => "Not found", :status => :not_found

app/models/attachment.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,10 @@ def submit_to_canvadocs(attempt = 1, opts = {})
11981198
# ... or crocodoc (this will go away soon)
11991199
return if Attachment.skip_3rd_party_submits?
12001200

1201-
if opts[:wants_annotation] && crocodocable?
1201+
submit_to_crocodoc_instead = opts[:wants_annotation] &&
1202+
crocodocable? &&
1203+
!Canvadocs.annotations_supported?
1204+
if submit_to_crocodoc_instead
12021205
# get crocodoc off the canvadocs strand
12031206
# (maybe :wants_annotation was a dumb idea)
12041207
send_later_enqueue_args :submit_to_crocodoc, {
@@ -1208,12 +1211,12 @@ def submit_to_canvadocs(attempt = 1, opts = {})
12081211
}, attempt
12091212
elsif canvadocable?
12101213
doc = canvadoc || create_canvadoc
1211-
doc.upload
1214+
doc.upload annotatable: opts[:wants_annotation]
12121215
update_attribute(:workflow_state, 'processing')
12131216
end
12141217
rescue => e
12151218
update_attribute(:workflow_state, 'errored')
1216-
Canvas::Errors.capture(e, type: :canvadocs, attachment_id: id)
1219+
Canvas::Errors.capture(e, type: :canvadocs, attachment_id: id, annotatable: opts[:wants_annotation])
12171220

12181221
if attempt <= Setting.get('max_canvadocs_attempts', '5').to_i
12191222
send_later_enqueue_args :submit_to_canvadocs, {

app/models/canvadoc.rb

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,69 @@ class Canvadoc < ActiveRecord::Base
2121

2222
belongs_to :attachment
2323

24-
def upload
24+
def upload(opts = {})
2525
return if document_id.present?
2626

2727
url = attachment.authenticated_s3_url(:expires => 1.day)
2828

2929
response = Canvas.timeout_protection("canvadocs") {
30-
canvadocs_api.upload(url)
30+
canvadocs_api.upload(url, opts)
3131
}
3232

3333
if response && response['id']
34-
update_attributes document_id: response['id'], process_state: response['status']
34+
self.document_id = response['id']
35+
self.process_state = response['status']
36+
self.has_annotations = opts[:annotatable]
37+
self.save!
3538
elsif response.nil?
3639
raise "no response received (request timed out?)"
3740
else
3841
raise response.inspect
3942
end
4043
end
4144

42-
def session_url
45+
def session_url(opts = {})
46+
user = opts.delete(:user)
47+
opts.merge! annotation_opts(user)
48+
4349
Canvas.timeout_protection("canvadocs", raise_on_timeout: true) do
44-
session = canvadocs_api.session(document_id)
50+
session = canvadocs_api.session(document_id, opts)
4551
canvadocs_api.view(session["id"])
4652
end
4753
end
4854

55+
def annotation_opts(user)
56+
return {} if !user || !has_annotations?
57+
58+
opts = {
59+
annotation_context: "default",
60+
permissions: "readwrite",
61+
user_id: user.global_id,
62+
user_name: user.short_name.gsub(",", ""),
63+
user_role: "",
64+
user_filter: user.global_id,
65+
}
66+
67+
submissions = attachment.attachment_associations.
68+
where(:context_type => 'Submission').
69+
preload(context: [:assignment]).
70+
map(&:context)
71+
72+
return opts if submissions.empty?
73+
74+
if submissions.any? { |s| s.grants_right? user, :read_grade }
75+
opts.delete :user_filter
76+
end
77+
78+
# no commenting when anonymous peer reviews are enabled
79+
if submissions.map(&:assignment).any? { |a| a.peer_reviews? && a.anonymouis_peer_reviews? }
80+
opts = {}
81+
end
82+
83+
opts
84+
end
85+
private :annotation_opts
86+
4987
def available?
5088
!!(document_id && process_state != 'error' && Canvadocs.enabled?)
5189
end

app/views/plugins/_canvadocs_settings.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
<td><%= f.blabel :base_url, :en => "Base URL" %></td>
1515
<td><%= f.text_field :base_url %></td>
1616
</tr>
17+
<tr>
18+
<td><%= f.blabel :annotations_supported, :en => "Annotations Supported" %></td>
19+
<td><%= f.check_box :annotations_supported %></td>
1720
</table>
1821
<% end %>
1922

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddHasAnnotationsToCanvadocs < ActiveRecord::Migration
2+
tag :predeploy
3+
4+
def change
5+
add_column :canvadocs, :has_annotations, :bool
6+
end
7+
end

lib/canvadocs.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,20 @@ def initialize(opts)
3535
# Public: Create a document with the file at the given url.
3636
#
3737
# obj - a url string
38+
# params - other post params
3839
#
3940
# Examples
4041
#
4142
# upload("http://www.example.com/test.doc")
4243
# # => { "id": 1234, "status": "queued" }
4344
#
4445
# Returns a hash containing the document's id and status
45-
def upload(obj)
46+
def upload(obj, extra_params = {})
4647
params = if obj.is_a?(File)
47-
{ :file => obj }
48+
{ :file => obj }.merge(extra_params)
4849
raise Canvadocs::Error, "TODO: support raw files"
4950
else
50-
{ :url => obj.to_s }
51+
{ :url => obj.to_s }.merge(extra_params)
5152
end
5253

5354
raw_body = api_call(:post, "documents", params)
@@ -175,4 +176,8 @@ def self.config
175176
def self.enabled?
176177
!!config
177178
end
179+
180+
def self.annotations_supported?
181+
Canvas::Plugin.value_to_boolean config["annotations_supported"]
182+
end
178183
end

spec/models/attachment_spec.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ def configure_crocodoc
6969
Crocodoc::API.any_instance.stubs(:upload).returns 'uuid' => '1234567890'
7070
end
7171

72+
def configure_canvadocs(opts = {})
73+
ps = PluginSetting.where(name: "canvadocs").first_or_create
74+
ps.update_attribute :settings, {
75+
"api_key" => "blahblahblahblahblah",
76+
"base_url" => "http://example.com",
77+
"annotations_supported" => true
78+
}.merge(opts)
79+
end
80+
7281
context "crocodoc" do
7382
include HmacHelper
7483
let_once(:user) { user_model }
@@ -148,9 +157,7 @@ def configure_crocodoc
148157

149158
context "canvadocs" do
150159
before :once do
151-
PluginSetting.create! :name => 'canvadocs',
152-
:settings => {"api_key" => "blahblahblahblahblah",
153-
"base_url" => "http://example.com"}
160+
configure_canvadocs
154161
end
155162

156163
before :each do
@@ -183,8 +190,16 @@ def configure_crocodoc
183190
}
184191
end
185192

186-
it "prefers crocodoc when annotation is requested" do
193+
it "sends annotatable documents to canvadocs if supported" do
194+
configure_crocodoc
195+
a = crocodocable_attachment_model
196+
a.submit_to_canvadocs 1, wants_annotation: true
197+
expect(a.canvadoc).not_to be_nil
198+
end
199+
200+
it "prefers crocodoc when annotation is requested and canvadocs can't annotate" do
187201
configure_crocodoc
202+
configure_canvadocs "annotations_supported" => false
188203
Setting.set('canvadoc_mime_types',
189204
(Canvadoc.mime_types << "application/blah").to_json)
190205

0 commit comments

Comments
 (0)