canvas_sync 0.10.2 → 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +5 -5
  2. data/lib/canvas_sync/generators/install_live_events_generator.rb +5 -1
  3. data/lib/canvas_sync/generators/templates/migrations/create_accounts.rb +1 -1
  4. data/lib/canvas_sync/generators/templates/migrations/create_admins.rb +1 -1
  5. data/lib/canvas_sync/generators/templates/migrations/create_assignment_groups.rb +1 -1
  6. data/lib/canvas_sync/generators/templates/migrations/create_assignments.rb +1 -1
  7. data/lib/canvas_sync/generators/templates/migrations/create_context_module_items.rb +1 -1
  8. data/lib/canvas_sync/generators/templates/migrations/create_context_modules.rb +1 -1
  9. data/lib/canvas_sync/generators/templates/migrations/create_courses.rb +1 -1
  10. data/lib/canvas_sync/generators/templates/migrations/create_enrollments.rb +1 -1
  11. data/lib/canvas_sync/generators/templates/migrations/create_roles.rb +1 -1
  12. data/lib/canvas_sync/generators/templates/migrations/create_sections.rb +1 -1
  13. data/lib/canvas_sync/generators/templates/migrations/create_submissions.rb +1 -1
  14. data/lib/canvas_sync/generators/templates/migrations/create_terms.rb +1 -1
  15. data/lib/canvas_sync/generators/templates/migrations/create_users.rb +1 -1
  16. data/lib/canvas_sync/generators/templates/models/account.rb +1 -1
  17. data/lib/canvas_sync/generators/templates/models/admin.rb +1 -1
  18. data/lib/canvas_sync/generators/templates/models/assignment.rb +1 -1
  19. data/lib/canvas_sync/generators/templates/models/assignment_group.rb +1 -1
  20. data/lib/canvas_sync/generators/templates/models/context_module.rb +8 -1
  21. data/lib/canvas_sync/generators/templates/models/context_module_item.rb +8 -1
  22. data/lib/canvas_sync/generators/templates/models/course.rb +1 -1
  23. data/lib/canvas_sync/generators/templates/models/enrollment.rb +1 -1
  24. data/lib/canvas_sync/generators/templates/models/role.rb +1 -1
  25. data/lib/canvas_sync/generators/templates/models/section.rb +2 -1
  26. data/lib/canvas_sync/generators/templates/models/submission.rb +1 -1
  27. data/lib/canvas_sync/generators/templates/models/term.rb +1 -1
  28. data/lib/canvas_sync/generators/templates/models/user.rb +1 -1
  29. data/lib/canvas_sync/generators/templates/services/live_events/assignment_event.rb +33 -0
  30. data/lib/canvas_sync/generators/templates/services/live_events/assignment_group_event.rb +24 -0
  31. data/lib/canvas_sync/generators/templates/services/live_events/base_event.rb +22 -0
  32. data/lib/canvas_sync/generators/templates/services/live_events/course_event.rb +20 -0
  33. data/lib/canvas_sync/generators/templates/services/live_events/course_section_event.rb +18 -0
  34. data/lib/canvas_sync/generators/templates/services/live_events/enrollment_event.rb +20 -0
  35. data/lib/canvas_sync/generators/templates/services/live_events/grade_event.rb +26 -0
  36. data/lib/canvas_sync/generators/templates/services/live_events/module_event.rb +16 -0
  37. data/lib/canvas_sync/generators/templates/services/live_events/module_item_event.rb +15 -0
  38. data/lib/canvas_sync/generators/templates/services/live_events/submission_event.rb +18 -0
  39. data/lib/canvas_sync/generators/templates/services/live_events/syllabus_event.rb +21 -0
  40. data/lib/canvas_sync/generators/templates/services/live_events/user_event.rb +19 -0
  41. data/lib/canvas_sync/version.rb +1 -1
  42. data/spec/canvas_sync/services/module_event_spec.rb +2 -2
  43. data/spec/canvas_sync/services/module_item_event_spec.rb +2 -2
  44. data/spec/dummy/app/models/account.rb +1 -1
  45. data/spec/dummy/app/models/admin.rb +1 -1
  46. data/spec/dummy/app/models/assignment.rb +1 -1
  47. data/spec/dummy/app/models/assignment_group.rb +3 -2
  48. data/spec/dummy/app/models/context_module.rb +8 -1
  49. data/spec/dummy/app/models/context_module_item.rb +8 -1
  50. data/spec/dummy/app/models/course.rb +1 -1
  51. data/spec/dummy/app/models/enrollment.rb +1 -1
  52. data/spec/dummy/app/models/role.rb +1 -1
  53. data/spec/dummy/app/models/section.rb +2 -1
  54. data/spec/dummy/app/models/submission.rb +1 -1
  55. data/spec/dummy/app/models/term.rb +1 -1
  56. data/spec/dummy/app/models/user.rb +1 -1
  57. data/spec/dummy/app/services/live_events/assignment_event.rb +19 -36
  58. data/spec/dummy/app/services/live_events/base_event.rb +26 -0
  59. data/spec/dummy/app/services/live_events/course_event.rb +15 -27
  60. data/spec/dummy/app/services/live_events/course_section_event.rb +13 -39
  61. data/spec/dummy/app/services/live_events/enrollment_event.rb +17 -48
  62. data/spec/dummy/app/services/live_events/grade_event.rb +9 -34
  63. data/spec/dummy/app/services/live_events/module_event.rb +10 -29
  64. data/spec/dummy/app/services/live_events/module_item_event.rb +10 -28
  65. data/spec/dummy/app/services/live_events/submission_event.rb +12 -42
  66. data/spec/dummy/app/services/live_events/syllabus_event.rb +11 -20
  67. data/spec/dummy/app/services/live_events/user_event.rb +14 -25
  68. data/spec/dummy/db/migrate/{20190603215718_create_users.rb → 20190702203620_create_users.rb} +1 -1
  69. data/spec/dummy/db/migrate/{20190603220011_create_courses.rb → 20190702203621_create_courses.rb} +1 -1
  70. data/spec/dummy/db/migrate/{20190604193942_create_accounts.rb → 20190702203622_create_accounts.rb} +1 -1
  71. data/spec/dummy/db/migrate/{20190603215721_create_terms.rb → 20190702203623_create_terms.rb} +1 -1
  72. data/spec/dummy/db/migrate/{20190603220013_create_enrollments.rb → 20190702203624_create_enrollments.rb} +1 -1
  73. data/spec/dummy/db/migrate/{20190603215722_create_sections.rb → 20190702203625_create_sections.rb} +1 -1
  74. data/spec/dummy/db/migrate/{20190603192310_create_assignments.rb → 20190702203626_create_assignments.rb} +1 -1
  75. data/spec/dummy/db/migrate/{20190521003447_create_submissions.rb → 20190702203627_create_submissions.rb} +1 -1
  76. data/spec/dummy/db/migrate/{20190606161037_create_roles.rb → 20190702203628_create_roles.rb} +1 -1
  77. data/spec/dummy/db/migrate/{20190521003449_create_admins.rb → 20190702203629_create_admins.rb} +1 -1
  78. data/spec/dummy/db/migrate/{20190521003450_create_assignment_groups.rb → 20190702203630_create_assignment_groups.rb} +1 -1
  79. data/spec/dummy/db/migrate/{20190521003451_create_context_modules.rb → 20190702203631_create_context_modules.rb} +1 -1
  80. data/spec/dummy/db/migrate/{20190603193502_create_context_module_items.rb → 20190702203632_create_context_module_items.rb} +1 -1
  81. data/spec/dummy/db/schema.rb +1 -1
  82. metadata +41 -67
  83. data/lib/canvas_sync/generators/templates/services/live_events/assignment/assignment_created_event.rb +0 -6
  84. data/lib/canvas_sync/generators/templates/services/live_events/assignment/assignment_event.rb +0 -54
  85. data/lib/canvas_sync/generators/templates/services/live_events/assignment/assignment_updated_event.rb +0 -6
  86. data/lib/canvas_sync/generators/templates/services/live_events/course/course_created_event.rb +0 -6
  87. data/lib/canvas_sync/generators/templates/services/live_events/course/course_event.rb +0 -36
  88. data/lib/canvas_sync/generators/templates/services/live_events/course/course_updated_event.rb +0 -6
  89. data/lib/canvas_sync/generators/templates/services/live_events/course_section/course_section_created_event.rb +0 -6
  90. data/lib/canvas_sync/generators/templates/services/live_events/course_section/course_section_event.rb +0 -48
  91. data/lib/canvas_sync/generators/templates/services/live_events/course_section/course_section_updated_event.rb +0 -6
  92. data/lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_created_event.rb +0 -6
  93. data/lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb +0 -55
  94. data/lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_updated_event.rb +0 -6
  95. data/lib/canvas_sync/generators/templates/services/live_events/grade/grade_changed_event.rb +0 -6
  96. data/lib/canvas_sync/generators/templates/services/live_events/grade/grade_event.rb +0 -55
  97. data/lib/canvas_sync/generators/templates/services/live_events/module/module_created_event.rb +0 -6
  98. data/lib/canvas_sync/generators/templates/services/live_events/module/module_event.rb +0 -39
  99. data/lib/canvas_sync/generators/templates/services/live_events/module/module_updated_event.rb +0 -6
  100. data/lib/canvas_sync/generators/templates/services/live_events/module_item/module_item_created_event.rb +0 -6
  101. data/lib/canvas_sync/generators/templates/services/live_events/module_item/module_item_event.rb +0 -37
  102. data/lib/canvas_sync/generators/templates/services/live_events/module_item/module_item_updated_event.rb +0 -6
  103. data/lib/canvas_sync/generators/templates/services/live_events/submission/submission_created_event.rb +0 -6
  104. data/lib/canvas_sync/generators/templates/services/live_events/submission/submission_event.rb +0 -52
  105. data/lib/canvas_sync/generators/templates/services/live_events/submission/submission_updated_event.rb +0 -6
  106. data/lib/canvas_sync/generators/templates/services/live_events/syllabus/syllabus_event.rb +0 -34
  107. data/lib/canvas_sync/generators/templates/services/live_events/syllabus/syllabus_updated_event.rb +0 -6
  108. data/lib/canvas_sync/generators/templates/services/live_events/user/user_created_event.rb +0 -6
  109. data/lib/canvas_sync/generators/templates/services/live_events/user/user_event.rb +0 -34
  110. data/lib/canvas_sync/generators/templates/services/live_events/user/user_updated_event.rb +0 -6
  111. data/spec/dummy/db/test.sqlite3 +0 -0
  112. data/spec/dummy/log/development.log +0 -1248
  113. data/spec/dummy/log/test.log +0 -43258
  114. data/spec/support/fixtures/reports/provisioning_csv_unzipped/courses.csv +0 -3
  115. data/spec/support/fixtures/reports/provisioning_csv_unzipped/users.csv +0 -4
