perf: Refactor of permission loading (#10988)

* perf: Refactor of permission loading

* Simplify condition
This commit is contained in:
Tom Moor authored and GitHub committed 2025-12-23 11:53:31 -05:00
1 parent 0185dc5aca
commit cf1cd8efd5
2 files changed
+53 -23

No files matched your search

+52 -15
View File
@@ -462,28 +462,65 @@ class User extends ParanoidModel<
* @returns An array of collection ids
*/
public collectionIds = async (options: FindOptions<Collection> = {}) => {
const collectionStubs = await Collection.scope({
method: ["withMembership", this.id],
}).findAll({
attributes: ["id", "permission"],
const collectionStubs = await Collection.findAll({
attributes: ["id"],
where: {
teamId: this.teamId,
[Op.or]: [
...(this.isGuest
? []
: [
{
permission: {
[Op.in]: Object.values(CollectionPermission),
},
},
]),
{
"$memberships.id$": { [Op.ne]: null },
},
{
"$groupMemberships.id$": { [Op.ne]: null },
},
],
},
include: [
{
association: "memberships",
attributes: [],
required: false,
where: {
userId: this.id,
},
},
{
association: "groupMemberships",
attributes: [],
required: false,
include: [
{
association: "group",
attributes: [],
required: true,
include: [
{
association: "groupUsers",
attributes: [],
required: true,
where: {
userId: this.id,
},
},
],
},
],
},
],
paranoid: true,
...options,
});
return collectionStubs
.filter(
(c) =>
(Object.values(CollectionPermission).includes(
c.permission as CollectionPermission
) &&
!this.isGuest) ||
c.memberships.length > 0 ||
c.groupMemberships.length > 0
)
.map((c) => c.id);
return Array.from(new Set(collectionStubs.map((c) => c.id)));
};
updateActiveAt = async (ctx: Context, force = false) => {
+1 -8
View File
@@ -461,18 +461,11 @@ router.post(
teamId: user.teamId,
collectionId: collectionIds,
},
include: [
{
model: Collection.scope({
method: ["withMembership", userId],
}),
as: "collection",
},
],
},
],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
subQuery: false,
});
const documents = views.map((view) => {
const document = view.document;