sandstorm.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Sandstorm context is detected using the METEOR_SETTINGS environment variable
  2. // in the package definition.
  3. var isSandstorm = Meteor.settings && Meteor.settings.public &&
  4. Meteor.settings.public.sandstorm;
  5. // In sandstorm we only have one board per sandstorm instance. Since we want to
  6. // keep most of our code unchanged, we simply hard-code a board `_id` and
  7. // redirect the user to this particular board.
  8. var sandstormBoard = {
  9. _id: 'sandstorm',
  10. slug: 'board',
  11. // XXX Should be shared with the grain instance name.
  12. title: 'LibreBoard',
  13. permission: 'public',
  14. background: {
  15. type: 'color',
  16. color: '#16A085'
  17. },
  18. // XXX Not certain this is a bug, but we except these fields to get inserted
  19. // by the `Lists.before.insert` collection-hook. Since this hook is not called
  20. // in this case, we have to duplicate the logic and set them here.
  21. archived: false,
  22. createdAt: new Date()
  23. };
  24. // On the first launch of the instance a user is automatically created thanks to
  25. // the `accounts-sandstorm` package. After its creation we insert the unique
  26. // board document. Note that when the `Users.after.insert` hook is called, the
  27. // user is inserted into the database but not connected. So despite the
  28. // appearances `userId` is null in this block.
  29. //
  30. // If the hard-coded board already exists and we are inserting a new user, we
  31. // assume that the owner of the board want to share write privileges with the
  32. // new user.
  33. // XXX Improve that when the Sandstorm sharing model (“Powerbox”) arrives.
  34. if (isSandstorm && Meteor.isServer) {
  35. Users.after.insert(function(userId, doc) {
  36. if (! Boards.findOne(sandstormBoard._id)) {
  37. Boards.insert(_.extend(sandstormBoard, { userId: doc._id }));
  38. Boards.update(sandstormBoard._id, {
  39. $set: {
  40. 'members.0.userId': doc._id
  41. }
  42. });
  43. Activities.update({
  44. activityTypeId: sandstormBoard._id
  45. }, {
  46. $set: {
  47. userId: doc._id
  48. }
  49. });
  50. } else {
  51. Boards.update({
  52. _id: sandstormBoard._id,
  53. permission: 'public'
  54. }, {
  55. $push: {
  56. members: doc._id
  57. }
  58. });
  59. }
  60. });
  61. }
  62. // On the client, redirect the user to the hard-coded board. On the first launch
  63. // the user will be redirected to the board before its creation. But that’s not
  64. // a problem thanks to the reactive board publication.
  65. if (isSandstorm && Meteor.isClient) {
  66. Router.go('Board', {
  67. boardId: sandstormBoard._id,
  68. slug: getSlug(sandstormBoard.title)
  69. });
  70. // XXX Hack. `Meteor.absoluteUrl` doesn't work in Sandstorm, since every
  71. // session has a different URL whereas Meteor computes absoluteUrl based on
  72. // the ROOT_URL environment variable. So we overwrite this function on a
  73. // sandstorm client to return relative paths instead of absolutes.
  74. var _absoluteUrl = Meteor.absoluteUrl;
  75. var _defaultOptions = Meteor.absoluteUrl.defaultOptions;
  76. Meteor.absoluteUrl = function(path, options) {
  77. var url = _absoluteUrl(path, options);
  78. return url.replace(/^https?:\/\/127\.0\.0\.1:[0-9]{2,5}/, '');
  79. };
  80. Meteor.absoluteUrl.defaultOptions = _defaultOptions;
  81. }
  82. // We use this blaze helper in the UI to hide some template that does not make
  83. // sense in the context of sandstorm, like board staring, board archiving, user
  84. // name edition, etc.
  85. Blaze.registerHelper('isSandstorm', function() {
  86. return isSandstorm;
  87. });