-
Notifications
You must be signed in to change notification settings - Fork 1
/
Post.js
187 lines (159 loc) · 3.83 KB
/
Post.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var private = {}, self = null,
library = null, modules = null;
function Post(cb, _library) {
self = this;
self.type = 6
library = _library;
cb(null, self);
}
Post.prototype.create = function (data, trs) {
// base data
trs.asset = {
post: {
title: data.title,
text: data.text
}
};
return trs;
}
Post.prototype.calculateFee = function (trs) {
return 10000000;
}
Post.prototype.verify = function (trs, sender, cb, scope) {
if (trs.asset.post.title.length > 100 || trs.asset.post.title.length == 0) {
return setImmediate(cb, "Title must contain from 0 to 100 letters, now there is " + trs.asset.post.title.length + " letters");
}
if (trs.asset.post.text.length > 5000 || trs.asset.post.text == 0) {
return setImmediate(cb, "Text must contain from 0 to 50000 letters, now there is " + trs.asset.post.text.length + " letters");
}
setImmediate(cb, null, trs);
}
Post.prototype.getBytes = function (trs) {
var titleBuffer = new Buffer(trs.asset.post.title, 'utf8');
var textBuffer = new Buffer(trs.asset.post.text, 'utf8');
return Buffer.concat([titleBuffer, textBuffer]);
}
Post.prototype.apply = function (trs, sender, cb, scope) {
modules.blockchain.accounts.mergeAccountAndGet({
address: sender.address,
balance: -trs.fee
}, cb);
}
Post.prototype.undo = function (trs, sender, cb, scope) {
modules.blockchain.accounts.undoMerging({
address: sender.address,
balance: -trs.fee
}, cb);
}
Post.prototype.applyUnconfirmed = function (trs, sender, cb, scope) {
if (sender.u_balance < trs.fee) {
return setImmediate(cb, "Sender don't have enough amount");
}
modules.blockchain.accounts.mergeAccountAndGet({
address: sender.address,
u_balance: -trs.fee
}, cb);
}
Post.prototype.undoUnconfirmed = function (trs, sender, cb, scope) {
modules.blockchain.accounts.undoMerging({
address: sender.address,
u_balance: -trs.fee
}, cb);
}
Post.prototype.ready = function (trs, sender, cb, scope) {
setImmediate(cb);
}
Post.prototype.save = function (trs, cb) {
modules.api.sql.insert({
table: "asset_posts",
values: {
transactionId: trs.id,
title: trs.asset.post.title,
text: trs.asset.post.text
}
}, cb);
}
Post.prototype.dbRead = function (row) {
if (!row.p_title) {
return null;
}
return {
post: {
title: row.p_title,
text: row.p_text
}
}
}
Post.prototype.normalize = function (asset, cb) {
library.validator.validate(asset.post, {
type: "object",
properties: {
title: {
type: "string",
minLength: 1,
maxLength: 100
},
text: {
type: "string",
minLength: 1,
maxLength: 5000
}
},
required: ["title", "text"]
}, cb);
}
Post.prototype.add = function (cb, query) {
library.validator.validate(query, {
type: "object",
properties: {
title: {
type: "string",
minLength: 1,
maxLength: 100
},
text: {
type: "string",
minLength: 1,
maxLength: 5000
},
secret: {
type: "string",
minLength: 1,
maxLength: 100
}
},
required: ["title", "text", "secret"]
}, function (err) {
if (err) {
return cb(err[0].message);
}
var secret = query.secret,
title = query.title,
text = query.text;
var keypair = modules.api.crypto.keypair(secret);
modules.blockchain.accounts.setAccountAndGet({
publicKey: keypair.publicKey.toString('hex')
}, function (err, account) {
if (err) {
return cb(err.toString());
}
try {
var transaction = library.modules.logic.transaction.create({
type: self.type,
title: title,
sender: account,
keypair: keypair,
text: text
});
} catch (e) {
return setImmediate(cb, e.toString());
}
modules.blockchain.transactions.processUnconfirmedTransaction(transaction, cb);
});
});
}
Post.prototype.onBind = function (_modules) {
modules = _modules;
modules.logic.transaction.attachAssetType(self.type, self);
}
module.exports = Post;