@@ -4,51 +4,25 @@
4
4
  # You can customize it as needed, but make sure you test
5
5
  # any changes you make to the auto generated methods.
6
6
  #
7
+ # LiveEvent message formats can be found at https://canvas.instructure.com/doc/api/file.live_events.html
8
+ # In the general case, LiveEvent message content should not be trusted - it is highly possible that events may
9
+ # arrive out of order. Most of the CanvasSync LiveEvent templates solve this by always fetching the object from the API.
10
+ #
7
11
 
8
12
 
9
13
  module LiveEvents
10
14
  class CourseSectionEvent < LiveEvents::BaseEvent
11
- # The following is provided in the live events call:
12
- # {
13
- # course_section_id: section.id,
14
- # sis_id: section.sis_id,
15
- # sis_batch_id: section.sis_batch_id,
16
- # course_id: section.course_id,
17
- # root_account_id: section.root_account_id,
18
- # enrollment_term_id: section.enrollment_term_id,
19
- # name: section.name,
20
- # default_section: section.default_section,
21
- # accepting_enrollments: section.accepting_enrollments,
22
- # can_manually_enroll: section.can_manually_enroll,
23
- # start_at: section.start_at,
24
- # end_at: section.end_at,
25
- # workflow_state: section.workflow_state,
26
- # restrict_enrollments_to_section_dates: section.restrict_enrollments_to_section_dates,
27
- # nonxlist_course_id: section.nonxlist_course_id,
28
- # stuck_sis_fields: section.stuck_sis_fields,
29
- # integration_id: section.integration_id
30
- # }
31
15
 
