<commit_before>import SimpleSchema from 'simpl-schema';
import { Utils, getCollection, Connectors, Locales } from 'meteor/vulcan:lib'; // import from vulcan:lib because vulcan:core isn't loaded yet

///////////////////////////////////////
// Order for the Schema is as follows. Change as you see fit:
// 00.
// 10. Display Name
// 20. Email
// 30. Bio
// 40. Slug
// 50. Website
// 60. Twitter username
// 70.
// 80.
// 90.
// 100.
// Anything else..
///////////////////////////////////////

const createDisplayName = user => {
  const profileName = Utils.getNestedProperty(user, 'profile.name');
  const twitterName = Utils.getNestedProperty(user, 'services.twitter.screenName');
  const linkedinFirstName = Utils.getNestedProperty(user, 'services.linkedin.firstName');
  if (profileName) return profileName;
  if (twitterName) return twitterName;
  if (linkedinFirstName)
    return `${linkedinFirstName} ${Utils.getNestedProperty(user, 'services.linkedin.lastName')}`;
  if (user.username) return user.username;
  if (user.email) return user.email.slice(0, user.email.indexOf('@'));
  return undefined;
};

const adminGroup = {
  name: 'admin',
  order: 100,
};

const ownsOrIsAdmin = (user, document) => {
  return getCollection('Users').owns(user, document) || getCollection('Users').isAdmin(user);
};

const UserEmail = {
  address: {
    type: String,
    regEx: SimpleSchema.RegEx.Email,
    optional: true,
  },
  verified: {
    type: Boolean,
    optional: true,
  },
};

/**
 * @summary Users schema
 * @type {Object}
 */
const schema = {
  _id: {
    type: String,
    optional: true,
    canRead: ['guests'],
  },
  username: {
    type: String,
    optional: true,
    canRead: ['guests'],
    canUpdate: ['admins'],
    canCreate: ['members'],
    onCreate: ({ document: user }) => {
      if (
        !user.username &&
        user.services &&
        user.services.twitter &&
        user.services.twitter.screenName
      ) {
        return user.services.twitter.screenName;
      }
    },
    searchable: true,
  },
  // emails: {
  //   type: Array,
  //   optional: true,
  // },
  // 'emails.$': {
  //   type: Object,
  //   optional: true,
  // },
  createdAt: {
    type: Date,
    optional: true,
    canRead: ['admins'],
    onCreate: () => {
      return new Date();
    },
  },
  isAdmin: {
    type: Boolean,
    label: 'Admin',
    input: 'checkbox',
    optional: true,
    canCreate: ['admins'],
    canUpdate: ['admins'],
    canRead: ['guests'],
    group: adminGroup,
  },
  locale: {
    type: String,
    label: 'Preferred Language',<commit_msg>Fix Users collection<commit_after>import SimpleSchema from 'simpl-schema';
import { Utils, getCollection, Connectors, Locales, getType, addTypeAndResolvers } from 'meteor/vulcan:lib'; // import from vulcan:lib because vulcan:core isn't loaded yet

///////////////////////////////////////
// Order for the Schema is as follows. Change as you see fit:
// 00.
// 10. Display Name
// 20. Email
// 30. Bio
// 40. Slug
// 50. Website
// 60. Twitter username
// 70.
// 80.
// 90.
// 100.
// Anything else..
///////////////////////////////////////

const createDisplayName = user => {
  const profileName = Utils.getNestedProperty(user, 'profile.name');
  const twitterName = Utils.getNestedProperty(user, 'services.twitter.screenName');
  const linkedinFirstName = Utils.getNestedProperty(user, 'services.linkedin.firstName');
  if (profileName) return profileName;
  if (twitterName) return twitterName;
  if (linkedinFirstName)
    return `${linkedinFirstName} ${Utils.getNestedProperty(user, 'services.linkedin.lastName')}`;
  if (user.username) return user.username;
  if (user.email) return user.email.slice(0, user.email.indexOf('@'));
  return undefined;
};

const adminGroup = {
  name: 'admin',
  order: 100,
};

const ownsOrIsAdmin = (user, document) => {
  return getCollection('Users').owns(user, document) || getCollection('Users').isAdmin(user);
};

const UserEmail = {
  address: {
    type: String,
    regEx: SimpleSchema.RegEx.Email,
    optional: true,
    canRead: ['admin']
  },
  verified: {
    type: Boolean,
    optional: true,
    canRead: ['admin']
  },
};
addTypeAndResolvers({ typeName: 'UserEmail', schema: new SimpleSchema(UserEmail), description: 'The known emails of the user' });

/**
 * @summary Users schema
 * @type {Object}
 */
const schema = {
  _id: {
    type: String,
    optional: true,
    canRead: ['guests'],
  },
  username: {
    type: String,
    optional: true,
    canRead: ['guests'],
    canUpdate: ['admins'],
    canCreate: ['members'],
    onCreate: ({ document: user }) => {
      if (
        !user.username &&
        user.services &&
        user.services.twitter &&
        user.services.twitter.screenName
      ) {
        return user.services.twitter.screenName;
      }
    },
    searchable: true,
  },
  emails: {
    type: Array,
    optional: true,
    canRead: ['admin']
  },
  'emails.$': {
    ...getType('UserEmail'),
    optional: true,
  },
  createdAt: {
    type: Date,
    optional: true,
    canRead: ['admins'],
    onCreate: () => {
      return new Date();
    },
  },
  isAdmin: {
    type: Boolean,
    label: 'Admin',
    input: 'checkbox',
    optional: true,
    canCreate: ['admins'],
    canUpdate: ['admins'],
    canRead: ['guests'],
    group: adminGroup,
  },
  locale: {
    type: String,
    label: 'Preferred Language',
{'fraction_non_alphanumeric': 0.18116076395327277, 'fraction_numerical': 0.010012979788614871, 'mean_word_length': 2.651997291807718, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4142246', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1707, 'n_words': 423}