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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
var db = module.exports
var connection = require("./bookshelf")
var bookshelf = connection.bookshelf
var knex = connection.knex
var upload = require('../util/upload')
/* MODELS */
var User = db.User = bookshelf.Model.extend({
tableName: 'users',
hasTimestamps: false,
})
var Thread = db.Thread = bookshelf.Model.extend({
tableName: 'threads',
hasTimestamps: false,
})
var Comment = db.Comment = bookshelf.Model.extend({
tableName: 'comments',
hasTimestamps: false,
})
var File = db.File = bookshelf.Model.extend({
tableName: 'files',
hasTimestamps: false,
})
var Keyword = db.Keyword = bookshelf.Model.extend({
tableName: 'keywords',
hasTimestamps: false,
})
var Mailbox = db.Mailbox = bookshelf.Model.extend({
tableName: 'boxes',
hasTimestamps: false,
})
var Message = db.Message = bookshelf.Model.extend({
tableName: 'messages',
hasTimestamps: false,
})
/* USERS */
db.createUser = function(data){
return new db.User(data).save()
}
db.getUsers = function () {
return User.query(function(qb){
qb.orderBy("id", "desc")
}).fetchAll()
}
db.getUser = function(id) {
var model = new User({'id': id})
return model.fetch()
}
db.getUserByUsername = function(username) {
var model = new User({'username': username})
return model.fetch()
}
db.getLastlog = function(limit){
return knex.column('username').column('lastseen').select().from('users').orderBy('lastseen', 'desc').limit(limit || 10)
}
/* THREADS */
db.getLatestThreads = function () {
return Thread.query(function(qb){
qb.orderBy("lastmodified", "desc").limit(50)
}).fetchAll()
}
db.getThreadsForKeyword = function (keyword) {
return Thread.query(function(qb){
qb.where("keyword", "=", keyword).orderBy("id", "desc")
}).fetchAll()
}
db.getThread = function (id) {
return Thread.query("where", "id", "=", id).fetch()
}
db.getThreadsById = function(ids){
return Thread.where("id", "in", ids).fetchAll()
}
db.createThread = function(data){
return new db.Thread(data).save()
}
db.updateThread = function(data){
}
db.destroyThread = function(id){
}
/* FILES */
db.getFilesForThread = function (id){
return File.query("where", "thread", "=", id).fetchAll()
}
db.getFileCounts = function(ids){
return knex.column('thread').count('* as count').select().from('files').where('thread', 'in', ids).groupBy('thread')
}
db.getFileSizes = function(ids){
return knex.column('thread').sum('size as size').select().from('files').where('thread', 'in', ids).groupBy('thread')
}
db.getFilesById = function(ids){
return File.where("id", "in", ids).fetchAll()
}
db.createFile = function(data){
return new db.File(data).save()
}
db.destroyFile = function(id){
}
db.destroyFiles = function(files){
var s3client = upload.client()
var rmPromises = files.map((file) => {
return new Promise ((resolve, reject) => {
var thread_id = file.get('thread')
var filename = file.get('filename')
if (! thread_id || ! filename) {
return resolve()
}
var filePath = '/bucky/data/' + thread_id + '/' + filename
console.log(filePath)
s3client.deleteFile(filePath, function(err, res){
// check `err`, then do `res.pipe(..)` or `res.resume()` or whatever.
resolve()
})
})
})
var filePromises = files.map((file) => {
return file.destroy()
})
return Promise.all(rmPromises.join(filePromises))
}
/* COMMENTS */
db.getCommentsForThread = function (id, limit, offset, order){
order = order || "asc"
return Comment.query(function(qb){
qb.where("thread", "=", id).orderBy("id", order)
if (limit) {
qb.limit(limit)
}
if (offset) {
qb.offset(offset)
}
}).fetchAll().then(function(comments){
comments.forEach(function(comment){
comment.set("comment", comment.get("comment").toString() )
})
return comments
})
}
db.getCommentById = function(id){
return (new Comment({'id': id})).fetch()
}
db.getCommentsById = function(ids){
return Comment.where("id", "in", ids).fetchAll()
}
db.getCommentCounts = function(ids){
return knex.column('thread').count('* as count').select().from('comments').where('thread', 'in', ids).groupBy('thread')
}
db.createComment = function(data){
return new db.Comment(data).save()
}
db.updateComment = function(data){
}
db.destroyComment = function(id){
}
/* KEYWORDS */
db.getKeywords = function (keywords){
if (keywords) {
return Keyword.query("where", "keyword", "in", keywords).fetchAll()
}
return Keyword.fetchAll()
}
db.getKeyword = function (keyword) {
return Keyword.query("where", "keyword", "=", keyword).fetch()
}
db.createKeyword = function(data){
return new db.Keyword(data).save()
}
db.updateKeyword = function(data){
}
db.destroyKeyword = function(id){
}
/* MAILBOXES */
db.getMailboxes = function(username){
return Mailbox.query("where", "owner", "=", username).fetchAll()
}
db.getMailboxCounts = function(boxes){
return knex.column('mbox').count('* as count').select().from('messages').where('mbox', 'in', boxes).groupBy('mbox')
}
db.createMailbox = function(data){
return new db.Mailbox(data).save()
}
/* MESSAGES */
db.getMessages = function(username, box, limit, offset){
var mbox = username + "." + box
return Message.query(function(qb){
qb.column('id', 'mbox', 'unread', 'sender', 'recipient', 'date', 'subject', knex.raw("CHAR_LENGTH(body) as size")).where("mbox", "=", mbox).orderBy("id", "desc")
if (limit) {
qb.limit(limit)
}
if (offset) {
qb.offset(offset)
}
}).fetchAll()
}
db.getMessage = function (id){
var model = new Message({'id': id})
return model.fetch().then(function(message){
message.set("body", message.get("body").toString() )
return message
})
}
db.createMessage = function(data){
return new db.Message(data).save()
}
db.updateMessage = function(data){
}
db.destroyMessage = function(id){
}
|