var accountInfo = {}; var eventStreamInfo = { from: "END" }; var roomInfo = []; var memberInfo = []; var viewingRoomId; // ************** Event Streaming ************** var longpollEventStream = function() { var url = "http://localhost:8008/_matrix/client/api/v1/events?access_token=$token&from=$from"; url = url.replace("$token", accountInfo.access_token); url = url.replace("$from", eventStreamInfo.from); $.getJSON(url, function(data) { eventStreamInfo.from = data.end; var hasNewLatestMessage = false; var updatedMemberList = false; var i=0; var j=0; for (i=0; i 0) { data.room_alias_name = roomAlias; } $.ajax({ url: "http://localhost:8008/_matrix/client/api/v1/createRoom?access_token="+accountInfo.access_token, type: "POST", contentType: "application/json; charset=utf-8", data: JSON.stringify(data), dataType: "json", success: function(response) { $("#roomAlias").val(""); response.membership = "join"; // you are automatically joined into every room you make. response.latest_message = ""; roomInfo.push(response); setRooms(roomInfo); }, error: function(err) { alert(JSON.stringify($.parseJSON(err.responseText))); } }); }); // ************** Getting current state ************** var getCurrentRoomList = function() { var url = "http://localhost:8008/_matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1"; $.getJSON(url, function(data) { var rooms = data.rooms; for (var i=0; i=0; --i) { addMessage(data.chunk[i]); } }); }; var getMemberList = function(roomId) { $("#members").empty(); memberInfo = []; var url = "http://localhost:8008/_matrix/client/api/v1/rooms/" + encodeURIComponent(roomId) + "/members?access_token=" + accountInfo.access_token; $.getJSON(url, function(data) { for (var i=0; i"+roomList[i].room_id+"" + ""+roomList[i].membership+"" + ""+roomList[i].latest_message+"" + ""; rows += row; } $("#rooms").append(rows); $('#rooms').find("tr").click(function(){ var roomId = $(this).find('td:eq(0)').text(); var membership = $(this).find('td:eq(1)').text(); if (membership !== "join") { console.log("Joining room " + roomId); var url = "http://localhost:8008/_matrix/client/api/v1/rooms/$roomid/join?access_token=$token"; url = url.replace("$token", accountInfo.access_token); url = url.replace("$roomid", encodeURIComponent(roomId)); $.ajax({ url: url, type: "POST", contentType: "application/json; charset=utf-8", data: JSON.stringify({membership: "join"}), dataType: "json", success: function(data) { loadRoomContent(roomId); getCurrentRoomList(); }, error: function(err) { alert(JSON.stringify($.parseJSON(err.responseText))); } }); } else { loadRoomContent(roomId); } }); }; var addMessage = function(data) { var msg = data.content.body; if (data.type === "m.room.member") { if (data.content.membership === undefined) { return; } if (data.content.membership === "invite") { msg = "invited " + data.state_key + " to the room"; } else if (data.content.membership === "join") { msg = "joined the room"; } else if (data.content.membership === "leave") { msg = "left the room"; } else if (data.content.membership === "ban") { msg = "was banned from the room"; } } if (msg === undefined) { return; } var row = "" + ""+data.user_id+"" + ""+msg+"" + ""; $("#messages").append(row); }; var addMember = function(data) { var row = "" + ""+data.state_key+"" + ""+data.content.membership+"" + ""; $("#members").append(row); };