32
- def perform(_event_payload)
33
- super
34
- attrs = {
35
- canvas_section_id: local_canvas_id(payload[:course_section_id]),
36
- canvas_course_id: local_canvas_id(payload[:course_id]),
37
- name: payload[:name],
38
- workflow_state: payload[:workflow_state],
39
- start_at: payload[:start_at],
40
- end_at: payload[:end_at],
41
- canvas_nonxlist_course_id: payload[:nonxlist_course_id],
42
- }
43
- create_or_update(attrs)
16
+ def process
17
+ section = Section.where(canvas_id: local_canvas_id(payload[:course_section_id])).first_or_initialize
18
+ section.sync_from_api
19
+ # A section change could constitute a crosslisting change, which means
20
+ # we need to make sure all our enrollments are pointing to the correct course
21
+ section.enrollments.update_all(canvas_course_id: section.canvas_course_id)
44
22
  end
45
23
 
46
- private
47
-
48
- def create_or_update(attrs)
49
- section = Section.where(canvas_section_id: attrs[:canvas_section_id]).first_or_initialize
50
- section.assign_attributes(attrs)
51
- section.save!
52
- end
53
24
  end
25
+
26
+ class CourseSectionCreatedEvent < LiveEvents::CourseSectionEvent; end
27
+ class CourseSectionUpdatedEvent < LiveEvents::CourseSectionEvent; end
54
28
  end
