Skip to content

Commit

Permalink
Small improvements to federation callbacks/hooks (#13946)
Browse files Browse the repository at this point in the history
* Improve federation hooks

* Verify if user joined is federated
  • Loading branch information
sampaiodiego authored Mar 29, 2019
1 parent 65b5be3 commit c470673
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
37 changes: 32 additions & 5 deletions app/federation/server/PeerClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ export class PeerClient {
FederationEvents.on('createEvent', this.wrapEnabled(this.onCreateEvent.bind(this)));

callbacks.add('afterCreateDirectRoom', this.wrapEnabled(this.afterCreateDirectRoom.bind(this)), callbacks.priority.LOW, 'federation-create-direct-room');
callbacks.add('afterCreateRoom', this.wrapEnabled(this.afterCreateRoom.bind(this)), callbacks.priority.LOW, 'federation-join-room');
callbacks.add('afterCreateRoom', this.wrapEnabled(this.afterCreateRoom.bind(this)), callbacks.priority.LOW, 'federation-create-room');
callbacks.add('afterSaveRoomSettings', this.wrapEnabled(this.afterSaveRoomSettings.bind(this)), callbacks.priority.LOW, 'federation-after-save-room-settings');
callbacks.add('afterAddedToRoom', this.wrapEnabled(this.afterAddedToRoom.bind(this)), callbacks.priority.LOW, 'federation-join-room');
callbacks.add('afterAddedToRoom', this.wrapEnabled(this.afterAddedToRoom.bind(this)), callbacks.priority.LOW, 'federation-added-to-room');
callbacks.add('beforeLeaveRoom', this.wrapEnabled(this.beforeLeaveRoom.bind(this)), callbacks.priority.LOW, 'federation-leave-room');
callbacks.add('beforeRemoveFromRoom', this.wrapEnabled(this.beforeRemoveFromRoom.bind(this)), callbacks.priority.LOW, 'federation-leave-room');
callbacks.add('beforeRemoveFromRoom', this.wrapEnabled(this.beforeRemoveFromRoom.bind(this)), callbacks.priority.LOW, 'federation-remove-from-room');
callbacks.add('afterSaveMessage', this.wrapEnabled(this.afterSaveMessage.bind(this)), callbacks.priority.LOW, 'federation-save-message');
callbacks.add('afterDeleteMessage', this.wrapEnabled(this.afterDeleteMessage.bind(this)), callbacks.priority.LOW, 'federation-delete-message');
callbacks.add('afterReadMessages', this.wrapEnabled(this.afterReadMessages.bind(this)), callbacks.priority.LOW, 'federation-read-messages');
Expand Down Expand Up @@ -317,6 +317,8 @@ export class PeerClient {
federatedRoom.refreshFederation();

FederationEvents.directRoomCreated(federatedRoom, { skipPeers: [localPeerDomain] });

return room;
}

afterCreateRoom(roomOwner, room) {
Expand All @@ -343,6 +345,8 @@ export class PeerClient {
federatedRoom.refreshFederation();

FederationEvents.roomCreated(federatedRoom, { skipPeers: [localPeerDomain] });

return roomOwner;
}

afterSaveRoomSettings(/* room */) {
Expand All @@ -359,8 +363,11 @@ export class PeerClient {

const { peer: { domain: localPeerDomain } } = this;

// Check if room is federated
if (!FederatedRoom.isFederated(localPeerDomain, room, { checkUsingUsers: true })) { return users; }
// Check if room or user who joined are federated
if ((!userWhoJoined.federation || userWhoJoined.federation.peer === localPeerDomain) &&
!FederatedRoom.isFederated(localPeerDomain, room)) {
return users;
}

const extras = {};

Expand Down Expand Up @@ -408,6 +415,8 @@ export class PeerClient {
} else {
FederationEvents.userJoined(federatedRoom, federatedUserWhoJoined, { skipPeers: [localPeerDomain] });
}

return users;
}

beforeLeaveRoom(userWhoLeft, room) {
Expand All @@ -433,6 +442,8 @@ export class PeerClient {

// Refresh room's federation
federatedRoom.refreshFederation();

return userWhoLeft;
}

beforeRemoveFromRoom(users, room) {
Expand Down Expand Up @@ -461,6 +472,8 @@ export class PeerClient {

// Refresh room's federation
federatedRoom.refreshFederation();

return users;
}

afterSaveMessage(message, room) {
Expand Down Expand Up @@ -488,6 +501,8 @@ export class PeerClient {
} else {
FederationEvents.messageCreated(federatedRoom, federatedMessage, { skipPeers: [localPeerDomain] });
}

return message;
}

afterDeleteMessage(message) {
Expand All @@ -508,6 +523,8 @@ export class PeerClient {
const federatedMessage = new FederatedMessage(localPeerDomain, message);

FederationEvents.messageDeleted(federatedRoom, federatedMessage, { skipPeers: [localPeerDomain] });

return message;
}

afterReadMessages(roomId, { userId }) {
Expand All @@ -531,6 +548,8 @@ export class PeerClient {
const federatedUser = FederatedUser.loadByFederationId(localPeerDomain, user.federation._id);

FederationEvents.messagesRead(federatedRoom, federatedUser, { skipPeers: [localPeerDomain] });

return roomId;
}

afterSetReaction(message, { user, reaction, shouldReact }) {
Expand All @@ -550,6 +569,8 @@ export class PeerClient {
const federatedRoom = FederatedRoom.loadByFederationId(localPeerDomain, room.federation._id);

FederationEvents.messagesSetReaction(federatedRoom, federatedMessage, federatedUser, reaction, shouldReact, { skipPeers: [localPeerDomain] });

return message;
}

afterUnsetReaction(message, { user, reaction, shouldReact }) {
Expand All @@ -569,6 +590,8 @@ export class PeerClient {
const federatedRoom = FederatedRoom.loadByFederationId(localPeerDomain, room.federation._id);

FederationEvents.messagesUnsetReaction(federatedRoom, federatedMessage, federatedUser, reaction, shouldReact, { skipPeers: [localPeerDomain] });

return message;
}

afterMuteUser(users, room) {
Expand All @@ -588,6 +611,8 @@ export class PeerClient {
const federatedUserWhoMuted = FederatedUser.loadByFederationId(localPeerDomain, fromUser.federation._id);

FederationEvents.userMuted(federatedRoom, federatedMutedUser, federatedUserWhoMuted, { skipPeers: [localPeerDomain] });

return users;
}

afterUnmuteUser(users, room) {
Expand All @@ -607,5 +632,7 @@ export class PeerClient {
const federatedUserWhoUnmuted = FederatedUser.loadByFederationId(localPeerDomain, fromUser.federation._id);

FederationEvents.userUnmuted(federatedRoom, federatedUnmutedUser, federatedUserWhoUnmuted, { skipPeers: [localPeerDomain] });

return users;
}
}
6 changes: 3 additions & 3 deletions app/federation/server/federatedResources/FederatedRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,21 @@ export class FederatedRoom extends FederatedResource {
}
}

FederatedRoom.loadByFederationId = function loadByFederationId(localPeerIdentifier, federationId) {
FederatedRoom.loadByFederationId = function _loadByFederationId(localPeerIdentifier, federationId) {
const localRoom = Rooms.findOne({ 'federation._id': federationId });

if (!localRoom) { return; }

return new FederatedRoom(localPeerIdentifier, localRoom);
};

FederatedRoom.loadRoomUsers = function loadRoomUsers(room) {
FederatedRoom.loadRoomUsers = function _loadRoomUsers(room) {
const subscriptions = Subscriptions.findByRoomIdWhenUsernameExists(room._id, { fields: { 'u._id': 1 } }).fetch();
const userIds = subscriptions.map((s) => s.u._id);
return Users.findUsersWithUsernameByIds(userIds).fetch();
};

FederatedRoom.isFederated = function isFederated(localPeerIdentifier, room, options = {}) {
FederatedRoom.isFederated = function _isFederated(localPeerIdentifier, room, options = {}) {
this.log('federated-room', `${ room._id } - isFederated?`);

let isFederated = false;
Expand Down

0 comments on commit c470673

Please sign in to comment.