Class: ActiveEdit

Inherits:
Sequel::Model
  • Object
show all
Defined in:
backend/app/model/active_edit.rb

Constant Summary collapse

EXPIRE_SECONDS =

If we haven’t seen an update from a client in this amount of time, assume they’ve stopped editing.

This value needs to be at least as much as the client interval plus the SYNC_WITH_BACKEND_SECONDS value from the frontend. We add a bit of a margin so that we’re not expiring messages immediately after receiving them.

30

Class Method Summary collapse

Class Method Details

.lock_versions_for(uris) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'backend/app/model/active_edit.rb', line 42

def self.lock_versions_for(uris)
  record_groups = {}

  uris.each do |uri|
    parsed = JSONModel.parse_reference(uri)

    next if !parsed

    model = Kernel.const_get(parsed[:type].to_s.camelize)

    record_groups[model] ||= {}
    record_groups[model][parsed[:id]] = uri
  end

  result = {}
  record_groups.each do |model, records|
    model.where(:id => records.keys).
          select(:id, :lock_version).all.each do |row|
      result[records[row[:id]]] = row[:lock_version]
    end
  end

  result
end

.update_with(active_edits) ⇒ Object



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
# File 'backend/app/model/active_edit.rb', line 11

def self.update_with(active_edits)
  # Add the new ones
  active_edits['active_edits'].each do |edit|
    ActiveEdit.create(:uri => edit['uri'],
                      :operator => edit['user'],
                      :timestamp => Time.parse(edit['time']))
  end

  # Expire the old ones
  ActiveEdit.where { timestamp < (Time.now - EXPIRE_SECONDS) }.delete

  # Tally up the remaining ones
  result = {}

  # Keep track of when each URI was last edited by each user
  ActiveEdit.order(:timestamp).all.each do |edit|
    result[edit.uri] ||= {:edited_by => {}}
    result[edit.uri][:edited_by][edit.operator] = edit.timestamp
  end

  # Record the current lock version for each URI
  lock_versions = lock_versions_for(result.keys)

  result.keys.each do |uri|
    result[uri][:lock_version] = lock_versions[uri]
  end

  result
end