-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW] Assign oldest active user as owner when deleting last room owner (
#16088) Co-authored-by: Diego Sampaio <chinello@gmail.com>
- Loading branch information
1 parent
46856e4
commit 50334ad
Showing
20 changed files
with
396 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { subscriptionHasRole } from '../../../authorization/server'; | ||
import { Users, Subscriptions } from '../../../models/server'; | ||
|
||
export function shouldRemoveOrChangeOwner(subscribedRooms) { | ||
return subscribedRooms | ||
.some(({ shouldBeRemoved, shouldChangeOwner }) => shouldBeRemoved || shouldChangeOwner); | ||
} | ||
|
||
export function getSubscribedRoomsForUserWithDetails(userId) { | ||
const subscribedRooms = []; | ||
|
||
// Iterate through all the rooms the user is subscribed to, to check if he is the last owner of any of them. | ||
Subscriptions.findByUserIdExceptType(userId, 'd').forEach((subscription) => { | ||
const roomData = { | ||
rid: subscription.rid, | ||
t: subscription.t, | ||
shouldBeRemoved: false, | ||
shouldChangeOwner: false, | ||
newOwner: null, | ||
}; | ||
|
||
if (subscriptionHasRole(subscription, 'owner')) { | ||
// Fetch the number of owners | ||
const numOwners = Subscriptions.findByRoomIdAndRoles(subscription.rid, ['owner']).count(); | ||
|
||
// If it's only one, then this user is the only owner. | ||
if (numOwners === 1) { | ||
// Let's check how many subscribers the room has. | ||
const options = { fields: { 'u._id': 1 }, sort: { ts: 1 } }; | ||
const subscribersCursor = Subscriptions.findByRoomId(subscription.rid, options); | ||
|
||
subscribersCursor.forEach(({ u: { _id: uid } }) => { | ||
// If we already changed the owner or this subscription is for the user we are removing, then don't try to give it ownership | ||
if (roomData.shouldChangeOwner || uid === userId) { | ||
return; | ||
} | ||
const newOwner = Users.findOneActiveById(uid, { fields: { _id: 1 } }); | ||
if (!newOwner) { | ||
return; | ||
} | ||
|
||
roomData.newOwner = uid; | ||
roomData.shouldChangeOwner = true; | ||
}); | ||
|
||
// If there's no subscriber available to be the new owner and it's not a public room, we can remove it. | ||
if (!roomData.shouldChangeOwner && roomData.t !== 'c') { | ||
roomData.shouldBeRemoved = true; | ||
} | ||
} | ||
} else if (roomData.t !== 'c') { | ||
// If the user is not an owner, remove the room if the user is the only subscriber | ||
roomData.shouldBeRemoved = Subscriptions.findByRoomId(roomData.rid).count() === 1; | ||
} | ||
|
||
subscribedRooms.push(roomData); | ||
}); | ||
|
||
return subscribedRooms; | ||
} |
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,25 @@ | ||
import { Rooms } from '../../../models/server'; | ||
|
||
export const getUserSingleOwnedRooms = function(subscribedRooms) { | ||
const roomsThatWillChangeOwner = subscribedRooms.filter(({ shouldChangeOwner }) => shouldChangeOwner).map(({ rid }) => rid); | ||
const roomsThatWillBeRemoved = subscribedRooms.filter(({ shouldBeRemoved }) => shouldBeRemoved).map(({ rid }) => rid); | ||
|
||
const roomIds = roomsThatWillBeRemoved.concat(roomsThatWillChangeOwner); | ||
const rooms = Rooms.findByIds(roomIds, { fields: { _id: 1, name: 1, fname: 1 } }); | ||
|
||
const result = { | ||
shouldBeRemoved: [], | ||
shouldChangeOwner: [], | ||
}; | ||
|
||
rooms.forEach((room) => { | ||
const name = room.fname || room.name; | ||
if (roomsThatWillBeRemoved.includes(room._id)) { | ||
result.shouldBeRemoved.push(name); | ||
} else { | ||
result.shouldChangeOwner.push(name); | ||
} | ||
}); | ||
|
||
return result; | ||
}; |
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,30 @@ | ||
import { FileUpload } from '../../../file-upload/server'; | ||
import { Subscriptions, Messages, Rooms, Roles } from '../../../models/server'; | ||
|
||
const bulkRoomCleanUp = (rids) => { | ||
// no bulk deletion for files | ||
rids.forEach((rid) => FileUpload.removeFilesByRoomId(rid)); | ||
|
||
return Promise.await(Promise.all([ | ||
Subscriptions.removeByRoomIds(rids), | ||
Messages.removeByRoomIds(rids), | ||
Rooms.removeByIds(rids), | ||
])); | ||
}; | ||
|
||
export const relinquishRoomOwnerships = function(userId, subscribedRooms, removeDirectMessages = true) { | ||
// change owners | ||
subscribedRooms | ||
.filter(({ shouldChangeOwner }) => shouldChangeOwner) | ||
.forEach(({ newOwner, rid }) => Roles.addUserRoles(newOwner, ['owner'], rid)); | ||
|
||
const roomIdsToRemove = subscribedRooms.filter(({ shouldBeRemoved }) => shouldBeRemoved).map(({ rid }) => rid); | ||
|
||
if (removeDirectMessages) { | ||
Rooms.find1On1ByUserId(userId, { fields: { _id: 1 } }).forEach(({ _id }) => roomIdsToRemove.push(_id)); | ||
} | ||
|
||
bulkRoomCleanUp(roomIdsToRemove); | ||
|
||
return subscribedRooms; | ||
}; |
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.