chatter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. data/.gitignore +6 -0
  2. data/Gemfile +4 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +36 -0
  5. data/Rakefile +29 -0
  6. data/chatter.gemspec +57 -0
  7. data/config/routes.rb +18 -0
  8. data/lib/chatter/engine.rb +11 -0
  9. data/lib/chatter/version.rb +4 -0
  10. data/lib/chatter.rb +19 -0
  11. data/lib/rails/generators/chatter/install_generator.rb +64 -0
  12. data/lib/rails/generators/chatter/templates/add_name_to_users.rb +8 -0
  13. data/lib/rails/generators/chatter/templates/add_status_to_users.rb +7 -0
  14. data/lib/rails/generators/chatter/templates/add_username_to_users.rb +6 -0
  15. data/lib/rails/generators/chatter/templates/builder.js +136 -0
  16. data/lib/rails/generators/chatter/templates/chat.rb +24 -0
  17. data/lib/rails/generators/chatter/templates/chats.css.less +151 -0
  18. data/lib/rails/generators/chatter/templates/chats.js +504 -0
  19. data/lib/rails/generators/chatter/templates/chats_controller.rb +125 -0
  20. data/lib/rails/generators/chatter/templates/controls.js +963 -0
  21. data/lib/rails/generators/chatter/templates/create_chats.rb +16 -0
  22. data/lib/rails/generators/chatter/templates/css_browser_selector.js +8 -0
  23. data/lib/rails/generators/chatter/templates/dragdrop.js +973 -0
  24. data/lib/rails/generators/chatter/templates/effects.js +1128 -0
  25. data/lib/rails/generators/chatter/templates/jquery.js +19 -0
  26. data/lib/rails/generators/chatter/templates/ol_chatter_image.png +0 -0
  27. data/lib/rails/generators/chatter/templates/ol_chatter_status.png +0 -0
  28. data/lib/rails/generators/chatter/templates/samplea.html.erb +24 -0
  29. data/lib/rails/generators/chatter/templates/screen.css.less +12 -0
  30. metadata +124 -0
