-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.js
182 lines (136 loc) · 4.84 KB
/
library.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
// Sort by Date
"use strict";
let Categories = require.main.require('./src/categories')
let User = require.main.require('./src/user')
let Topics = require.main.require('./src/topics')
let SocketAdmin = require.main.require('./src/socket.io/admin')
let db = require.main.require('./src/database')
let async = require.main.require('async')
let winston = require.main.require('winston')
let nconf = require.main.require('nconf')
let _ = require.main.require('lodash')
let utils = require.main.require('./public/src/utils')
let version = '1.4.8'
exports.init = (params, next) => {
winston.info('[sort-by-date] Loading sort by date...')
params.router.get('/admin/plugins/category-sort-by-date', params.middleware.admin.buildHeader, renderAdmin)
params.router.get('/api/admin/plugins/category-sort-by-date', renderAdmin)
function renderAdmin (req, res, next) {
res.render('admin/plugins/category-sort-by-date', {})
}
let getTopicIds = Categories.getTopicIds
Categories.getTopicIds = function (data, next) {
let { sort, cid, start, stop, } = data
if (sort !== 'topics_newest_to_oldest' && sort !== 'topics_oldest_to_newest') return getTopicIds(data, next)
let pinnedTids
let method, min, max, set
if (sort === 'topics_oldest_to_newest') {
method = 'getSortedSetRevRangeByLex'
min = '+'
max = '-'
} else {
method = 'getSortedSetRangeByLex'
min = '-'
max = '+'
}
async.waterfall([
next => {
let dataForPinned = _.cloneDeep(data)
dataForPinned.start = 0
dataForPinned.stop = -1
Categories.getPinnedTids(dataForPinned, next)
},
(_pinnedTids, next) => {
let totalPinnedCount = _pinnedTids.length
pinnedTids = _pinnedTids.slice(start, stop === -1 ? undefined : stop + 1);
let pinnedCount = pinnedTids.length;
let topicsPerPage = stop - start + 1;
let normalTidsToGet = Math.max(0, topicsPerPage - pinnedCount);
if (!normalTidsToGet && stop !== -1) return next(null, [])
set = `cid:${cid}:tids:csbt`
if (start > 0 && totalPinnedCount) start -= totalPinnedCount - pinnedCount
stop = stop === -1 ? stop : start + normalTidsToGet - 1
db[method](set, min, max, start, stop - start, next)
},
(topicValues, next) => {
let tids = []
let tid = ''
topicValues.forEach(function (value) {
tid = value.split(':')
tid = tid[tid.length - 1]
tids.push(tid)
})
next(null, tids)
},
(normalTids, next) => {
normalTids = normalTids.filter(tid => pinnedTids.indexOf(tid) === -1)
next(null, pinnedTids.concat(normalTids))
},
], next)
}
SocketAdmin.sortbydate = {}
SocketAdmin.sortbydate.reindex = (socket, data, next) => {
reindex(next)
}
next()
if (!(nconf.get('isPrimary') === 'true' && !nconf.get('jobsDisabled'))) return
db.get('sortbydate', function (err, ver) {
if (err) return
if (ver === version) return
reindex()
})
}
function reindex(next) {
next = next || (() => {})
winston.info('[sort-by-date] Re-indexing topics...')
async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
function (cids, next) {
let keys = cids.map(function (cid) { return 'cid:' + cid + ':tids:csbt' })
db.deleteAll(keys, next)
},
async.apply(db.getSortedSetRange, 'topics:tid', 0, -1),
function (tids, next) {
Topics.getTopicsFields(tids, ['tid', 'cid', 'timestamp'], next)
},
function (topics, next) {
async.each(topics, function (topic, next) {
db.sortedSetAdd('cid:' + topic.cid + ':tids:csbt', 0, topic.timestamp + ':' + topic.tid, next)
}, next)
},
async.apply(db.set, 'sortbydate', version),
], (err) => {
next(err)
if (err) {
winston.error(err)
} else {
winston.info('[sort-by-date] Finished re-indexing topics.')
}
})
}
exports.topicPost = function (data) {
let topic = data.topic
db.sortedSetAdd('cid:' + topic.cid + ':tids:csbt', 0, topic.timestamp + ':' + topic.tid)
}
exports.topicPurge = function (data) {
let topic = data.topic
db.sortedSetRemove('cid:' + topic.cid + ':tids:csbt', topic.timestamp + ':' + topic.tid)
}
exports.topicMove = function (topic) {
Topics.getTopicField(topic.tid, 'timestamp', function (err, timestamp) {
db.sortedSetRemove('cid:' + topic.fromCid + ':tids:csbt', timestamp + ':' + topic.tid)
db.sortedSetAdd('cid:' + topic.toCid + ':tids:csbt', 0, timestamp + ':' + topic.tid)
})
}
exports.categoryDelete = function (data) {
let cid = data.cid
db.delete('cid:' + cid + ':tids:csbt')
}
exports.adminBuild = (header, next) => {
header.plugins.push({
route : '/plugins/category-sort-by-date',
icon : 'fa-sort-alpha-asc',
name : 'Category Sort by Date'
})
next(null, header)
}