mad_chatter 0.2.9 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/.gitignore +2 -1
  2. data/.rvmrc +1 -0
  3. data/Gemfile.lock +1 -3
  4. data/README.md +25 -20
  5. data/lib/mad_chatter/actions/dsl.rb +4 -10
  6. data/lib/mad_chatter/actions.rb +29 -0
  7. data/lib/mad_chatter/channel.rb +56 -0
  8. data/lib/mad_chatter/config.rb +22 -5
  9. data/lib/mad_chatter/connection.rb +29 -0
  10. data/lib/mad_chatter/extensions.rb +12 -0
  11. data/lib/mad_chatter/markdown_renderer.rb +6 -0
  12. data/lib/mad_chatter/message.rb +39 -12
  13. data/lib/mad_chatter/message_history.rb +8 -5
  14. data/lib/mad_chatter/message_listener.rb +17 -0
  15. data/lib/mad_chatter/message_listeners/code.rb +30 -0
  16. data/lib/mad_chatter/message_listeners/image.rb +27 -0
  17. data/lib/mad_chatter/message_listeners/join.rb +30 -0
  18. data/lib/mad_chatter/message_listeners/markdown.rb +45 -0
  19. data/lib/mad_chatter/message_listeners/nick.rb +26 -0
  20. data/lib/mad_chatter/server.rb +0 -55
  21. data/lib/mad_chatter/servers/em_websocket.rb +5 -14
  22. data/lib/mad_chatter/user.rb +78 -0
  23. data/lib/mad_chatter/users.rb +32 -24
  24. data/lib/mad_chatter/version.rb +1 -1
  25. data/lib/mad_chatter.rb +98 -34
  26. data/mad_chatter.gemspec +1 -1
  27. data/screenshot.png +0 -0
  28. data/spec/actions_spec.rb +7 -0
  29. data/spec/message_history_spec.rb +12 -10
  30. data/spec/message_listeners/code_spec.rb +37 -0
  31. data/spec/message_listeners/join_spec.rb +34 -0
  32. data/spec/message_listeners/markdown_spec.rb +47 -0
  33. data/spec/message_spec.rb +28 -16
  34. data/spec/server_spec.rb +43 -0
  35. data/spec/users_spec.rb +23 -0
  36. data/templates/extensions/example.rb +1 -1
  37. data/templates/extensions.rb +16 -14
  38. data/templates/web/css/bootstrap.css +3363 -0
  39. data/templates/web/css/bootstrap.min.css +610 -0
  40. data/templates/web/css/bootstrap.min.responsive.css +3 -0
  41. data/templates/web/css/bootstrap.responsive.css +567 -0
  42. data/templates/web/css/mad_chatter.css +59 -0
  43. data/templates/web/{styles.css → css/styles.css} +26 -20
  44. data/templates/web/img/glyphicons-halflings-white.png +0 -0
  45. data/templates/web/img/glyphicons-halflings.png +0 -0
  46. data/templates/web/index.html +173 -40
  47. data/templates/web/js/bootstrap.js +1722 -0
  48. data/templates/web/js/bootstrap.min.js +1 -0
  49. data/templates/web/js/mad_chatter.js +292 -0
  50. data/templates/web/{mad_chatter_actions.js → js/mad_chatter_actions.js} +0 -0
  51. data/templates/web/js/mad_chatter_config.js +3 -0
  52. data/templates/web/js/swfobject.js +4 -0
  53. data/templates/web/js/web_socket.js +389 -0
  54. data/templates/web/swf/WebSocketMain.swf +0 -0
  55. metadata +65 -62
  56. data/TODO.txt +0 -10
  57. data/templates/web/mad_chatter.js +0 -161
  58. data/templates/web/mad_chatter_config.js +0 -1
  59. data/templates/web/markitup/jquery.markitup.js +0 -593
  60. data/templates/web/markitup/sets/markdown/images/bold.png +0 -0
  61. data/templates/web/markitup/sets/markdown/images/code.png +0 -0
  62. data/templates/web/markitup/sets/markdown/images/h1.png +0 -0
  63. data/templates/web/markitup/sets/markdown/images/h2.png +0 -0
  64. data/templates/web/markitup/sets/markdown/images/h3.png +0 -0
  65. data/templates/web/markitup/sets/markdown/images/h4.png +0 -0
  66. data/templates/web/markitup/sets/markdown/images/h5.png +0 -0
  67. data/templates/web/markitup/sets/markdown/images/h6.png +0 -0
  68. data/templates/web/markitup/sets/markdown/images/italic.png +0 -0
  69. data/templates/web/markitup/sets/markdown/images/link.png +0 -0
  70. data/templates/web/markitup/sets/markdown/images/list-bullet.png +0 -0
  71. data/templates/web/markitup/sets/markdown/images/list-numeric.png +0 -0
  72. data/templates/web/markitup/sets/markdown/images/picture.png +0 -0
  73. data/templates/web/markitup/sets/markdown/images/preview.png +0 -0
  74. data/templates/web/markitup/sets/markdown/images/quotes.png +0 -0
  75. data/templates/web/markitup/sets/markdown/readme.txt +0 -11
  76. data/templates/web/markitup/sets/markdown/style.css +0 -6
  77. data/templates/web/markitup/skins/mad_chatter/images/handle.png +0 -0
  78. data/templates/web/markitup/skins/mad_chatter/images/menu.png +0 -0
  79. data/templates/web/markitup/skins/mad_chatter/images/submenu.png +0 -0
  80. data/templates/web/markitup/skins/mad_chatter/style.css +0 -121
