Fix JSFiddles to work with the new C-S API.

pull/977/head
Kegan Dougal 10 years ago committed by Mark Haines
parent d5da30422c
commit 1f3811b1b8

@ -25,11 +25,12 @@ $('.login').live('click', function() {
}); });
var getCurrentRoomList = function() { var getCurrentRoomList = function() {
var url = "http://localhost:8080/matrix/client/api/v1/im/sync?access_token=" + accountInfo.access_token + "&from=END&to=START&limit=1"; var url = "http://localhost:8080/matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1";
$.getJSON(url, function(data) { $.getJSON(url, function(data) {
for (var i=0; i<data.length; ++i) { var rooms = data.rooms;
data[i].latest_message = data[i].messages.chunk[0].content.body; for (var i=0; i<rooms.length; ++i) {
addRoom(data[i]); rooms[i].latest_message = rooms[i].messages.chunk[0].content.body;
addRoom(rooms[i]);
} }
}).fail(function(err) { }).fail(function(err) {
alert(JSON.stringify($.parseJSON(err.responseText))); alert(JSON.stringify($.parseJSON(err.responseText)));
@ -43,7 +44,7 @@ $('.createRoom').live('click', function() {
data.room_alias_name = roomAlias; data.room_alias_name = roomAlias;
} }
$.ajax({ $.ajax({
url: "http://localhost:8080/matrix/client/api/v1/rooms?access_token="+accountInfo.access_token, url: "http://localhost:8080/matrix/client/api/v1/createRoom?access_token="+accountInfo.access_token,
type: "POST", type: "POST",
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
data: JSON.stringify(data), data: JSON.stringify(data),
@ -78,11 +79,9 @@ $('.sendMessage').live('click', function() {
return; return;
} }
var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/messages/$user/$msgid?access_token=$token"; var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/send/m.room.message?access_token=$token";
url = url.replace("$token", accountInfo.access_token); url = url.replace("$token", accountInfo.access_token);
url = url.replace("$roomid", encodeURIComponent(roomId)); url = url.replace("$roomid", encodeURIComponent(roomId));
url = url.replace("$user", encodeURIComponent(accountInfo.user_id));
url = url.replace("$msgid", msgId);
var data = { var data = {
msgtype: "m.text", msgtype: "m.text",
@ -91,7 +90,7 @@ $('.sendMessage').live('click', function() {
$.ajax({ $.ajax({
url: url, url: url,
type: "PUT", type: "POST",
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
data: JSON.stringify(data), data: JSON.stringify(data),
dataType: "json", dataType: "json",

@ -65,14 +65,15 @@ $('.login').live('click', function() {
var getCurrentRoomList = function() { var getCurrentRoomList = function() {
$("#roomId").val(""); $("#roomId").val("");
var url = "http://localhost:8080/matrix/client/api/v1/im/sync?access_token=" + accountInfo.access_token + "&from=END&to=START&limit=1"; var url = "http://localhost:8080/matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1";
$.getJSON(url, function(data) { $.getJSON(url, function(data) {
for (var i=0; i<data.length; ++i) { var rooms = data.rooms;
if ("messages" in data[i]) { for (var i=0; i<rooms.length; ++i) {
data[i].latest_message = data[i].messages.chunk[0].content.body; if ("messages" in rooms[i]) {
rooms[i].latest_message = rooms[i].messages.chunk[0].content.body;
} }
} }
roomInfo = data; roomInfo = rooms;
setRooms(roomInfo); setRooms(roomInfo);
}).fail(function(err) { }).fail(function(err) {
alert(JSON.stringify($.parseJSON(err.responseText))); alert(JSON.stringify($.parseJSON(err.responseText)));
@ -92,17 +93,14 @@ $('.sendMessage').live('click', function() {
var sendMessage = function(roomId) { var sendMessage = function(roomId) {
var body = "jsfiddle message @" + $.now(); var body = "jsfiddle message @" + $.now();
var msgId = $.now();
if (roomId.length === 0) { if (roomId.length === 0) {
return; return;
} }
var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/messages/$user/$msgid?access_token=$token"; var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/send/m.room.message?access_token=$token";
url = url.replace("$token", accountInfo.access_token); url = url.replace("$token", accountInfo.access_token);
url = url.replace("$roomid", encodeURIComponent(roomId)); url = url.replace("$roomid", encodeURIComponent(roomId));
url = url.replace("$user", encodeURIComponent(accountInfo.user_id));
url = url.replace("$msgid", msgId);
var data = { var data = {
msgtype: "m.text", msgtype: "m.text",
@ -111,7 +109,7 @@ var sendMessage = function(roomId) {
$.ajax({ $.ajax({
url: url, url: url,
type: "PUT", type: "POST",
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
data: JSON.stringify(data), data: JSON.stringify(data),
dataType: "json", dataType: "json",

@ -38,8 +38,9 @@ var longpollEventStream = function() {
else if (data.chunk[i].type === "m.room.member") { else if (data.chunk[i].type === "m.room.member") {
if (viewingRoomId === data.chunk[i].room_id) { if (viewingRoomId === data.chunk[i].room_id) {
console.log("Got new member: " + JSON.stringify(data.chunk[i])); console.log("Got new member: " + JSON.stringify(data.chunk[i]));
addMessage(data.chunk[i]);
for (j=0; j<memberInfo.length; ++j) { for (j=0; j<memberInfo.length; ++j) {
if (memberInfo[j].target_user_id === data.chunk[i].target_user_id) { if (memberInfo[j].state_key === data.chunk[i].state_key) {
memberInfo[j] = data.chunk[i]; memberInfo[j] = data.chunk[i];
updatedMemberList = true; updatedMemberList = true;
break; break;
@ -50,7 +51,7 @@ var longpollEventStream = function() {
updatedMemberList = true; updatedMemberList = true;
} }
} }
if (data.chunk[i].target_user_id === accountInfo.user_id) { if (data.chunk[i].state_key === accountInfo.user_id) {
getCurrentRoomList(); // update our join/invite list getCurrentRoomList(); // update our join/invite list
} }
} }
@ -133,7 +134,7 @@ $('.createRoom').live('click', function() {
data.room_alias_name = roomAlias; data.room_alias_name = roomAlias;
} }
$.ajax({ $.ajax({
url: "http://localhost:8080/matrix/client/api/v1/rooms?access_token="+accountInfo.access_token, url: "http://localhost:8080/matrix/client/api/v1/createRoom?access_token="+accountInfo.access_token,
type: "POST", type: "POST",
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
data: JSON.stringify(data), data: JSON.stringify(data),
@ -154,14 +155,15 @@ $('.createRoom').live('click', function() {
// ************** Getting current state ************** // ************** Getting current state **************
var getCurrentRoomList = function() { var getCurrentRoomList = function() {
var url = "http://localhost:8080/matrix/client/api/v1/im/sync?access_token=" + accountInfo.access_token + "&from=END&to=START&limit=1"; var url = "http://localhost:8080/matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1";
$.getJSON(url, function(data) { $.getJSON(url, function(data) {
for (var i=0; i<data.length; ++i) { var rooms = data.rooms;
if ("messages" in data[i]) { for (var i=0; i<rooms.length; ++i) {
data[i].latest_message = data[i].messages.chunk[0].content.body; if ("messages" in rooms[i]) {
rooms[i].latest_message = rooms[i].messages.chunk[0].content.body;
} }
} }
roomInfo = data; roomInfo = rooms;
setRooms(roomInfo); setRooms(roomInfo);
}).fail(function(err) { }).fail(function(err) {
alert(JSON.stringify($.parseJSON(err.responseText))); alert(JSON.stringify($.parseJSON(err.responseText)));
@ -179,7 +181,8 @@ var loadRoomContent = function(roomId) {
var getMessages = function(roomId) { var getMessages = function(roomId) {
$("#messages").empty(); $("#messages").empty();
var url = "http://localhost:8080/matrix/client/api/v1/rooms/" + roomId + "/messages/list?access_token=" + accountInfo.access_token + "&from=END&to=START&limit=10"; var url = "http://localhost:8080/matrix/client/api/v1/rooms/" +
encodeURIComponent(roomId) + "/messages?access_token=" + accountInfo.access_token + "&from=END&dir=b&limit=10";
$.getJSON(url, function(data) { $.getJSON(url, function(data) {
for (var i=data.chunk.length-1; i>=0; --i) { for (var i=data.chunk.length-1; i>=0; --i) {
addMessage(data.chunk[i]); addMessage(data.chunk[i]);
@ -190,7 +193,8 @@ var getMessages = function(roomId) {
var getMemberList = function(roomId) { var getMemberList = function(roomId) {
$("#members").empty(); $("#members").empty();
memberInfo = []; memberInfo = [];
var url = "http://localhost:8080/matrix/client/api/v1/rooms/" + roomId + "/members/list?access_token=" + accountInfo.access_token; var url = "http://localhost:8080/matrix/client/api/v1/rooms/" +
encodeURIComponent(roomId) + "/members?access_token=" + accountInfo.access_token;
$.getJSON(url, function(data) { $.getJSON(url, function(data) {
for (var i=0; i<data.chunk.length; ++i) { for (var i=0; i<data.chunk.length; ++i) {
memberInfo.push(data.chunk[i]); memberInfo.push(data.chunk[i]);
@ -212,11 +216,9 @@ $('.sendMessage').live('click', function() {
var sendMessage = function(roomId, body) { var sendMessage = function(roomId, body) {
var msgId = $.now(); var msgId = $.now();
var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/messages/$user/$msgid?access_token=$token"; var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/send/m.room.message?access_token=$token";
url = url.replace("$token", accountInfo.access_token); url = url.replace("$token", accountInfo.access_token);
url = url.replace("$roomid", encodeURIComponent(roomId)); url = url.replace("$roomid", encodeURIComponent(roomId));
url = url.replace("$user", encodeURIComponent(accountInfo.user_id));
url = url.replace("$msgid", msgId);
var data = { var data = {
msgtype: "m.text", msgtype: "m.text",
@ -225,7 +227,7 @@ var sendMessage = function(roomId, body) {
$.ajax({ $.ajax({
url: url, url: url,
type: "PUT", type: "POST",
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
data: JSON.stringify(data), data: JSON.stringify(data),
dataType: "json", dataType: "json",
@ -260,13 +262,12 @@ var setRooms = function(roomList) {
var membership = $(this).find('td:eq(1)').text(); var membership = $(this).find('td:eq(1)').text();
if (membership !== "join") { if (membership !== "join") {
console.log("Joining room " + roomId); console.log("Joining room " + roomId);
var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/members/$user/state?access_token=$token"; var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/join?access_token=$token";
url = url.replace("$token", accountInfo.access_token); url = url.replace("$token", accountInfo.access_token);
url = url.replace("$roomid", encodeURIComponent(roomId)); url = url.replace("$roomid", encodeURIComponent(roomId));
url = url.replace("$user", encodeURIComponent(accountInfo.user_id));
$.ajax({ $.ajax({
url: url, url: url,
type: "PUT", type: "POST",
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
data: JSON.stringify({membership: "join"}), data: JSON.stringify({membership: "join"}),
dataType: "json", dataType: "json",
@ -286,16 +287,33 @@ var setRooms = function(roomList) {
}; };
var addMessage = function(data) { var addMessage = function(data) {
var msg = data.content.body;
if (data.type === "m.room.member") {
if (data.content.membership === "invite") {
msg = "<em>invited " + data.state_key + " to the room</em>";
}
else if (data.content.membership === "join") {
msg = "<em>joined the room</em>";
}
else if (data.content.membership === "leave") {
msg = "<em>left the room</em>";
}
else {
msg = "<em>" + data.content.membership + "</em>";
}
}
var row = "<tr>" + var row = "<tr>" +
"<td>"+data.user_id+"</td>" + "<td>"+data.user_id+"</td>" +
"<td>"+data.content.body+"</td>" + "<td>"+msg+"</td>" +
"</tr>"; "</tr>";
$("#messages").append(row); $("#messages").append(row);
}; };
var addMember = function(data) { var addMember = function(data) {
var row = "<tr>" + var row = "<tr>" +
"<td>"+data.target_user_id+"</td>" + "<td>"+data.state_key+"</td>" +
"<td>"+data.content.membership+"</td>" + "<td>"+data.content.membership+"</td>" +
"</tr>"; "</tr>";
$("#members").append(row); $("#members").append(row);

@ -45,7 +45,7 @@ $('.login').live('click', function() {
var user = $("#userLogin").val(); var user = $("#userLogin").val();
var password = $("#passwordLogin").val(); var password = $("#passwordLogin").val();
$.getJSON("http://localhost:8080/matrix/client/api/v1/login", function(data) { $.getJSON("http://localhost:8080/matrix/client/api/v1/login", function(data) {
if (data.type !== "m.login.password") { if (data.flows[0].type !== "m.login.password") {
alert("I don't know how to login with this type: " + data.type); alert("I don't know how to login with this type: " + data.type);
return; return;
} }
@ -60,7 +60,7 @@ $('.logout').live('click', function() {
}); });
$('.testToken').live('click', function() { $('.testToken').live('click', function() {
var url = "http://localhost:8080/matrix/client/api/v1/im/sync?access_token=" + accountInfo.access_token + "&from=END&to=START&limit=1"; var url = "http://localhost:8080/matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1";
$.getJSON(url, function(data) { $.getJSON(url, function(data) {
$("#imSyncText").text(JSON.stringify(data, undefined, 2)); $("#imSyncText").text(JSON.stringify(data, undefined, 2));
}).fail(function(err) { }).fail(function(err) {

@ -14,9 +14,9 @@
<input type="text" id="roomId" placeholder="Room ID"></input> <input type="text" id="roomId" placeholder="Room ID"></input>
<input type="text" id="targetUser" placeholder="Target User ID"></input> <input type="text" id="targetUser" placeholder="Target User ID"></input>
<select id="membership"> <select id="membership">
<option value="invite">Invite</option> <option value="invite">invite</option>
<option value="join">Join</option> <option value="join">join</option>
<option value="leave">Leave</option> <option value="leave">leave</option>
</select> </select>
<input type="button" class="changeMembership" value="Change Membership"></input> <input type="button" class="changeMembership" value="Change Membership"></input>
</form> </form>

@ -4,6 +4,14 @@ var showLoggedIn = function(data) {
accountInfo = data; accountInfo = data;
getCurrentRoomList(); getCurrentRoomList();
$(".loggedin").css({visibility: "visible"}); $(".loggedin").css({visibility: "visible"});
$("#membership").change(function() {
if ($("#membership").val() === "invite") {
$("#targetUser").css({visibility: "visible"});
}
else {
$("#targetUser").css({visibility: "hidden"});
}
});
}; };
$('.login').live('click', function() { $('.login').live('click', function() {
@ -31,10 +39,11 @@ var getCurrentRoomList = function() {
// solution but that is out of scope of this fiddle. // solution but that is out of scope of this fiddle.
$("#rooms").find("tr:gt(0)").remove(); $("#rooms").find("tr:gt(0)").remove();
var url = "http://localhost:8080/matrix/client/api/v1/im/sync?access_token=" + accountInfo.access_token + "&from=END&to=START&limit=1"; var url = "http://localhost:8080/matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1";
$.getJSON(url, function(data) { $.getJSON(url, function(data) {
for (var i=0; i<data.length; ++i) { var rooms = data.rooms;
addRoom(data[i]); for (var i=0; i<rooms.length; ++i) {
addRoom(rooms[i]);
} }
}).fail(function(err) { }).fail(function(err) {
alert(JSON.stringify($.parseJSON(err.responseText))); alert(JSON.stringify($.parseJSON(err.responseText)));
@ -44,7 +53,7 @@ var getCurrentRoomList = function() {
$('.createRoom').live('click', function() { $('.createRoom').live('click', function() {
var data = {}; var data = {};
$.ajax({ $.ajax({
url: "http://localhost:8080/matrix/client/api/v1/rooms?access_token="+accountInfo.access_token, url: "http://localhost:8080/matrix/client/api/v1/createRoom?access_token="+accountInfo.access_token,
type: "POST", type: "POST",
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
data: JSON.stringify(data), data: JSON.stringify(data),
@ -78,44 +87,32 @@ $('.changeMembership').live('click', function() {
return; return;
} }
var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/members/$user/state?access_token=$token"; var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/$membership?access_token=$token";
url = url.replace("$token", accountInfo.access_token); url = url.replace("$token", accountInfo.access_token);
url = url.replace("$roomid", encodeURIComponent(roomId)); url = url.replace("$roomid", encodeURIComponent(roomId));
url = url.replace("$user", encodeURIComponent(member)); url = url.replace("$membership", membership);
if (membership === "leave") { var data = {};
$.ajax({
url: url,
type: "DELETE",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
getCurrentRoomList();
},
error: function(err) {
alert(JSON.stringify($.parseJSON(err.responseText)));
}
});
}
else {
var data = {
membership: membership
};
$.ajax({ if (membership === "invite") {
url: url, data = {
type: "PUT", user_id: member
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)));
}
});
} }
$.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)));
}
});
}); });
$('.joinAlias').live('click', function() { $('.joinAlias').live('click', function() {
@ -125,7 +122,7 @@ $('.joinAlias').live('click', function() {
url = url.replace("$roomalias", encodeURIComponent(roomAlias)); url = url.replace("$roomalias", encodeURIComponent(roomAlias));
$.ajax({ $.ajax({
url: url, url: url,
type: "PUT", type: "POST",
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
data: JSON.stringify({}), data: JSON.stringify({}),
dataType: "json", dataType: "json",

Loading…
Cancel
Save