Class: JsChat::Room

Inherits:
Object
  • Object
show all
Defined in:
lib/jschat/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Room

Returns a new instance of Room.



119
120
121
122
# File 'lib/jschat/server.rb', line 119

def initialize(name)
  @name = name
  @users = []
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



117
118
119
# File 'lib/jschat/server.rb', line 117

def name
  @name
end

#usersObject

Returns the value of attribute users.



117
118
119
# File 'lib/jschat/server.rb', line 117

def users
  @users
end

Class Method Details

.find(item) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/jschat/server.rb', line 128

def self.find(item)
  @@rooms ||= []

  if item.kind_of? String
    @@rooms.find { |room| room.name.downcase == item.downcase if room.name }
  elsif item.kind_of? User
    @@rooms.find_all { |room| room.users.include? item }
  end
end

.find_or_create(room_name) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/jschat/server.rb', line 138

def self.find_or_create(room_name)
  room = find room_name
  if room.nil?
    room = new(room_name)
    @@rooms << room
  end
  room
end

.roomsObject



147
148
149
# File 'lib/jschat/server.rb', line 147

def self.rooms
  @@rooms
end

.valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/jschat/server.rb', line 124

def self.valid_name?(name)
  User.valid_name?(name[1..-1]) and name[0].chr == '#'
end

Instance Method Details

#add_to_lastlog(message) ⇒ Object



177
178
179
180
181
182
# File 'lib/jschat/server.rb', line 177

def add_to_lastlog(message)
  if message
    message['time'] = Time.now.utc
    JsChat::Storage.driver.log message, name
  end
end

#change_notice(user, response) ⇒ Object



235
236
237
# File 'lib/jschat/server.rb', line 235

def change_notice(user, response)
  notice(user, response, true)
end

#join(user) ⇒ Object



184
185
186
187
188
189
190
191
192
193
# File 'lib/jschat/server.rb', line 184

def join(user)
  if @users.include? user
    Error.new(:already_joined, 'Already in that room')
  else
    @users << user
    user.rooms << self
    join_notice user
    { 'display' => 'join', 'join' => { 'user' => user.name, 'room' => @name } }
  end
end

#join_notice(user) ⇒ Object



239
240
241
# File 'lib/jschat/server.rb', line 239

def join_notice(user)
  notice(user, { 'display' => 'join_notice', 'join_notice' => { 'user' => user.name, 'room' => @name } })
end

#last_update_timeObject



159
160
161
162
# File 'lib/jschat/server.rb', line 159

def last_update_time
  message = JsChat::Storage.driver.lastlog(LASTLOG_DEFAULT, name).last
  message['time'] if message
end

#lastlog(since = nil) ⇒ Object



151
152
153
# File 'lib/jschat/server.rb', line 151

def lastlog(since = nil)
  { 'display' => 'messages', 'messages' => messages_since(since) }
end

#member_namesObject



217
218
219
# File 'lib/jschat/server.rb', line 217

def member_names
  @users.collect { |user| user.name }
end

#message_search(query, limit) ⇒ Object



173
174
175
# File 'lib/jschat/server.rb', line 173

def message_search(query, limit)
  JsChat::Storage.driver.search(query, name, limit)
end

#messages_since(since) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/jschat/server.rb', line 164

def messages_since(since)
  messages = JsChat::Storage.driver.lastlog(LASTLOG_DEFAULT, name)
  if since.nil?
    messages
  else
    messages.select { |m| m['time'] && m['time'] > since }
  end
end

#notice(user, message, all = false) ⇒ Object



225
226
227
228
229
230
231
232
233
# File 'lib/jschat/server.rb', line 225

def notice(user, message, all = false)
  add_to_lastlog message

  @users.each do |u|
    if (u != user and !all) or all
      u.connection.send_response(message)
    end
  end
end

#part(user) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/jschat/server.rb', line 195

def part(user)
  if not @users.include?(user)
    Error.new(:not_in_room, 'Not in that room')
  else
    user.rooms.delete_if { |r| r == self }
    @users.delete_if { |u| u == user }
    part_notice user
    { 'display' => 'part', 'part' => { 'user' => user.name, 'room' => @name } }
  end
end

#part_notice(user) ⇒ Object



243
244
245
# File 'lib/jschat/server.rb', line 243

def part_notice(user)
  notice(user, { 'display' => 'part_notice', 'part_notice' => { 'user' => user.name, 'room' => @name } })
end

#quit_notice(user) ⇒ Object



247
248
249
250
# File 'lib/jschat/server.rb', line 247

def quit_notice(user)
  notice(user, { 'display' => 'quit_notice', 'quit_notice' => { 'user' => user.name, 'room' => @name } })
  @users.delete_if { |u| u == user }
end

#search(query, limit = 100) ⇒ Object



155
156
157
# File 'lib/jschat/server.rb', line 155

def search(query, limit = 100)
  { 'display' => 'messages', 'messages' => message_search(query, limit) }
end

#send_message(message) ⇒ Object



206
207
208
209
210
211
212
213
214
215
# File 'lib/jschat/server.rb', line 206

def send_message(message)
  message['room'] = name
  response = { 'display' => 'message', 'message' => message }

  add_to_lastlog response

  @users.each do |user|
    user.connection.send_response response
  end
end

#to_json(*a) ⇒ Object



221
222
223
# File 'lib/jschat/server.rb', line 221

def to_json(*a)
  { 'name' => @name, 'members' => member_names }.to_json(*a)
end