Skip to content

Commit 75bf182

Browse files
author
Andy Zehady
committed
Multi comma separated tag functionality.
1 parent dd62d11 commit 75bf182

1 file changed

Lines changed: 54 additions & 7 deletions

File tree

src/transactions/vote.js

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { config } = require("signale");
2+
13
module.exports = {
24
fields: ['link', 'author', 'vt', 'tag'],
35
validate: (tx, ts, legitUser, cb) => {
@@ -53,14 +55,59 @@ module.exports = {
5355
// update top tags
5456
var topTags = []
5557
for (let i = 0; i < content.votes.length; i++) {
56-
var exists = false
57-
for (let y = 0; y < topTags.length; y++)
58-
if (topTags[y].tag === content.votes[i].tag) {
59-
exists = true
60-
topTags[y].vt += Math.abs(content.votes[i].vt)
58+
var newTags = {}
59+
for (let y = 0; y < topTags.length; y++) {
60+
contentTags = []
61+
tmpTags = content.votes[i].tag
62+
if (tmpTags.length > config.tagMaxLength)
63+
tmpTags = tmpTags.substring(0, config.tagMaxLength)
64+
tmpTags = content.votes[i].tag.trim().split(",")
65+
for (let j = 0; j < tmpTags.length; j++) {
66+
contentTags.push(tmpTags[j].trim())
67+
}
68+
69+
// idea is to distribute the vt equally to all comma separated tags, the extra is given to the first tag
70+
// process for each comma separated tags
71+
nTag = contentTags.length
72+
totalTagVt = content.votes[i].vt
73+
for (let j = 0; j < nTag; j++) {
74+
curTag = contentTags[j]
75+
curTagVt = (totalTagVt / nTag) + (totalTagVt % nTag) // the modulo part will be zero for j > 0 because of the next if block, see example below
76+
77+
if (j == 0) {
78+
totalTagVt = totalTagVt - (totalTagVt % nTag) // removing the extra modulo part for all other tags except the first one
79+
}
80+
81+
if (topTags[y].tag === curTag) {
82+
topTags[y].vt += Math.abs(curTagVt)
83+
newTags[curTag] = null // tag already exists, so nullifying pre-pushed tag
84+
} else {
85+
newTags[curTag] = {tag: curTag, vt: Math.abs(curTagVt)}
86+
}
87+
}
88+
/*
89+
Example:
90+
content.votes[i].tag = "hike, hiking, hiker"
91+
totalTagVt = 313
92+
nt = 3
93+
94+
topTags[0] = {tag: "hike", vt: 313/3 + 313%3 = 104 + 1 = 105} // hike
95+
96+
totalTagVt = 313 - (313%3) = 313 - 1 = 312
97+
98+
topTags[1] = {tag: "hiking", vt: 312/3 + 312%3 = 104} // hiking
99+
100+
topTags[2] = {tag: "hiker", vt: 312/3 + 312%3 = 104} // hiker
101+
102+
105 + 104 + 104 = 313 total
103+
*/
104+
}
105+
106+
for (let [tagKey, tagVal] of newTags) {
107+
if (!tagVal && tagVal.tag) {// if tagKey not null, then exists false, then push
108+
topTags.push(tagVal)
61109
}
62-
if (!exists && content.votes[i].tag)
63-
topTags.push({tag: content.votes[i].tag, vt: Math.abs(content.votes[i].vt)})
110+
}
64111
}
65112

66113
topTags = topTags.sort(function(a,b) {

0 commit comments

Comments
 (0)