social_stream-presence 0.0.21 → 0.1.0

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 (27) hide show
  1. data/app/assets/javascripts/chat_audio.js +73 -0
  2. data/app/assets/javascripts/chat_interface_manager.js.erb +197 -0
  3. data/app/assets/javascripts/chat_parser.js +243 -0
  4. data/app/assets/javascripts/chat_utilities.js +71 -0
  5. data/app/assets/javascripts/chat_window_manager.js +138 -0
  6. data/app/assets/javascripts/social_stream-presence.js +3 -0
  7. data/app/assets/javascripts/store.js +0 -7
  8. data/app/assets/javascripts/xmpp_client_management.js.erb +304 -0
  9. data/app/assets/stylesheets/chat.css +49 -3
  10. data/app/assets/stylesheets/social_stream-presence.css +1 -0
  11. data/app/controllers/xmpp_controller.rb +43 -9
  12. data/app/views/{xmpp/_chat_connecting.html.erb → chat/_connecting.html.erb} +0 -0
  13. data/app/views/{xmpp/_chat_contacts.html.erb → chat/_contacts.html.erb} +0 -0
  14. data/app/views/{xmpp/_chat.html.erb → chat/_index.html.erb} +3 -13
  15. data/app/views/{xmpp/_chat_off.html.erb → chat/_off.html.erb} +0 -0
  16. data/app/views/chat/_settings.html.erb +38 -0
  17. data/config/routes.rb +1 -1
  18. data/db/migrate/20111116194112_add_chat_enabled_column_to_user.rb +9 -0
  19. data/lib/social_stream/presence/version.rb +1 -1
  20. data/{app → vendor}/assets/javascripts/jquery.tools.tooltip.js +0 -0
  21. data/{app → vendor}/assets/javascripts/jquery.ui.chatbox.js +14 -12
  22. data/{app → vendor}/assets/javascripts/strophe.js +0 -0
  23. data/{app → vendor}/assets/stylesheets/jquery.ui.chatbox.css +0 -0
  24. metadata +19 -14
  25. data/app/assets/javascripts/jquery-ui-1.8.14.custom.min.js +0 -789
  26. data/app/assets/javascripts/jquery.tools.min.js +0 -17
  27. data/app/assets/javascripts/xmpp_client.js.erb +0 -925
