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.
[IMPROVE] Replace userData subscriptions by REST (RocketChat#15916)
- Loading branch information
1 parent
a50a170
commit c2f6f82
Showing
10 changed files
with
67 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Users } from '../../../models/server'; | ||
import { Notifications } from '../../../notifications/server'; | ||
|
||
Users.on('change', ({ clientAction, id, data, diff }) => { | ||
switch (clientAction) { | ||
case 'updated': | ||
Notifications.notifyUser(id, 'userData', { diff, type: clientAction }); | ||
break; | ||
case 'inserted': | ||
Notifications.notifyUser(id, 'userData', { data, type: clientAction }); | ||
break; | ||
case 'removed': | ||
Notifications.notifyUser(id, 'userData', { id, type: clientAction }); | ||
break; | ||
} | ||
}); |
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,40 @@ | ||
import { ReactiveVar } from 'meteor/reactive-var'; | ||
import { Meteor } from 'meteor/meteor'; | ||
|
||
import { APIClient } from '../../app/utils/client'; | ||
import { Users } from '../../app/models/client'; | ||
import { Notifications } from '../../app/notifications/client'; | ||
|
||
export const isSyncReady = new ReactiveVar(false); | ||
|
||
function updateUser(userData) { | ||
const user = Users.findOne({ _id: userData._id }); | ||
if (!user || userData._updatedAt > user._updatedAt) { | ||
return Meteor.users.upsert({ _id: userData._id }, userData); | ||
} | ||
// delete data already on user's collection as those are newer | ||
Object.keys(user).forEach((key) => delete userData[key]); | ||
Meteor.users.update({ _id: user._id }, { $set: userData }); | ||
} | ||
|
||
const onUserEvents = { | ||
inserted: (_id, data) => Meteor.users.insert(data), | ||
updated: (_id, { diff }) => Meteor.users.upsert({ _id }, { $set: diff }), | ||
removed: (_id) => Meteor.users.remove({ _id }), | ||
}; | ||
|
||
export const syncUserdata = async (uid) => { | ||
if (!uid) { | ||
return; | ||
} | ||
|
||
await Notifications.onUser('userData', ({ type, id, ...data }) => onUserEvents[type](uid, data)); | ||
|
||
const userData = await APIClient.v1.get('me'); | ||
if (userData) { | ||
updateUser(userData); | ||
} | ||
isSyncReady.set(true); | ||
|
||
return userData; | ||
}; |
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