@@ -1,15 +1,9 @@
1
1
  module MadChatter
2
-
3
2
  class Server
4
3
 
5
4
  def initialize(config)
6
5
  @config = config
7
6
  @server = initialize_server
8
- @subscribers = {};
9
- end
10
-
11
- def self.main_channel
12
- @main_channel ||= EventMachine::Channel.new
13
7
  end
14
8
 
15
9
  def initialize_server
@@ -28,55 +22,6 @@ module MadChatter
28
22
  end
29
23
  end
30
24
 
31
- def register_connection(&send_message)
32
- subscriber_id = MadChatter::Server.main_channel.subscribe(send_message)
33
- token = generate_token
34
- send_message.call(MadChatter::Message.new('token', token).to_json)
35
- @subscribers[subscriber_id] = token
36
- MadChatter::MessageHistory.all.each do |json|
37
- send_message.call(json)
38
- end
39
- subscriber_id
40
- end
41
-
42
- def generate_token
43
- Digest::SHA1.hexdigest(Time.now.to_s)
44
- end
45
-
46
- def connection_closed(id)
47
- token = @subscribers.delete(id)
48
- username = MadChatter::Users.find_username_by_token(token)
49
- MadChatter::Server.main_channel.unsubscribe(id)
50
- MadChatter::Users.remove(token)
51
- MadChatter::Server.send_json(MadChatter::Message.new('status', "#{username} has left the chatroom").to_json)
52
- MadChatter::Server.send_json(MadChatter::Message.new('users', MadChatter::Users.current).to_json)
53
- end
54
-
55
- def message_received(json)
56
- msg = JSON.parse(json)
57
- username = MadChatter::Users.find_username_by_token(msg['token'])
58
- message = MadChatter::Message.new(msg['type'], msg['message'], msg['token'], username)
59
- message.filter
60
-
61
- if message.token.nil?
62
- return # Token is required to send messages
63
- end
64
-
65
- begin
66
- MadChatter.message_listeners.each do |listener|
67
- listener.handle(message)
68
- end
69
-
70
- MadChatter::Server.send_json(message.to_json)
71
- rescue RuntimeError
72
- # dont need to do anything, just prevent any errors from stopping the server
73
- end
74
- end
75
-
76
- def self.send_json(json)
77
- MadChatter::Server.main_channel.push(json)
78
- end
79
-
80
25
  end
81
26
 
82
27
  end
@@ -14,21 +14,12 @@ module MadChatter
14
14
 
15
15
  def start
16
16
  EventMachine::WebSocket.start(@config) do |ws|
17
-
18
17
  ws.onopen do
19
-
20
- connection_id = @main_server.register_connection do |msg|
21
- ws.send(msg)
22
- end
23
-
24
- ws.onclose do
25
- @main_server.connection_closed(connection_id)
26
- end
27
-
28
- ws.onmessage do |msg|
29
- @main_server.message_received(msg)
30
- end
31
-
18
+ user = MadChatter::User.new
19
+ user.on_send { |msg| ws.send(msg) }
20
+ ws.onclose { user.disconnected }
21
+ ws.onmessage { |msg| MadChatter.message_received(msg) }
22
+ user.connected
32
23
  end
33
24
  end
34
25
  end