@@ -4,58 +4,27 @@
4
4
  # You can customize it as needed, but make sure you test
5
5
  # any changes you make to the auto generated methods.
6
6
  #
7
+ # LiveEvent message formats can be found at https://canvas.instructure.com/doc/api/file.live_events.html
8
+ # In the general case, LiveEvent message content should not be trusted - it is highly possible that events may
9
+ # arrive out of order. Most of the CanvasSync LiveEvent templates solve this by always fetching the object from the API.
10
+ #
7
11
 
8
12
 
9
13
  module LiveEvents
10
14
  class EnrollmentEvent < LiveEvents::BaseEvent
11
- # The following is provided in the live events call:
12
- # {
13
- #
14
- # enrollment_id: enrollment.global_id,
15
- # course_id: enrollment.global_course_id,
16
- # user_id: enrollment.global_user_id,
17
- # user_name: enrollment.user_name,
18
- # type: enrollment.type,
19
- # created_at: enrollment.created_at,
20
- # updated_at: enrollment.updated_at,
21
- # limit_privileges_to_course_section: enrollment.limit_privileges_to_course_section,
22
- # course_section_id: enrollment.global_course_section_id,
23
- # workflow_state: enrollment.workflow_state
24
- # }
25
-
26
- def perform(payload)
27
- super
28
- return if course.nil? || section.nil? || user.nil?
29
- attrs = {
30
- canvas_enrollment_id: local_canvas_id(payload[:enrollment_id]),
31
- canvas_course_id: course.try(:id),
32
- canvas_user_id: user.try(:id),
33
- role: payload[:type],
34
- canvas_section_id: section.try(:id),
35
- status: payload[:workflow_state],
36
- }
37
- create_or_update(attrs)
38
- end
39
-
40
- private
41
-
42
- def create_or_update(attrs)
43
- enrollment = Enrollment.where(canvas_enrollment_id: attrs[:canvas_enrollment_id]).first_or_initialize
44
- enrollment.assign_attributes(attrs)
45
- enrollment.save!
46
- enrollment
47
- end
48
-
49
- def course
50
- @course ||= Course.find_by(canvas_course_id: local_canvas_id(payload[:course_id]))
51
- end
52
-
53
- def section
54
- @section ||= Section.find_by(canvas_section_id: local_canvas_id(payload[:section_id]))
55
- end
56
-
57
- def user
58
- @user ||= User.find_by(canvas_user_id: local_canvas_id(payload[:user_id]))
15
+ attr_accessor :enrollment
16
+
17
+ def process()
18
+ @enrollment = Enrollment.where(canvas_id: local_canvas_id(payload[:enrollment_id])).first_or_initialize
19
+ enrollment.canvas_course_id = local_canvas_id(payload[:course_id])
20
+ enrollment.sync_from_api
21
+ if enrollment.user.nil?
22
+ u = User.new(canvas_id: enrollment.canvas_user_id)
23
+ u.sync_from_api
24
+ end
59
25
  end
60
26
  end
27
+
28
+ class EnrollmentCreatedEvent < LiveEvents::EnrollmentEvent; end
29
+ class EnrollmentUpdatedEvent < LiveEvents::EnrollmentEvent; end
61
30
  end
@@ -4,48 +4,21 @@
4
4
  # You can customize it as needed, but make sure you test
5
5
  # any changes you make to the auto generated methods.
6
6
  #
