Add createdAt and modifiedAt to all collections

This commit is contained in:
Justin Reynolds 2019-06-26 17:47:27 -05:00
parent fb728baf0c
commit c60e80d25b
37 changed files with 3722 additions and 2168 deletions

View file

@ -1,67 +1,85 @@
Settings = new Mongo.Collection('settings');
Settings.attachSchema(new SimpleSchema({
disableRegistration: {
type: Boolean,
},
'mailServer.username': {
type: String,
optional: true,
},
'mailServer.password': {
type: String,
optional: true,
},
'mailServer.host': {
type: String,
optional: true,
},
'mailServer.port': {
type: String,
optional: true,
},
'mailServer.enableTLS': {
type: Boolean,
optional: true,
},
'mailServer.from': {
type: String,
optional: true,
},
productName: {
type: String,
optional: true,
},
customHTMLafterBodyStart: {
type: String,
optional: true,
},
customHTMLbeforeBodyEnd: {
type: String,
optional: true,
},
displayAuthenticationMethod: {
type: Boolean,
optional: true,
},
defaultAuthenticationMethod: {
type: String,
optional: false,
},
hideLogo: {
type: Boolean,
optional: true,
},
createdAt: {
type: Date,
denyUpdate: true,
},
modifiedAt: {
type: Date,
},
}));
Settings.attachSchema(
new SimpleSchema({
disableRegistration: {
type: Boolean,
},
'mailServer.username': {
type: String,
optional: true,
},
'mailServer.password': {
type: String,
optional: true,
},
'mailServer.host': {
type: String,
optional: true,
},
'mailServer.port': {
type: String,
optional: true,
},
'mailServer.enableTLS': {
type: Boolean,
optional: true,
},
'mailServer.from': {
type: String,
optional: true,
},
productName: {
type: String,
optional: true,
},
customHTMLafterBodyStart: {
type: String,
optional: true,
},
customHTMLbeforeBodyEnd: {
type: String,
optional: true,
},
displayAuthenticationMethod: {
type: Boolean,
optional: true,
},
defaultAuthenticationMethod: {
type: String,
optional: false,
},
hideLogo: {
type: Boolean,
optional: true,
},
createdAt: {
type: Date,
denyUpdate: true,
// eslint-disable-next-line consistent-return
autoValue() {
if (this.isInsert) {
return new Date();
} else {
this.unset();
}
},
},
modifiedAt: {
type: Date,
// eslint-disable-next-line consistent-return
autoValue() {
if (this.isInsert || this.isUpsert || this.isUpdate) {
return new Date();
} else {
this.unset();
}
},
},
})
);
Settings.helpers({
mailUrl () {
mailUrl() {
if (!this.mailServer.host) {
return null;
}
@ -69,7 +87,9 @@ Settings.helpers({
if (!this.mailServer.username && !this.mailServer.password) {
return `${protocol}${this.mailServer.host}:${this.mailServer.port}/`;
}
return `${protocol}${this.mailServer.username}:${encodeURIComponent(this.mailServer.password)}@${this.mailServer.host}:${this.mailServer.port}/`;
return `${protocol}${this.mailServer.username}:${encodeURIComponent(
this.mailServer.password
)}@${this.mailServer.host}:${this.mailServer.port}/`;
},
});
Settings.allow({
@ -86,50 +106,75 @@ Settings.before.update((userId, doc, fieldNames, modifier) => {
if (Meteor.isServer) {
Meteor.startup(() => {
Settings._collection._ensureIndex({ modifiedAt: -1 });
const setting = Settings.findOne({});
if(!setting){
if (!setting) {
const now = new Date();
const domain = process.env.ROOT_URL.match(/\/\/(?:www\.)?(.*)?(?:\/)?/)[1];
const domain = process.env.ROOT_URL.match(
/\/\/(?:www\.)?(.*)?(?:\/)?/
)[1];
const from = `Boards Support <support@${domain}>`;
const defaultSetting = {disableRegistration: false, mailServer: {
username: '', password: '', host: '', port: '', enableTLS: false, from,
}, createdAt: now, modifiedAt: now, displayAuthenticationMethod: true,
defaultAuthenticationMethod: 'password'};
const defaultSetting = {
disableRegistration: false,
mailServer: {
username: '',
password: '',
host: '',
port: '',
enableTLS: false,
from,
},
createdAt: now,
modifiedAt: now,
displayAuthenticationMethod: true,
defaultAuthenticationMethod: 'password',
};
Settings.insert(defaultSetting);
}
const newSetting = Settings.findOne();
if (!process.env.MAIL_URL && newSetting.mailUrl())
process.env.MAIL_URL = newSetting.mailUrl();
Accounts.emailTemplates.from = process.env.MAIL_FROM ? process.env.MAIL_FROM : newSetting.mailServer.from;
Accounts.emailTemplates.from = process.env.MAIL_FROM
? process.env.MAIL_FROM
: newSetting.mailServer.from;
});
Settings.after.update((userId, doc, fieldNames) => {
// assign new values to mail-from & MAIL_URL in environment
if (_.contains(fieldNames, 'mailServer') && doc.mailServer.host) {
const protocol = doc.mailServer.enableTLS ? 'smtps://' : 'smtp://';
if (!doc.mailServer.username && !doc.mailServer.password) {
process.env.MAIL_URL = `${protocol}${doc.mailServer.host}:${doc.mailServer.port}/`;
process.env.MAIL_URL = `${protocol}${doc.mailServer.host}:${
doc.mailServer.port
}/`;
} else {
process.env.MAIL_URL = `${protocol}${doc.mailServer.username}:${encodeURIComponent(doc.mailServer.password)}@${doc.mailServer.host}:${doc.mailServer.port}/`;
process.env.MAIL_URL = `${protocol}${
doc.mailServer.username
}:${encodeURIComponent(doc.mailServer.password)}@${
doc.mailServer.host
}:${doc.mailServer.port}/`;
}
Accounts.emailTemplates.from = doc.mailServer.from;
}
});
function getRandomNum (min, max) {
function getRandomNum(min, max) {
const range = max - min;
const rand = Math.random();
return (min + Math.round(rand * range));
return min + Math.round(rand * range);
}
function getEnvVar(name){
function getEnvVar(name) {
const value = process.env[name];
if (value){
if (value) {
return value;
}
throw new Meteor.Error(['var-not-exist', `The environment variable ${name} does not exist`]);
throw new Meteor.Error([
'var-not-exist',
`The environment variable ${name} does not exist`,
]);
}
function sendInvitationEmail (_id){
function sendInvitationEmail(_id) {
const icode = InvitationCodes.findOne(_id);
const author = Users.findOne(Meteor.userId());
try {
@ -172,30 +217,47 @@ if (Meteor.isServer) {
check(boards, [String]);
const user = Users.findOne(Meteor.userId());
if(!user.isAdmin){
if (!user.isAdmin) {
throw new Meteor.Error('not-allowed');
}
emails.forEach((email) => {
if (email && SimpleSchema.RegEx.Email.test(email)) {
// Checks if the email is already link to an account.
const userExist = Users.findOne({email});
if (userExist){
throw new Meteor.Error('user-exist', `The user with the email ${email} has already an account.`);
const userExist = Users.findOne({ email });
if (userExist) {
throw new Meteor.Error(
'user-exist',
`The user with the email ${email} has already an account.`
);
}
// Checks if the email is already link to an invitation.
const invitation = InvitationCodes.findOne({email});
if (invitation){
InvitationCodes.update(invitation, {$set : {boardsToBeInvited: boards}});
sendInvitationEmail(invitation._id);
}else {
const code = getRandomNum(100000, 999999);
InvitationCodes.insert({code, email, boardsToBeInvited: boards, createdAt: new Date(), authorId: Meteor.userId()}, function(err, _id){
if (!err && _id) {
sendInvitationEmail(_id);
} else {
throw new Meteor.Error('invitation-generated-fail', err.message);
}
const invitation = InvitationCodes.findOne({ email });
if (invitation) {
InvitationCodes.update(invitation, {
$set: { boardsToBeInvited: boards },
});
sendInvitationEmail(invitation._id);
} else {
const code = getRandomNum(100000, 999999);
InvitationCodes.insert(
{
code,
email,
boardsToBeInvited: boards,
createdAt: new Date(),
authorId: Meteor.userId(),
},
function(err, _id) {
if (!err && _id) {
sendInvitationEmail(_id);
} else {
throw new Meteor.Error(
'invitation-generated-fail',
err.message
);
}
}
);
}
}
});
@ -215,11 +277,15 @@ if (Meteor.isServer) {
Email.send({
to: user.emails[0].address,
from: Accounts.emailTemplates.from,
subject: TAPi18n.__('email-smtp-test-subject', {lng: lang}),
text: TAPi18n.__('email-smtp-test-text', {lng: lang}),
subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
});
} catch ({message}) {
throw new Meteor.Error('email-fail', `${TAPi18n.__('email-fail-text', {lng: lang})}: ${ message }`, message);
} catch ({ message }) {
throw new Meteor.Error(
'email-fail',
`${TAPi18n.__('email-fail-text', { lng: lang })}: ${message}`,
message
);
}
return {
message: 'email-sent',
@ -227,7 +293,7 @@ if (Meteor.isServer) {
};
},
getCustomUI(){
getCustomUI() {
const setting = Settings.findOne({});
if (!setting.productName) {
return {
@ -240,7 +306,7 @@ if (Meteor.isServer) {
}
},
getMatomoConf(){
getMatomoConf() {
return {
address: getEnvVar('MATOMO_ADDRESS'),
siteId: getEnvVar('MATOMO_SITE_ID'),
@ -275,3 +341,5 @@ if (Meteor.isServer) {
},
});
}
export default Settings;