forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request RocketChat#341 from bhardwajaditya/push_notificati…
…on_subscription_handle Push notification subscription handle
- Loading branch information
Showing
25 changed files
with
556 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Meteor } from 'meteor/meteor'; | ||
import { check } from 'meteor/check'; | ||
|
||
import { PushNotificationSubscriptions } from '../../../models'; | ||
|
||
Meteor.methods({ | ||
addUserToPushSubscription(endpoint) { | ||
check(endpoint, String); | ||
|
||
let user = Meteor.user(); | ||
if (user) { | ||
user = { | ||
_id: user._id, | ||
username: user.username, | ||
}; | ||
} | ||
|
||
PushNotificationSubscriptions.updateUserIdWithSubscriptionEndpoint(endpoint, user); | ||
|
||
return endpoint; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Meteor } from 'meteor/meteor'; | ||
import { check } from 'meteor/check'; | ||
|
||
import { PushNotificationSubscriptions } from '../../../models'; | ||
|
||
Meteor.methods({ | ||
removeUserFromPushSubscription(endpoint) { | ||
check(endpoint, String); | ||
|
||
PushNotificationSubscriptions.updateUserIdWithSubscriptionEndpoint(endpoint); | ||
|
||
return endpoint; | ||
}, | ||
}); |
23 changes: 23 additions & 0 deletions
23
app/lib/server/methods/savePushNotificationSubscription.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Meteor } from 'meteor/meteor'; | ||
import { check } from 'meteor/check'; | ||
|
||
import { PushNotificationSubscriptions } from '../../../models'; | ||
|
||
Meteor.methods({ | ||
savePushNotificationSubscription(subscription) { | ||
subscription = JSON.parse(subscription); | ||
|
||
check(subscription.endpoint, String); | ||
|
||
let user = Meteor.user(); | ||
if (user) { | ||
user = { | ||
_id: user._id, | ||
username: user.username, | ||
}; | ||
} | ||
PushNotificationSubscriptions.createWithUserAndSubscription(user, subscription); | ||
|
||
return subscription; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Base } from './_Base'; | ||
|
||
export class PushNotificationSubscriptions extends Base { | ||
constructor(...args) { | ||
super(...args); | ||
|
||
this.tryEnsureIndex({ u: 1 }); | ||
} | ||
|
||
createWithUserAndSubscription(user, subscription) { | ||
const pushNotificationSubscription = { | ||
...subscription, | ||
}; | ||
if (user && user._id != null) { | ||
pushNotificationSubscription.u = { ...user }; | ||
} | ||
const result = this.insert(pushNotificationSubscription); | ||
return result; | ||
} | ||
|
||
findByUserId(userId) { | ||
const query = { | ||
'u._id': userId, | ||
}; | ||
return this.find(query); | ||
} | ||
|
||
findSubscriptionsWithNoUser() { | ||
const query = { | ||
user: { | ||
$exists: false, | ||
}, | ||
}; | ||
return this.find(query); | ||
} | ||
|
||
updateUserIdWithSubscriptionEndpoint(endpoint, user = null) { | ||
const query = { | ||
endpoint, | ||
}; | ||
|
||
const update = {}; | ||
|
||
if (user !== null) { | ||
update.$set = { u: user }; | ||
} else { | ||
update.$unset = { u: 1 }; | ||
} | ||
|
||
return this.update(query, update); | ||
} | ||
|
||
removeById(_id) { | ||
return this.remove(_id); | ||
} | ||
} | ||
|
||
export default new PushNotificationSubscriptions('pushNotificationSubscriptions'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.