@@ -0,0 +1,71 @@
1
+ ////////////////////
2
+ //Test functions
3
+ ////////////////////
4
+
5
+ function log(msg) {
6
+ //console.log(msg)
7
+ }
8
+
9
+
10
+ ////////////////////
11
+ //Blink page title when focus lost on new messages
12
+ ////////////////////
13
+
14
+ var chatFocus;
15
+
16
+ function onChatBlur() {
17
+ chatFocus = false;
18
+ };
19
+
20
+ function onChatFocus(){
21
+ stopBlink();
22
+ titles = []; //Remove titles after StopBlink!
23
+ chatFocus = true;
24
+ };
25
+
26
+ function initFocusListeners(){
27
+ if (/*@cc_on!@*/false) { // check for Internet Explorer
28
+ document.onfocusin = onFocus;
29
+ document.onfocusout = onBlur;
30
+ } else {
31
+ window.onfocus = onChatFocus;
32
+ window.onblur = onChatBlur;
33
+ }
34
+ }
35
+
36
+
37
+ var blinkTimer;
38
+ var titles=[];
39
+
40
+ function blinkTitle(titles,index){
41
+ $(document).attr("title", titles[index]);
42
+ index = (index+1)%titles.length
43
+ blinkTimer=setTimeout(function(){blinkTitle(titles,index)}, 2000);
44
+ }
45
+
46
+ function stopBlink(){
47
+ clearTimeout(blinkTimer);
48
+ if (titles.length > 0) {
49
+ $(document).attr("title", titles[0]);
50
+ }
51
+ }
52
+
53
+ function blinkTitleOnMessage(username){
54
+ if (!chatFocus){
55
+ if (titles.length==0){
56
+ titles.push($(document).attr("title"))
57
+ }
58
+ if (titles.indexOf(username) == -1){
59
+ titles.push(username + " says...")
60
+ }
61
+ stopBlink();
62
+ blinkTitle(titles,titles.length-1);
63
+ }
64
+ }
65
+
66
+
67
+ ////////////////////
68
+ //Next features...
69
+ ////////////////////
70
+
71
+
@@ -0,0 +1,138 @@
1
+ ////////////////////
2
+ //Chat functions
3
+ ////////////////////
4
+
5
+ var nBox = 0;
6
+ var maxBox = 5;
7
+ var chatBoxWidth = 230;
8
+ var visibleChatBoxes = new Array();
9
+ var chatBoxSeparation = chatBoxWidth+12;
10
+
11
+
12
+ function createChatBox(guest_slug,guest_name,guest_jid,user_name,user_jid){
13
+
14
+ //Create chatbox for new conversations
15
+ //Open chatbox for old conversations
16
+
17
+ //Box Variable name = getChatVariableFromSlug(guest_slug)
18
+ if (typeof window[getChatVariableFromSlug(guest_slug)] == 'undefined') {
19
+
20
+ //Add div with id = guest_slug
21
+ $("#chat_divs").append("<div id=" + guest_slug + " name=" + guest_name + "></div>")
22
+
23
+ //Add CSS [...]
24
+
25
+
26
+ //Offset Management for new box
27
+ boxParams = getBoxParams();
28
+ var offset = boxParams[0];
29
+ var position = boxParams[1];
30
+
31
+
32
+ window[getChatVariableFromSlug(guest_slug)] = $("#" + guest_slug).chatbox({id: user_name,
33
+ user:{key : "value"},
34
+ hidden: false,
35
+ offset: offset, // relative to right edge of the browser window
36
+ width: chatBoxWidth, // width of the chatbox
37
+ title : guest_name,
38
+ position: position,
39
+ priority: visibleChatBoxes.length+1,
40
+ boxClosed: function(id) {
41
+
42
+ position = $("#" + guest_slug).chatbox("option", "position");
43
+
44
+ for (i=position+1;i<visibleChatBoxes.length+1;i++){
45
+ visibleChatBoxes[i-1].chatbox("option", "offset", visibleChatBoxes[i-1].chatbox("option", "offset") - chatBoxSeparation);
46
+ visibleChatBoxes[i-1].chatbox("option", "position", visibleChatBoxes[i-1].chatbox("option", "position") - 1 );
47
+ }
48
+
49
+ visibleChatBoxes.splice(position-1,1);
50
+ $("#" + guest_slug).chatbox("option", "hidden", true);
51
+ nBox--;
52
+ },
53
+
54
+ messageSent : function(id, user, msg) {
55
+ rotatePriority(guest_slug);
56
+ $("#" + guest_slug).chatbox("option", "boxManager").addMsg(id, getParsedContent(msg,true));
57
+ sendChatMessage(user_jid,guest_jid,msg);
58
+ }});
59
+
60
+ visibleChatBoxes[position-1] = window[getChatVariableFromSlug(guest_slug)];
61
+
62
+ return true;
63
+
64
+ } else {
65
+
66
+ if (visibleChatBoxes.indexOf(window[getChatVariableFromSlug(guest_slug)]) == -1) {
67
+
68
+ //Offset Management for old box
69
+ boxParams = getBoxParams();
70
+ var offset = boxParams[0];
71
+ var position = boxParams[1];
72
+
73
+ window[getChatVariableFromSlug(guest_slug)].chatbox("option", "offset", offset);
74
+ window[getChatVariableFromSlug(guest_slug)].chatbox("option", "position", position);
75
+ visibleChatBoxes[position-1] = window[getChatVariableFromSlug(guest_slug)];
76
+ }
77
+
78
+ window[getChatVariableFromSlug(guest_slug)].chatbox("option", "hidden", false);
79
+ return false;
80
+ }
81
+
82
+ }
83
+
84
+ function getBoxParams(){
85
+
86
+ var boxParams = new Array(2);
87
+
88
+ if (nBox==maxBox){
89
+ //Select box to replaced
90
+ replaced = visibleChatBoxes[getBoxIndexToReplace()];
91
+ replaced.chatbox("option", "hidden", true)
92
+ index = visibleChatBoxes.indexOf(replaced);
93
+ boxParams[0] = replaced.chatbox("option", "offset")
94
+ boxParams[1] = replaced.chatbox("option", "position")
95
+ } else {
96
+ nBox++;
97
+ boxParams[0] = (nBox-1)*(chatBoxSeparation);
98
+ boxParams[1] = nBox;
99
+ }
100
+
101
+ return boxParams
102
+ }
103
+
104
+
105
+ function getBoxIndexToReplace(){
106
+
107
+ tmp = visibleChatBoxes[0];
108
+ for (i=0;i<visibleChatBoxes.length;i++){
109
+ if (visibleChatBoxes[i].chatbox("option", "priority") > tmp.chatbox("option", "priority")) {
110
+ tmp = visibleChatBoxes[i];
111
+ }
112
+ }
113
+
114
+ return visibleChatBoxes.indexOf(tmp);
115
+ }
116
+
117
+
118
+ function rotatePriority(guest_slug){
119
+ priority = $("#" + guest_slug).chatbox("option", "priority")
120
+ if(priority>1){
121
+ for (i=0;i<visibleChatBoxes.length;i++){
122
+ if(visibleChatBoxes[i].chatbox("option", "priority")<priority){
123
+ visibleChatBoxes[i].chatbox("option", "priority",visibleChatBoxes[i].chatbox("option", "priority")+1);
124
+ }
125
+ }
126
+ $("#" + guest_slug).chatbox("option", "priority", 1);
127
+ }
128
+ }
129
+
130
+
131
+ function getChatVariableFromSlug(slug){
132
+ return "slug_" + slug;
133
+ }
134
+
135
+
136
+ function getSlugFromChatVariable(variable){
137
+ return variable.split("_")[1];
138
+ }
@@ -1,2 +1,5 @@
1
1
  //= require jquery