7
+ # LiveEvent message formats can be found at https://canvas.instructure.com/doc/api/file.live_events.html
8
+ # In the general case, LiveEvent message content should not be trusted - it is highly possible that events may
9
+ # arrive out of order. Most of the CanvasSync LiveEvent templates solve this by always fetching the object from the API.
10
+ #
7
11
 
8
12
 
9
13
  module LiveEvents
10
14
  class GradeEvent < LiveEvents::BaseEvent
11
- # The following is provided in the live events call:
12
- # {
13
- # submission_id: submission.global_id,
14
- # assignment_id: submission.global_assignment_id,
15
- # grade: submission.grade,
16
- # old_grade: old_submission.try(:grade),
17
- # score: submission.score,
18
- # old_score: old_submission.try(:score),
19
- # points_possible: submission.assignment.points_possible,
20
- # old_points_possible: old_assignment.points_possible,
21
- # grader_id: grader_id,
22
- # student_id: submission.global_user_id,
23
- # user_id: submission.global_user_id,
24
- # grading_complete: submission.graded?,
25
- # muted: submission.muted_assignment?
26
- # }
27
-
28
- def perform(event_payload)
29
- super
30
- attrs = {
31
- submission_id: submission.try(:id),
32
- assignment_id: assignment.try(:id),
33
- score: payload[:score],
34
- points_possible: payload[:points_possible],
35
- grader_id: payload[:grader_id],
36
- user_id: user.try(:id),
37
- grading_complete: payload[:grading_complete],
38
- muted: payload[:muted],
39
- }
40
- create_or_update(attrs)
15
+
16
+ def process
17
+ raise "process must be implemented in your subclass"
41
18
  end
42
19
 
43
20
  private
44
21
 
45
- def create_or_update(attrs)
46
- # TODO: add your code here
47
- end
48
-
49
22
  def submission
50
23
  Submission.find_by(canvas_submission_id: local_canvas_id(payload[:submission_id]))
51
24
  end
@@ -58,4 +31,6 @@ module LiveEvents
58
31
  User.find_by(canvas_user_id: local_canvas_id(payload[:user_id]))
59
32
  end
60
33
  end
34
+
35
+ class GradeChangedEvent < LiveEvents::GradeEvent; end
61
36
  end
@@ -4,42 +4,23 @@
4
4
  # You can customize it as needed, but make sure you test
5
5
  # any changes you make to the auto generated methods.
6
6
  #
7
+ # LiveEvent message formats can be found at https://canvas.instructure.com/doc/api/file.live_events.html
8
+ # In the general case, LiveEvent message content should not be trusted - it is highly possible that events may
9
+ # arrive out of order. Most of the CanvasSync LiveEvent templates solve this by always fetching the object from the API.
10
+ #
7
11
 
8
12
 
9
13
  module LiveEvents
10
14
  class ModuleEvent < LiveEvents::BaseEvent
11
- # The following is provided in the live events call:
12
- # {
13
- # module_id,
14
- # context_id,
15
- # context_type,
16
- # name,
17
- # position,
18
- # workflow_state,
19
- # }
20
-
21
- def perform(_event_payload)
22
- super
23
- create_or_update_from_api(payload)
24
- end
25
-
26
- private
27
15
 
28
- def create_or_update_from_api(payload)
16
+ def process
29
17
  return unless payload["context_type"] == "Course"
30
18
  context_module = ContextModule.find_or_initialize_by(canvas_id: payload["module_id"])
31
- api_params = canvas_sync_client.course_module(payload["context_id"], payload["module_id"])
32
-
33
- context_module.assign_attributes(
34
- canvas_context_id: payload["context_id"],
35
- canvas_context_type: payload["context_type"],
36
- position: api_params["position"],
37
- name: api_params["name"],
38
- workflow_state: payload["workflow_state"],
39
- unlock_at: api_params["unlock_at"],
40
- )
41
-
42
- context_module.save! if context_module.changed?
19
+ context_module.sync_from_api
43
20
  end
21
+
44
22
  end
23
+
24
+ class ModuleCreatedEvent < LiveEvents::ModuleEvent; end
25
+ class ModuleUpdatedEvent < LiveEvents::ModuleEvent; end
45
26
  end
@@ -4,40 +4,22 @@
4
4
  # You can customize it as needed, but make sure you test
5
5
  # any changes you make to the auto generated methods.
6
6
  #
