Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Views: 39598
1
##############################################################################
2
#
3
# CoCalc: Collaborative Calculation in the Cloud
4
#
5
# Copyright (C) 2016, Sagemath Inc.
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#
20
###############################################################################
21
22
###
23
Describes how the client course editor syncs with the database
24
###
25
26
immutable = require('immutable')
27
28
# SMC libraries
29
misc = require('smc-util/misc')
30
{defaults, required} = misc
31
schema = require('smc-util/schema')
32
{webapp_client} = require('../webapp_client')
33
34
exports.create_sync_db = (redux, actions, store) =>
35
return if not redux? or not actions? or not store?
36
37
syncdb = webapp_client.sync_db
38
project_id : store.get('course_project_id')
39
path : store.get('course_filename')
40
primary_keys : ['table', 'handout_id', 'student_id', 'assignment_id']
41
string_cols : ['note', 'description', 'title', 'email_invite']
42
change_throttle : 500 # helps when doing a lot of assign/collect, etc.
43
save_interval : 3000 # wait at least 3s between saving changes to backend
44
45
46
syncdb.once 'init', (err) =>
47
if err
48
actions?.set_error(err)
49
console.warn("Error opening '#{store.course_filename}' -- #{err}")
50
return
51
i = store.get('course_filename').lastIndexOf('.')
52
t =
53
settings :
54
title : store.get('course_filename').slice(0,i)
55
description : 'No description'
56
assignments : {}
57
students : {}
58
handouts : {}
59
for x in syncdb.get().toJS()
60
if x.table == 'settings'
61
misc.merge(t.settings, misc.copy_without(x, 'table'))
62
else if x.table == 'students'
63
t.students[x.student_id] = misc.copy_without(x, 'table')
64
else if x.table == 'assignments'
65
t.assignments[x.assignment_id] = misc.copy_without(x, 'table')
66
else if x.table == 'handouts'
67
t.handouts[x.handout_id] = misc.copy_without(x, 'table')
68
for k, v of t
69
t[k] = immutable.fromJS(v)
70
actions?.setState(t)
71
syncdb.on('change', (changes) -> actions?._syncdb_change(changes))
72
syncdb.on('sync', => redux.getProjectActions(@project_id).flag_file_activity(@filename))
73
74
# Wait until the projects store has data about users of our project before configuring anything.
75
projects_store = redux.getStore('projects')
76
projects_store.wait
77
until : (p_store) -> p_store.get_users(store.get('course_project_id'))?
78
timeout : 30
79
cb : ->
80
actions = actions
81
if not actions?
82
return
83
actions.lookup_nonregistered_students()
84
actions.configure_all_projects()
85
86
# Also
87
projects_store.on 'change', actions.handle_projects_store_update
88
actions.handle_projects_store_update(projects_store) # initialize
89
90
return syncdb
91