@@ -0,0 +1,78 @@
1
+ module MadChatter
2
+ class User
3
+
4
+ attr_accessor :token, :username
5
+
6
+ def initialize(token = nil, username = nil)
7
+ @token = token
8
+ @username = username
9
+ end
10
+
11
+ def on_send(&blk)
12
+ @on_send = blk
13
+ end
14
+
15
+ def send(json)
16
+ @on_send.call(json) if @on_send
17
+ end
18
+
19
+ def connected
20
+ @token = generate_new_token unless @token
21
+ MadChatter.users << self
22
+ send_token
23
+ send_channels
24
+ end
25
+
26
+ def generate_new_token
27
+ Digest::SHA1.hexdigest(Time.now.to_s)
28
+ end
29
+
30
+ def send_token
31
+ send JSON.generate({
32
+ type: 'token',
33
+ text: @token,
34
+ })
35
+ end
36
+
37
+ def send_channels
38
+ channels = MadChatter.channels.collect do |c|
39
+ {:id => c.id, :name => c.name }
40
+ end
41
+ send JSON.generate({
42
+ type: 'channels',
43
+ json: channels,
44
+ })
45
+ end
46
+
47
+ def update_username(username)
48
+ old_username = @username
49
+ @username = username
50
+ send_users_list
51
+ MadChatter.channels.each do |channel|
52
+ channel.users.each do |user|
53
+ if user == self
54
+ channel.send_message MadChatter::Message.new('status', "#{old_username} is now known as #{@username}")
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ def send_users_list
61
+ MadChatter.channels.each do |channel|
62
+ channel.users.each do |user|
63
+ channel.send_users_list if user == self
64
+ end
65
+ end
66
+ end
67
+
68
+ def disconnected
69
+ MadChatter.channels.each do |channel|
70
+ channel.remove_user(self)
71
+ end
72
+ end
73
+
74
+ def has_token?(token)
75
+ @token == token
76
+ end
77
+ end
78
+ end
@@ -1,31 +1,39 @@
1
1
  module MadChatter
2
2
  class Users
3
3
 
4
- class << self
5
-
6
- def users
7
- @users ||= {}
8
- end
4
+ def initialize
5
+ @users = {}
6
+ end
7
+
8
+ def add(token, username = nil)
9
+ @users[token] = username
10
+ end
11
+
12
+ def update(token, username)
13
+ @users[token] = username
14
+ end
15
+
16
+ def remove(token)
17
+ @users.delete(token)
18
+ end
19
+
20
+ def find_username_by_token(token)
21
+ @users[token]
22
+ end
23
+
24
+ def current
25
+ @users.values
26
+ end
27
+
28
+ def token_exists?(token)
29
+ @users[token].exists?
30
+ end
9
31
 
10
- def update(token, username)
11
- MadChatter::Users.users[token] = username
12
- end
13
-
14
- def remove(token)
15
- MadChatter::Users.users.delete(token)
16
- end
17
-
18
- def find_username_by_token(token)
19
- MadChatter::Users.users[token]
20
- end
21
-
22
- def current
23
- MadChatter::Users.users.values
24
- end
25
-
26
- def token_exists?(token)
27
- MadChatter::Users.users[token].exists?
28
- end
32
+ def to_json
33
+ JSON.generate({
34
+ type: 'users',
35
+ json: current,
36
+ })
29
37
  end
30
38
 
31
39
  end
@@ -1,5 +1,5 @@
1
1
  module MadChatter
2
2
 
3
- VERSION = '0.2.9'
3
+ VERSION = '0.3.0'
4
4
 
5
5
  end
data/lib/mad_chatter.rb CHANGED
@@ -2,47 +2,111 @@ lib_dir = File.expand_path('..', __FILE__)
2
2
  $:.unshift( lib_dir ) unless $:.include?( lib_dir )
3
3
 
4
4
  require 'eventmachine'
5
- require 'redcarpet'
6
5
  require 'yaml'
7
6
  require 'json'
8
7
  require 'digest/sha1'
9
8
 
9
+ require 'mad_chatter/version'
10
+ require 'mad_chatter/config'
11
+
12
+ require 'mad_chatter/server'
13
+ require 'mad_chatter/servers/em_websocket'
14
+
15
+ require 'mad_chatter/channel'
16
+ require 'mad_chatter/user'
17
+ require 'mad_chatter/users'
18
+
19
+ require 'mad_chatter/message'
20
+ require 'mad_chatter/message_history'
21
+
22
+ require 'mad_chatter/actions'
23
+ require 'mad_chatter/message_listeners/join'
24
+ require 'mad_chatter/message_listeners/markdown'
25
+ require 'mad_chatter/message_listeners/nick'
26
+ require 'mad_chatter/message_listeners/image'
27
+ require 'mad_chatter/message_listeners/code'
28
+
29
+ require 'mad_chatter/extensions'
30
+ require 'mad_chatter/message_listener'
31
+
10
32
  module MadChatter