@@ -0,0 +1,504 @@
1
+ /*
2
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
3
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
4
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
5
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
6
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
7
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
8
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
9
+ OTHER DEALINGS IN THE SOFTWARE.
10
+
11
+ */
12
+
13
+ var windowFocus = true;
14
+ var username;
15
+ var chatHeartbeatCount = 0;
16
+ var minChatHeartbeat = 1000;
17
+ var maxChatHeartbeat = 33000;
18
+ var chatHeartbeatTime = minChatHeartbeat;
19
+ var originalTitle;
20
+ var blinkOrder = 0;
21
+
22
+ var chatboxFocus = new Array();
23
+ var newMessages = new Array();
24
+ var newMessagesWin = new Array();
25
+ var chatBoxes = new Array();
26
+
27
+ $(document).ready(function(){
28
+ originalTitle = document.title;
29
+ startChatSession();
30
+ $([window, document]).blur(function(){
31
+ windowFocus = false;
32
+ }).focus(function(){
33
+ windowFocus = true;
34
+ document.title = originalTitle;
35
+ });
36
+ });
37
+
38
+ function restructureChatBoxes() {
39
+ align = 0;
40
+
41
+ for (x in chatBoxes) {
42
+
43
+ if ((chatBoxes[x].indexOf("{")) != -1)
44
+ {
45
+ chatboxtitle = chatBoxes[x].toString().substring(1,chatBoxes[x].length - 1);
46
+ }
47
+ else
48
+ {
49
+ chatboxtitle = chatBoxes[x];
50
+ }
51
+
52
+
53
+ if ($("#chatbox_"+chatboxtitle).css('display') != 'none') {
54
+
55
+ if (align == 0) {
56
+ $("#chatbox_"+chatboxtitle).css('right', '180px');
57
+ } else {
58
+ width = (align)*(225+7)+180;
59
+ $("#chatbox_"+chatboxtitle).css('right', width+'px');
60
+ }
61
+ align++;
62
+ }
63
+ }
64
+ }
65
+
66
+ function chatWith(chatuser) {
67
+
68
+ createChatBox(chatuser);
69
+ $("#chatbox_"+chatuser+" .chatboxtextarea").focus();
70
+ }
71
+
72
+ function createChatBox(chatboxtitle,minimizeChatBox) {
73
+
74
+
75
+ if ((chatboxtitle.indexOf("{")) != -1)
76
+ {
77
+ chatboxtitle = chatboxtitle.toString().substring(1,chatboxtitle.length - 1);
78
+ }
79
+
80
+
81
+
82
+ if ($("#chatbox_"+chatboxtitle).length > 0) {
83
+ if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
84
+ $("#chatbox_"+chatboxtitle).css('display','block');
85
+ restructureChatBoxes();
86
+ }
87
+ $("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
88
+ return;
89
+ }
90
+
91
+ $(" <div />" ).attr("id","chatbox_"+chatboxtitle)
92
+ .addClass("chatbox")
93
+ .html('<div class="chatboxhead"><div class="chatboxtitle">'+chatboxtitle+'</div><div class="chatboxoptions"><a href="javascript:void(0)" onclick="javascript:toggleChatBoxGrowth(\''+chatboxtitle+'\')">-</a> <a href="javascript:void(0)" onclick="javascript:closeChatBox(\''+chatboxtitle+'\')">X</a></div><br clear="all"/></div><div class="chatboxcontent"></div><div class="chatboxinput"><textarea class="chatboxtextarea" onkeydown="javascript:return checkChatBoxInputKey(event,this,\''+chatboxtitle+'\');"></textarea></div>')
94
+ .appendTo($( "body" ));
95
+
96
+ $("#chatbox_"+chatboxtitle).css('bottom', '0px');
97
+
98
+ chatBoxeslength = 0;
99
+
100
+ chatBoxes.push(chatboxtitle);
101
+
102
+
103
+ for (x in chatBoxes) {
104
+
105
+ if ((chatBoxes[x].indexOf("{")) != -1)
106
+ {
107
+ chatboxtitle = chatBoxes[x].toString().substring(1,chatBoxes[x].length - 1);
108
+ }
109
+ else
110
+ {
111
+ chatboxtitle = chatBoxes[x];
112
+ }
113
+
114
+
115
+
116
+ if ($("#chatbox_"+chatboxtitle).css('display') != 'none') {
117
+ chatBoxeslength++;
118
+ }
119
+
120
+ }
121
+
122
+ if (chatBoxeslength == 0) {
123
+ $("#chatbox_"+chatboxtitle).css('right', '180px');
124
+ } else {
125
+ width = (chatBoxeslength)*(225+7)+180;
126
+ $("#chatbox_"+chatboxtitle).css('right', width+'px');
127
+ }
128
+
129
+
130
+ //chatBoxes.push(chatboxtitle);
131
+
132
+ if (minimizeChatBox == 1) {
133
+ minimizedChatBoxes = new Array();
134
+
135
+ if ($.cookie('chatbox_minimized')) {
136
+ minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
137
+ }
138
+ minimize = 0;
139
+ for (j=0;j<minimizedChatBoxes.length;j++) {
140
+ if (minimizedChatBoxes[j] == chatboxtitle) {
141
+ minimize = 1;
142
+ }
143
+ }
144
+
145
+ if (minimize == 1) {
146
+ $('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
147
+ $('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
148
+ }
149
+ }
150
+
151
+ chatboxFocus[chatboxtitle] = false;
152
+
153
+ $("#chatbox_"+chatboxtitle+" .chatboxtextarea").blur(function(){
154
+ chatboxFocus[chatboxtitle] = false;
155
+ $("#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
156
+ }).focus(function(){
157
+ chatboxFocus[chatboxtitle] = true;
158
+ newMessages[chatboxtitle] = false;
159
+ $('#chatbox_'+chatboxtitle+' .chatboxhead').removeClass('chatboxblink');
160
+ $("#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
161
+ });
162
+
163
+ $("#chatbox_"+chatboxtitle).click(function() {
164
+ if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
165
+ $("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
166
+ }
167
+ });
168
+
169
+ $("#chatbox_"+chatboxtitle).show();
170
+ }
171
+
172
+
173
+ function chatHeartbeat(){
174
+
175
+ var itemsfound = 0;
176
+
177
+ if (windowFocus == false) {
178
+
179
+ var blinkNumber = 0;
180
+ var titleChanged = 0;
181
+ for (x in newMessagesWin) {
182
+ if (newMessagesWin[x] == true) {
183
+ ++blinkNumber;
184
+ if (blinkNumber >= blinkOrder) {
185
+ document.title = x+' says...';
186
+ titleChanged = 1;
187
+ break;
188
+ }
189
+ }
190
+ }
191
+
192
+ if (titleChanged == 0) {
193
+ document.title = originalTitle;
194
+ blinkOrder = 0;
195
+ } else {
196
+ ++blinkOrder;
197
+ }
198
+
199
+ } else {
200
+ for (x in newMessagesWin) {
201
+ newMessagesWin[x] = false;
202
+ }
203
+ }
204
+
205
+ for (x in newMessages) {
206
+ if (newMessages[x] == true) {
207
+ if (chatboxFocus[x] == false) {
208
+ //FIXME: add toggle all or none policy, otherwise it looks funny
209
+ $('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
210
+ }
211
+ }
212
+ }
213
+
214
+ $.ajax({
215
+ url: "/chats/chat_heartbeat",
216
+ cache: false,
217
+ dataType: "json",
218
+ success: function(data) {
219
+
220
+ $.each(data.items, function(i,item){
221
+ if (item) { // fix strange ie bug
222
+
223
+ chatboxtitle = item.f;
224
+
225
+ if ($("#chatbox_"+chatboxtitle).length <= 0) {
226
+ createChatBox(chatboxtitle);
227
+ }
228
+ if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
229
+ $("#chatbox_"+chatboxtitle).css('display','block');
230
+ restructureChatBoxes();
231
+ }
232
+
233
+ if (item.s == 1) {
234
+
235
+ item.f = username;
236
+ }
237
+
238
+ if (item.s == 2) {
239
+
240
+ $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m.substring(1,item.m.length - 1)+'</span></div>');
241
+ } else {
242
+
243
+ newMessages[chatboxtitle] = true;
244
+ newMessagesWin[chatboxtitle] = true;
245
+ $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
246
+ }
247
+
248
+ $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
249
+ itemsfound += 1;
250
+ }
251
+ });
252
+
253
+ chatHeartbeatCount++;
254
+
255
+ if (itemsfound > 0) {
256
+ chatHeartbeatTime = minChatHeartbeat;
257
+ chatHeartbeatCount = 1;
258
+ } else if (chatHeartbeatCount >= 10) {
259
+ chatHeartbeatTime *= 2;
260
+ chatHeartbeatCount = 1;
261
+ if (chatHeartbeatTime > maxChatHeartbeat) {
262
+ chatHeartbeatTime = maxChatHeartbeat;
263
+ }
264
+ }
265
+
266
+ setTimeout('chatHeartbeat();',chatHeartbeatTime);
267
+ }});
268
+ }
269
+
270
+ function closeChatBox(chatboxtitle) {
271
+ $('#chatbox_'+chatboxtitle).css('display','none');
272
+ restructureChatBoxes();
273
+ var sendData = {
274
+ 'chatbox' : chatboxtitle
275
+ };
276
+
277
+ $.ajax({url: "/chats/close_chat",
278
+
279
+ type: 'POST',
280
+
281
+ dataType: 'json',
282
+
283
+ beforeSend: function(xhr) {
284
+
285
+ xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
286
+ },
287
+
288
+ data: sendData,
289
+
290
+ success: function(response){
291
+
292
+ }
293
+
294
+ });
295
+ //$.post("/chats/close_chat", { chatbox: chatboxtitle} , function(data){
296
+ //});
297
+
298
+ }
299
+
300
+ function toggleChatBoxGrowth(chatboxtitle) {
301
+ if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') == 'none') {
302
+
303
+ var minimizedChatBoxes = new Array();
304
+
305
+ if ($.cookie('chatbox_minimized')) {
306
+ minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
307
+ }
308
+
309
+ var newCookie = '';
310
+
311
+ for (i=0;i<minimizedChatBoxes.length;i++) {
312
+ if (minimizedChatBoxes[i] != chatboxtitle) {
313
+ newCookie += chatboxtitle+'|';
314
+ }
315
+ }
316
+
317
+ newCookie = newCookie.slice(0, -1)
318
+
319
+
320
+ $.cookie('chatbox_minimized', newCookie);
321
+ $('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','block');
322
+ $('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','block');
323
+ $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
324
+ } else {
325
+
326
+ var newCookie = chatboxtitle;
327
+
328
+ if ($.cookie('chatbox_minimized')) {
329
+ newCookie += '|'+$.cookie('chatbox_minimized');
330
+ }
331
+
332
+
333
+ $.cookie('chatbox_minimized',newCookie);
334
+ $('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
335
+ $('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
336
+ }
337
+
338
+ }
339
+
340
+ function checkChatBoxInputKey(event,chatboxtextarea,chatboxtitle) {
341
+
342
+ if(event.keyCode == 13 && event.shiftKey == 0) {
343
+ message = $(chatboxtextarea).val();
344
+ message = message.replace(/^\s+|\s+$/g,"");
345
+
346
+ $(chatboxtextarea).val('');
347
+ $(chatboxtextarea).focus();
348
+ $(chatboxtextarea).css('height','44px');
349
+ if (message != '') {
350
+
351
+ var postData = {
352
+ 'to' : chatboxtitle,
353
+ 'message' : message
354
+ };
355
+
356
+
357
+ $.ajax({url: "/chats/send_chat",
358
+
359
+ type: 'POST',
360
+
361
+ dataType: 'json',
362
+
363
+ beforeSend: function(xhr) {
364
+
365
+ xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
366
+ },
367
+
368
+ data: postData,
369
+
370
+ success: function(response){
371
+
372
+ message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
373
+ $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+username+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+message+'</span></div>');
374
+ $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
375
+
376
+ }
377
+
378
+ });
379
+ //$.post("/chats/send_chat", {to: chatboxtitle, message: message} , function(data){
380
+ //message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
381
+ //$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+username+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+message+'</span></div>');
382
+ //$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
383
+ //});
384
+ }
385
+ chatHeartbeatTime = minChatHeartbeat;
386
+ chatHeartbeatCount = 1;
387
+
388
+ return false;
389
+ }
390
+
391
+ var adjustedHeight = chatboxtextarea.clientHeight;
392
+ var maxHeight = 94;
393
+
394
+ if (maxHeight > adjustedHeight) {
395
+ adjustedHeight = Math.max(chatboxtextarea.scrollHeight, adjustedHeight);
396
+ if (maxHeight)
397
+ adjustedHeight = Math.min(maxHeight, adjustedHeight);
398
+ if (adjustedHeight > chatboxtextarea.clientHeight)
399
+ $(chatboxtextarea).css('height',adjustedHeight+8 +'px');
400
+ } else {
401
+ $(chatboxtextarea).css('overflow','auto');
402
+ }
403
+
404
+ }
405
+
406
+ function startChatSession(){
407
+ $.ajax({
408
+ url: "/chats/start_chat_session",
409
+ cache: false,
410
+ dataType: "json",
411
+ success: function(data) {
412
+ username = data.username;
413
+
414
+ $.each(data.items, function(i,item){
415
+ if (item) { // fix strange ie bug
416
+
417
+ chatboxtitle = item.f;
418
+
419
+ if ($("#chatbox_"+chatboxtitle).length <= 0) {
420
+ createChatBox(chatboxtitle,1);
421
+ }
422
+
423
+ if (item.s == 1) {
424
+ item.f = username;
425
+ }
426
+
427
+ if (item.s == 2) {
428
+ $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m.substring(1,item.m.length - 1)+'</span></div>');
429
+ } else {
430
+ $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
431
+ }
432
+ }
433
+ });
434
+
435
+
436
+
437
+ for (i=0;i<chatBoxes.length;i++) {
438
+
439
+
440
+ if ((chatBoxes[i].indexOf("{")) != -1)
441
+ chatboxtitle = chatBoxes[i].toString().substring(1,chatBoxes[i].length - 1);
442
+ else
443
+ chatboxtitle = chatBoxes[i];
444
+
445
+ $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
446
+ setTimeout('$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);', 100); // yet another strange ie bug
447
+ }
448
+
449
+ setTimeout('chatHeartbeat();',chatHeartbeatTime);
450
+
451
+ }});
452
+ }
453
+
454
+ /**
455
+ * Cookie plugin
456
+ *
457
+ * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
458
+ * Dual licensed under the MIT and GPL licenses:
459
+ * http://www.opensource.org/licenses/mit-license.php
460
+ * http://www.gnu.org/licenses/gpl.html
461
+ *
462
+ */
463
+
464
+ jQuery.cookie = function(name, value, options) {
465
+ if (typeof value != 'undefined') { // name and value given, set cookie
466
+ options = options || {};
467
+ if (value === null) {
468
+ value = '';
469
+ options.expires = -1;
470
+ }
471
+ var expires = '';
472
+ if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
473
+ var date;
474
+ if (typeof options.expires == 'number') {
475
+ date = new Date();
476
+ date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
477
+ } else {
478
+ date = options.expires;
479
+ }
480
+ expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
481
+ }
482
+ // CAUTION: Needed to parenthesize options.path and options.domain
483
+ // in the following expressions, otherwise they evaluate to undefined
484
+ // in the packed version for some reason...
485
+ var path = options.path ? '; path=' + (options.path) : '';
486
+ var domain = options.domain ? '; domain=' + (options.domain) : '';
487
+ var secure = options.secure ? '; secure' : '';
488
+ document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
489
+ } else { // only name given, get cookie
490
+ var cookieValue = null;
491
+ if (document.cookie && document.cookie != '') {
492
+ var cookies = document.cookie.split(';');
493
+ for (var i = 0; i < cookies.length; i++) {
494
+ var cookie = jQuery.trim(cookies[i]);
495
+ // Does this cookie string begin with the name we want?
496
+ if (cookie.substring(0, name.length + 1) == (name + '=')) {
497
+ cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
498
+ break;
499
+ }
500
+ }
501
+ }
502
+ return cookieValue;
503
+ }
504
+ };
@@ -0,0 +1,125 @@
1
+ # Don't forget to set the session[:username] after login
2
+
3
+ class ChatsController < ::ApplicationController
4
+ include ActionView::Helpers::SanitizeHelper
5
+
6
+ before_filter :set_arrays
7
+
8
+ def samplea
9
+
10
+ if user_signed_in?
11
+ Chat.update_status(current_user)
12
+ session[:username] = current_user.username
13
+
14
+ @onlineusers = User.find(:all, :conditions => ["status= 'online' and id != '#{current_user.id}'"])
15
+
16
+ end
17
+
18
+
19
+ end
20
+
21
+
22
+
23
+ def chat_heartbeat
24
+
25
+
26
+ items = []
27
+ Chat.find(:all, :conditions => {:to => session[:username], :recd => 0}, :order => 'id ASC').each do |chat|
28
+ items = session[:chat_history][chat.from] if session[:open_chat_boxes][chat.from].nil? and session[:chat_history][chat.from]
29
+ chat.message = sanitize(chat.message)
30
+
31
+
32
+
33
+ items << {:s => 0, :f => chat.from, :m => chat.message}
34
+
35
+ session[:chat_history][chat.from] = [] if session[:chat_history][chat.from].nil?
36
+
37
+ session[:chat_history][chat.from] << {:s => 0, :f => chat.from, :m => chat.message}
38
+
39
+ session[:ts_chat_boxes][chat.from] = nil
40
+
41
+ session[:open_chat_boxes][chat.from] = chat.sent
42
+
43
+
44
+ chat.recd = 1
45
+ chat.save!
46
+ end
47
+ if !session[:open_chat_boxes].empty?
48
+ session[:open_chat_boxes].each do |chatbox, value|
49
+ if session[:ts_chat_boxes][chatbox].nil?
50
+
51
+
52
+ time = DateTime.parse(value.to_s)
53
+ now = (DateTime.now - time) * 86400
54
+ stime = time.strftime("%I:%MAM %b #{time.day.ordinalize}")
55
+ message = "Sent at #{stime}"
56
+ if now > 120
57
+ items << {:s => 2, :f => chatbox, :m => "{#{message}}"}
58
+ session[:chat_history][chatbox] = [] if session[:chat_history][chatbox].nil?
59
+ session[:chat_history][chatbox] << {:s => 2, :f => chatbox, :m => "{#{message}}"}
60
+ session[:ts_chat_boxes][chatbox] = 1;
61
+ end
62
+ end
63
+ end
64
+ end
65
+ render :json => {:items => items}.to_json
66
+ end
67
+
68
+ def start_chat_session
69
+
70
+ items = []
71
+ if !session[:open_chat_boxes].empty?
72
+ session[:open_chat_boxes].each do |chatbox, item|
73
+ items += chat_box_session(chatbox)
74
+ end
75
+ end
76
+ render :json => {:username => current_user.try(:username).to_s, :items => items}.to_json
77
+ end
78
+
79
+ def send_chat
80
+
81
+ from = current_user.username
82
+
83
+ to = params[:to]
84
+
85
+ message = params[:message]
86
+
87
+ session[:open_chat_boxes][to] = DateTime.now.strftime("%Y-%m-%d %H:%M:%S")
88
+ messagesan = sanitize(message)
89
+ session[:chat_history][to] = [] if session[:chat_history][to].nil?
90
+ session[:chat_history][to] << {:s => 1, :f => "{#{to}}", :m => "#{messagesan}"}
91
+ session[:ts_chat_boxes][to] = nil
92
+ Chat.create(:from => from, :to => to, :message => messagesan, :sent => DateTime.now)
93
+
94
+ render :text => 1
95
+ end
96
+
97
+
98
+
99
+
100
+ def close_chat
101
+
102
+ #session[:open_chat_boxes][params[:chatbox]] = nil
103
+ render :text => 1
104
+ end
105
+
106
+
107
+ private
108
+ def chat_box_session(chatbox)
109
+
110
+ return session[:chat_history][chatbox] if session[:chat_history][chatbox]
111
+ return []
112
+ end
113
+
114
+ def set_arrays
115
+
116
+ session[:chat_history] = Hash.new if session[:chat_history].nil?
117
+ session[:open_chat_boxes] = Hash.new if session[:open_chat_boxes].nil?
118
+ session[:ts_chat_boxes] = Hash.new if session[:ts_chat_boxes].nil?
119
+ end
120
+
121
+
122
+ end
123
+
124
+
125
+