7
+ # LiveEvent message formats can be found at https://canvas.instructure.com/doc/api/file.live_events.html
8
+ # In the general case, LiveEvent message content should not be trusted - it is highly possible that events may
9
+ # arrive out of order. Most of the CanvasSync LiveEvent templates solve this by always fetching the object from the API.
10
+ #
7
11
 
8
12
 
9
13
  module LiveEvents
10
14
  class ModuleItemEvent < LiveEvents::BaseEvent
11
- # The following is provided in the live events call:
12
- # {
13
- # module_item_id,
14
- # module_id,
15
- # context_id,
16
- # context_type,
17
- # position,
18
- # workflow_state,
19
- # }
20
-
21
- def perform(_event_payload)
22
- super
23
- create_or_update_from_api(payload)
24
- end
25
-
26
- private
27
15
 
28
- def create_or_update_from_api(payload)
16
+ def process
29
17
  context_module_item = ContextModuleItem.find_or_initialize_by(canvas_id: payload["module_item_id"])
30
- api_params = canvas_sync_client.module_item(payload["context_id"], payload["module_id"], payload["module_item_id"])
31
-
32
- context_module_item.assign_attributes(
33
- canvas_context_module_id: payload["module_id"],
34
- position: payload["position"],
35
- workflow_state: payload["workflow_state"],
36
- canvas_content_id: api_params["content_id"],
37
- canvas_content_type: api_params["type"],
38
- )
39
-
40
- context_module_item.save! if context_module_item.changed?
18
+ context_module_item.sync_from_api
41
19
  end
20
+
42
21
  end
22
+
23
+ class ModuleItemCreatedEvent < LiveEvents::ModuleItemEvent; end
24
+ class ModuleItemUpdatedEvent < LiveEvents::ModuleItemEvent; end
43
25
  end
@@ -4,55 +4,25 @@
4
4
  # You can customize it as needed, but make sure you test
5
5
  # any changes you make to the auto generated methods.
6
6
  #
7
+ # LiveEvent message formats can be found at https://canvas.instructure.com/doc/api/file.live_events.html
8
+ # In the general case, LiveEvent message content should not be trusted - it is highly possible that events may
9
+ # arrive out of order. Most of the CanvasSync LiveEvent templates solve this by always fetching the object from the API.
10
+ #
7
11
 
8
12
 
9
13
  module LiveEvents
10
14
  class SubmissionEvent < LiveEvents::BaseEvent
11
- # The following is provided in the live events call:
12
- # {
13
- # submission_id: submission.global_id,
14
- # assignment_id: submission.global_assignment_id,
15
- # user_id: submission.global_user_id,
16
- # submitted_at: submission.submitted_at,
17
- # graded_at: submission.graded_at,
18
- # updated_at: submission.updated_at,
19
- # score: submission.score,
20
- # grade: submission.grade,
21
- # submission_type: submission.submission_type,
22
- # body: LiveEvents.truncate(submission.body),
23
- # url: submission.url,
24
- # attempt: submission.attempt
25
- # }
26
15
 
27
- def perform(event_payload)
28
- super
29
- return if assignment.nil? || user.nil?
30
- attrs = {
31
- canvas_submission_id: local_canvas_id(payload[:submission_id]),
32
- canvas_assignment_id: assignment.try(:id),
33
- canvas_user_id: user.try(:id),
34
- submitted_at: payload[:submitted_at],
35
- graded_at: payload[:graded_at],
36
- score: payload[:score],
37
- }
38
- create_or_update(attrs)
16
+ def process
17
+ submission = Submission.where(canvas_id: local_canvas_id(payload["submission_id"])).first_or_initialize
18
+ submission.canvas_assignment_id = local_canvas_id(payload["assignment_id"])
19
+ submission.canvas_user_id = local_canvas_id(payload["user_id"])
20
+ submission.canvas_course_id = submission.assignment.context.canvas_id
21
+ submission.sync_from_api
39
22
  end
40
- end
41
-
42
- private
43
23
 
44
- def create_or_update(attrs)
45
- submission = Submission.where(canvas_submission_id: attrs[:canvas_submission_id]).first_or_initialize
46
- submission.assign_attributes(attrs)
47
- submission.save!
48
- submission
49
24
  end
50
25
 
51
- def assignment
52
- @assignment ||= Assignment.find_by(canvas_assignment_id: local_canvas_id(payload[:assignment_id]))
53
- end
54
-
55
- def user
56
- @user ||= User.find_by(canvas_user_id: local_canvas_id(payload[:user_id]))
57
- end
26
+ class SubmissionCreatedEvent < LiveEvents::SubmissionEvent; end
27
+ class SubmissionUpdatedEvent < LiveEvents::SubmissionEvent; end
58
28
  end