11
33
 
12
- def self.message_listeners
13
- @@message_listeners ||= []
14
- end
15
-
16
- require 'mad_chatter/config'
17
- require 'mad_chatter/message'
18
- require 'mad_chatter/message_history'
19
- require 'mad_chatter/markdown_renderer'
20
- require 'mad_chatter/server'
21
- require 'mad_chatter/users'
22
- require 'mad_chatter/version'
23
- require 'mad_chatter/actions/dsl'
24
- require 'mad_chatter/actions/base'
25
- require 'mad_chatter/actions/join'
26
- require 'mad_chatter/actions/rename'
27
-
28
- require 'mad_chatter/servers/em_websocket'
34
+ # attr_accessor :users, :channels, :message_listeners
29
35
 
30
- def self.markdown
31
- redcarpet_config = {
32
- :autolink => true,
33
- :no_intra_emphasis => true
34
- }
35
- renderer_config = {
36
- :filter_html => true,
37
- :hard_wrap => true
38
- }
39
- @markdown ||= Redcarpet::Markdown.new(MadChatter::MarkdownRenderer.new(renderer_config), redcarpet_config)
40
- end
36
+ class << self
37
+
38
+ def users
39
+ @users ||= []
40
+ end
41
+
42
+ def users=(array)
43
+ @users = array
44
+ end
45
+
46
+ def find_user_by_token(token)
47
+ users.each do |user|
48
+ return user if user.token == token
49
+ end
50
+ end
51
+
52
+ def channels
53
+ @channels ||= []
54
+ end
55
+
56
+ def channels=(array)
57
+ @channels = array
58
+ end
41
59
 
42
- def self.start
43
- config = MadChatter::Config.init
44
- server = MadChatter::Server.new(config)
45
- server.start
60
+ def find_channel_by_id(id)
61
+ channels.each do |channel|
62
+ return channel if channel.id == id
63
+ end
64
+ end
65
+
66
+ def find_channel_by_name(name)
67
+ channels.each do |channel|
68
+ return channel if channel.name == name
69
+ end
70
+ end
71
+
72
+ def message_listeners
73
+ @message_listeners ||= []
74
+ end
75
+
76
+ def message_listeners=(array)
77
+ @message_listeners = array
78
+ end
79
+
80
+ def start
81
+ config = MadChatter::Config.init
82
+ server = MadChatter::Server.new(config)
83
+ server.start
84
+ end
85
+
86
+ def message_received(json)
87
+ msg = JSON.parse(json)
88
+
89
+ if msg['token'].nil?
90
+ return # Token is required to send messages
91
+ end
92
+
93
+ message = MadChatter::Message.new('message', msg['message'], msg['token'], msg['channel'])
94
+
95
+ begin
96
+ message_listeners.each do |listener|
97
+ listener.handle(message)
98
+ end
99
+ send_message(message)
100
+ rescue RuntimeError
101
+ # dont need to do anything, just prevent any errors from stopping the server
102
+ end
103
+ end
104
+
105
+ def send_message(message)
106
+ channels.each do |channel|
107
+ channel.send_message(message) if channel.id == message.channel
108
+ end
109
+ end
110
+
46
111
  end
47
-
48
112
  end
data/mad_chatter.gemspec CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.add_runtime_dependency "thor"
22
22
  s.add_runtime_dependency "eventmachine"
23
23
  s.add_runtime_dependency "em-websocket"
24
- s.add_runtime_dependency "redcarpet"
24
+ # s.add_runtime_dependency "redcarpet"
25
25
  s.add_runtime_dependency "daemons", "1.1.4"
26
26
 
27
27
  s.add_development_dependency "rspec"
data/screenshot.png ADDED
Binary file
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe MadChatter::Actions do
4
+
5
+ # TODO
6
+
7
+ end
@@ -2,28 +2,30 @@ require 'spec_helper'
2
2
 
3
3
  describe MadChatter::MessageHistory do
4
4
 
5
+ let (:history) { MadChatter::MessageHistory.new }
6
+
5
7
  before(:each) do
6
- MadChatter::MessageHistory.clear
8
+
9
+ # MadChatter::MessageHistory.clear
7
10
  end
