Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- var okmail = function() {
- LocalContractStorage.defineMapProperty(this, "account")
- LocalContractStorage.defineMapProperty(this, "names")
- LocalContractStorage.defineMapProperty(this, "mail")
- LocalContractStorage.defineMapProperty(this, "myMail")
- LocalContractStorage.defineProperty(this, "mailCounter", null)
- }
- okmail.prototype = {
- init: function() {
- this.mailCounter = 0
- },
- createAccount: function(name) {
- var from = Blockchain.transaction.from;
- name = name.trim() + "@okmail";
- if (name === "") {
- throw new Error("Name must contain at least 1 symbol.");
- }
- if (this.account.get(from)) {
- throw new Error("You have already created account.");
- }
- if (this.names.get(name)) {
- throw new Error("This name is already taken.");
- }
- this.names.set(name, from)
- this.account.set(from, name)
- from = this.account.get(from)
- this.mail.set(from, { inbox: [], outbox: [] })
- return true
- },
- sendMessage: function(to, title, text) {
- var from = Blockchain.transaction.from;
- if (!this.account.get(from)) {
- throw new Error("You haven't created account yet.");
- }
- if (!this.names.get(to)) {
- throw new Error("This recipient doesn't exist.");
- }
- var date = new Date();
- date = date.toString();
- from = this.account.get(from)
- var mailCounter = new BigNumber(this.mailCounter).plus(1)
- this.mailCounter = mailCounter
- var inboxFrom = this.mail.get(from).inbox
- var outboxFrom = this.mail.get(from).outbox
- var inboxTo = this.mail.get(to).inbox
- var outboxTo = this.mail.get(to).outbox
- var msg = {
- id: mailCounter,
- from: from,
- to: to,
- title: title,
- text: text,
- date: date,
- isSpam: false,
- isMarked: false
- }
- inboxTo.push(msg)
- outboxFrom.push(msg)
- this.mail.set(from, { inbox: inboxFrom, outbox: outboxFrom })
- this.mail.set(to, { inbox: inboxTo, outbox: outboxTo })
- return true
- },
- loadMail: function() {
- var from = Blockchain.transaction.from;
- if (!this.account.get(from)) {
- throw new Error("You haven't created account yet.");
- }
- from = this.account.get(from)
- return this.mail.get(from)
- },
- delMsgById: function(msgId) {
- var from = Blockchain.transaction.from;
- if (!this.account.get(from)) {
- throw new Error("You haven't created account yet.");
- }
- from = this.account.get(from)
- msgId = Number.parseInt(msgId)
- var inboxFrom = this.mail.get(from).inbox
- var outboxFrom = this.mail.get(from).outbox
- if (inboxFrom.length > 0) {
- for (var i = 0; i < inboxFrom.length; i++) {
- if (inboxFrom[i].id === msgId) {
- inboxFrom.splice(i, 1)
- }
- }
- }
- if (outboxFrom.length > 0) {
- for (var i = 0; i < outboxFrom.length; i++) {
- if (outboxFrom[i].id === msgId) {
- outboxFrom.splice(i, 1)
- }
- }
- }
- this.mail.set(from, { inbox: inboxFrom, outbox: outboxFrom })
- return true
- }
- }
- module.exports = okmail
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement