Class: Mbeditor::EditorChannel

Inherits:
CableBaseClass
  • Object
show all
Defined in:
app/channels/mbeditor/editor_channel.rb

Constant Summary collapse

STATE_MAX_BYTES =
1 * 1024 * 1024
SAFE_BRANCH_NAME =
/\A[a-zA-Z0-9._\-\/]+\z/

Instance Method Summary collapse

Instance Method Details

#save_branch_state(data) ⇒ Object

Called via WebSocketService.perform(‘save_branch_state’, { branch: …, state: … })



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 'app/channels/mbeditor/editor_channel.rb', line 42

def save_branch_state(data)
  Rails.logger.silence do
    branch = data["branch"].to_s.strip
    return unless branch.match?(SAFE_BRANCH_NAME)

    state_data = data["state"]
    payload_json = state_data.to_json
    return if payload_json.bytesize > STATE_MAX_BYTES

    root = workspace_root
    path = root.join("tmp", "mbeditor_branch_states.json")
    FileUtils.mkdir_p(root.join("tmp"))
    File.open(path, File::RDWR | File::CREAT) do |f|
      f.flock(File::LOCK_EX)
      existing = f.size > 0 ? JSON.parse(f.read) : {}
      existing[branch] = state_data
      f.truncate(0)
      f.rewind
      f.write(existing.to_json)
    end
  end
rescue StandardError
  # Never let a state-save failure crash the WebSocket connection
end

#save_state(data) ⇒ Object

Called via WebSocketService.perform(‘save_state’, { state: … })



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/channels/mbeditor/editor_channel.rb', line 22

def save_state(data)
  Rails.logger.silence do
    payload = (data["state"] || data).to_json
    return if payload.bytesize > STATE_MAX_BYTES

    root = workspace_root
    path = root.join("tmp", "mbeditor_workspace.json")
    FileUtils.mkdir_p(root.join("tmp"))
    File.open(path, File::RDWR | File::CREAT) do |f|
      f.flock(File::LOCK_EX)
      f.truncate(0)
      f.rewind
      f.write(payload)
    end
  end
rescue StandardError
  # Never let a state-save failure crash the WebSocket connection
end

#subscribedObject



13
14
15
# File 'app/channels/mbeditor/editor_channel.rb', line 13

def subscribed
  stream_from "mbeditor_editor" if respond_to?(:stream_from)
end

#unsubscribedObject



17
18
19
# File 'app/channels/mbeditor/editor_channel.rb', line 17

def unsubscribed
  # no-op
end