8
11
 
9
12
  it 'should only keep 10 recent messages' do
10
13
  15.times do
11
- MadChatter::MessageHistory.add MadChatter::Message.new('message', 'Here is a dummy chat message')
14
+ history.add MadChatter::Message.new('message', 'Here is a dummy chat message')
12
15
  end
13
- MadChatter::MessageHistory.all.length.should == 10
16
+ history.all.length.should == 10
14
17
  end
15
18
 
16
19
  it 'should allow you to clear the history' do
17
- MadChatter::MessageHistory.add MadChatter::Message.new('message', 'Here is a dummy chat message')
18
- MadChatter::MessageHistory.all.length.should == 1
19
- MadChatter::MessageHistory.clear
20
- MadChatter::MessageHistory.all.length.should == 0
20
+ history.add MadChatter::Message.new('message', 'Here is a dummy chat message')
21
+ history.all.length.should == 1
22
+ history.clear
23
+ history.all.length.should == 0
21
24
  end
22
25
 
23
26
  context '#all' do
24
- it 'should still return an empty array if there is message chat history' do
25
- history = MadChatter::MessageHistory.all
26
- history.should == []
27
+ it 'should still return an empty array if there is no chat history' do
28
+ history.all.should == []
27
29
  end
28
30
  end
29
31
 
@@ -0,0 +1,37 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe MadChatter::MessageListeners::Code do
4
+
5
+ let (:server) { MadChatter::Server.new({'websocket_backend' => 'MadChatter::Servers::EventMachineWebSocket'}) }
6
+ let (:user) { MadChatter::User.new('usertoken') }
7
+ let (:channel) { MadChatter::Channel.new('myroom') }
8
+ let (:listener) { MadChatter::MessageListeners::Code.new }
9
+
10
+ before(:each) do
11
+ MadChatter.users = []
12
+ MadChatter.channels = []
13
+ end
14
+
15
+ it 'should set html correctly' do
16
+ MadChatter.users << user
17
+ MadChatter.channels << channel
18
+ begin
19
+ message_text = <<-EOS
20
+ /code <?php
21
+ echo "multiline";
22
+ echo "code sample";
23
+ ?>
24
+ EOS
25
+ message = MadChatter::Message.new('message', message_text, user.token, channel.id)
26
+ listener.handle(message)
27
+ message.html.should == <<-EOS
28
+ <pre><?php
29
+ echo "multiline";
30
+ echo "code sample";
31
+ ?></pre>
32
+ EOS
33
+ rescue RuntimeError
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,34 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe MadChatter::MessageListeners::Join do
4
+
5
+ let (:server) { MadChatter::Server.new({'websocket_backend' => 'MadChatter::Servers::EventMachineWebSocket'}) }
6
+ let (:user) { MadChatter::User.new('usertoken') }
7
+ let (:channel) { MadChatter::Channel.new('myroom') }
8
+ let (:listener) { MadChatter::MessageListeners::Join.new }
9
+
10
+ before(:each) do
11
+ MadChatter.users = []
12
+ MadChatter.channels = []
13
+ end
14
+
15
+ it 'should add user to list of users in server and channel' do
16
+ MadChatter.users << user
17
+ MadChatter.channels << channel
18
+ begin
19
+ MadChatter.users.include?(user).should be_true
20
+ MadChatter.find_channel_by_name('myroom').users.include?(user).should be_false
21
+ message = MadChatter::Message.new('message', '/join', user.token, channel.id)
22
+ listener.handle(message)
23
+ MadChatter.find_channel_by_name('myroom').users.include?(user).should be_true
24
+ rescue RuntimeError
25
+ end
26
+ end
27
+
28
+ it 'should parse messages correctly' do
29
+ message = MadChatter::Message.new('message', 'this /join should not count')
30
+ listener.handle(message)
31
+ # how to check that nothing happened?
32
+ end
33
+
34
+ end
@@ -0,0 +1,47 @@
1
+ require_relative '../spec_helper'
2
+ require 'mad_chatter/message_listeners/markdown'
3
+
4
+ describe MadChatter::MessageListeners::Markdown do
5
+
6
+ let (:listener) { MadChatter::MessageListeners::Markdown.new }
7
+
8
+ it 'should autolink urls with target="_blank"' do
9
+ message = MadChatter::Message.new('message', 'here is a link: http://example.com.')
10
+ listener.handle(message)
11
+ message.html.should == 'here is a link: <a target="_blank" href="http://example.com">http://example.com</a>.'
12
+ end
13
+
14
+ it 'should autolink email addresses' do
15
+ message = MadChatter::Message.new('message', "feel free to email me at madchatter@example.com")
16
+ listener.handle(message)
17
+ message.html.should == 'feel free to email me at <a href="mailto:madchatter@example.com">madchatter@example.com</a>'
18
+ end
19
+
20
+ it 'should parse italics' do
21
+ message = MadChatter::Message.new('message', "_this_ should have emphasis but this_one_should_not")
22
+ listener.handle(message)
23
+ message.html.should == '<em>this</em> should have emphasis but this_one_should_not'
24
+
25
+ message = MadChatter::Message.new('message', "you can use *asterisks*")
26
+ listener.handle(message)
27
+ message.html.should == 'you can use <em>asterisks</em>'
28
+
29
+ message = MadChatter::Message.new('message', "this _doesnt count either")
30
+ listener.handle(message)
31
+ message.html.should == 'this _doesnt count either'
32
+ end
33
+
34
+ it 'should parse bold' do
35
+ message = MadChatter::Message.new('message', "here is a **strong** statement")
36
+ listener.handle(message)
37
+ message.html.should == 'here is a <strong>strong</strong> statement'
38
+ end
39
+
40
+ # TODO
41
+ # it 'should interpret markdown style links' do
42
+ # message = MadChatter::Message.new('message', "[link me up](http://www.example.com)")
43
+ # listener.handle(message)
44
+ # message.html.should == '<a target="_blank" href="http://www.example.com">link me up</a>'
45
+ # end
46
+
47
+ end
data/spec/message_spec.rb CHANGED
@@ -1,37 +1,49 @@
1
1
  require 'spec_helper' # see http://stackoverflow.com/q/5061179/314318
