Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Add new events after user login, logout and change his status #25234

Merged
merged 14 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions apps/meteor/app/apps/server/bridges/listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export class AppListenerBridge {
case AppInterface.IPostUserCreated:
case AppInterface.IPostUserUpdated:
case AppInterface.IPostUserDeleted:
case AppInterface.IPostUserLogin:
case AppInterface.IPostUserLogout:
case AppInterface.IPostUserStatusChanged:
return 'userEvent';
default:
return 'defaultEvent';
Expand Down Expand Up @@ -140,15 +143,32 @@ export class AppListenerBridge {
}

async userEvent(inte, data) {
const context = {
user: this.orch.getConverters().get('users').convertToApp(data.user),
performedBy: this.orch.getConverters().get('users').convertToApp(data.performedBy),
};

if (inte === AppInterface.IPostUserUpdated) {
context.previousData = this.orch.getConverters().get('users').convertToApp(data.previousUser);
let context;
switch (inte) {
case AppInterface.IPostUserLoggedIn:
case AppInterface.IPostUserLogout:
context = this.orch.getConverters().get('users').convertToApp(data.user);
return this.orch.getManager().getListenerManager().executeListener(inte, context);
case AppInterface.IPostUserStatusChanged:
const { currentStatus, previousStatus } = data;
context = {
user: this.orch.getConverters().get('users').convertToApp(data.user),
currentStatus,
previousStatus,
};

return this.orch.getManager().getListenerManager().executeListener(inte, context);
case AppInterface.IPostUserCreated:
case AppInterface.IPostUserUpdated:
case AppInterface.IPostUserDeleted:
context = {
user: this.orch.getConverters().get('users').convertToApp(data.user),
performedBy: this.orch.getConverters().get('users').convertToApp(data.performedBy),
};
if (inte === AppInterface.IPostUserUpdated) {
context.previousData = this.orch.getConverters().get('users').convertToApp(data.previousUser);
}
return this.orch.getManager().getListenerManager().executeListener(inte, context);
}

return this.orch.getManager().getListenerManager().executeListener(inte, context);
}
}
1 change: 1 addition & 0 deletions apps/meteor/app/apps/server/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './cron';
import './status.ts';

export { Apps, AppEvents } from './orchestrator';
16 changes: 16 additions & 0 deletions apps/meteor/app/apps/server/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { UserPresenceMonitor } from 'meteor/konecty:user-presence';

import { AppEvents, Apps } from './orchestrator';

UserPresenceMonitor.onSetUserStatus((...args: any) => {
const [user, status] = args;

// App IPostUserStatusChanged event hook
Promise.await(
Apps.triggerEvent(AppEvents.IPostUserStatusChanged, {
user,
currentStatus: status,
previousStatus: user.status,
}),
);
});
9 changes: 9 additions & 0 deletions apps/meteor/app/authentication/server/startup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,15 @@ Accounts.validateLoginAttempt(function (login) {
return callbacks.run('afterValidateLogin', login);
});

/**
* Trigger the event only when the
* user does login in Rocket.chat
*/
if (login.type !== 'resume') {
// App IPostUserLoggedIn event hook
Promise.await(Apps.triggerEvent(AppEvents.IPostUserLoggedIn, login.user));
}

return true;
});

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/lib/server/functions/saveUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export const saveUser = function (userId, userData) {

validateUserEditing(userId, userData);

const oldUserData = Users.findOneById(userId);
const oldUserData = Users.findOneById(userData._id);

// update user
if (userData.hasOwnProperty('username') || userData.hasOwnProperty('name')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare module 'meteor/konecty:user-presence' {
namespace UserPresenceMonitor {
function processUserSession(userSession: any, event: string): void;
function onSetUserStatus(callback: any): void;
}

namespace UserPresence {
Expand Down
4 changes: 4 additions & 0 deletions apps/meteor/server/methods/logoutCleanUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';

import { callbacks } from '../../lib/callbacks';
import { AppEvents, Apps } from '../../app/apps/server/orchestrator';

Meteor.methods({
logoutCleanUp(user) {
Expand All @@ -10,5 +11,8 @@ Meteor.methods({
Meteor.defer(function () {
callbacks.run('afterLogoutCleanUp', user);
});

// App IPostUserLogout event hook
Promise.await(Apps.triggerEvent(AppEvents.IPostUserLoggedOut, user));
},
});