rate-core 0.5.2 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/index.js +10 -217
- package/lib/rate.js +3 -0
- package/package.json +2 -4
- package/src/core.ts +0 -74
- package/src/index.ts +10 -195
- package/src/rate.ts +11 -65
package/README.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
# rate
|
|
1
|
+
# rate-core
|
package/lib/index.js
CHANGED
|
@@ -1,17 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
extendStatics(d, b);
|
|
11
|
-
function __() { this.constructor = d; }
|
|
12
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
|
-
};
|
|
14
|
-
})();
|
|
15
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
16
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
17
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -53,170 +40,11 @@ function __export(m) {
|
|
|
53
40
|
}
|
|
54
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
42
|
__export(require("./rate"));
|
|
56
|
-
var
|
|
57
|
-
function
|
|
58
|
-
this.find = find;
|
|
43
|
+
var RateService = (function () {
|
|
44
|
+
function RateService(repository, infoRepository) {
|
|
59
45
|
this.repository = repository;
|
|
60
46
|
this.infoRepository = infoRepository;
|
|
61
|
-
this.
|
|
62
|
-
this.reactionRepository = reactionRepository;
|
|
63
|
-
this.queryURL = queryURL;
|
|
64
|
-
this.search = this.search.bind(this);
|
|
65
|
-
this.load = this.load.bind(this);
|
|
66
|
-
this.getRate = this.getRate.bind(this);
|
|
67
|
-
this.setUseful = this.setUseful.bind(this);
|
|
68
|
-
this.removeUseful = this.removeUseful.bind(this);
|
|
69
|
-
this.comment = this.comment.bind(this);
|
|
70
|
-
this.removeComment = this.removeComment.bind(this);
|
|
71
|
-
this.updateComment = this.updateComment.bind(this);
|
|
72
|
-
this.getComments = this.getComments.bind(this);
|
|
73
|
-
this.getComment = this.getComment.bind(this);
|
|
74
|
-
}
|
|
75
|
-
ReactionService.prototype.search = function (s, limit, offset, fields) {
|
|
76
|
-
var _this = this;
|
|
77
|
-
return this.find(s, limit, offset, fields).then(function (res) {
|
|
78
|
-
if (!_this.queryURL) {
|
|
79
|
-
return res;
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
if (res.list && res.list.length > 0) {
|
|
83
|
-
var ids = [];
|
|
84
|
-
for (var _i = 0, _a = res.list; _i < _a.length; _i++) {
|
|
85
|
-
var rate = _a[_i];
|
|
86
|
-
ids.push(rate.author);
|
|
87
|
-
}
|
|
88
|
-
return _this.queryURL(ids).then(function (urls) {
|
|
89
|
-
for (var _i = 0, _a = res.list; _i < _a.length; _i++) {
|
|
90
|
-
var rate = _a[_i];
|
|
91
|
-
var i = binarySearch(urls, rate.author);
|
|
92
|
-
if (i >= 0) {
|
|
93
|
-
rate.authorURL = urls[i].url;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return res;
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
return res;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
};
|
|
105
|
-
ReactionService.prototype.load = function (id, author) {
|
|
106
|
-
return this.repository.load(id, author);
|
|
107
|
-
};
|
|
108
|
-
ReactionService.prototype.getRate = function (id, author) {
|
|
109
|
-
return this.repository.load(id, author);
|
|
110
|
-
};
|
|
111
|
-
ReactionService.prototype.setUseful = function (id, author, userId) {
|
|
112
|
-
return this.reactionRepository.save(id, author, userId, 1);
|
|
113
|
-
};
|
|
114
|
-
ReactionService.prototype.removeUseful = function (id, author, userId) {
|
|
115
|
-
return this.reactionRepository.remove(id, author, userId);
|
|
116
|
-
};
|
|
117
|
-
ReactionService.prototype.comment = function (comment) {
|
|
118
|
-
var _this = this;
|
|
119
|
-
return this.repository.load(comment.id, comment.author).then(function (checkRate) {
|
|
120
|
-
if (!checkRate) {
|
|
121
|
-
return -1;
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
comment.time ? comment.time = comment.time : comment.time = new Date();
|
|
125
|
-
return _this.commentRepository.insert(comment);
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
};
|
|
129
|
-
ReactionService.prototype.removeComment = function (commentId, userId) {
|
|
130
|
-
var _this = this;
|
|
131
|
-
return this.commentRepository.load(commentId).then(function (comment) {
|
|
132
|
-
if (comment) {
|
|
133
|
-
if (userId === comment.author || userId === comment.userId) {
|
|
134
|
-
return _this.commentRepository.remove(commentId, comment.id, comment.author);
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
return -2;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
return -1;
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
};
|
|
145
|
-
ReactionService.prototype.updateComment = function (comment) {
|
|
146
|
-
var _this = this;
|
|
147
|
-
return this.commentRepository.load(comment.commentId).then(function (exist) {
|
|
148
|
-
if (!exist) {
|
|
149
|
-
return -1;
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
if (exist.userId !== comment.userId) {
|
|
153
|
-
return -2;
|
|
154
|
-
}
|
|
155
|
-
exist.updatedAt = new Date();
|
|
156
|
-
var c = { comment: exist.comment, time: exist.time };
|
|
157
|
-
if (exist.histories && exist.histories.length > 0) {
|
|
158
|
-
exist.histories.push(c);
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
exist.histories = [c];
|
|
162
|
-
}
|
|
163
|
-
exist.comment = comment.comment;
|
|
164
|
-
var res = _this.commentRepository.update(exist);
|
|
165
|
-
return res;
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
ReactionService.prototype.getComments = function (id, author, limit) {
|
|
170
|
-
var _this = this;
|
|
171
|
-
return this.commentRepository.getComments(id, author, limit).then(function (comments) {
|
|
172
|
-
if (_this.queryURL) {
|
|
173
|
-
var ids = [];
|
|
174
|
-
for (var _i = 0, comments_1 = comments; _i < comments_1.length; _i++) {
|
|
175
|
-
var comment = comments_1[_i];
|
|
176
|
-
ids.push(comment.userId);
|
|
177
|
-
}
|
|
178
|
-
return _this.queryURL(ids).then(function (urls) {
|
|
179
|
-
for (var _i = 0, comments_2 = comments; _i < comments_2.length; _i++) {
|
|
180
|
-
var comment = comments_2[_i];
|
|
181
|
-
var i = binarySearch(urls, comment.userId);
|
|
182
|
-
if (i >= 0) {
|
|
183
|
-
comment.userURL = urls[i].url;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
return comments;
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
else {
|
|
190
|
-
return comments;
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
};
|
|
194
|
-
ReactionService.prototype.getComment = function (id) {
|
|
195
|
-
var _this = this;
|
|
196
|
-
return this.commentRepository.load(id).then(function (comment) {
|
|
197
|
-
if (comment && _this.queryURL) {
|
|
198
|
-
return _this.queryURL([id]).then(function (urls) {
|
|
199
|
-
var i = binarySearch(urls, comment.userId);
|
|
200
|
-
if (i >= 0) {
|
|
201
|
-
comment.userURL = urls[i].url;
|
|
202
|
-
}
|
|
203
|
-
return comment;
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
return comment;
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
};
|
|
211
|
-
return ReactionService;
|
|
212
|
-
}());
|
|
213
|
-
exports.ReactionService = ReactionService;
|
|
214
|
-
var RateService = (function (_super) {
|
|
215
|
-
__extends(RateService, _super);
|
|
216
|
-
function RateService(find, repository, infoRepository, commentRepository, reactionRepository, queryURL) {
|
|
217
|
-
var _this = _super.call(this, find, repository, infoRepository, commentRepository, reactionRepository, queryURL) || this;
|
|
218
|
-
_this.rate = _this.rate.bind(_this);
|
|
219
|
-
return _this;
|
|
47
|
+
this.rate = this.rate.bind(this);
|
|
220
48
|
}
|
|
221
49
|
RateService.prototype.rate = function (rate) {
|
|
222
50
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -260,7 +88,7 @@ var RateService = (function (_super) {
|
|
|
260
88
|
});
|
|
261
89
|
};
|
|
262
90
|
return RateService;
|
|
263
|
-
}(
|
|
91
|
+
}());
|
|
264
92
|
exports.RateService = RateService;
|
|
265
93
|
function avg(n) {
|
|
266
94
|
var sum = 0;
|
|
@@ -271,12 +99,11 @@ function avg(n) {
|
|
|
271
99
|
return sum / n.length;
|
|
272
100
|
}
|
|
273
101
|
exports.avg = avg;
|
|
274
|
-
var RatesService = (function (
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
return _this;
|
|
102
|
+
var RatesService = (function () {
|
|
103
|
+
function RatesService(repository, infoRepository) {
|
|
104
|
+
this.repository = repository;
|
|
105
|
+
this.infoRepository = infoRepository;
|
|
106
|
+
this.rate = this.rate.bind(this);
|
|
280
107
|
}
|
|
281
108
|
RatesService.prototype.rate = function (rate) {
|
|
282
109
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -322,42 +149,8 @@ var RatesService = (function (_super) {
|
|
|
322
149
|
});
|
|
323
150
|
};
|
|
324
151
|
return RatesService;
|
|
325
|
-
}(ReactionService));
|
|
326
|
-
exports.RatesService = RatesService;
|
|
327
|
-
function binarySearch(ar, el) {
|
|
328
|
-
var m = 0;
|
|
329
|
-
var n = ar.length - 1;
|
|
330
|
-
while (m <= n) {
|
|
331
|
-
var k = (n + m) >> 1;
|
|
332
|
-
var cmp = compare(el, ar[k].id);
|
|
333
|
-
if (cmp > 0) {
|
|
334
|
-
m = k + 1;
|
|
335
|
-
}
|
|
336
|
-
else if (cmp < 0) {
|
|
337
|
-
n = k - 1;
|
|
338
|
-
}
|
|
339
|
-
else {
|
|
340
|
-
return k;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
return -m - 1;
|
|
344
|
-
}
|
|
345
|
-
function compare(s1, s2) {
|
|
346
|
-
return s1.localeCompare(s2);
|
|
347
|
-
}
|
|
348
|
-
var CommentValidator = (function () {
|
|
349
|
-
function CommentValidator(attributes, check) {
|
|
350
|
-
this.attributes = attributes;
|
|
351
|
-
this.check = check;
|
|
352
|
-
this.validate = this.validate.bind(this);
|
|
353
|
-
}
|
|
354
|
-
CommentValidator.prototype.validate = function (comment) {
|
|
355
|
-
var errs = this.check(comment, this.attributes);
|
|
356
|
-
return Promise.resolve(errs);
|
|
357
|
-
};
|
|
358
|
-
return CommentValidator;
|
|
359
152
|
}());
|
|
360
|
-
exports.
|
|
153
|
+
exports.RatesService = RatesService;
|
|
361
154
|
var RateValidator = (function () {
|
|
362
155
|
function RateValidator(attributes, check, max) {
|
|
363
156
|
this.attributes = attributes;
|
package/lib/rate.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rate-core",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.4",
|
|
4
4
|
"description": "rate",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -17,11 +17,9 @@
|
|
|
17
17
|
},
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
20
|
-
"url": "git@github.com/core-ts/
|
|
20
|
+
"url": "git@github.com/core-ts/rate"
|
|
21
21
|
},
|
|
22
22
|
"keywords": [
|
|
23
|
-
"reaction",
|
|
24
|
-
"reaction core",
|
|
25
23
|
"rate",
|
|
26
24
|
"rate core"
|
|
27
25
|
]
|
package/src/core.ts
CHANGED
|
@@ -1,17 +1,3 @@
|
|
|
1
|
-
export interface StringMap {
|
|
2
|
-
[key: string]: string;
|
|
3
|
-
}
|
|
4
|
-
export interface Statement {
|
|
5
|
-
query: string;
|
|
6
|
-
params?: any[];
|
|
7
|
-
}
|
|
8
|
-
export interface DB {
|
|
9
|
-
driver: string;
|
|
10
|
-
param(i: number): string;
|
|
11
|
-
exec(sql: string, args?: any[], ctx?: any): Promise<number>;
|
|
12
|
-
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
|
|
13
|
-
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
|
|
14
|
-
}
|
|
15
1
|
export type DataType = 'ObjectId' | 'date' | 'datetime' | 'time'
|
|
16
2
|
| 'boolean' | 'number' | 'integer' | 'string' | 'text'
|
|
17
3
|
| 'object' | 'array' | 'binary'
|
|
@@ -73,63 +59,3 @@ export interface Filter {
|
|
|
73
59
|
pageIndex?: number;
|
|
74
60
|
pageSize?: number;
|
|
75
61
|
}
|
|
76
|
-
export interface ViewRepository<T, ID> {
|
|
77
|
-
metadata?(): Attributes|undefined;
|
|
78
|
-
keys?(): string[];
|
|
79
|
-
all?(ctx?: any): Promise<T[]>;
|
|
80
|
-
load(id: ID, ctx?: any): Promise<T|null>;
|
|
81
|
-
exist(id: ID, ctx?: any): Promise<boolean>;
|
|
82
|
-
}
|
|
83
|
-
export interface Repository<T, ID> extends ViewRepository<T, ID> {
|
|
84
|
-
insert(obj: T, ctx?: any): Promise<number>;
|
|
85
|
-
update(obj: T, ctx?: any): Promise<number>;
|
|
86
|
-
patch?(obj: T, ctx?: any): Promise<number>;
|
|
87
|
-
save?(obj: T, ctx?: any): Promise<number>;
|
|
88
|
-
delete(id: ID, ctx?: any): Promise<number>;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export interface ViewService<T, ID> {
|
|
92
|
-
metadata?(): Attributes|undefined;
|
|
93
|
-
keys?(): string[];
|
|
94
|
-
all?(ctx?: any): Promise<T[]>;
|
|
95
|
-
load(id: ID, ctx?: any): Promise<T|null>;
|
|
96
|
-
exist?(id: ID, ctx?: any): Promise<boolean>;
|
|
97
|
-
}
|
|
98
|
-
export interface GenericService<T, ID, R> extends ViewService<T, ID> {
|
|
99
|
-
insert(obj: T, ctx?: any): Promise<R>;
|
|
100
|
-
update(obj: T, ctx?: any): Promise<R>;
|
|
101
|
-
patch?(obj: T, ctx?: any): Promise<R>;
|
|
102
|
-
save?(obj: T, ctx?: any): Promise<R>;
|
|
103
|
-
delete?(id: ID, ctx?: any): Promise<number>;
|
|
104
|
-
}
|
|
105
|
-
export interface SearchResult<T> {
|
|
106
|
-
list: T[];
|
|
107
|
-
total?: number;
|
|
108
|
-
last?: boolean;
|
|
109
|
-
nextPageToken?: string;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export interface ErrorMessage {
|
|
113
|
-
field: string;
|
|
114
|
-
code: string;
|
|
115
|
-
param?: string|number|Date;
|
|
116
|
-
message?: string;
|
|
117
|
-
}
|
|
118
|
-
export interface ResultInfo<T> {
|
|
119
|
-
status: number|string;
|
|
120
|
-
errors?: ErrorMessage[];
|
|
121
|
-
value?: T;
|
|
122
|
-
message?: string;
|
|
123
|
-
}
|
|
124
|
-
export type Result<T> = number | ResultInfo<T>;
|
|
125
|
-
export interface SearchService<T, F extends Filter> {
|
|
126
|
-
keys?(): string[];
|
|
127
|
-
search(s: F, limit?: number, offset?: number|string, fields?: string[], ctx?: any): Promise<SearchResult<T>>;
|
|
128
|
-
}
|
|
129
|
-
export interface GenericSearchService<T, ID, R, F extends Filter>
|
|
130
|
-
extends GenericService<T, ID, R>, SearchService<T, F> {
|
|
131
|
-
}
|
|
132
|
-
export interface Service<T, ID, F extends Filter> extends GenericSearchService<T, ID, Result<T>, F> {
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
export type Search<T, F> = (s: F, limit?: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>;
|
package/src/index.ts
CHANGED
|
@@ -1,164 +1,13 @@
|
|
|
1
|
-
import { Attributes
|
|
2
|
-
import {
|
|
3
|
-
BaseRate, BaseRepository, Comment, InfoRepository, Rate, RateCommentRepository, RateFilter, Rater, RateReactionRepository,
|
|
4
|
-
Rates, RatesFilter, ShortComment, ShortRate, ShortRates
|
|
5
|
-
} from './rate';
|
|
1
|
+
import { Attributes } from './core';
|
|
2
|
+
import { BaseRepository, InfoRepository, Rate, Rater, Rates, ShortRate, ShortRates } from './rate';
|
|
6
3
|
|
|
7
4
|
export * from './rate';
|
|
8
5
|
|
|
9
|
-
export interface URL {
|
|
10
|
-
id: string;
|
|
11
|
-
url: string;
|
|
12
|
-
}
|
|
13
|
-
export class ReactionService<R extends BaseRate, F, O> {
|
|
14
|
-
constructor(protected find: Search<R, F>,
|
|
15
|
-
public repository: BaseRepository<R>,
|
|
16
|
-
protected infoRepository: InfoRepository<O>,
|
|
17
|
-
protected commentRepository: RateCommentRepository,
|
|
18
|
-
protected reactionRepository: RateReactionRepository,
|
|
19
|
-
protected queryURL?: (ids: string[]) => Promise<URL[]>) {
|
|
20
|
-
this.search = this.search.bind(this);
|
|
21
|
-
this.load = this.load.bind(this);
|
|
22
|
-
this.getRate = this.getRate.bind(this);
|
|
23
|
-
this.setUseful = this.setUseful.bind(this);
|
|
24
|
-
this.removeUseful = this.removeUseful.bind(this);
|
|
25
|
-
this.comment = this.comment.bind(this);
|
|
26
|
-
this.removeComment = this.removeComment.bind(this);
|
|
27
|
-
this.updateComment = this.updateComment.bind(this);
|
|
28
|
-
this.getComments = this.getComments.bind(this);
|
|
29
|
-
this.getComment = this.getComment.bind(this);
|
|
30
|
-
}
|
|
31
|
-
search(s: F, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<R>> {
|
|
32
|
-
return this.find(s, limit, offset, fields).then(res => {
|
|
33
|
-
if (!this.queryURL) {
|
|
34
|
-
return res;
|
|
35
|
-
} else {
|
|
36
|
-
if (res.list && res.list.length > 0) {
|
|
37
|
-
const ids: string[] = [];
|
|
38
|
-
for (const rate of res.list) {
|
|
39
|
-
ids.push(rate.author);
|
|
40
|
-
}
|
|
41
|
-
return this.queryURL(ids).then(urls => {
|
|
42
|
-
for (const rate of res.list) {
|
|
43
|
-
const i = binarySearch(urls, rate.author);
|
|
44
|
-
if (i >= 0) {
|
|
45
|
-
rate.authorURL = urls[i].url;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return res;
|
|
49
|
-
});
|
|
50
|
-
} else {
|
|
51
|
-
return res;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
load(id: string, author: string): Promise<R | null> {
|
|
57
|
-
return this.repository.load(id, author);
|
|
58
|
-
}
|
|
59
|
-
getRate(id: string, author: string): Promise<R | null> {
|
|
60
|
-
return this.repository.load(id, author);
|
|
61
|
-
}
|
|
62
|
-
setUseful(id: string, author: string, userId: string): Promise<number> {
|
|
63
|
-
return this.reactionRepository.save(id, author, userId, 1);
|
|
64
|
-
}
|
|
65
|
-
removeUseful(id: string, author: string, userId: string): Promise<number> {
|
|
66
|
-
return this.reactionRepository.remove(id, author, userId);
|
|
67
|
-
}
|
|
68
|
-
comment(comment: Comment): Promise<number> {
|
|
69
|
-
return this.repository.load(comment.id, comment.author).then(checkRate => {
|
|
70
|
-
if (!checkRate) {
|
|
71
|
-
return -1;
|
|
72
|
-
} else {
|
|
73
|
-
comment.time ? comment.time = comment.time : comment.time = new Date();
|
|
74
|
-
return this.commentRepository.insert(comment);
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
removeComment(commentId: string, userId: string): Promise<number> {
|
|
79
|
-
return this.commentRepository.load(commentId).then(comment => {
|
|
80
|
-
if (comment) {
|
|
81
|
-
if (userId === comment.author || userId === comment.userId) {
|
|
82
|
-
return this.commentRepository.remove(commentId, comment.id, comment.author);
|
|
83
|
-
} else {
|
|
84
|
-
return -2;
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
return -1;
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
updateComment(comment: Comment): Promise<number> {
|
|
92
|
-
return this.commentRepository.load(comment.commentId).then(exist => {
|
|
93
|
-
if (!exist) {
|
|
94
|
-
return -1;
|
|
95
|
-
} else {
|
|
96
|
-
if (exist.userId !== comment.userId) {
|
|
97
|
-
return -2;
|
|
98
|
-
}
|
|
99
|
-
exist.updatedAt = new Date();
|
|
100
|
-
const c: ShortComment = { comment: exist.comment, time: exist.time };
|
|
101
|
-
if (exist.histories && exist.histories.length > 0) {
|
|
102
|
-
exist.histories.push(c);
|
|
103
|
-
} else {
|
|
104
|
-
exist.histories = [c];
|
|
105
|
-
}
|
|
106
|
-
exist.comment = comment.comment;
|
|
107
|
-
const res = this.commentRepository.update(exist);
|
|
108
|
-
return res;
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
getComments(id: string, author: string, limit?: number): Promise<Comment[]> {
|
|
113
|
-
return this.commentRepository.getComments(id, author, limit).then(comments => {
|
|
114
|
-
if (this.queryURL) {
|
|
115
|
-
const ids: string[] = [];
|
|
116
|
-
for (const comment of comments) {
|
|
117
|
-
ids.push(comment.userId);
|
|
118
|
-
}
|
|
119
|
-
return this.queryURL(ids).then(urls => {
|
|
120
|
-
for (const comment of comments) {
|
|
121
|
-
const i = binarySearch(urls, comment.userId);
|
|
122
|
-
if (i >= 0) {
|
|
123
|
-
comment.userURL = urls[i].url;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return comments;
|
|
127
|
-
});
|
|
128
|
-
} else {
|
|
129
|
-
return comments;
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
getComment(id: string): Promise<Comment | null> {
|
|
134
|
-
return this.commentRepository.load(id).then(comment => {
|
|
135
|
-
if (comment && this.queryURL) {
|
|
136
|
-
return this.queryURL([id]).then(urls => {
|
|
137
|
-
const i = binarySearch(urls, comment.userId);
|
|
138
|
-
if (i >= 0) {
|
|
139
|
-
comment.userURL = urls[i].url;
|
|
140
|
-
}
|
|
141
|
-
return comment;
|
|
142
|
-
});
|
|
143
|
-
} else {
|
|
144
|
-
return comment;
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
export interface CommentRepository {
|
|
150
|
-
load(commentId: string, ctx?: any): Promise<Comment | null>;
|
|
151
|
-
getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
|
|
152
|
-
}
|
|
153
6
|
// tslint:disable-next-line:max-classes-per-file
|
|
154
|
-
export class RateService
|
|
155
|
-
constructor(
|
|
156
|
-
repository: BaseRepository<Rate>,
|
|
157
|
-
infoRepository: InfoRepository
|
|
158
|
-
commentRepository: RateCommentRepository,
|
|
159
|
-
reactionRepository: RateReactionRepository,
|
|
160
|
-
queryURL?: (ids: string[]) => Promise<URL[]>) {
|
|
161
|
-
super(find, repository, infoRepository, commentRepository, reactionRepository, queryURL);
|
|
7
|
+
export class RateService implements Rater<Rate> {
|
|
8
|
+
constructor(
|
|
9
|
+
public repository: BaseRepository<Rate>,
|
|
10
|
+
public infoRepository: InfoRepository) {
|
|
162
11
|
this.rate = this.rate.bind(this);
|
|
163
12
|
}
|
|
164
13
|
async rate(rate: Rate): Promise<number> {
|
|
@@ -193,14 +42,10 @@ export function avg(n: number[]): number {
|
|
|
193
42
|
return sum / n.length;
|
|
194
43
|
}
|
|
195
44
|
// tslint:disable-next-line:max-classes-per-file
|
|
196
|
-
export class RatesService
|
|
197
|
-
constructor(
|
|
198
|
-
repository: BaseRepository<Rates>,
|
|
199
|
-
infoRepository: InfoRepository
|
|
200
|
-
commentRepository: RateCommentRepository,
|
|
201
|
-
reactionRepository: RateReactionRepository,
|
|
202
|
-
queryURL?: (ids: string[]) => Promise<URL[]>) {
|
|
203
|
-
super(find, repository, infoRepository, commentRepository, reactionRepository, queryURL);
|
|
45
|
+
export class RatesService implements Rater<Rates> {
|
|
46
|
+
constructor(
|
|
47
|
+
public repository: BaseRepository<Rates>,
|
|
48
|
+
public infoRepository: InfoRepository) {
|
|
204
49
|
this.rate = this.rate.bind(this);
|
|
205
50
|
}
|
|
206
51
|
async rate(rate: Rates): Promise<number> {
|
|
@@ -230,26 +75,6 @@ export class RatesService<O> extends ReactionService<Rates, RatesFilter, O> impl
|
|
|
230
75
|
return res;
|
|
231
76
|
}
|
|
232
77
|
}
|
|
233
|
-
function binarySearch(ar: URL[], el: string): number {
|
|
234
|
-
let m = 0;
|
|
235
|
-
let n = ar.length - 1;
|
|
236
|
-
while (m <= n) {
|
|
237
|
-
// tslint:disable-next-line:no-bitwise
|
|
238
|
-
const k = (n + m) >> 1;
|
|
239
|
-
const cmp = compare(el, ar[k].id);
|
|
240
|
-
if (cmp > 0) {
|
|
241
|
-
m = k + 1;
|
|
242
|
-
} else if (cmp < 0) {
|
|
243
|
-
n = k - 1;
|
|
244
|
-
} else {
|
|
245
|
-
return k;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
return -m - 1;
|
|
249
|
-
}
|
|
250
|
-
function compare(s1: string, s2: string): number {
|
|
251
|
-
return s1.localeCompare(s2);
|
|
252
|
-
}
|
|
253
78
|
interface ErrorMessage {
|
|
254
79
|
field: string;
|
|
255
80
|
code: string;
|
|
@@ -257,16 +82,6 @@ interface ErrorMessage {
|
|
|
257
82
|
message?: string;
|
|
258
83
|
}
|
|
259
84
|
// tslint:disable-next-line:max-classes-per-file
|
|
260
|
-
export class CommentValidator {
|
|
261
|
-
constructor(protected attributes: Attributes, protected check: (obj: any, attributes: Attributes) => ErrorMessage[]) {
|
|
262
|
-
this.validate = this.validate.bind(this);
|
|
263
|
-
}
|
|
264
|
-
validate(comment: Comment): Promise<ErrorMessage[]> {
|
|
265
|
-
const errs = this.check(comment, this.attributes);
|
|
266
|
-
return Promise.resolve(errs);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
// tslint:disable-next-line:max-classes-per-file
|
|
270
85
|
export class RateValidator {
|
|
271
86
|
constructor(protected attributes: Attributes, protected check: (obj: any, attributes: Attributes) => ErrorMessage[], protected max: number) {
|
|
272
87
|
this.validate = this.validate.bind(this);
|
package/src/rate.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Attributes, Filter
|
|
1
|
+
import { Attributes, Filter } from './core';
|
|
2
2
|
|
|
3
3
|
export interface RateId {
|
|
4
4
|
id: string;
|
|
@@ -7,6 +7,9 @@ export interface RateId {
|
|
|
7
7
|
export interface BaseRate {
|
|
8
8
|
author: string;
|
|
9
9
|
authorURL?: string;
|
|
10
|
+
name: string;
|
|
11
|
+
displayName: string;
|
|
12
|
+
anonymous: boolean;
|
|
10
13
|
rate: number;
|
|
11
14
|
}
|
|
12
15
|
export interface Rate extends BaseRate {
|
|
@@ -65,40 +68,13 @@ export interface RatesFilter extends RateFilter {
|
|
|
65
68
|
rate10: number;
|
|
66
69
|
}
|
|
67
70
|
export interface BaseRepository<R> {
|
|
68
|
-
// save(obj: Rate, info?: T, ctx?: any): Promise<number>;
|
|
69
71
|
insert(rate: R, newInfo?: boolean): Promise<number>;
|
|
70
72
|
update(rate: R, oldRate: number): Promise<number>;
|
|
71
73
|
load(id: string, author: string): Promise<R | null>;
|
|
72
74
|
}
|
|
73
|
-
export interface Rater<R, F extends Filter> {
|
|
74
|
-
search(s: F, limit?: number, offset?: number | string, fields?: string[], ctx?: any): Promise<SearchResult<R>>;
|
|
75
|
-
load(id: string, author: string): Promise<R | null>;
|
|
76
|
-
rate(rate: R): Promise<number>;
|
|
77
|
-
setUseful(id: string, author: string, userId: string, ctx?: any): Promise<number>;
|
|
78
|
-
removeUseful(id: string, author: string, userId: string, ctx?: any): Promise<number>;
|
|
79
|
-
comment(comment: Comment): Promise<number>;
|
|
80
|
-
removeComment(id: string, author: string, ctx?: any): Promise<number>;
|
|
81
|
-
updateComment(comment: Comment): Promise<number>;
|
|
82
|
-
getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
|
|
83
|
-
getComment(id: string): Promise<Comment | null>;
|
|
84
|
-
}
|
|
85
|
-
export interface RateReactionRepository {
|
|
86
|
-
remove(id: string, author: string, userId: string, ctx?: any): Promise<number>;
|
|
87
|
-
save(id: string, author: string, userId: string, type: number): Promise<number>;
|
|
88
|
-
}
|
|
89
75
|
|
|
90
|
-
export interface
|
|
91
|
-
|
|
92
|
-
getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export interface Query<T, ID, S> {
|
|
96
|
-
search: (s: S, limit?: number, skip?: number | string, fields?: string[]) => Promise<SearchResult<T>>;
|
|
97
|
-
metadata?(): Attributes | undefined;
|
|
98
|
-
load(id: ID, ctx?: any): Promise<T | null>;
|
|
99
|
-
}
|
|
100
|
-
export interface RateCommentQuery extends Query<Comment, string, CommentFilter> {
|
|
101
|
-
getComments(id: string, author: string, limit?: number): Promise<Comment[]>;
|
|
76
|
+
export interface Rater<R> {
|
|
77
|
+
rate(rate: R): Promise<number>;
|
|
102
78
|
}
|
|
103
79
|
export const rateHistoryModel: Attributes = {
|
|
104
80
|
rate: {
|
|
@@ -142,6 +118,9 @@ export const rateModel: Attributes = {
|
|
|
142
118
|
histories: {
|
|
143
119
|
type: 'array',
|
|
144
120
|
typeof: rateHistoryModel
|
|
121
|
+
},
|
|
122
|
+
anonymous: {
|
|
123
|
+
type: 'boolean',
|
|
145
124
|
}
|
|
146
125
|
};
|
|
147
126
|
|
|
@@ -245,40 +224,8 @@ export interface Info10 {
|
|
|
245
224
|
count: number;
|
|
246
225
|
score: number;
|
|
247
226
|
}
|
|
248
|
-
export interface InfoRepository
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
export interface CommentId {
|
|
252
|
-
id: string;
|
|
253
|
-
author: string;
|
|
254
|
-
userId: string;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export interface Comment {
|
|
258
|
-
commentId: string;
|
|
259
|
-
id: string;
|
|
260
|
-
author: string;
|
|
261
|
-
userId: string;
|
|
262
|
-
comment: string;
|
|
263
|
-
time: Date;
|
|
264
|
-
updatedAt?: Date;
|
|
265
|
-
histories?: ShortComment[];
|
|
266
|
-
userURL?: string;
|
|
267
|
-
authorURL?: string;
|
|
268
|
-
}
|
|
269
|
-
export interface ShortComment {
|
|
270
|
-
comment: string;
|
|
271
|
-
time: Date;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export interface CommentFilter extends Filter {
|
|
275
|
-
commentId?: string;
|
|
276
|
-
id?: string;
|
|
277
|
-
author?: string;
|
|
278
|
-
userId?: string;
|
|
279
|
-
comment?: string;
|
|
280
|
-
time?: Date;
|
|
281
|
-
updatedAt?: Date;
|
|
227
|
+
export interface InfoRepository {
|
|
228
|
+
exist(id: string, ctx?: any): Promise<boolean>;
|
|
282
229
|
}
|
|
283
230
|
|
|
284
231
|
export const rateInfoModel: Attributes = {
|
|
@@ -295,7 +242,6 @@ export const rateInfoModel: Attributes = {
|
|
|
295
242
|
type: 'number',
|
|
296
243
|
}
|
|
297
244
|
};
|
|
298
|
-
|
|
299
245
|
export const ratesModel: Attributes = {
|
|
300
246
|
id: {
|
|
301
247
|
key: true,
|