mirror of
https://github.com/wassname/talk.git
synced 2026-07-25 13:30:59 +08:00
Merge branch 'master' into gdpr-change-email-bug
This commit is contained in:
@@ -55,6 +55,18 @@ class SettingsLoader {
|
||||
// assembled Settings object.
|
||||
return zipObject(fields, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* get, like select, will retrieve the settings, but get will only return a
|
||||
* single setting.
|
||||
*
|
||||
* @param {String} field the field to get
|
||||
*/
|
||||
async get(field) {
|
||||
const value = await this._loader.load(field);
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = () => ({ Settings: new SettingsLoader() });
|
||||
|
||||
@@ -57,8 +57,8 @@ const Comment = {
|
||||
asset({ asset_id }, _, { loaders: { Assets } }) {
|
||||
return Assets.getByID.load(asset_id);
|
||||
},
|
||||
async editing(comment, _, { loaders: { Settings } }) {
|
||||
const { editCommentWindowLength } = await Settings.select(
|
||||
editing: async (comment, _, { loaders: { Settings } }) => {
|
||||
const editCommentWindowLength = await Settings.get(
|
||||
'editCommentWindowLength'
|
||||
);
|
||||
|
||||
|
||||
+6
-13
@@ -9,7 +9,8 @@ const Action = new Schema(
|
||||
id: {
|
||||
type: String,
|
||||
default: uuid.v4,
|
||||
unique: true,
|
||||
unique: 1,
|
||||
index: 1,
|
||||
},
|
||||
action_type: {
|
||||
type: String,
|
||||
@@ -19,7 +20,10 @@ const Action = new Schema(
|
||||
type: String,
|
||||
enum: ITEM_TYPES,
|
||||
},
|
||||
item_id: String,
|
||||
item_id: {
|
||||
type: String,
|
||||
index: 1,
|
||||
},
|
||||
user_id: String,
|
||||
|
||||
// The element that summaries will additionally group on in addtion to their action_type, item_type, and
|
||||
@@ -37,15 +41,4 @@ const Action = new Schema(
|
||||
}
|
||||
);
|
||||
|
||||
// Create an index on the `item_id` field so that queries looking for
|
||||
// actions based on the item id can resolve faster.
|
||||
Action.index(
|
||||
{
|
||||
item_id: 1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
}
|
||||
);
|
||||
|
||||
module.exports = Action;
|
||||
|
||||
+55
-104
@@ -55,12 +55,16 @@ const Comment = new Schema(
|
||||
type: String,
|
||||
default: uuid.v4,
|
||||
unique: true,
|
||||
index: true,
|
||||
},
|
||||
body: {
|
||||
type: String,
|
||||
},
|
||||
body_history: [BodyHistoryItemSchema],
|
||||
asset_id: String,
|
||||
asset_id: {
|
||||
type: String,
|
||||
index: true,
|
||||
},
|
||||
author_id: String,
|
||||
status_history: [Status],
|
||||
status: {
|
||||
@@ -90,7 +94,6 @@ const Comment = new Schema(
|
||||
// deleted_at stores the date that the given comment was deleted.
|
||||
deleted_at: {
|
||||
type: Date,
|
||||
default: null,
|
||||
},
|
||||
|
||||
// Additional metadata stored on the field.
|
||||
@@ -110,95 +113,67 @@ const Comment = new Schema(
|
||||
}
|
||||
);
|
||||
|
||||
// Add the indexes for the id of the comment.
|
||||
Comment.index(
|
||||
{
|
||||
id: 1,
|
||||
},
|
||||
{
|
||||
unique: true,
|
||||
background: false,
|
||||
}
|
||||
);
|
||||
|
||||
Comment.index(
|
||||
{
|
||||
status: 1,
|
||||
created_at: 1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
}
|
||||
);
|
||||
|
||||
Comment.index(
|
||||
{
|
||||
status: 1,
|
||||
created_at: 1,
|
||||
asset_id: 1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
}
|
||||
);
|
||||
|
||||
// Create a sparse index to search across.
|
||||
Comment.index(
|
||||
{
|
||||
created_at: 1,
|
||||
'action_counts.flag': 1,
|
||||
status: 1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
sparse: true,
|
||||
}
|
||||
);
|
||||
|
||||
// Create a sparse index to search across.
|
||||
Comment.index(
|
||||
{
|
||||
'action_counts.flag': 1,
|
||||
status: 1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
sparse: true,
|
||||
}
|
||||
);
|
||||
|
||||
// Add an index that is optimized for finding flagged comments.
|
||||
Comment.index(
|
||||
{
|
||||
asset_id: 1,
|
||||
created_at: 1,
|
||||
'action_counts.flag': 1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
}
|
||||
);
|
||||
|
||||
// Add an index for the reply sort.
|
||||
Comment.index(
|
||||
{
|
||||
asset_id: 1,
|
||||
deleted_at: 1,
|
||||
created_at: -1,
|
||||
reply_count: -1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
}
|
||||
{ partialFilterExpression: { deleted_at: null } }
|
||||
);
|
||||
|
||||
// Add an index that is optimized for finding a user's comments.
|
||||
Comment.index(
|
||||
{
|
||||
author_id: 1,
|
||||
deleted_at: 1,
|
||||
status: 1,
|
||||
created_at: -1,
|
||||
},
|
||||
{ partialFilterExpression: { deleted_at: null } }
|
||||
);
|
||||
|
||||
Comment.index({
|
||||
asset_id: 1,
|
||||
created_at: -1,
|
||||
});
|
||||
|
||||
Comment.index({
|
||||
asset_id: 1,
|
||||
created_at: 1,
|
||||
});
|
||||
|
||||
Comment.index({
|
||||
author_id: 1,
|
||||
created_at: -1,
|
||||
});
|
||||
|
||||
Comment.index({
|
||||
asset_id: 1,
|
||||
status: 1,
|
||||
});
|
||||
|
||||
Comment.index({
|
||||
asset_id: 1,
|
||||
parent_id: 1,
|
||||
reply_count: -1,
|
||||
created_at: -1,
|
||||
});
|
||||
|
||||
Comment.index({
|
||||
asset_id: 1,
|
||||
reply_count: -1,
|
||||
created_at: -1,
|
||||
});
|
||||
|
||||
Comment.index(
|
||||
{
|
||||
'action_counts.flag': 1,
|
||||
status: 1,
|
||||
created_at: -1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
partialFilterExpression: {
|
||||
'action_counts.flag': { $exists: true, $gt: 0 },
|
||||
deleted_at: null,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
@@ -210,34 +185,10 @@ Comment.index(
|
||||
status: 1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
}
|
||||
);
|
||||
|
||||
// Optimize for tag searches/counts.
|
||||
Comment.index(
|
||||
{
|
||||
'tags.tag.name': 1,
|
||||
status: 1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
sparse: true,
|
||||
}
|
||||
);
|
||||
|
||||
// Add an index that is optimized for sorting based on the created_at timestamp
|
||||
// but also good at locating comments that have a specific asset id.
|
||||
Comment.index(
|
||||
{
|
||||
asset_id: 1,
|
||||
created_at: 1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
}
|
||||
);
|
||||
|
||||
Comment.virtual('edited').get(function() {
|
||||
return this.body_history.length > 1;
|
||||
});
|
||||
|
||||
@@ -12,6 +12,8 @@ const Setting = new Schema(
|
||||
id: {
|
||||
type: String,
|
||||
default: '1',
|
||||
unique: 1,
|
||||
index: true,
|
||||
},
|
||||
moderation: {
|
||||
type: String,
|
||||
|
||||
+19
-28
@@ -58,6 +58,7 @@ const User = new Schema(
|
||||
default: uuid.v4,
|
||||
unique: true,
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
|
||||
// This is sourced from the social provider or set manually during user setup
|
||||
@@ -107,6 +108,7 @@ const User = new Schema(
|
||||
status: {
|
||||
type: String,
|
||||
enum: USER_STATUS_USERNAME,
|
||||
index: true,
|
||||
},
|
||||
|
||||
// History stores the history of username status changes.
|
||||
@@ -135,6 +137,7 @@ const User = new Schema(
|
||||
type: Boolean,
|
||||
required: true,
|
||||
default: false,
|
||||
index: true,
|
||||
},
|
||||
history: [
|
||||
{
|
||||
@@ -226,41 +229,26 @@ User.index(
|
||||
}
|
||||
);
|
||||
|
||||
User.index(
|
||||
{
|
||||
lowercaseUsername: 1,
|
||||
'profiles.id': 1,
|
||||
created_at: -1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
}
|
||||
);
|
||||
User.index({
|
||||
lowercaseUsername: 1,
|
||||
'profiles.id': 1,
|
||||
created_at: -1,
|
||||
});
|
||||
|
||||
// This query is executed often, to count the number of flagged accounts with
|
||||
// usernames.
|
||||
User.index(
|
||||
{
|
||||
'action_counts.flag': 1,
|
||||
'status.username.status': 1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
}
|
||||
);
|
||||
User.index({
|
||||
'action_counts.flag': 1,
|
||||
'status.username.status': 1,
|
||||
});
|
||||
|
||||
// Sorting users by created at is the default people search.
|
||||
User.index(
|
||||
{
|
||||
created_at: -1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
}
|
||||
);
|
||||
User.index({
|
||||
created_at: -1,
|
||||
});
|
||||
|
||||
/**
|
||||
* returns true if a commenter is staff
|
||||
* returns true if a commenter is staff.
|
||||
*/
|
||||
User.method('isStaff', function() {
|
||||
return this.role !== 'COMMENTER';
|
||||
@@ -330,6 +318,9 @@ User.virtual('hasVerifiedEmail').get(function() {
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* system returns true when the user is a system user.
|
||||
*/
|
||||
User.virtual('system')
|
||||
.get(function() {
|
||||
return this._system;
|
||||
|
||||
@@ -11,10 +11,22 @@ function getReactionConfig(reaction) {
|
||||
|
||||
if (CREATE_MONGO_INDEXES) {
|
||||
// Create the index on the comment model based on the reaction config.
|
||||
Comment.collection.createIndex(
|
||||
Comment.collection.ensureIndex(
|
||||
{
|
||||
created_at: 1,
|
||||
[`action_counts.${sc(reaction)}`]: 1,
|
||||
asset_id: 1,
|
||||
[`action_counts.${sc(reaction)}`]: -1,
|
||||
created_at: -1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
}
|
||||
);
|
||||
|
||||
Comment.collection.ensureIndex(
|
||||
{
|
||||
asset_id: 1,
|
||||
[`action_counts.${sc(reaction)}`]: -1,
|
||||
created_at: -1,
|
||||
},
|
||||
{
|
||||
background: true,
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
const Setting = require('../models/setting');
|
||||
const { ErrSettingsNotInit } = require('../errors');
|
||||
const { dotize } = require('./utils');
|
||||
const { isEmpty, zipObject, uniq } = require('lodash');
|
||||
const { isEmpty, zipObject } = require('lodash');
|
||||
const DataLoader = require('dataloader');
|
||||
|
||||
const selector = { id: '1' };
|
||||
|
||||
async function loadFn(fields = []) {
|
||||
const model = await Setting.findOne(selector).select(uniq(fields));
|
||||
async function loadFn(/* fields = [] */) {
|
||||
// Originally, we used the projection operation, turns out this isn't that
|
||||
// fast. We should utilize the redis cache instead here.
|
||||
// const model = await Setting.findOne(selector).select(uniq(fields));
|
||||
|
||||
const model = await Setting.findOne(selector);
|
||||
if (!model) {
|
||||
throw new ErrSettingsNotInit();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user