From 89ed81bb1f4fec8e20fb9d518d440307dacc55be Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 19 Aug 2014 17:34:31 +0100 Subject: More fiddles, more fun! --- jsfiddles/create_room_send_msg/demo.css | 17 +++++ jsfiddles/create_room_send_msg/demo.html | 30 +++++++++ jsfiddles/create_room_send_msg/demo.js | 109 +++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 jsfiddles/create_room_send_msg/demo.css create mode 100644 jsfiddles/create_room_send_msg/demo.html create mode 100644 jsfiddles/create_room_send_msg/demo.js diff --git a/jsfiddles/create_room_send_msg/demo.css b/jsfiddles/create_room_send_msg/demo.css new file mode 100644 index 0000000000..48a55f372d --- /dev/null +++ b/jsfiddles/create_room_send_msg/demo.css @@ -0,0 +1,17 @@ +.loggedin { + visibility: hidden; +} + +p { + font-family: monospace; +} + +table +{ + border-spacing:5px; +} + +th,td +{ + padding:5px; +} diff --git a/jsfiddles/create_room_send_msg/demo.html b/jsfiddles/create_room_send_msg/demo.html new file mode 100644 index 0000000000..31c26c7631 --- /dev/null +++ b/jsfiddles/create_room_send_msg/demo.html @@ -0,0 +1,30 @@ +
+

This room creation / message sending demo requires a home server to be running on http://localhost:8080

+
+
+ + + +
+
+
+ + +
+
+ + + +
+ + + + + + + + + +
Room IDMy stateRoom AliasLatest message
+
+ diff --git a/jsfiddles/create_room_send_msg/demo.js b/jsfiddles/create_room_send_msg/demo.js new file mode 100644 index 0000000000..c17eb26b98 --- /dev/null +++ b/jsfiddles/create_room_send_msg/demo.js @@ -0,0 +1,109 @@ +var accountInfo = {}; + +var showLoggedIn = function(data) { + accountInfo = data; + getCurrentRoomList(); + $(".loggedin").css({visibility: "visible"}); +}; + +$('.login').live('click', function() { + var user = $("#userLogin").val(); + var password = $("#passwordLogin").val(); + $.ajax({ + url: "http://localhost:8080/matrix/client/api/v1/login", + type: "POST", + contentType: "application/json; charset=utf-8", + data: JSON.stringify({ user: user, password: password, type: "m.login.password" }), + dataType: "json", + success: function(data) { + showLoggedIn(data); + }, + error: function(err) { + alert(JSON.stringify($.parseJSON(err.responseText))); + } + }); +}); + +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"; + $.getJSON(url, function(data) { + for (var i=0; i 0) { + data.room_alias_name = roomAlias; + } + $.ajax({ + url: "http://localhost:8080/matrix/client/api/v1/rooms?access_token="+accountInfo.access_token, + 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 = "" + + ""+data.room_id+"" + + ""+data.membership+"" + + ""+data.room_alias+"" + + ""+data.latest_message+"" + + ""; + $("#rooms").append(row); +}; + +$('.sendMessage').live('click', function() { + var roomId = $("#roomId").val(); + var body = $("#messageBody").val(); + var msgId = $.now(); + + if (roomId.length === 0 || body.length === 0) { + return; + } + + var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/messages/$user/$msgid?access_token=$token"; + url = url.replace("$token", accountInfo.access_token); + url = url.replace("$roomid", encodeURIComponent(roomId)); + url = url.replace("$user", encodeURIComponent(accountInfo.user_id)); + url = url.replace("$msgid", msgId); + + var data = { + msgtype: "m.text", + body: body + }; + + $.ajax({ + url: url, + type: "PUT", + contentType: "application/json; charset=utf-8", + data: JSON.stringify(data), + dataType: "json", + success: function(data) { + $("#messageBody").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(); + getCurrentRoomList(); + }, + error: function(err) { + alert(JSON.stringify($.parseJSON(err.responseText))); + } + }); +}); -- cgit 1.4.1