2014-08-20 16:47:20 +04:00
|
|
|
var accountInfo = {};
|
|
|
|
|
|
|
|
var showLoggedIn = function(data) {
|
|
|
|
accountInfo = data;
|
|
|
|
getCurrentRoomList();
|
|
|
|
$(".loggedin").css({visibility: "visible"});
|
2014-08-29 18:01:46 +04:00
|
|
|
$("#membership").change(function() {
|
|
|
|
if ($("#membership").val() === "invite") {
|
|
|
|
$("#targetUser").css({visibility: "visible"});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$("#targetUser").css({visibility: "hidden"});
|
|
|
|
}
|
|
|
|
});
|
2014-08-20 16:47:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
$('.login').live('click', function() {
|
|
|
|
var user = $("#userLogin").val();
|
|
|
|
var password = $("#passwordLogin").val();
|
|
|
|
$.ajax({
|
2014-09-02 18:29:38 +04:00
|
|
|
url: "http://localhost:8008/_matrix/client/api/v1/login",
|
2014-08-20 16:47:20 +04:00
|
|
|
type: "POST",
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
data: JSON.stringify({ user: user, password: password, type: "m.login.password" }),
|
|
|
|
dataType: "json",
|
|
|
|
success: function(data) {
|
|
|
|
$("#rooms").find("tr:gt(0)").remove();
|
|
|
|
showLoggedIn(data);
|
|
|
|
},
|
|
|
|
error: function(err) {
|
2014-09-09 01:53:58 +04:00
|
|
|
var errMsg = "To try this, you need a home server running!";
|
|
|
|
var errJson = $.parseJSON(err.responseText);
|
|
|
|
if (errJson) {
|
|
|
|
errMsg = JSON.stringify(errJson);
|
|
|
|
}
|
|
|
|
alert(errMsg);
|
2014-08-20 16:47:20 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var getCurrentRoomList = function() {
|
|
|
|
$("#roomId").val("");
|
|
|
|
// wipe the table and reload it. Using the event stream would be the best
|
|
|
|
// solution but that is out of scope of this fiddle.
|
|
|
|
$("#rooms").find("tr:gt(0)").remove();
|
|
|
|
|
2014-09-02 18:29:38 +04:00
|
|
|
var url = "http://localhost:8008/_matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1";
|
2014-08-20 16:47:20 +04:00
|
|
|
$.getJSON(url, function(data) {
|
2014-08-29 18:01:46 +04:00
|
|
|
var rooms = data.rooms;
|
|
|
|
for (var i=0; i<rooms.length; ++i) {
|
|
|
|
addRoom(rooms[i]);
|
2014-08-20 16:47:20 +04:00
|
|
|
}
|
|
|
|
}).fail(function(err) {
|
|
|
|
alert(JSON.stringify($.parseJSON(err.responseText)));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$('.createRoom').live('click', function() {
|
|
|
|
var data = {};
|
|
|
|
$.ajax({
|
2014-09-02 18:29:38 +04:00
|
|
|
url: "http://localhost:8008/_matrix/client/api/v1/createRoom?access_token="+accountInfo.access_token,
|
2014-08-20 16:47:20 +04:00
|
|
|
type: "POST",
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
dataType: "json",
|
|
|
|
success: function(data) {
|
|
|
|
data.membership = "join"; // you are automatically joined into every room you make.
|
|
|
|
data.latest_message = "";
|
|
|
|
addRoom(data);
|
|
|
|
},
|
|
|
|
error: function(err) {
|
|
|
|
alert(JSON.stringify($.parseJSON(err.responseText)));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var addRoom = function(data) {
|
|
|
|
row = "<tr>" +
|
|
|
|
"<td>"+data.room_id+"</td>" +
|
|
|
|
"<td>"+data.membership+"</td>" +
|
|
|
|
"<td>"+data.room_alias+"</td>" +
|
|
|
|
"</tr>";
|
|
|
|
$("#rooms").append(row);
|
|
|
|
};
|
|
|
|
|
|
|
|
$('.changeMembership').live('click', function() {
|
|
|
|
var roomId = $("#roomId").val();
|
|
|
|
var member = $("#targetUser").val();
|
|
|
|
var membership = $("#membership").val();
|
|
|
|
|
|
|
|
if (roomId.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-02 18:29:38 +04:00
|
|
|
var url = "http://localhost:8008/_matrix/client/api/v1/rooms/$roomid/$membership?access_token=$token";
|
2014-08-20 16:47:20 +04:00
|
|
|
url = url.replace("$token", accountInfo.access_token);
|
|
|
|
url = url.replace("$roomid", encodeURIComponent(roomId));
|
2014-08-29 18:01:46 +04:00
|
|
|
url = url.replace("$membership", membership);
|
2014-08-20 16:47:20 +04:00
|
|
|
|
2014-08-29 18:01:46 +04:00
|
|
|
var data = {};
|
|
|
|
|
|
|
|
if (membership === "invite") {
|
|
|
|
data = {
|
|
|
|
user_id: member
|
2014-08-20 16:47:20 +04:00
|
|
|
};
|
|
|
|
}
|
2014-08-29 18:01:46 +04:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
type: "POST",
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
dataType: "json",
|
|
|
|
success: function(data) {
|
|
|
|
getCurrentRoomList();
|
|
|
|
},
|
|
|
|
error: function(err) {
|
|
|
|
alert(JSON.stringify($.parseJSON(err.responseText)));
|
|
|
|
}
|
|
|
|
});
|
2014-08-20 16:47:20 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
$('.joinAlias').live('click', function() {
|
|
|
|
var roomAlias = $("#roomAlias").val();
|
2014-09-02 18:29:38 +04:00
|
|
|
var url = "http://localhost:8008/_matrix/client/api/v1/join/$roomalias?access_token=$token";
|
2014-08-20 16:47:20 +04:00
|
|
|
url = url.replace("$token", accountInfo.access_token);
|
|
|
|
url = url.replace("$roomalias", encodeURIComponent(roomAlias));
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
2014-08-29 18:01:46 +04:00
|
|
|
type: "POST",
|
2014-08-20 16:47:20 +04:00
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
data: JSON.stringify({}),
|
|
|
|
dataType: "json",
|
|
|
|
success: function(data) {
|
|
|
|
getCurrentRoomList();
|
|
|
|
},
|
|
|
|
error: function(err) {
|
|
|
|
alert(JSON.stringify($.parseJSON(err.responseText)));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|