Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. "use strict";
  2. var okmail = function() {
  3. LocalContractStorage.defineMapProperty(this, "account")
  4. LocalContractStorage.defineMapProperty(this, "names")
  5. LocalContractStorage.defineMapProperty(this, "mail")
  6. LocalContractStorage.defineMapProperty(this, "myMail")
  7. LocalContractStorage.defineProperty(this, "mailCounter", null)
  8. }
  9.  
  10. okmail.prototype = {
  11. init: function() {
  12. this.mailCounter = 0
  13. },
  14. createAccount: function(name) {
  15. var from = Blockchain.transaction.from;
  16. name = name.trim() + "@okmail";
  17. if (name === "") {
  18. throw new Error("Name must contain at least 1 symbol.");
  19. }
  20. if (this.account.get(from)) {
  21. throw new Error("You have already created account.");
  22. }
  23. if (this.names.get(name)) {
  24. throw new Error("This name is already taken.");
  25. }
  26. this.names.set(name, from)
  27.  
  28. this.account.set(from, name)
  29.  
  30. from = this.account.get(from)
  31. this.mail.set(from, { inbox: [], outbox: [] })
  32. return true
  33. },
  34. sendMessage: function(to, title, text) {
  35. var from = Blockchain.transaction.from;
  36. if (!this.account.get(from)) {
  37. throw new Error("You haven't created account yet.");
  38. }
  39. if (!this.names.get(to)) {
  40. throw new Error("This recipient doesn't exist.");
  41. }
  42. var date = new Date();
  43. date = date.toString();
  44. from = this.account.get(from)
  45. var mailCounter = new BigNumber(this.mailCounter).plus(1)
  46.  
  47. this.mailCounter = mailCounter
  48.  
  49. var inboxFrom = this.mail.get(from).inbox
  50. var outboxFrom = this.mail.get(from).outbox
  51.  
  52. var inboxTo = this.mail.get(to).inbox
  53. var outboxTo = this.mail.get(to).outbox
  54.  
  55. var msg = {
  56. id: mailCounter,
  57. from: from,
  58. to: to,
  59. title: title,
  60. text: text,
  61. date: date,
  62. isSpam: false,
  63. isMarked: false
  64. }
  65.  
  66. inboxTo.push(msg)
  67. outboxFrom.push(msg)
  68.  
  69. this.mail.set(from, { inbox: inboxFrom, outbox: outboxFrom })
  70. this.mail.set(to, { inbox: inboxTo, outbox: outboxTo })
  71.  
  72. return true
  73. },
  74. loadMail: function() {
  75. var from = Blockchain.transaction.from;
  76. if (!this.account.get(from)) {
  77. throw new Error("You haven't created account yet.");
  78. }
  79. from = this.account.get(from)
  80.  
  81. return this.mail.get(from)
  82.  
  83. },
  84. delMsgById: function(msgId) {
  85. var from = Blockchain.transaction.from;
  86. if (!this.account.get(from)) {
  87. throw new Error("You haven't created account yet.");
  88. }
  89. from = this.account.get(from)
  90. msgId = Number.parseInt(msgId)
  91.  
  92. var inboxFrom = this.mail.get(from).inbox
  93. var outboxFrom = this.mail.get(from).outbox
  94.  
  95. if (inboxFrom.length > 0) {
  96. for (var i = 0; i < inboxFrom.length; i++) {
  97.  
  98. if (inboxFrom[i].id === msgId) {
  99. inboxFrom.splice(i, 1)
  100. }
  101.  
  102. }
  103. }
  104. if (outboxFrom.length > 0) {
  105. for (var i = 0; i < outboxFrom.length; i++) {
  106.  
  107. if (outboxFrom[i].id === msgId) {
  108. outboxFrom.splice(i, 1)
  109. }
  110.  
  111. }
  112. }
  113.  
  114. this.mail.set(from, { inbox: inboxFrom, outbox: outboxFrom })
  115. return true
  116.  
  117. }
  118. }
  119.  
  120. module.exports = okmail
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement