announcements.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 {
  28. this.unset();
  29. }
  30. },
  31. },
  32. modifiedAt: {
  33. type: Date,
  34. denyUpdate: false,
  35. // eslint-disable-next-line consistent-return
  36. autoValue() {
  37. if (this.isInsert || this.isUpsert || this.isUpdate) {
  38. return new Date();
  39. } else {
  40. this.unset();
  41. }
  42. },
  43. },
  44. })
  45. );
  46. Announcements.allow({
  47. update(userId) {
  48. const user = Users.findOne(userId);
  49. return user && user.isAdmin;
  50. },
  51. });
  52. Announcements.before.update((userId, doc, fieldNames, modifier, options) => {
  53. modifier.$set = modifier.$set || {};
  54. modifier.$set.modifiedAt = Date.now();
  55. });
  56. if (Meteor.isServer) {
  57. Meteor.startup(() => {
  58. Announcements._collection._ensureIndex({ modifiedAt: -1 });
  59. const announcements = Announcements.findOne({});
  60. if (!announcements) {
  61. Announcements.insert({ enabled: false, sort: 0 });
  62. }
  63. });
  64. }
  65. export default Announcements;