event.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. const constants = require('../config/constants');
  3. const config = require('../config/config');
  4. const notification = require('../components/notification');
  5. const ipcRender = require('../ipc/render-proecss');
  6. const getFinalText = function (textTemplate, taskContext) {
  7. let finalText = textTemplate;
  8. finalText = finalText.replace('${taskname}', taskContext.taskName || '');
  9. return finalText;
  10. };
  11. let nativeProcessTaskEvent = function (gid, template) {
  12. const taskContext = {
  13. taskName: gid
  14. };
  15. const title = getFinalText(template.title, taskContext);
  16. const body = getFinalText(template.text, taskContext);
  17. notification.showNotification(title, body, !config.notificationSound);
  18. };
  19. let nativeProcessTaskMessage = function (context) {
  20. const message = context.message;
  21. let content = null;
  22. try {
  23. content = JSON.parse(message);
  24. } catch (ex) {
  25. ipcRender.notifyRenderProcessLogError('[task/event.nativeProcessTaskMessage] cannot parse message json, message=' + message, ex);
  26. return;
  27. }
  28. if (!content || content.id || !content.method) { // Not Event
  29. return;
  30. }
  31. if (!content.params || !content.params[0] || !content.params[0].gid) {
  32. ipcRender.notifyRenderProcessLogError('[task/event.nativeProcessTaskMessage] content params is invalid', content);
  33. return;
  34. }
  35. try {
  36. let methodName = content.method;
  37. if (methodName.indexOf(constants.aria2Constants.rpcServiceName) !== 0) {
  38. ipcRender.notifyRenderProcessLogError('[task/event.nativeProcessTaskMessage] event method name is invalid', content);
  39. return;
  40. }
  41. methodName = methodName.substring(constants.aria2Constants.rpcServiceName.length + 1);
  42. const template = notification.getNotificationMessageTemplate(methodName);
  43. if (template) {
  44. nativeProcessTaskEvent(content.params[0].gid, template);
  45. }
  46. } catch (ex) {
  47. ipcRender.notifyRenderProcessLogError('[task/event.nativeProcessTaskMessage] cannot process task event, message=' + message, ex);
  48. }
  49. };
  50. module.exports = {
  51. nativeProcessTaskMessage: nativeProcessTaskMessage
  52. };