msgpackager.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. let WebIM = require("../../utils/WebIM.js")["default"];
  2. let msgType = require("./msgtype.js");
  3. module.exports = function (sendableMsg, type, myName) {
  4. var time = WebIM.time();
  5. var renderableMsg = {
  6. info: {
  7. from: sendableMsg.body.from,
  8. to: sendableMsg.body.to
  9. },
  10. username: sendableMsg.body.from == myName ? sendableMsg.body.to : sendableMsg.body.from,
  11. yourname: sendableMsg.body.from,
  12. msg: {
  13. type: type,
  14. url: sendableMsg.body.url ? sendableMsg.body.url : '',
  15. data: getMsgData(sendableMsg, type),
  16. ext: sendableMsg.body.ext
  17. },
  18. style: sendableMsg.body.from == myName ? "self" : "",
  19. time: time,
  20. mid: sendableMsg.type + sendableMsg.id,
  21. chatType: sendableMsg.body.chatType
  22. };
  23. if (type == msgType.IMAGE) {
  24. renderableMsg.msg.size = {
  25. width: sendableMsg.body.body.size.width,
  26. height: sendableMsg.body.body.size.height
  27. };
  28. } else if (type == msgType.AUDIO) {
  29. renderableMsg.msg.length = sendableMsg.body.length;
  30. } else if (type == msgType.FILE) {
  31. renderableMsg.msg.data = [{
  32. data: "[当前不支持此格式消息展示]",
  33. type: "txt"
  34. }];
  35. renderableMsg.msg.type = 'txt';
  36. }
  37. return renderableMsg;
  38. function getMsgData(sendableMsg, type) {
  39. if (type == msgType.TEXT) {
  40. return WebIM.parseEmoji(sendableMsg.value.replace(/\n/mg, ""));
  41. } else if (type == msgType.EMOJI) {
  42. return sendableMsg.value;
  43. } else if (type == msgType.IMAGE || type == msgType.VIDEO || type == msgType.AUDIO) {
  44. return sendableMsg.body.body.url;
  45. } else if (type == msgType.FILE) {
  46. return sendableMsg.body.body.msg;
  47. }
  48. return "";
  49. }
  50. };