Source: jsxc.lib.Message.js

  1. /**
  2. * Load message object with given uid.
  3. *
  4. * @class Message
  5. * @memberOf jsxc
  6. * @param {string} uid Unified identifier from message object
  7. */
  8. /**
  9. * Create new message object.
  10. *
  11. * @class Message
  12. * @memberOf jsxc
  13. * @param {object} args New message properties
  14. * @param {string} args.bid
  15. * @param {direction} args.direction
  16. * @param {string} args.msg
  17. * @param {boolean} args.encrypted
  18. * @param {boolean} args.forwarded
  19. * @param {boolean} args.sender
  20. * @param {integer} args.stamp
  21. * @param {object} args.attachment Attached data
  22. * @param {string} args.attachment.name File name
  23. * @param {string} args.attachment.size File size
  24. * @param {string} args.attachment.type File type
  25. * @param {string} args.attachment.data File data
  26. */
  27. jsxc.Message = function() {
  28. /** @member {string} */
  29. this._uid = null;
  30. /** @member {boolean} */
  31. this._received = false;
  32. /** @member {boolean} */
  33. this.encrypted = null;
  34. /** @member {boolean} */
  35. this.forwarded = false;
  36. /** @member {integer} */
  37. this.stamp = new Date().getTime();
  38. this.type = jsxc.Message.PLAIN;
  39. if (typeof arguments[0] === 'string' && arguments[0].length > 0 && arguments.length === 1) {
  40. this._uid = arguments[0];
  41. this.load(this._uid);
  42. } else if (typeof arguments[0] === 'object' && arguments[0] !== null) {
  43. $.extend(this, arguments[0]);
  44. }
  45. if (!this._uid) {
  46. this._uid = new Date().getTime() + ':msg';
  47. }
  48. };
  49. /**
  50. * Load message properties.
  51. *
  52. * @memberof jsxc.Message
  53. * @param {string} uid
  54. */
  55. jsxc.Message.prototype.load = function(uid) {
  56. var data = jsxc.storage.getUserItem('msg', uid);
  57. if (!data) {
  58. jsxc.debug('Could not load message with uid ' + uid);
  59. }
  60. $.extend(this, data);
  61. };
  62. /**
  63. * Save message properties and create thumbnail.
  64. *
  65. * @memberOf jsxc.Message
  66. * @return {Message} this object
  67. */
  68. jsxc.Message.prototype.save = function() {
  69. var history;
  70. if (this.bid) {
  71. history = jsxc.storage.getUserItem('history', this.bid) || [];
  72. if (history.indexOf(this._uid) < 0) {
  73. if (history.length > jsxc.options.get('numberOfMsg')) {
  74. jsxc.Message.delete(history.pop());
  75. }
  76. } else {
  77. history = null;
  78. }
  79. }
  80. if (Image && this.attachment && this.attachment.type.match(/^image\//i) && this.attachment.data && !this.attachment.thumbnail) {
  81. var sHeight, sWidth, sx, sy;
  82. var dHeight = 100,
  83. dWidth = 100;
  84. var canvas = $("<canvas>").get(0);
  85. canvas.width = dWidth;
  86. canvas.height = dHeight;
  87. var ctx = canvas.getContext("2d");
  88. var img = new Image();
  89. img.src = this.attachment.data;
  90. if (img.height > img.width) {
  91. sHeight = img.width;
  92. sWidth = img.width;
  93. sx = 0;
  94. sy = (img.height - img.width) / 2;
  95. } else {
  96. sHeight = img.height;
  97. sWidth = img.height;
  98. sx = (img.width - img.height) / 2;
  99. sy = 0;
  100. }
  101. ctx.drawImage(img, sx, sy, sWidth, sHeight, 0, 0, dWidth, dHeight);
  102. this.attachment.thumbnail = canvas.toDataURL('image/jpeg', 0.3);
  103. if (this.direction === 'out') {
  104. // save storage
  105. this.attachment.data = null;
  106. }
  107. }
  108. var data;
  109. if (this.attachment && this.attachment.size > jsxc.options.maxStorableSize && this.direction === 'in') {
  110. jsxc.debug('Attachment to large to store');
  111. data = this.attachment.data;
  112. this.attachment.data = null;
  113. this.attachment.persistent = false;
  114. //@TODO inform user
  115. }
  116. jsxc.storage.setUserItem('msg', this._uid, this);
  117. if (history) {
  118. history.unshift(this._uid);
  119. jsxc.storage.setUserItem('history', this.bid, history);
  120. }
  121. if (data && this.attachment) {
  122. this.attachment.data = data;
  123. }
  124. return this;
  125. };
  126. /**
  127. * Remove object from storage.
  128. *
  129. * @memberOf jsxc.Message
  130. */
  131. jsxc.Message.prototype.delete = function() {
  132. jsxc.Message.delete(this._uid);
  133. };
  134. /**
  135. * Returns object as jquery object.
  136. *
  137. * @memberOf jsxc.Message
  138. * @return {jQuery} Representation in DOM
  139. */
  140. jsxc.Message.prototype.getDOM = function() {
  141. return jsxc.Message.getDOM(this._uid);
  142. };
  143. /**
  144. * Mark message as received.
  145. *
  146. * @memberOf jsxc.Message
  147. */
  148. jsxc.Message.prototype.received = function() {
  149. this._received = true;
  150. this.save();
  151. this.getDOM().addClass('jsxc_received');
  152. };
  153. /**
  154. * Returns true if the message was already received.
  155. *
  156. * @memberOf jsxc.Message
  157. * @return {boolean} true means received
  158. */
  159. jsxc.Message.prototype.isReceived = function() {
  160. return this._received;
  161. };
  162. /**
  163. * Remove message with uid.
  164. *
  165. * @memberOf jsxc.Message
  166. * @static
  167. * @param {string} uid message uid
  168. */
  169. jsxc.Message.delete = function(uid) {
  170. var data = jsxc.storage.getUserItem('msg', uid);
  171. if (data) {
  172. jsxc.storage.removeUserItem('msg', uid);
  173. if (data.bid) {
  174. var history = jsxc.storage.getUserItem('history', data.bid) || [];
  175. history = $.grep(history, function(el) {
  176. return el !== uid;
  177. });
  178. jsxc.storage.setUserItem('history', data.bid, history);
  179. }
  180. }
  181. };
  182. /**
  183. * Returns message object as jquery object.
  184. *
  185. * @memberOf jsxc.Message
  186. * @static
  187. * @param {string} uid message uid
  188. * @return {jQuery} jQuery representation in DOM
  189. */
  190. jsxc.Message.getDOM = function(uid) {
  191. return $('#' + uid.replace(/:/g, '-'));
  192. };
  193. /**
  194. * Message direction can be incoming, outgoing or system.
  195. *
  196. * @typedef {(jsxc.Message.IN|jsxc.Message.OUT|jsxc.Message.SYS)} direction
  197. */
  198. /**
  199. * @constant
  200. * @type {string}
  201. * @default
  202. */
  203. jsxc.Message.IN = 'in';
  204. /**
  205. * @constant
  206. * @type {string}
  207. * @default
  208. */
  209. jsxc.Message.OUT = 'out';
  210. /**
  211. * @constant
  212. * @type {string}
  213. * @default
  214. */
  215. jsxc.Message.SYS = 'sys';
  216. jsxc.Message.HTML = 'html';
  217. jsxc.Message.PLAIN = 'plain';