Permalink
Browse files

Merge branch 'api' of github.com:webim/webim-js into api

  • Loading branch information...
emqplus committed Apr 5, 2014
2 parents 84e9ff5 + 211547c commit 62756690a8b3ef65c198cd8ea9ce651b348cfcd9
Showing with 261 additions and 119 deletions.
  1. +108 −0 doc/room.md
  2. +139 −101 src/room.js
  3. +1 −3 src/setting.js
  4. +13 −15 src/webim.js
View
@@ -0,0 +1,108 @@
New Room API
============
Invite
------------
Descr:
Create a temporary room and invite members
API:
POST /webim/room/invite
Request:
id: room id
nick: room nick
members: member id list
Response:
{
"id":"d94781a658a1a939",
"nick":"TestRoom",
"temporary":true,
"pic_url":"images/chat.png",
}
Join
------------
Descr:
Join a temporary room
API:
POST /webim/room/join
Request:
id: room id
Response:
ok???
Leave
------------
Descr:
Leave a temporary room
API:
POST /webim/room/leave
Request:
id: room id
Response:
ok???
Block
------------
Descr:
Block a room, don't receive messages
API:
POST /webim/room/block
Request:
id: room id
Response:
ok
Unblock
------------
Descr:
Unblock a room
API:
POST /webim/room/unblock
Request:
id: room id
Response:
ok
View
@@ -19,110 +19,31 @@
else
return this.data;
},
block: function(id) {
var self = this, d = self.dataHash[id];
if(d && !d.blocked){
d.blocked = true;
var list = [];
each(self.dataHash,function(n,v){
if(!v.temporary && v.blocked) list.push(v.id);
});
self.trigger("block",[id, list]);
}
},
unblock: function(id) {
var self = this, d = self.dataHash[id];
if(d && d.blocked){
d.blocked = false;
var list = [];
each(self.dataHash,function(n,v){
if(!v.temporary && v.blocked) list.push(v.id);
});
self.trigger("unblock",[id, list]);
}
},
set: function(d) {
var self = this, data = self.data, dataHash = self.dataHash, status = {};
each(d,function(k,v){
var id = v.id;
if(id){
v.members = v.members || [];
v.count = v.count || 0;
v.all_count = v.all_count || 0;
if(!dataHash[id]){
dataHash[id] = v;
data.push(v);
}
else extend(dataHash[id], v);
self.trigger("join",[dataHash[id]]);
}
});
},
addMember: function(room_id, info){
var self = this;
if(isArray(info)){
each(info, function(k,v){
self.addMember(room_id, v);
});
return;
};
var room = self.dataHash[room_id];
if(room){
var members = room.members, member;
for (var i = members.length; i--; i){
if (members[i].id == info.id) {
member = members[i];
}
}
if(!member){
info.nick = info.nick;
members.push(info);
room.count = members.length;
self.trigger("addMember",[room_id, info]);
}
}
},
removeMember: function(room_id, member_id){
var self = this
, room = this.dataHash[room_id];
if(room){
var members = room.members, member;
for (var i = members.length; i--; i){
if (members[i].id == member_id) {
member = members[i];
members.splice(i, 1);
room.count--;
}
}
member && self.trigger("removeMember",[room_id, member]);
}
},
initMember: function(id){
var room = this.dataHash[id];
if(room && !room.initMember){
room.initMember = true;
this.loadMember(id);
}
},
loadMember: function(id){
var self = this, options = self.options;
ajax( {
type: "get",
//Invite members to create a temporary room
invite: function(id, nick, members, callback) {
var self = this, options = self.options, user = options.user;
ajax({
type: "post",
cache: false,
url: route( "members" ),
url: route( "invite" ),
data: {
ticket: options.ticket,
id: id,
nick: nick || "",
members: members.join(","),
csrf_token: webim.csrf_token
},
success: function(data){
self.addMember(id, data);
//data is a room object
success: function( data ) {
self.set( [ data ] );
self.loadMember( id );
callback && callback( data );
}
});
},
},
join:function(id, nick, callback){
var self = this, options = self.options, user = options.user;
var self = this, options = self.options, d = self.dataHash[id], user = options.user;
ajax({
type: "post",
@@ -131,20 +52,20 @@
data: {
ticket: options.ticket,
id: id,
//temporary: d.temporary,
nick: nick || "",
csrf_token: webim.csrf_token
},
success: function( data ) {
self.initMember( id );
self.set( [ data ] );
self.loadMember( id );
callback && callback( data );
}
});
},
leave: function(id){
leave: function(id) {
var self = this, options = self.options, d = self.dataHash[id], user = options.user;
if(d){
d.initMember = false;
if(d) {
ajax({
type: "post",
cache: false,
@@ -153,12 +74,129 @@
ticket: options.ticket,
id: id,
nick: user.nick,
temporary: d.temporary,
csrf_token: webim.csrf_token
}
},
success: function( data ) {
delete self.dataHash[id];
self.trigger("leaved",[id]);
}
});
self.trigger("leave",[d]);
}
},
block: function(id) {
var self = this, options = self.options, d = self.dataHash[id];
if(d && !d.blocked){
d.blocked = true;
var list = [];
each(self.dataHash, function(n,v){
if(!v.temporary && v.blocked) list.push(v.id);
});
ajax({
type: "post",
cache: false,
url: route( "block" ),
data: {
ticket: options.ticket,
id: id,
csrf_token: webim.csrf_token
},
success: function(data) {
self.trigger("blocked",[id, list]);
}
});
}
},
unblock: function(id) {
var self = this, options = self.options, d = self.dataHash[id];
if(d && d.blocked){
d.blocked = false;
var list = [];
each(self.dataHash,function(n,v){
if(!v.temporary && v.blocked) list.push(v.id);
});
ajax({
type: "post",
cache: false,
url: route( "unblock" ),
data: {
ticket: options.ticket,
id: id,
csrf_token: webim.csrf_token
},
success: function(data) {
self.trigger("unblocked",[id, list]);
}
});
}
},
set: function(d) {
var self = this, data = self.data, dataHash = self.dataHash, status = {};
each(d,function(k,v){
var id = v.id;
if(!id) return;
v.members = v.members || [];
v.all_count = v.members.length;
v.count = 0;
each(v.members, function(k, m) {
if(m.presence == "online") {
v.count += 1;
}
});
if(!dataHash[id]){
dataHash[id] = v;
data.push(v);
} else {
extend(dataHash[id], v);
//TODO: compare and trigger
}
self.trigger("updated", dataHash[id]);
});
},
loadMember: function(id) {
var self = this, options = self.options;
ajax( {
type: "get",
cache: false,
url: route( "members" ),
data: {
ticket: options.ticket,
id: id,
csrf_token: webim.csrf_token
},
success: function(data){
self.updateMember(id, data);
}
});
},
updateMember: function(room_id, data) {
var room = this.dataHash[room_id];
if(room) {
room.memberLoaded = true;
room.members = data;
this.set([room]);
}
},
onPresence: function(presence) {
var self = this, tp = presence.type;
if( (tp == "join") || (tp == "leave") ) {
var roomId = presence.to || presence.status;
var oneRoom = this.dataHash[roomId];
if(oneRoom && oneRoom.memberLoaded) {
//alert("reloading " + roomId);
self.loadMember(roomId);
}
if(tp == "join") {
self.trigger("memberJoined", [roomId, presence]);
} else {
self.trigger("memberLeaved", [roomId, presence]);
}
}
},
clear:function(){
var self = this;
self.data = [];
Oops, something went wrong.

0 comments on commit 6275669

Please sign in to comment.