Class: Fenetre::VideoChatChannel

Inherits:
ActionCable::Channel::Base
  • Object
show all
Defined in:
app/channels/fenetre/video_chat_channel.rb

Constant Summary collapse

@@participants =

Handles signaling messages for WebRTC

Hash.new { |h, k| h[k] = [] }

Instance Method Summary collapse

Instance Method Details

#chat(data) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/channels/fenetre/video_chat_channel.rb', line 83

def chat(data)
  return unless @room_id

  init_participants(@room_id)
  return if (data['to'] || data[:to]) && !params[:enable_private_chat]

  message = data['message'] || data[:message]
  to = data['to'] || data[:to]
  ActionCable.server.broadcast(room_stream(@room_id),
                               { 'type' => 'chat', 'from' => current_user.id,
                                 'payload' => { 'message' => message, 'to' => to } })
end

#confirmed?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/channels/fenetre/video_chat_channel.rb', line 111

def confirmed?
  !rejected?
end

#join_room(_data) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/channels/fenetre/video_chat_channel.rb', line 40

def join_room(_data)
  return unless @room_id

  add_participant(@room_id, current_user.id)
  message = {
    'type' => 'join',
    'from' => current_user.id,
    'participants' => participants(@room_id).dup
  }
  message['topic'] = params[:room_topic] if params[:room_topic]
  message['max_participants'] = params[:max_participants] if params[:max_participants]
  message['turbo_stream'] = '<turbo-stream action="append">...</turbo-stream>'
  ActionCable.server.broadcast(room_stream(@room_id), message)
  log_analytics(:join, current_user.id, @room_id)
end

#kick(data) ⇒ Object



56
57
58
59
60
61
62
63
# File 'app/channels/fenetre/video_chat_channel.rb', line 56

def kick(data)
  return unless user_role == :host

  user_id = data['user_id'] || data[:user_id]
  ActionCable.server.broadcast(room_stream(@room_id),
                               { 'type' => 'kick', 'from' => current_user.id,
                                 'payload' => { 'user_id' => user_id } })
end

#mute(data) ⇒ Object



65
66
67
68
69
70
71
72
# File 'app/channels/fenetre/video_chat_channel.rb', line 65

def mute(data)
  return unless user_role == :host

  user_id = data['user_id'] || data[:user_id]
  ActionCable.server.broadcast(room_stream(@room_id),
                               { 'type' => 'mute', 'from' => current_user.id,
                                 'payload' => { 'user_id' => user_id } })
end

#perform(action, data = {}) ⇒ Object

Raises:

  • (NoMethodError)


96
97
98
99
100
# File 'app/channels/fenetre/video_chat_channel.rb', line 96

def perform(action, data = {})
  raise NoMethodError, "undefined action '#{action}' for #{self.class}" unless respond_to?(action)

  public_send(action, data)
end

#rejectObject



102
103
104
105
# File 'app/channels/fenetre/video_chat_channel.rb', line 102

def reject
  @_rejected = true
  super if defined?(super)
end

#rejected?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/channels/fenetre/video_chat_channel.rb', line 107

def rejected?
  !!@_rejected
end

#signal(data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'app/channels/fenetre/video_chat_channel.rb', line 29

def signal(data)
  return unless @room_id

  init_participants(@room_id)
  type = data['type'] || data[:type]
  payload = data['payload'] || data[:payload]
  return if type == 'screen_share' && !params[:enable_screen_sharing]

  broadcast_signal(@room_id, type, current_user.id, payload)
end

#subscribedObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/channels/fenetre/video_chat_channel.rb', line 8

def subscribed
  return reject unless current_user

  @room_id = params[:room_id]
  return reject if @room_id.blank?

  init_participants(@room_id)
  return reject if params[:room_locked] && user_role != :host
  return reject if params[:max_participants] && participants(@room_id).size >= params[:max_participants].to_i

  stream_from room_stream(@room_id)
end

#unmute(data) ⇒ Object



74
75
76
77
78
79
80
81
# File 'app/channels/fenetre/video_chat_channel.rb', line 74

def unmute(data)
  return unless user_role == :host

  user_id = data['user_id'] || data[:user_id]
  ActionCable.server.broadcast(room_stream(@room_id),
                               { 'type' => 'unmute', 'from' => current_user.id,
                                 'payload' => { 'user_id' => user_id } })
end

#unsubscribedObject



21
22
23
24
25
26
27
# File 'app/channels/fenetre/video_chat_channel.rb', line 21

def unsubscribed
  return unless @room_id

  remove_participant(@room_id, current_user.id)
  broadcast_leave(@room_id, current_user.id)
  log_analytics(:leave, current_user.id, @room_id)
end