announcements.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. Announcements = new Mongo.Collection('announcements');
  2. Announcements.attachSchema(
  3. new SimpleSchema({
  4. enabled: {
  5. type: Boolean,
  6. defaultValue: false,
  7. },
  8. title: {
  9. type: String,
  10. optional: true,
  11. },
  12. body: {
  13. type: String,
  14. optional: true,
  15. },
  16. sort: {
  17. type: Number,
  18. decimal: true,
  19. },
  20. createdAt: {
  21. type: Date,
  22. optional: true,
  23. // eslint-disable-next-line consistent-return
  24. autoValue() {
  25. if (this.isInsert) {
  26. return new Date();
  27. } else if (this.isUpsert) {
  28. return { $setOnInsert: new Date() };
  29. } else {
  30. this.unset();
  31. }
  32. },
  33. },
  34. modifiedAt: {
  35. type: Date,
  36. denyUpdate: false,
  37. // eslint-disable-next-line consistent-return
  38. autoValue() {
  39. if (this.isInsert || this.isUpsert || this.isUpdate) {
  40. return new Date();
  41. } else {
  42. this.unset();
  43. }
  44. },
  45. },
  46. }),
  47. );
  48. Announcements.allow({
  49. update(userId) {
  50. const user = Users.findOne(userId);
  51. return user && user.isAdmin;
  52. },
  53. });
  54. if (Meteor.isServer) {
  55. Meteor.startup(() => {
  56. Announcements._collection.createIndex({ modifiedAt: -1 });
  57. const announcements = Announcements.findOne({});
  58. if (!announcements) {
  59. Announcements.insert({ enabled: false, sort: 0 });
  60. }
  61. });
  62. }
  63. export default Announcements;