sandstorm.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // XXX Should be shared with the grain instance name.
  11. title: 'LibreBoard',
  12. slug: 'libreboard',
  13. // Board access security is handled by sandstorm, so in our point of view we
  14. // can alway assume that the board is public (unauthorized users won’t be able
  15. // to access it anyway).
  16. permission: 'public'
  17. };
  18. // The list of permissions a user have is provided by sandstorm accounts
  19. // package.
  20. var userHasPermission = function(user, permission) {
  21. var userPermissions = user.services.sandstorm.permissions;
  22. return userPermissions.indexOf(permission) > -1;
  23. };
  24. if (isSandstorm && Meteor.isServer) {
  25. // Redirect the user to the hard-coded board. On the first launch the user
  26. // will be redirected to the board before its creation. But that’s not a
  27. // problem thanks to the reactive board publication. We used to do this
  28. // redirection on the client side but that was sometime visible on loading,
  29. // and the home page was accessible by pressing the back button of the
  30. // browser, a server-side redirection solves both of these issues.
  31. //
  32. // XXX Maybe sandstorm manifest could provide some kind of "home url"?
  33. Router.route('/', function() {
  34. var base = this.request.headers['x-sandstorm-base-path'];
  35. // XXX If this routing scheme changes, this will break. We should generation
  36. // the location url using the router, but at the time of writting, the
  37. // router is only accessible on the client.
  38. var path = '/boards/' + sandstormBoard._id + '/' + sandstormBoard.slug;
  39. this.response.writeHead(301, {
  40. Location: base + path
  41. });
  42. this.response.end();
  43. }, { where: 'server' });
  44. // On the first launch of the instance a user is automatically created thanks
  45. // to the `accounts-sandstorm` package. After its creation we insert the
  46. // unique board document. Note that when the `Users.after.insert` hook is
  47. // called, the user is inserted into the database but not connected. So
  48. // despite the appearances `userId` is null in this block.
  49. Users.after.insert(function(userId, doc) {
  50. if (! Boards.findOne(sandstormBoard._id)) {
  51. Boards.insert(sandstormBoard, {validate: false});
  52. Boards.update(sandstormBoard._id, {
  53. $set: {
  54. // The first member (the grain creator) has all rights
  55. 'members.0': {
  56. userId: doc._id,
  57. isActive: true,
  58. isAdmin: true
  59. }
  60. }
  61. });
  62. Activities.update(
  63. { activityTypeId: sandstormBoard._id }, {
  64. $set: { userId: doc._id }
  65. });
  66. }
  67. // If the hard-coded board already exists and we are inserting a new user,
  68. // we need to update our user collection.
  69. else if (userHasPermission(doc, 'participate')) {
  70. Boards.update({
  71. _id: sandstormBoard._id,
  72. permission: 'public'
  73. }, {
  74. $push: {
  75. members: {
  76. userId: doc._id,
  77. isActive: true,
  78. isAdmin: userHasPermission(doc, 'configure')
  79. }
  80. }
  81. });
  82. }
  83. });
  84. }
  85. if (isSandstorm && Meteor.isClient) {
  86. // XXX Hack. `Meteor.absoluteUrl` doesn't work in Sandstorm, since every
  87. // session has a different URL whereas Meteor computes absoluteUrl based on
  88. // the ROOT_URL environment variable. So we overwrite this function on a
  89. // sandstorm client to return relative paths instead of absolutes.
  90. var _absoluteUrl = Meteor.absoluteUrl;
  91. var _defaultOptions = Meteor.absoluteUrl.defaultOptions;
  92. Meteor.absoluteUrl = function(path, options) {
  93. var url = _absoluteUrl(path, options);
  94. return url.replace(/^https?:\/\/127\.0\.0\.1:[0-9]{2,5}/, '');
  95. };
  96. Meteor.absoluteUrl.defaultOptions = _defaultOptions;
  97. }
  98. // We use this blaze helper in the UI to hide some templates that does not make
  99. // sense in the context of sandstorm, like board staring, board archiving, user
  100. // name edition, etc.
  101. Blaze.registerHelper('isSandstorm', function() {
  102. return isSandstorm;
  103. });