Vulcan/packages/nova-users/lib/schema.js

337 lines
6.2 KiB
JavaScript
Raw Normal View History

2016-08-08 11:18:21 +09:00
import Telescope from 'meteor/nova:lib';
2016-06-23 15:00:58 +09:00
import Users from './collection.js';
const adminGroup = {
name: "admin",
order: 10
};
2016-07-21 09:53:58 +09:00
// 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");
2016-06-23 15:00:58 +09:00
/**
* @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",
2016-07-21 09:53:58 +09:00
insertableIf: canInsert,
editableIf: canEdit,
// form: {
2016-06-23 15:00:58 +09:00
// 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",
2016-07-21 09:53:58 +09:00
insertableIf: canInsert,
editableIf: canEdit
2016-06-23 15:00:58 +09:00
},
/**
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",
2016-07-21 09:53:58 +09:00
insertableIf: canInsert,
editableIf: canEdit
2016-06-23 15:00:58 +09:00
// 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: {
2016-06-23 15:00:58 +09:00
// 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,
2016-07-21 09:53:58 +09:00
// editableIf: canEdit,
2016-06-23 15:00:58 +09:00
// blackbox: true,
// form: {
2016-06-23 15:00:58 +09:00
// 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",
2016-07-21 09:53:58 +09:00
insertableIf: canInsert,
editableIf: canEdit,
2016-06-23 15:00:58 +09:00
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",
2016-07-21 09:53:58 +09:00
insertableIf: canInsert,
editableIf: canEdit
2016-07-19 17:30:59 +09:00
},
/**
Groups
*/
groups: {
type: [String],
optional: true,
control: "checkboxgroup",
2016-07-21 09:53:58 +09:00
insertableIf: canEditAll,
editableIf: canEditAll,
form: {
2016-07-19 17:30:59 +09:00
options: function () {
const groups = _.without(_.keys(Users.groups), "anonymous", "default", "admins");
return groups.map(group => {return {value: group, label: group};});
}
},
},
2016-06-23 15:00:58 +09:00
});
/**
* @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,
2016-07-21 09:53:58 +09:00
insertableIf: canEditAll,
editableIf: canEditAll,
2016-06-23 15:00:58 +09:00
group: adminGroup
// form: {
2016-06-23 15:00:58 +09:00
// 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
2016-06-23 15:00:58 +09:00
*/
Users.attachSchema(Users.schema);
Users.graphQLSchema = `
type User {
_id: String
username: String
createdAt: String
isAdmin: Boolean
telescope: UserTelescope
}
type UserTelescope {
bio: String
commentCount: Float
displayName: String
downvotedComments: [Vote]
downvotedPosts: [Vote]
email: String
emailHash: String
htmlBio: String
karma: Float
postCount: Int
slug: String
twitterUsername: String
upvotedComments: [Vote]
upvotedPosts: [Vote]
website: String
groups: [String]
notifications_users: Boolean
notifications_posts: Boolean
newsletter_subscribeToNewsletter: Boolean
isDummy: Boolean
}
type Vote {
itemId: String
power: Float
votedAt: String
}
`;
Telescope.graphQL.addSchema(Users.graphQLSchema);
Telescope.graphQL.addQuery(`
users: [User]
user(_id: String, slug: String): User
currentUser: User
`);