demo.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var accountInfo = {};
  2. var showLoggedIn = function(data) {
  3. accountInfo = data;
  4. getCurrentRoomList();
  5. $(".loggedin").css({visibility: "visible"});
  6. $("#membership").change(function() {
  7. if ($("#membership").val() === "invite") {
  8. $("#targetUser").css({visibility: "visible"});
  9. }
  10. else {
  11. $("#targetUser").css({visibility: "hidden"});
  12. }
  13. });
  14. };
  15. $('.login').live('click', function() {
  16. var user = $("#userLogin").val();
  17. var password = $("#passwordLogin").val();
  18. $.ajax({
  19. url: "http://localhost:8008/_matrix/client/api/v1/login",
  20. type: "POST",
  21. contentType: "application/json; charset=utf-8",
  22. data: JSON.stringify({ user: user, password: password, type: "m.login.password" }),
  23. dataType: "json",
  24. success: function(data) {
  25. $("#rooms").find("tr:gt(0)").remove();
  26. showLoggedIn(data);
  27. },
  28. error: function(err) {
  29. var errMsg = "To try this, you need a home server running!";
  30. var errJson = $.parseJSON(err.responseText);
  31. if (errJson) {
  32. errMsg = JSON.stringify(errJson);
  33. }
  34. alert(errMsg);
  35. }
  36. });
  37. });
  38. var getCurrentRoomList = function() {
  39. $("#roomId").val("");
  40. // wipe the table and reload it. Using the event stream would be the best
  41. // solution but that is out of scope of this fiddle.
  42. $("#rooms").find("tr:gt(0)").remove();
  43. var url = "http://localhost:8008/_matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1";
  44. $.getJSON(url, function(data) {
  45. var rooms = data.rooms;
  46. for (var i=0; i<rooms.length; ++i) {
  47. addRoom(rooms[i]);
  48. }
  49. }).fail(function(err) {
  50. alert(JSON.stringify($.parseJSON(err.responseText)));
  51. });
  52. };
  53. $('.createRoom').live('click', function() {
  54. var data = {};
  55. $.ajax({
  56. url: "http://localhost:8008/_matrix/client/api/v1/createRoom?access_token="+accountInfo.access_token,
  57. type: "POST",
  58. contentType: "application/json; charset=utf-8",
  59. data: JSON.stringify(data),
  60. dataType: "json",
  61. success: function(data) {
  62. data.membership = "join"; // you are automatically joined into every room you make.
  63. data.latest_message = "";
  64. addRoom(data);
  65. },
  66. error: function(err) {
  67. alert(JSON.stringify($.parseJSON(err.responseText)));
  68. }
  69. });
  70. });
  71. var addRoom = function(data) {
  72. row = "<tr>" +
  73. "<td>"+data.room_id+"</td>" +
  74. "<td>"+data.membership+"</td>" +
  75. "<td>"+data.room_alias+"</td>" +
  76. "</tr>";
  77. $("#rooms").append(row);
  78. };
  79. $('.changeMembership').live('click', function() {
  80. var roomId = $("#roomId").val();
  81. var member = $("#targetUser").val();
  82. var membership = $("#membership").val();
  83. if (roomId.length === 0) {
  84. return;
  85. }
  86. var url = "http://localhost:8008/_matrix/client/api/v1/rooms/$roomid/$membership?access_token=$token";
  87. url = url.replace("$token", accountInfo.access_token);
  88. url = url.replace("$roomid", encodeURIComponent(roomId));
  89. url = url.replace("$membership", membership);
  90. var data = {};
  91. if (membership === "invite") {
  92. data = {
  93. user_id: member
  94. };
  95. }
  96. $.ajax({
  97. url: url,
  98. type: "POST",
  99. contentType: "application/json; charset=utf-8",
  100. data: JSON.stringify(data),
  101. dataType: "json",
  102. success: function(data) {
  103. getCurrentRoomList();
  104. },
  105. error: function(err) {
  106. alert(JSON.stringify($.parseJSON(err.responseText)));
  107. }
  108. });
  109. });
  110. $('.joinAlias').live('click', function() {
  111. var roomAlias = $("#roomAlias").val();
  112. var url = "http://localhost:8008/_matrix/client/api/v1/join/$roomalias?access_token=$token";
  113. url = url.replace("$token", accountInfo.access_token);
  114. url = url.replace("$roomalias", encodeURIComponent(roomAlias));
  115. $.ajax({
  116. url: url,
  117. type: "POST",
  118. contentType: "application/json; charset=utf-8",
  119. data: JSON.stringify({}),
  120. dataType: "json",
  121. success: function(data) {
  122. getCurrentRoomList();
  123. },
  124. error: function(err) {
  125. alert(JSON.stringify($.parseJSON(err.responseText)));
  126. }
  127. });
  128. });