2
+ //= require strophe
3
+ //= require jquery.tools.tooltip
4
+ //= require jquery.ui.chatbox
2
5
  //= require_tree .
@@ -2,13 +2,6 @@
2
2
  //Store password with session storage
3
3
  ////////////////////
4
4
 
5
- $(document).ready(function () {
6
- $('.storePass').bind('click', function () {
7
- storePassword();
8
- });
9
- });
10
-
11
-
12
5
  function storePassword() {
13
6
 
14
7
  //Dont store password if cookie authentication is enable
@@ -0,0 +1,304 @@
1
+ ////////////////////
2
+ //Hash table
3
+ ////////////////////
4
+ var statusMessage = new Array();
5
+ statusMessage[''] = "";
6
+ statusMessage['chat'] = "";
7
+ statusMessage['away'] = "Away";
8
+ statusMessage['xa'] = "Away";
9
+ statusMessage['dnd'] = "Busy";
10
+
11
+
12
+
13
+ ////////////////////
14
+ //Connect functions
15
+ ////////////////////
16
+
17
+ function connectToServerWithCookie(){
18
+ try {
19
+ connection = new Strophe.Connection(BOSH_SERVICE);
20
+ connection.connect(user_jid, cookie, onConnect);
21
+ } catch (err) {
22
+ //"Handle errors"
23
+ return false;
24
+ }
25
+ }
26
+
27
+ //Password: Get from chatPassword param if exists, instead try to get from sessionStorage.
28
+ function connectToServerWithPassword(chatPassword){
29
+
30
+ //Get Password
31
+ if ((chatPassword!=null)&&(chatPassword!="")){
32
+ var password = chatPassword;
33
+ } else if ((window.sessionStorage)&&(sessionStorage.getItem("ss_user_pass") != null)) {
34
+ var password = sessionStorage.getItem("ss_user_pass");
35
+ } else {
36
+ return false;
37
+ }
38
+
39
+ try {
40
+ //Connect actual user to the chat
41
+ connection = new Strophe.Connection(BOSH_SERVICE);
42
+ connection.connect(user_jid, password, onConnect);
43
+ } catch (err) {
44
+ //"Handle errors"
45
+ return false;
46
+ }
47
+
48
+ return true;
49
+ }
50
+
51
+ ////////
52
+ //Auth Methods
53
+ ///////
54
+ function authByCookie(){
55
+ var authMethod = '<%= SocialStream::Presence.auth_method %>';
56
+ return authMethod=="cookie";
57
+ }
58
+
59
+ function authByPassword(){
60
+ var authMethod = '<%= SocialStream::Presence.auth_method %>';
61
+ return authMethod=="password";
62
+ }
63
+
64
+ function ifCookie(){
65
+ return (!(typeof cookie == 'undefined'))
66
+ }
67
+
68
+
69
+ ////////////////////
70
+ //Stanza management using Strophe
71
+ ////////////////////
72
+
73
+ //Global variables
74
+ var userStatus = "chat";
75
+ var awayTimerPeriod = 16000;
76
+ var timerPeriod = 5000;
77
+ var refreshMinTime = 3*timerPeriod;
78
+ var awayTime = 300000;
79
+ var awayCounter = 0;
80
+ var timerCounter = 0;
81
+ var connection = null;
82
+ var userConnected = false;
83
+ var reconnectAttempts = 3;
84
+ var awayTimer;
85
+ var timer;
86
+ var requestContacts=false;
87
+ var cyclesToRefresh = (refreshMinTime/timerPeriod);
88
+
89
+
90
+ function onConnect(status) {
91
+
92
+ //Status.ERROR An error has occurred
93
+ //Status.CONNECTING The connection is currently being made
94
+ //Status.CONNFAIL The connection attempt failed
95
+ //Status.AUTHENTICATING The connection is authenticating
96
+ //Status.AUTHFAIL The authentication attempt failed
97
+ //Status.CONNECTED The connection has succeeded
98
+ //Status.DISCONNECTED The connection has been terminated
99
+ //Status.DISCONNECTING The connection is currently being terminated
100
+ //Status.ATTACHED The connection has been attached
101
+
102
+ log('Strophe onConnect callback call with status ' + status);
103
+
104
+ if (status == Strophe.Status.ATTACHED){
105
+ log('Strophe connection attached');
106
+ return;
107
+ }
108
+
109
+ if (status == Strophe.Status.AUTHENTICATING ){
110
+ log('Strophe connection AUTHENTICATING');
111
+ return;
112
+ }
113
+
114
+ if (status == Strophe.Status.CONNECTING) {
115
+ log('Strophe is connecting.');
116
+ return;
117
+ }
118
+
119
+ clearTimeout(initialTimer);
120
+
121
+ if (status == Strophe.Status.CONNFAIL) {
122
+ log('Strophe failed to connect.');
123
+ userConnected = false;
124
+ setTimeout ("onReconnect()", 3000);
125
+ } else if (status == Strophe.Status.AUTHFAIL) {
126
+ log('Strophe authentication fail.');
127
+ if ((window.sessionStorage)&&(sessionStorage.getItem("ss_user_pass") != null)){
128
+ sessionStorage.setItem("ss_user_pass",null);
129
+ }
130
+ userConnected = false;
131
+ } else if (status == Strophe.Status.ERROR) {
132
+ log('Strophe error.');
133
+ userConnected = false;
134
+ } else if (status == Strophe.Status.DISCONNECTED) {
135
+ log('Strophe is disconnected.');
136
+ userConnected = false;
137
+ clearTimeout(awayTimer);
138
+ setTimeout ("onReconnect()", 3000);
139
+ } else if (status == Strophe.Status.CONNECTED) {
140
+ log('Strophe is connected.');
141
+ log('Presenze stanza send for:' + connection.jid);
142
+ connection.addHandler(onMessage, null, 'message', null, null, null);
143
+ connection.addHandler(onPresence, null, 'presence', null, null, null);
144
+ //addHandler:(callback, namespace to match, stanza name, stanza type, stanza id , stanza from, options)
145
+ sendStatus(userStatus);
146
+ userConnected = true;
147
+ awayTimer = setInterval("awayTimerFunction()", awayTimerPeriod);
148
+ timer = setInterval("timerFunction()", timerPeriod);
149
+ }
150
+
151
+ updateChatWindow();
152
+ }
153
+
154
+ function onReconnect(){
155
+ if ((connection != null)&&(!userConnected)) {
156
+ if (reconnectAttempts>0) {
157
+ reconnectAttempts--;
158
+
159
+ if (authByCookie()){
160
+ //Authentication by cookie
161
+ connectToServerWithCookie();
162
+ } else {
163
+ //Authentication by password
164
+ connectToServerWithPassword(null);
165
+ }
166
+ setTimeout ("onReconnect()", 9000);
167
+ } else {
168
+ //Notify issue to Rails App Server?
169
+ }
170
+ }
171
+ }
172
+
173
+
174
+ ////////
175
+ //Manage Message stanzas
176
+ ///////
177
+ function onMessage(msg) {
178
+ var to = msg.getAttribute('to');
179
+ var from = msg.getAttribute('from');
180
+ var type = msg.getAttribute('type');
181
+ var elems = msg.getElementsByTagName('body');
182
+
183
+ if (type == "chat" && elems.length > 0) {
184
+
185
+ var body = elems[0];
186
+ var from_slug = from.split("@")[0];
187
+ var from_name = $("#" + from_slug).attr("name");
188
+ var from_jid = from_slug + "@" + domain;
189
+
190
+
191
+ if (typeof ($('div.user_presence[slug=' + from_slug + ']').attr('name')) == 'undefined') {
192
+ //No connectionBox for this user!
193
+ var from_name = from_slug;
194
+ refreshChatWindow();
195
+ } else {
196
+ showConnectionBoxFromSlug(from_slug);
197
+ var from_name = $('div.user_presence[slug=' + from_slug + ']').attr('name');
198
+ }
199
+
200
+ if (createChatBox(from_slug,from_name,from_jid,user_name,user_jid)) {
201
+ } else {
202
+ window[getChatVariableFromSlug(from_slug)].chatbox("option", "boxManager").toggleBox(true);
203
+ }
204
+
205
+ var content = getParsedContent(Strophe.getText(body),false)
206
+ //Send message to chatBox and post-message functions.
207
+ $("#" + from_slug).chatbox("option", "boxManager").addMsg(from_name, content);
208
+ rotatePriority(from_slug);
209
+ blinkTitleOnMessage(from_name);
210
+ if (mustPlaySoundForChatWindow(window[getChatVariableFromSlug(from_slug)])){
211
+ playSound("onMessageAudio");
212
+ }
213
+
214
+ }
215
+
216
+ // we must return true to keep the handler alive.
217
+ // returning false would remove it after it finishes.
218
+ return true;
219
+ }
220
+
221
+
222
+ ////////
223
+ //Manage Presence stanzas
224
+ ///////
225
+ function onPresence(presence) {
226
+
227
+ //Check presence stanza type
228
+ ptype = $(presence).attr('type');
229
+
230
+ switch (ptype){
231
+ case undefined:
232
+ processAvailablePresenceStanza(presence)
233
+ break;
234
+ case "available":
235
+ processAvailablePresenceStanza(presence)
236
+ break;
237
+ case "unavailable":
238
+ processUnavailablePresenceStanza(presence)
239
+ break;
240
+ default :
241
+ //Stanza type not recognize
242
+ processAvailablePresenceStanza(presence)
243
+ }
244
+
245
+ return true;
246
+ }
247
+
248
+
249
+ function processAvailablePresenceStanza(presence){
250
+ from = $(presence).attr('from');
251
+ slug = from.split("@")[0];
252
+
253
+ if (slug != user_slug) {
254
+ if (getConnectionBoxFromSlug(slug)!=null){
255
+ status = $(presence).find('show').text();
256
+ setUserIconStatus(slug, status);
257
+ if (cacheConnectedUsers.indexOf(slug) != -1) {
258
+ showConnectionBoxFromSlug(slug);
259
+ }
260
+ } else {
261
+ setTimeout("refreshChatWindow()", 3000);
262
+ }
263
+ }
264
+ }
265
+
266
+ function processUnavailablePresenceStanza(presence){
267
+ from = $(presence).attr('from');
268
+ slug = from.split("@")[0];
269
+
270
+ if (slug != user_slug) {
271
+ if (getConnectionBoxFromSlug(slug)!=null){
272
+ hideConnectionBoxFromSlug(slug)
273
+ }
274
+ }
275
+ }
276
+
277
+ ////////
278
+ //Send Message stanzas
279
+ ///////
280
+ function sendChatMessage(from,to,text){
281
+ var type = "chat";
282
+ var body= $build("body");
283
+ body.t(text);
284
+ var message = $msg({to: to, from: from, type: 'chat'}).cnode(body.tree());
285
+ connection.send(message.tree());
286
+ resumeAwayTimerIfAway();
287
+ return true;
288
+ }
289
+
290
+
291
+ ////////
292
+ //Send Presence stanzas with status
293
+ ///////
294
+ function sendStatus(status){
295
+ if (status in statusMessage){
296
+ //Send status to the XMPP Server
297
+ var pres = $pres()
298
+ .c('status')
299
+ .t(statusMessage[status]).up() //Status message
300
+ .c('show')
301
+ .t(status);
302
+ connection.send(pres.tree());
303
+ }
304
+ }