-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.js
117 lines (104 loc) · 3.28 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*jslint sloppy:true */
/*globals freedom */
/**
* Chat demo backend.
* Because the Social API provides message passing primitives,
* this backend simply forwards messages between the front-end and our Social provider
* Note that you should be able to plug-and-play a variety of social providers
* and still have a working demo
*
**/
var logger;
freedom.core().getLogger('[Chat Backend]').then(function (log) { logger = log; });
var Chat = function (dispatchEvent) {
this.dispatchEvent = dispatchEvent;
this.userList = {}; //Keep track of the roster
this.clientList = {};
this.myClientState = null;
this.social = freedom.socialprovider();
this.boot();
};
Chat.prototype.login = function() {
var promise = this.social.login({
agent: 'chatdemo',
version: '0.1',
url: '',
interactive: true,
rememberLogin: false
});
promise.then(function (ret) {
this.myClientState = ret;
logger.log("onLogin", this.myClientState);
if (ret.status === this.social.STATUS.ONLINE) {
this.dispatchEvent('recv-uid', ret.clientId);
this.dispatchEvent('recv-status', "online");
} else {
this.dispatchEvent('recv-status', "offline");
}
}.bind(this), function (err) {
logger.log("Log In Failed", err);
this.dispatchEvent("recv-err", err);
}.bind(this));
return promise;
};
/**
* sent messages should be forwarded to the Social provider.
**/
Chat.prototype.send = function (to, message) {
return this.social.sendMessage(to, message);
};
Chat.prototype.boot = function (arg) {
this.updateBuddyList();
/**
* on an 'onMessage' event from the Social provider
* Just forward it to the outer page
*/
this.social.on('onMessage', function (data) {
logger.info("Message Received", data);
this.dispatchEvent('recv-message', data);
}.bind(this));
/**
* On user profile changes, let's keep track of them
**/
this.social.on('onUserProfile', function (data) {
//Just save it for now
this.userList[data.userId] = data;
this.updateBuddyList();
}.bind(this));
/**
* On newly online or offline clients, let's update the roster
**/
this.social.on('onClientState', function (data) {
logger.debug("Roster Change", data);
if (data.status === this.social.STATUS.OFFLINE) {
if (this.clientList.hasOwnProperty(data.clientId)) {
delete this.clientList[data.clientId];
}
} else { //Only track non-offline clients
this.clientList[data.clientId] = data;
}
//If mine, send to the page
if (this.myClientState !== null && data.clientId === this.myClientState.clientId) {
if (data.status === this.social.STATUS.ONLINE) {
this.dispatchEvent('recv-status', "online");
} else {
this.dispatchEvent('recv-status', "offline");
}
}
this.updateBuddyList();
}.bind(this));
};
Chat.prototype.updateBuddyList = function () {
// Iterate over our roster and send over user profiles where there is at least 1 client online
var buddylist = {}, k, userId;
for (k in this.clientList) {
if (this.clientList.hasOwnProperty(k)) {
userId = this.clientList[k].userId;
if (this.userList[userId]) {
buddylist[userId] = this.userList[userId];
}
}
}
this.dispatchEvent('recv-buddylist', buddylist);
};
freedom().providePromises(Chat);