-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
server.js
43 lines (33 loc) · 1.14 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Meteor } from 'meteor/meteor'
import { CollectionHooks } from './collection-hooks'
import './wrappers'
const publishUserId = new Meteor.EnvironmentVariable()
CollectionHooks.getUserId = function getUserId () {
let userId
try {
// Will throw an error unless within method call.
// Attempt to recover gracefully by catching:
userId = Meteor.userId && Meteor.userId()
} catch (e) {}
if (userId == null) {
// Get the userId if we are in a publish function.
userId = publishUserId.get()
}
if (userId == null) {
userId = CollectionHooks.defaultUserId
}
return userId
}
const _publish = Meteor.publish
Meteor.publish = function (name, handler, options) {
return publishUserId.withValue(this && this.userId, () => _publish.call(this, name, function (...args) {
// This function is called repeatedly in publications
return handler.apply(this, args)
}, options))
}
// Make the above available for packages with hooks that want to determine
// whether they are running inside a publish function or not.
CollectionHooks.isWithinPublish = () => publishUserId.get() !== undefined
export {
CollectionHooks
}