@@ -4,37 +4,28 @@
4
4
  # You can customize it as needed, but make sure you test
5
5
  # any changes you make to the auto generated methods.
6
6
  #
7
+ # LiveEvent message formats can be found at https://canvas.instructure.com/doc/api/file.live_events.html
8
+ # In the general case, LiveEvent message content should not be trusted - it is highly possible that events may
9
+ # arrive out of order. Most of the CanvasSync LiveEvent templates solve this by always fetching the object from the API.
10
+ #
7
11
 
8
12
 
9
13
  module LiveEvents
10
14
  class SyllabusEvent < LiveEvents::BaseEvent
11
- # The following is provided in the live events call:
12
- # {
13
- # course_id: course.global_id,
14
- # syllabus_body: LiveEvents.truncate(course.syllabus_body),
15
- # old_syllabus_body: LiveEvents.truncate(old_syllabus_body)
16
- # }
17
- #
18
15
 
19
- def perform(event_payload)
20
- super
21
- attrs = {
22
- course_id: course.try(:id),
23
- syllabus_body: payload[:syllabus_body],
24
- }
25
- create_or_update(attrs)
16
+ def process
17
+ # syllabus = Syllabus.where(course_id: attrs[:course_id]).first_or_initialize
18
+ # syllabus.body = attrs[:syllabus_body] # Syllabus Body can get truncated by live events
19
+ # syllabus.save!
20
+ raise "process must be implemented in your subclass"
26
21
  end
27
22
 
28
23
  private
29
24
 
30
- def create_or_update(attrs)
31
- syllabus = Syllabus.where(course_id: attrs[:course_id]).first_or_initialize
32
- syllabus.body = attrs[:syllabus_body] # Syllabus Body can get truncated by live events
33
- syllabus.save!
34
- end
35
-
36
25
  def course
37
26
  Course.find_by(canvas_course_id: local_canvas_id(payload[:course_id]))
38
27
  end
39
28
  end
29
+
30
+ class SyllabusUpdatedEvent < LiveEvents::SyllabusEvent; end
40
31
  end
@@ -4,37 +4,26 @@
4
4
  # You can customize it as needed, but make sure you test
5
5
  # any changes you make to the auto generated methods.
6
6
  #
7
+ # LiveEvent message formats can be found at https://canvas.instructure.com/doc/api/file.live_events.html
8
+ # In the general case, LiveEvent message content should not be trusted - it is highly possible that events may
9
+ # arrive out of order. Most of the CanvasSync LiveEvent templates solve this by always fetching the object from the API.
10
+ #
7
11
 
8
12
 
9
13
  module LiveEvents
10
14
  class UserEvent < LiveEvents::BaseEvent
11
- # The following is provided in the live events call:
12
- # {
13
- # user_id: user.global_id,
14
- # uuid: user.uuid,
15
- # name: user.name,
16
- # short_name: user.short_name,
17
- # workflow_state: user.workflow_state,
18
- # created_at: user.created_at,
19
- # updated_at: user.updated_at
20
- # }
21
15
 
22
- def perform(event_payload)
23
- super
24
- attrs = {
25
- canvas_user_id: local_canvas_id(payload[:user_id]),
26
- name: payload[:name],
27
- status: payload[:workflow_state],
28
- }
29
- create_or_update(attrs)
16
+ def process
17
+ canvas_user_id = local_canvas_id(payload[:user_id])
18
+ user = User.where(canvas_id: canvas_user_id).first_or_initialize
19
+ user.sync_from_api
20
+ rescue Footrest::HttpError::Unauthorized => e
21
+ # This can happen when a new user is created, but hasn't setup a login on Canvas yet.
22
+ Rails.logger.info("Failed to fetch user #{canvas_user_id}: #{e.backtrace}")
30
23
  end
31
24
 
32
- private
33
-
34
- def create_or_update(attrs)
35
- user = User.where(canvas_user_id: attrs[:canvas_user_id]).first_or_initialize
36
- user.assign_attributes(attrs)
37
- user.save!
38
- end
39
25
  end
26
+
27
+ class UserCreatedEvent < LiveEvents::UserEvent; end
28
+ class UserUpdatedEvent < LiveEvents::UserEvent; end
40
29
  end