forked from AHSFNU/syzoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquote.ts
330 lines (270 loc) · 8.8 KB
/
quote.ts
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import * as TypeORM from "typeorm";
import Model from "./common";
import User from "./user";
import QuoteFrom from "./quote-from";
import QuoteUserVote from "./quote-user-vote";
import { QuoteType, HitokotoQuoteContent, ImageQuoteContent, VoteSummary, VoteType, UserQuote, DialogItem } from "./interfaces";
import * as fs from "fs-extra";
import * as pathlib from "path";
declare var syzoj: any;
const assert: (flag: any, message?: string) => void = syzoj.utils.assert;
function exists(path: string): Promise<boolean> {
return new Promise(resolve => {
fs.access(path, fs.constants.F_OK, err => resolve(!err));
});
}
interface LeaderBoardItem {
from: string;
quote_count: number;
vote_up_sum: number;
}
@TypeORM.Entity()
export default class Quote extends Model {
static cache = true;
static cacheAll = true;
static baseDir = syzoj.utils.resolvePath(syzoj.config.upload_dir, 'quote_image');
@TypeORM.PrimaryGeneratedColumn()
id: number;
@TypeORM.Column({ type: "enum", enum: QuoteType })
type: QuoteType;
@TypeORM.Column({ default: JSON.stringify({}), type: "json" })
content: HitokotoQuoteContent | ImageQuoteContent;
@TypeORM.Column({ type: "integer" })
provider_id: number;
@TypeORM.Column({ default: 1, type: "double" })
weight: number;
@TypeORM.Column({ type: "datetime" })
creation_time: Date;
@TypeORM.Column({ type: 'datetime' })
update_time: Date;
@TypeORM.Index()
@TypeORM.Column({ type: "integer", default: 0 })
vote_up: number;
@TypeORM.Column({ type: "integer", default: 0 })
vote_down: number;
provider?: User;
from?: string[];
static isValidImageExt(ext: string): boolean {
return /^\.(png|jpg|gif)$/i.test(ext);
}
static getSavePath(filename: string): string {
return pathlib.join(this.baseDir, filename);
}
static getBackupSavePath(filename: string): string {
return this.getSavePath('deleted.' + filename);
}
async loadRelationships() {
this.provider = await User.findById(this.provider_id);
this.from = (await QuoteFrom.find({
where: { quote_id: this.id }
})).map(item => item.from);
}
async isAllowedManageBy(user: User): Promise<boolean> {
return user && (this.provider_id === user.id || await user.hasPrivilege('manage_quote'));
}
static async pickAll(where): Promise<Quote[]> {
where = where || {};
let quotes: Quote[];
if (where.from) {
const quoteIds = (await QuoteFrom.find({
where: { from: where.from }
})).map(({ quote_id }) => quote_id);
quotes = await Promise.all(quoteIds.map(quoteId => Quote.findById(quoteId)));
quotes = quotes.filter(quote => !!quote);
} else {
quotes = (await this.findAll()) as Quote[];
}
type FilterCallback = (quote: Quote) => boolean;
const fn: FilterCallback[] = [];
for (const key of ['type', 'provider_id']) {
if (key in where && where[key] !== null)
fn.push((quote: Quote) => quote[key] === where[key]);
}
if (fn.length) quotes = quotes.filter(quote => fn.every(cb => cb(quote)));
return quotes;
}
static async pickOne(where): Promise<Quote> {
const quotes = await this.pickAll(where);
if (!quotes.length) return null;
const total = quotes.map(({ weight }) => weight).reduce((a, b) => a + b);
let current = Math.random() * total;
for (const quote of quotes) {
current -= quote.weight;
if (current < 0) return quote;
}
return quotes[quotes.length - 1];
}
static async getLeaderboards(): Promise<LeaderBoardItem[]> {
const query = QuoteFrom.createQueryBuilder()
.select('`from`')
.addSelect('COUNT(*)', 'quote_count')
.addSelect('SUM(vote_up)', 'vote_up_sum')
.innerJoin(qb => {
return qb.from(Quote, "quote")
.select(["id", "vote_up"]);
}, "q", "q.id = quote_id")
.groupBy("`from`")
.orderBy("quote_count", "DESC")
.addOrderBy("vote_up_sum", "DESC");
const results = await query.getRawMany();
return results.map(item => ({
from: item.from,
quote_count: parseInt(item.quote_count),
vote_up_sum: parseInt(item.vote_up_sum || 0)
}));
}
async getVoteBy(user: User): Promise<QuoteUserVote> {
return await QuoteUserVote.findOne({
quote_id: this.id,
user_id: user.id
});
}
async setVoteBy(user: User, vote: VoteType) {
let voteItem = await this.getVoteBy(user);
if (vote) {
if (voteItem) {
voteItem.vote = vote;
} else {
voteItem = QuoteUserVote.create({
quote_id: this.id,
user_id: user.id,
vote: vote
});
}
await voteItem.save();
} else {
if (voteItem) {
await voteItem.destroy();
}
}
await this.updateVotes();
}
async updateVotes() {
[this.vote_up, this.vote_down] = await Promise.all(
[VoteType.up, VoteType.down].map(type => QuoteUserVote.count({
quote_id: this.id,
vote: type
})));
await this.save();
}
async getVoteSummary(user: User): Promise<VoteSummary> {
const voteItem = await this.getVoteBy(user);
return {
self: voteItem && voteItem.vote,
total: {
up: this.vote_up,
down: this.vote_down
}
};
}
async render() {
switch (this.type) {
case QuoteType.hitokoto: {
const content = this.content as HitokotoQuoteContent;
if (!content.is_dialog || !await renderDialog(content)) {
content.html = await syzoj.utils.markdown(content.hitokoto);
}
break;
}
case QuoteType.image: {
const content = this.content as ImageQuoteContent;
content.url = syzoj.utils.makeUrl(['quote-image', content.filename]);
break;
}
}
}
async setImage(file: { filename: string, path: string, size: number }) {
assert(file.size > 0, "图片不能为空");
assert(file.size < 1048576, "图片大小超过限制");
const extname = pathlib.extname(file.filename);
assert(Quote.isValidImageExt(extname), "图片扩展名不合法");
const buffer = await fs.readFile(file.path);
const md5 = syzoj.utils.md5(buffer);
const filename = md5 + extname;
const savePath = Quote.getSavePath(filename);
const backupPath = Quote.getBackupSavePath(filename);
await syzoj.utils.lock(['Quote::Image'], async () => {
assert(!await exists(savePath), "图片已存在");
await fs.rename(file.path, savePath);
try {
await fs.unlink(backupPath);
} catch (err) {}
});
const content = this.content as ImageQuoteContent;
content.filename = filename;
content.size = file.size;
}
async setFrom(from: string[], isNew: boolean) {
const fromList = new Set(from);
if (!isNew) {
const items = await QuoteFrom.find({
where: { quote_id: this.id }
});
await Promise.all(items.map(item => {
if (fromList.has(item.from)) {
fromList.delete(item.from);
} else {
return item.destroy();
}
}));
}
for (const from of fromList) {
const item = QuoteFrom.create({
quote_id: this.id, from
});
await item.save();
}
}
async delete() {
await QuoteFrom.createQueryBuilder()
.delete()
.where("quote_id = :id", { id: this.id })
.execute();
await QuoteUserVote.createQueryBuilder()
.delete()
.where("quote_id = :id", { id: this.id })
.execute();
if (this.type === QuoteType.image) {
const filename = (this.content as ImageQuoteContent).filename;
const path = Quote.getSavePath(filename);
const pathNew = Quote.getBackupSavePath(filename);
await syzoj.utils.lock(['Quote::Image'], () => fs.rename(path, pathNew));
}
await this.destroy();
}
toJSON(privileged = false) {
let result: UserQuote = {
id: this.id,
type: this.type,
content: JSON.parse(JSON.stringify(this.content)),
from: this.from.slice(0),
creation_time: syzoj.utils.formatDate(this.creation_time.getTime() / 1000),
update_time: syzoj.utils.formatDate(this.update_time.getTime() / 1000)
};
if (privileged) {
result = { ...result,
provider: {
id: this.provider.id,
username: this.provider.username
},
weight: this.weight
};
}
return result;
}
}
async function renderDialog(content: HitokotoQuoteContent): Promise<boolean> {
let lines = content.hitokoto.trim().split(/ \n|<br>\n|\n\n/);
if (lines.length <= 1) return false;
let items: DialogItem[] = [];
for (const line of lines) {
let result = line.match(/^((.+?)(:|:))(.+)$/);
if (!result) return false;
items.push({ from: result[1], content: result[4] });
}
await Promise.all(items.map(async item => {
item.content = await syzoj.utils.markdown(item.content);
}));
content.dialog = items;
return true;
}