Source: plugins/wtapi.chat.js

  1. import WTAPI from '../wtapi'
  2. import {JSJaCMessage} from '../jsjac';
  3. /**
  4. * A plugin that provides Chat functionality
  5. *
  6. * @version 1.0.0
  7. */
  8. /**
  9. * Constructor for Chat plugin.
  10. * Instance will be created each time when new WTAPI instance is created. <br />
  11. * Plugin could be accessible thought WTAPI with <b>chat</b> property.
  12. *
  13. * @tutorial chat-plugin
  14. * @memberof WTAPI
  15. * @alias WTAPI.ChatPlugin
  16. * @augments Observer
  17. * @public
  18. * @constructor
  19. */
  20. function ChatPlugin(wtapi) {
  21. this._wtapi = wtapi;
  22. this._wtapi.registerHandler("message", this._handleMessage, this, 100);
  23. this._roster = this._wtapi.roster;
  24. // Initialize supper
  25. WTAPI.Observable.call(this);
  26. }
  27. ChatPlugin.prototype = Object.create(WTAPI.Observable.prototype);
  28. // ============================================================
  29. // Public events
  30. // ============================================================
  31. /**
  32. * Indicates that new message is received from remote user.
  33. *
  34. * @event WTAPI.ChatPlugin#message_received
  35. * @property {WTAPI.User} user
  36. * @property {WTAPI.Message} message
  37. */
  38. /**
  39. * Indicates that message is read by destination.
  40. *
  41. * @event WTAPI.ChatPlugin#message_delivered
  42. * @property {WTAPI.User} user
  43. * @property {string} id Id message delivered
  44. */
  45. // ============================================================
  46. // Public functions
  47. // ============================================================
  48. /**
  49. * Sends a message to user.
  50. *
  51. * @param {WTAPI.User} user
  52. * @param {WTAPI.Message} message
  53. */
  54. ChatPlugin.prototype.send = function (user, message) {
  55. var packet = new JSJaCMessage();
  56. packet.setID(message.getId());
  57. packet.setTo(user.getJid());
  58. packet.setType('chat');
  59. packet.setBody(message.getBody());
  60. packet.appendNode('request', {'xmlns': 'urn:xmpp:receipts'});
  61. packet.appendNode('markable', {'xmlns': 'urn:xmpp:chat-markers:0'});
  62. this._wtapi.getConnection().send(packet);
  63. }
  64. /**
  65. * Sends a notification that message are read by user.
  66. *
  67. * @param {WTAPI.User} user A user that send message
  68. * @param {WTAPI.Message} message
  69. */
  70. ChatPlugin.prototype.sendDeliveryConfirmation = function (user, message) {
  71. var packet = new JSJaCMessage();
  72. packet.setTo(user.getJid());
  73. packet.setType('chat');
  74. packet.appendNode('received', {'xmlns': 'urn:xmpp:receipts', 'id': message.getId()});
  75. packet.appendNode('displayed', {'xmlns': 'urn:xmpp:chat-markers:0', 'id': message.getId()});
  76. packet.appendNode('store', {'xmlns': 'urn:xmpp:hints'});
  77. this._wtapi.getConnection().send(packet);
  78. }
  79. // ============================================================
  80. // ChatPlugin callbacks
  81. // ============================================================
  82. /**
  83. * @private
  84. */
  85. ChatPlugin.prototype._handleMessage = function (packet) {
  86. if (this._handleMessageDelivery(packet)) {
  87. return true;
  88. }
  89. // Default chat message handling
  90. var type = packet.getType();
  91. var body = packet.getBody();
  92. if (!(type == "" || type == "chat" || type == "normal") || body.length == 0) {
  93. return false;
  94. }
  95. var id = packet.getID();
  96. var builder = new WTAPI.Message.Builder();
  97. builder.withId(id);
  98. builder.withBody(body);
  99. var message = builder.build();
  100. var nick = packet.getChildVal('nick', 'http://jabber.org/protocol/nick')
  101. var from = packet.getFromJID();
  102. var jid = from.removeResource().toString();
  103. var user = this._roster.getUser(jid);
  104. if (user === null) {
  105. if (nick == '') {
  106. nick = null;
  107. }
  108. user = this._roster.createUser(jid, nick);
  109. }
  110. this._fire("message_received", user, message);
  111. }
  112. /**
  113. * @private
  114. */
  115. ChatPlugin.prototype._handleMessageDelivery = function (packet) {
  116. var els = packet.getNode().getElementsByTagName('received');
  117. if (els.length == 1) {
  118. if (els.item(0).namespaceURI != 'urn:xmpp:receipts') {
  119. return false;
  120. }
  121. } else {
  122. els = packet.getNode().getElementsByTagName('displayed');
  123. }
  124. if (els.length == 1) {
  125. var received = els.item(0);
  126. var id = received.getAttribute('id');
  127. var from = packet.getFromJID();
  128. var jid = from.removeResource().toString();
  129. var user = this._roster.getUser(jid);
  130. if (user && id) {
  131. this._fire("message_delivered", user, id);
  132. }
  133. }
  134. }
  135. export default ChatPlugin;