Sparrow is a libre front-end forum software powered by the Matrix protocol and using a Matrix homeserver as its back-end

index.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import * as matrixSDK from 'matrix-js-sdk'
  2. function fetchTopics() {
  3. matrixSDK.createClient({
  4. baseUrl: 'https://matrix.trancendances.fr',
  5. })
  6. .registerGuest()
  7. .then((data) => {
  8. return matrixSDK.createClient({
  9. baseUrl: 'https://matrix.trancendances.fr',
  10. accessToken: data['access_token'],
  11. userId: data['user_id'],
  12. deviceId: data['device_id'],
  13. })
  14. })
  15. .then((client) => {
  16. client.publicRooms(function(err, data) {
  17. for (let publicRoom of data.chunk) {
  18. if (!publicRoom['guest_can_join']) {
  19. continue
  20. }
  21. client.roomInitialSync(publicRoom["room_id"], 20)
  22. .then(async (syncData) => {
  23. let name, creator, creationDate, categories
  24. let nbMessages = 0
  25. for (let stateEvent of syncData.state) {
  26. if (stateEvent.type === "m.room.create") {
  27. let userID = stateEvent.content.creator
  28. let profileInfo = await client.getProfileInfo(stateEvent.content.creator, 'displayname')
  29. creator = {
  30. userID: userID,
  31. displayName: profileInfo.displayname,
  32. }
  33. creationDate = new Date(stateEvent['origin_server_ts'])
  34. }
  35. if (stateEvent.type === "m.room.name") {
  36. name = stateEvent.content.name
  37. }
  38. if (stateEvent.type === "m.forum.topic.categories") {
  39. categories = stateEvent.content.categories.join(',')
  40. }
  41. }
  42. for (let messageEvent of syncData.messages.chunk) {
  43. if (messageEvent.type === "m.room.message") {
  44. nbMessages++;
  45. }
  46. }
  47. appendTopic(name, publicRoom['room_id'], creator, creationDate, nbMessages, categories)
  48. })
  49. }
  50. });
  51. })
  52. .catch((err) => {
  53. console.log(err);
  54. })
  55. }
  56. function appendTopic(name, roomID, creator, creationDate, nbMessages, categories) {
  57. let list = document.getElementById('topics')
  58. let el = document.createElement('li')
  59. let topicMsg = "Topic " + name + " (id " + roomID + ") created by "
  60. + creator.displayName + " (" + creator.userID + ") on "
  61. + creationDate.toString() + " containing " + nbMessages + " messages"
  62. if (categories) {
  63. topicMsg += ", listed in categorie(s) " + categories
  64. }
  65. el.innerHTML = topicMsg
  66. list.appendChild(el)
  67. }
  68. fetchTopics()