Class: Colossus::Faye::Extension

Inherits:
Object
  • Object
show all
Defined in:
lib/colossus/faye/extension.rb

Overview

Faye extension implementing all the presence, authorization, authentification and push logic.

Constant Summary collapse

VALID_STATUSES =
%w(disconnected away active).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(faye, colossus = Colossus.new) ⇒ Extension

Returns a new instance of Extension.



10
11
12
13
14
# File 'lib/colossus/faye/extension.rb', line 10

def initialize(faye, colossus = Colossus.new)
  @faye     = faye
  @colossus = colossus
  faye.add_extension(self)
end

Instance Attribute Details

#colossusObject (readonly)

Returns the value of attribute colossus.



6
7
8
# File 'lib/colossus/faye/extension.rb', line 6

def colossus
  @colossus
end

#fayeObject (readonly)

Returns the value of attribute faye.



6
7
8
# File 'lib/colossus/faye/extension.rb', line 6

def faye
  @faye
end

Instance Method Details

#acceptable?(message) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/colossus/faye/extension.rb', line 32

def acceptable?(message)
  message['ext'] &&
    (message['ext']['user_token'] || message['ext']['writer_token'])
end

#handle_invalid_token(message) ⇒ Object



51
52
53
54
# File 'lib/colossus/faye/extension.rb', line 51

def handle_invalid_token(message)
  message['error'] = 'Invalid Token'
  message
end

#handle_set_status(message) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/colossus/faye/extension.rb', line 72

def handle_set_status(message)
  token   = message['ext']['user_token']
  user_id = message['channel'].partition('/users/').last
  status  = message['data'] && message['data']['status']

  if invalid_user_channel?(user_id)
    message['error'] = 'The only accepted channel_name is users/:user_id'
    return message
  end

  unless status && VALID_STATUSES.include?(status)
    message['error'] = 'Invalid Status'
    return message
  end

  unless colossus.verifier.verify_token(token, user_id)
    message['error'] = 'Invalid Token'
    return message
  end

  colossus.set(user_id, message['clientId'], status)

  message
end

#handle_subscribe(message) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/colossus/faye/extension.rb', line 56

def handle_subscribe(message)
  token   = message['ext']['user_token']
  user_id = message['subscription'].partition('/users/').last

  if invalid_user_channel?(user_id)
    message['error'] = 'The only accepted channel_name is users/:user_id'
    return message
  end

  unless colossus.verifier.verify_token(token, user_id)
    message['error'] = 'Invalid Token'
  end

  message
end

#handle_user_action(message) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/colossus/faye/extension.rb', line 37

def handle_user_action(message)
  if message['channel'] == '/meta/subscribe'
    handle_subscribe(message)
  elsif message['channel'].start_with?('/meta/')
    message
  elsif message['channel'].start_with?('/users/')
    handle_set_status(message)
  else
    message['error'] = 'Unknown Action'
  end

  message
end

#incoming(message, callback) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/colossus/faye/extension.rb', line 16

def incoming(message, callback)
  if !acceptable?(message)
    handle_invalid_token(message)
    message.delete('data')
    message.delete('ext')
  elsif message['ext']['user_token']
    handle_user_action(message)
    message.delete('data')
    message.delete('ext')
  elsif message['ext']['writer_token']
    message.delete('ext')
  end

  callback.call(message)
end

#invalid_user_channel?(user_id) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/colossus/faye/extension.rb', line 97

def invalid_user_channel?(user_id)
  user_id.empty? || user_id.include?('*') || user_id.include?('/')
end