|
@@ -1,8 +1,9 @@
|
|
|
import {
|
|
import {
|
|
|
- Types, Document, Model, Schema, model,
|
|
|
|
|
|
|
+ Types, Document, Model, Schema,
|
|
|
} from 'mongoose';
|
|
} from 'mongoose';
|
|
|
|
|
|
|
|
import ActivityDefine from '../util/activityDefine';
|
|
import ActivityDefine from '../util/activityDefine';
|
|
|
|
|
+import { getOrCreateModel } from '../util/mongoose-utils';
|
|
|
|
|
|
|
|
const STATUS_WATCH = 'WATCH';
|
|
const STATUS_WATCH = 'WATCH';
|
|
|
const STATUS_IGNORE = 'IGNORE';
|
|
const STATUS_IGNORE = 'IGNORE';
|
|
@@ -29,75 +30,70 @@ export interface WatcherModel extends Model<WatcherDocument> {
|
|
|
getIgnorers(target: Types.ObjectId): Promise<Types.ObjectId[]>
|
|
getIgnorers(target: Types.ObjectId): Promise<Types.ObjectId[]>
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export default (): WatcherModel => {
|
|
|
|
|
-
|
|
|
|
|
- const watcherSchema = new Schema<WatcherDocument, WatcherModel>({
|
|
|
|
|
- user: {
|
|
|
|
|
- type: Schema.Types.ObjectId,
|
|
|
|
|
- ref: 'User',
|
|
|
|
|
- index: true,
|
|
|
|
|
- required: true,
|
|
|
|
|
- },
|
|
|
|
|
- targetModel: {
|
|
|
|
|
- type: String,
|
|
|
|
|
- require: true,
|
|
|
|
|
- enum: ActivityDefine.getSupportTargetModelNames(),
|
|
|
|
|
- },
|
|
|
|
|
- target: {
|
|
|
|
|
- type: Schema.Types.ObjectId,
|
|
|
|
|
- refPath: 'targetModel',
|
|
|
|
|
- require: true,
|
|
|
|
|
- },
|
|
|
|
|
- status: {
|
|
|
|
|
- type: String,
|
|
|
|
|
- require: true,
|
|
|
|
|
- enum: STATUSES,
|
|
|
|
|
- },
|
|
|
|
|
- createdAt: { type: Date, default: Date.now },
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- watcherSchema.methods.isWatching = function() {
|
|
|
|
|
- return this.status === STATUS_WATCH;
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- watcherSchema.methods.isIgnoring = function() {
|
|
|
|
|
- return this.status === STATUS_IGNORE;
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- watcherSchema.statics.findByUserIdAndTargetId = function(userId, targetId) {
|
|
|
|
|
- return this.findOne({ user: userId, target: targetId });
|
|
|
|
|
- };
|
|
|
|
|
|
|
+const watcherSchema = new Schema<WatcherDocument, WatcherModel>({
|
|
|
|
|
+ user: {
|
|
|
|
|
+ type: Schema.Types.ObjectId,
|
|
|
|
|
+ ref: 'User',
|
|
|
|
|
+ index: true,
|
|
|
|
|
+ required: true,
|
|
|
|
|
+ },
|
|
|
|
|
+ targetModel: {
|
|
|
|
|
+ type: String,
|
|
|
|
|
+ require: true,
|
|
|
|
|
+ enum: ActivityDefine.getSupportTargetModelNames(),
|
|
|
|
|
+ },
|
|
|
|
|
+ target: {
|
|
|
|
|
+ type: Schema.Types.ObjectId,
|
|
|
|
|
+ refPath: 'targetModel',
|
|
|
|
|
+ require: true,
|
|
|
|
|
+ },
|
|
|
|
|
+ status: {
|
|
|
|
|
+ type: String,
|
|
|
|
|
+ require: true,
|
|
|
|
|
+ enum: STATUSES,
|
|
|
|
|
+ },
|
|
|
|
|
+ createdAt: { type: Date, default: Date.now },
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+watcherSchema.methods.isWatching = function() {
|
|
|
|
|
+ return this.status === STATUS_WATCH;
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- watcherSchema.statics.upsertWatcher = function(user, targetModel, target, status) {
|
|
|
|
|
- const query = { user, targetModel, target };
|
|
|
|
|
- const doc = { ...query, status };
|
|
|
|
|
- const options = {
|
|
|
|
|
- upsert: true, new: true, setDefaultsOnInsert: true, runValidators: true,
|
|
|
|
|
- };
|
|
|
|
|
- return Watcher.findOneAndUpdate(query, doc, options);
|
|
|
|
|
- };
|
|
|
|
|
|
|
+watcherSchema.methods.isIgnoring = function() {
|
|
|
|
|
+ return this.status === STATUS_IGNORE;
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- watcherSchema.statics.watchByPageId = function(user, pageId, status) {
|
|
|
|
|
- return this.upsertWatcher(user, 'Page', pageId, status);
|
|
|
|
|
- };
|
|
|
|
|
|
|
+watcherSchema.statics.findByUserIdAndTargetId = function(userId, targetId) {
|
|
|
|
|
+ return this.findOne({ user: userId, target: targetId });
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- watcherSchema.statics.getWatchers = async function(target) {
|
|
|
|
|
- return Watcher.find({ target, status: STATUS_WATCH }).distinct('user');
|
|
|
|
|
|
|
+watcherSchema.statics.upsertWatcher = function(user, targetModel, target, status) {
|
|
|
|
|
+ const query = { user, targetModel, target };
|
|
|
|
|
+ const doc = { ...query, status };
|
|
|
|
|
+ const options = {
|
|
|
|
|
+ upsert: true, new: true, setDefaultsOnInsert: true, runValidators: true,
|
|
|
};
|
|
};
|
|
|
|
|
+ return this.findOneAndUpdate(query, doc, options);
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- watcherSchema.statics.getIgnorers = async function(target) {
|
|
|
|
|
- return Watcher.find({ target, status: STATUS_IGNORE }).distinct('user');
|
|
|
|
|
- };
|
|
|
|
|
|
|
+watcherSchema.statics.watchByPageId = function(user, pageId, status) {
|
|
|
|
|
+ return this.upsertWatcher(user, 'Page', pageId, status);
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- watcherSchema.statics.STATUS_WATCH = function() {
|
|
|
|
|
- return STATUS_WATCH;
|
|
|
|
|
- };
|
|
|
|
|
|
|
+watcherSchema.statics.getWatchers = async function(target) {
|
|
|
|
|
+ return this.find({ target, status: STATUS_WATCH }).distinct('user');
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- watcherSchema.statics.STATUS_IGNORE = function() {
|
|
|
|
|
- return STATUS_IGNORE;
|
|
|
|
|
- };
|
|
|
|
|
|
|
+watcherSchema.statics.getIgnorers = async function(target) {
|
|
|
|
|
+ return this.find({ target, status: STATUS_IGNORE }).distinct('user');
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- const Watcher = model<WatcherDocument, WatcherModel>('Watcher', watcherSchema);
|
|
|
|
|
|
|
+watcherSchema.statics.STATUS_WATCH = function() {
|
|
|
|
|
+ return STATUS_WATCH;
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- return Watcher;
|
|
|
|
|
|
|
+watcherSchema.statics.STATUS_IGNORE = function() {
|
|
|
|
|
+ return STATUS_IGNORE;
|
|
|
};
|
|
};
|
|
|
|
|
+
|
|
|
|
|
+export default getOrCreateModel<WatcherDocument, WatcherModel>('Wather', watcherSchema);
|