Vulcan/packages/nova-users/lib/schema.js
Comus Leong 464e20a96c eslint & clean up code, also fixed some bugs (#1515)
* [eslint] update eslint rules & add .eslintignore to ignore non-ready nova packages

* [clean-up] nova-voting

* [clean-up] [bug] nova-users: missing user parameter

* [clean-up] nova-users

* [clean-up] nova-subscribe

* [clean-up] nova-settings

* [clean-up] nova-rss

* [clean-up] [bug] nova-posts: correct UsersRemoveDeletePosts

* [clean-up] nova-posts

* [clean-up] nova-notifications

* [clean-up] [bug] nova-newsletter: no error.message on throw error

* [clean-up] nova-newsletter

* [clean-up] nova-lib

* [clean-up] nova-kadira

* [clean-up] nova-inject-data

* [clean-up] nova-getting-started

* [clean-up] nova-forms

* [clean-up] nova-events

* [clean-up] [bug] nova-embedly: no FlowRouter

* [clean-up] nova-embedly

* [clean-up] nova-email-templates

* [clean-up] nova-email

* [clean-up] nova-debug

* [clean-up] nova-core

* [clean-up] [bug] nova-comments: correct UsersRemoveDeleteComments

* [clean-up] nova-comments

* [clean-up] [bug] nova-cloudinary: use Telescope.settings.collection instand

* [clean-up] nova-cloudinary

* [clean-up] nova-categories

* [clean-up] nova-base-components

* [clean-up] nova-api

* [eslint] extends react recommended

* [clean-up] for jsx files

* [eslint] extends meteor recommended

* i forgot this one little change
2016-11-25 13:46:55 -05:00

291 lines
5.4 KiB
JavaScript

import Telescope from 'meteor/nova:lib';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import Users from './collection.js';
const adminGroup = {
name: "admin",
order: 10
};
// check if user can create a new user
const canInsert = user => Users.canDo(user, "users.new");
// check if user can edit a user
const canEdit = Users.canEdit;
// check if user can edit *all* users
const canEditAll = user => Users.canDo(user, "users.edit.all");
/**
* @summary Vote schema
* @type {SimpleSchema}
*/
Telescope.schemas.votes = new SimpleSchema({
itemId: {
type: String
},
power: {
type: Number,
optional: true
},
votedAt: {
type: Date,
optional: true
}
});
/**
* @summary User Data schema
* @type {SimpleSchema}
*/
Telescope.schemas.userData = new SimpleSchema({
/**
Bio (Markdown version)
*/
bio: {
type: String,
optional: true,
control: "textarea",
insertableIf: canInsert,
editableIf: canEdit,
// form: {
// rows: 5
// }
},
/**
Total comment count
*/
commentCount: {
type: Number,
publish: true,
optional: true
},
/**
The name displayed throughout the app. Can contain spaces and special characters, doesn't need to be unique
*/
displayName: {
type: String,
optional: true,
publish: true,
profile: true,
control: "text",
insertableIf: canInsert,
editableIf: canEdit
},
/**
An array containing comment downvotes
*/
downvotedComments: {
type: [Telescope.schemas.votes],
publish: false,
optional: true
},
/**
An array containing posts downvotes
*/
downvotedPosts: {
type: [Telescope.schemas.votes],
publish: false,
optional: true
},
/**
The user's email. Modifiable.
*/
email: {
type: String,
optional: true,
regEx: SimpleSchema.RegEx.Email,
required: true,
control: "text",
insertableIf: canInsert,
editableIf: canEdit
// unique: true // note: find a way to fix duplicate accounts before enabling this
},
/**
A hash of the email, used for Gravatar // TODO: change this when email changes
*/
emailHash: {
type: String,
publish: true,
optional: true
},
/**
The HTML version of the bio field
*/
htmlBio: {
type: String,
publish: true,
profile: true,
optional: true,
// form: {
// omit: true
// },
template: "user_profile_bio"
},
/**
The user's karma
*/
karma: {
type: Number,
decimal: true,
publish: true,
optional: true
},
/**
Total post count
*/
postCount: {
type: Number,
publish: true,
optional: true
},
/**
A blackbox modifiable object to store the user's settings
*/
// settings: {
// type: Object,
// optional: true,
// editableIf: canEdit,
// blackbox: true,
// form: {
// omit: true
// }
// },
/**
The user's profile URL slug // TODO: change this when displayName changes
*/
slug: {
type: String,
publish: true,
optional: true
},
/**
The user's Twitter username
*/
twitterUsername: {
type: String,
optional: true,
publish: true,
profile: true,
control: "text",
insertableIf: canInsert,
editableIf: canEdit,
template: "user_profile_twitter"
},
/**
An array containing comments upvotes
*/
upvotedComments: {
type: [Telescope.schemas.votes],
publish: false,
optional: true
},
/**
An array containing posts upvotes
*/
upvotedPosts: {
type: [Telescope.schemas.votes],
publish: false,
optional: true
},
/**
A link to the user's homepage
*/
website: {
type: String,
regEx: SimpleSchema.RegEx.Url,
publish: true,
profile: true,
optional: true,
control: "text",
insertableIf: canInsert,
editableIf: canEdit
},
/**
Groups
*/
groups: {
type: [String],
optional: true,
control: "checkboxgroup",
insertableIf: canEditAll,
editableIf: canEditAll,
form: {
options: function () {
const groups = _.without(_.keys(Users.groups), "anonymous", "default", "admins");
return groups.map(group => {return {value: group, label: group};});
}
},
},
});
/**
* @summary Users schema
* @type {SimpleSchema}
*/
Users.schema = new SimpleSchema({
_id: {
type: String,
publish: true,
optional: true
},
username: {
type: String,
// regEx: /^[a-z0-9A-Z_]{3,15}$/,
publish: true,
optional: true
},
emails: {
type: [Object],
optional: true
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email,
optional: true
},
"emails.$.verified": {
type: Boolean,
optional: true
},
createdAt: {
type: Date,
publish: true,
optional: true
},
isAdmin: {
type: Boolean,
label: "Admin",
control: "checkbox",
optional: true,
insertableIf: canEditAll,
editableIf: canEditAll,
group: adminGroup
// form: {
// omit: true
// }
},
profile: {
type: Object,
optional: true,
blackbox: true
},
telescope: { // telescope-specific data
type: Telescope.schemas.userData,
optional: true
},
services: {
type: Object,
optional: true,
blackbox: true
}
});
// Meteor.startup(function(){
// Users.internationalize();
// });
/**
* @summary Attach schema to Users (Meteor.users at the moment) collection
*/
Users.attachSchema(Users.schema);