2
2
 
3
3
  describe MadChatter::Message do
4
- let (:message) { MadChatter::Message.new('joy', 'I am the model of the modern major general') }
5
4
 
5
+ before(:each) do
6
+ MadChatter.users = []
7
+ MadChatter.channels = []
8
+ end
9
+
6
10
  it 'should have a well-known interface' do
11
+ message = MadChatter::Message.new('message', 'my chat message')
7
12
  [:text, :to_json, :filter].each do |m|
8
13
  message.should respond_to(m)
9
14
  end
10
15
  end
11
16
 
12
17
  it 'should encode into JSON correctly' do
13
- message.to_json.should match /modern major general/
18
+ message = MadChatter::Message.new('type', 'message', 'token', 'channel')
19
+ message.to_json.should ==
20
+ '{"type":"type","text":"message","html":"message","username":null,"channel":"channel","growl":"message"}'
14
21
  end
15
-
16
- context '#filter' do
17
- it 'should remove <p> tags' do
18
- message.filter.should_not match /<[\s\\]*p[\s]*>/
19
- end
22
+
23
+ context '#username' do
24
+ let (:user) { MadChatter::User.new('usertoken', 'myusername') }
25
+ let (:message) { MadChatter::Message.new('message', 'text', 'usertoken') }
20
26
 
21
- it 'should be able to handle messages made of empty space' do
22
- message = MadChatter::Message.new('message', " ")
23
- message.filter.should == ''
27
+ it 'should find username correctly' do
28
+ MadChatter.users << user
29
+ message.username.should == 'myusername'
24
30
  end
31
+ end
32
+
33
+ context '#filter' do
34
+
35
+ let (:message) { MadChatter::Message.new('message', 'text') }
25
36
 
26
- it 'should add target="_blank" to links' do
27
- message = MadChatter::Message.new('message', "[link me up](http://www.example.com)")
28
- message.filter.should match %r{<a target="_blank" href="http://www.example.com">link me up</a>}
37
+ it 'should escape all html tags' do
38
+ html = "<strong>bold</strong><script>alert('text')</script>"
39
+ message.filter(html).should == "&lt;strong&gt;bold&lt;/strong&gt;&lt;script&gt;alert('text')&lt;/script&gt;"
29
40
  end
30
41
 
31
- it 'should also add target="_blank" to links that were autolinked' do
32
- message = MadChatter::Message.new('message', 'http://www.example.com')
33
- message.filter.should match %r{<a target="_blank" href="http://www.example.com">http://www.example.com</a>}
42
+ it 'should be able to handle messages made of empty space' do
43
+ text = " "
44
+ message.filter(text).should == ''
34
45
  end
46
+
35
47
  end
36
48
 
37
49
  end