From 229cae771e4183fa332b258425d3e1523b748fed Mon Sep 17 00:00:00 2001 From: Kristina <7614497+khjrtbrg@users.noreply.github.com> Date: Mon, 3 Feb 2025 10:09:42 +0100 Subject: [PATCH] Merge pull request #23259 from overleaf/kh-rm-dead-manager-code [web] remove unused functions to support legacy read/write link sharing GitOrigin-RevId: 504c1eb721caa7131ed685b1e0fa5e0d0b460888 --- .../Collaborators/CollaboratorsGetter.js | 12 -- .../CollaboratorsInviteGetter.js | 7 - .../Subscription/LimitationsManager.js | 14 -- .../TokenAccess/TokenAccessHandler.js | 18 -- .../CollaboratorsInviteControllerTests.mjs | 5 +- .../CollaboratorsInviteGetterTests.js | 30 --- .../Subscription/LimitationsManagerTests.js | 171 ------------------ .../TokenAccessControllerTests.mjs | 1 - .../TokenAccess/TokenAccessHandlerTests.js | 48 ----- 9 files changed, 2 insertions(+), 304 deletions(-) diff --git a/services/web/app/src/Features/Collaborators/CollaboratorsGetter.js b/services/web/app/src/Features/Collaborators/CollaboratorsGetter.js index c2e1163b65..c810eae8fa 100644 --- a/services/web/app/src/Features/Collaborators/CollaboratorsGetter.js +++ b/services/web/app/src/Features/Collaborators/CollaboratorsGetter.js @@ -22,7 +22,6 @@ module.exports = { getInvitedMembersWithPrivilegeLevelsFromFields ), getMemberIdPrivilegeLevel: callbackify(getMemberIdPrivilegeLevel), - getInvitedCollaboratorCount: callbackify(getInvitedCollaboratorCount), getProjectsUserIsMemberOf: callbackify(getProjectsUserIsMemberOf), dangerouslyGetAllProjectsUserIsMemberOf: callbackify( dangerouslyGetAllProjectsUserIsMemberOf @@ -38,7 +37,6 @@ module.exports = { getInvitedMembersWithPrivilegeLevels, getInvitedMembersWithPrivilegeLevelsFromFields, getMemberIdPrivilegeLevel, - getInvitedCollaboratorCount, getInvitedEditCollaboratorCount, getInvitedPendingEditorCount, getProjectsUserIsMemberOf, @@ -126,11 +124,6 @@ async function getMemberIdPrivilegeLevel(userId, projectId) { return PrivilegeLevels.NONE } -async function getInvitedCollaboratorCount(projectId) { - const count = await _getInvitedMemberCount(projectId) - return count - 1 // Don't count project owner -} - async function getInvitedEditCollaboratorCount(projectId) { // Only counts invited members with readAndWrite privilege const members = await getMemberIdsWithPrivilegeLevels(projectId) @@ -315,11 +308,6 @@ async function userIsReadWriteTokenMember(userId, projectId) { return project != null } -async function _getInvitedMemberCount(projectId) { - const members = await getMemberIdsWithPrivilegeLevels(projectId) - return members.filter(m => m.source !== Sources.TOKEN).length -} - function _getMemberIdsWithPrivilegeLevelsFromFields( ownerId, collaboratorIds, diff --git a/services/web/app/src/Features/Collaborators/CollaboratorsInviteGetter.js b/services/web/app/src/Features/Collaborators/CollaboratorsInviteGetter.js index 4aecfe71ab..e3f692e74b 100644 --- a/services/web/app/src/Features/Collaborators/CollaboratorsInviteGetter.js +++ b/services/web/app/src/Features/Collaborators/CollaboratorsInviteGetter.js @@ -15,12 +15,6 @@ async function getAllInvites(projectId) { return invites } -async function getInviteCount(projectId) { - logger.debug({ projectId }, 'counting invites for project') - const count = await ProjectInvite.countDocuments({ projectId }).exec() - return count -} - async function getEditInviteCount(projectId) { logger.debug({ projectId }, 'counting edit invites for project') const count = await ProjectInvite.countDocuments({ @@ -48,7 +42,6 @@ async function getInviteByToken(projectId, tokenString) { module.exports = { promises: { getAllInvites, - getInviteCount, getEditInviteCount, getInviteByToken, }, diff --git a/services/web/app/src/Features/Subscription/LimitationsManager.js b/services/web/app/src/Features/Subscription/LimitationsManager.js index 717d641389..346bd0420a 100644 --- a/services/web/app/src/Features/Subscription/LimitationsManager.js +++ b/services/web/app/src/Features/Subscription/LimitationsManager.js @@ -38,18 +38,6 @@ async function canAcceptEditCollaboratorInvite(projectId) { return currentEditors + 1 <= allowedNumber } -async function canAddXCollaborators(projectId, numberOfNewCollaborators) { - const allowedNumber = await allowedNumberOfCollaboratorsInProject(projectId) - if (allowedNumber < 0) { - return true // -1 means unlimited - } - const currentNumber = - await CollaboratorsGetter.promises.getInvitedCollaboratorCount(projectId) - const inviteCount = - await CollaboratorsInvitesGetter.promises.getInviteCount(projectId) - return currentNumber + inviteCount + numberOfNewCollaborators <= allowedNumber -} - async function canAddXEditCollaborators( projectId, numberOfNewEditCollaborators @@ -133,7 +121,6 @@ const LimitationsManager = { allowedNumberOfCollaboratorsForUser: callbackify( allowedNumberOfCollaboratorsForUser ), - canAddXCollaborators: callbackify(canAddXCollaborators), canAddXEditCollaborators: callbackify(canAddXEditCollaborators), hasPaidSubscription: callbackifyMultiResult(hasPaidSubscription, [ 'hasPaidSubscription', @@ -162,7 +149,6 @@ const LimitationsManager = { allowedNumberOfCollaboratorsInProject, allowedNumberOfCollaboratorsForUser, canAcceptEditCollaboratorInvite, - canAddXCollaborators, canAddXEditCollaborators, hasPaidSubscription, userHasSubscriptionOrIsGroupMember, diff --git a/services/web/app/src/Features/TokenAccess/TokenAccessHandler.js b/services/web/app/src/Features/TokenAccess/TokenAccessHandler.js index f81168d3f8..06a5735e86 100644 --- a/services/web/app/src/Features/TokenAccess/TokenAccessHandler.js +++ b/services/web/app/src/Features/TokenAccess/TokenAccessHandler.js @@ -169,24 +169,6 @@ const TokenAccessHandler = { ).exec() }, - async addReadAndWriteUserToProject(userId, projectId) { - userId = new ObjectId(userId.toString()) - projectId = new ObjectId(projectId.toString()) - Analytics.recordEventForUserInBackground(userId, 'project-joined', { - mode: 'read-write', - projectId: projectId.toString(), - }) - - return await Project.updateOne( - { - _id: projectId, - }, - { - $addToSet: { tokenAccessReadAndWrite_refs: userId }, - } - ).exec() - }, - async removeReadAndWriteUserFromProject(userId, projectId) { userId = new ObjectId(userId.toString()) projectId = new ObjectId(projectId.toString()) diff --git a/services/web/test/unit/src/Collaborators/CollaboratorsInviteControllerTests.mjs b/services/web/test/unit/src/Collaborators/CollaboratorsInviteControllerTests.mjs index 1a6b0a5b63..53f49d0eb2 100644 --- a/services/web/test/unit/src/Collaborators/CollaboratorsInviteControllerTests.mjs +++ b/services/web/test/unit/src/Collaborators/CollaboratorsInviteControllerTests.mjs @@ -59,7 +59,6 @@ describe('CollaboratorsInviteController', function () { this.LimitationsManager = { promises: { allowedNumberOfCollaboratorsForUser: sinon.stub(), - canAddXCollaborators: sinon.stub().resolves(true), canAddXEditCollaborators: sinon.stub().resolves(true), }, } @@ -589,8 +588,8 @@ describe('CollaboratorsInviteController', function () { }) }) - it('should not have called canAddXCollaborators', function () { - this.LimitationsManager.promises.canAddXCollaborators.callCount.should.equal( + it('should not have called canAddXEditCollaborators', function () { + this.LimitationsManager.promises.canAddXEditCollaborators.callCount.should.equal( 0 ) }) diff --git a/services/web/test/unit/src/Collaborators/CollaboratorsInviteGetterTests.js b/services/web/test/unit/src/Collaborators/CollaboratorsInviteGetterTests.js index f031868c69..b190713f95 100644 --- a/services/web/test/unit/src/Collaborators/CollaboratorsInviteGetterTests.js +++ b/services/web/test/unit/src/Collaborators/CollaboratorsInviteGetterTests.js @@ -63,36 +63,6 @@ describe('CollaboratorsInviteGetter', function () { } }) - describe('getInviteCount', function () { - beforeEach(function () { - this.ProjectInvite.countDocuments.returns({ - exec: sinon.stub().resolves(2), - }) - this.call = async () => { - return await this.CollaboratorsInviteGetter.promises.getInviteCount( - this.projectId - ) - } - }) - - it('should produce the count of documents', async function () { - const count = await this.call() - expect(count).to.equal(2) - }) - - describe('when model.countDocuments produces an error', function () { - beforeEach(function () { - this.ProjectInvite.countDocuments.returns({ - exec: sinon.stub().rejects(new Error('woops')), - }) - }) - - it('should produce an error', async function () { - await expect(this.call()).to.be.rejectedWith(Error) - }) - }) - }) - describe('getEditInviteCount', function () { beforeEach(function () { this.ProjectInvite.countDocuments.returns({ diff --git a/services/web/test/unit/src/Subscription/LimitationsManagerTests.js b/services/web/test/unit/src/Subscription/LimitationsManagerTests.js index 694183547a..0ea71ed179 100644 --- a/services/web/test/unit/src/Subscription/LimitationsManagerTests.js +++ b/services/web/test/unit/src/Subscription/LimitationsManagerTests.js @@ -50,14 +50,12 @@ describe('LimitationsManager', function () { this.CollaboratorsGetter = { promises: { - getInvitedCollaboratorCount: sinon.stub().resolves(), getInvitedEditCollaboratorCount: sinon.stub().resolves(), }, } this.CollaboratorsInviteGetter = { promises: { - getInviteCount: sinon.stub().resolves(), getEditInviteCount: sinon.stub().resolves(), }, } @@ -222,175 +220,6 @@ describe('LimitationsManager', function () { }) }) - describe('canAddXCollaborators', function () { - describe('when the project has fewer collaborators than allowed', function () { - beforeEach(function (done) { - this.current_number = 1 - this.user.features.collaborators = 2 - this.invite_count = 0 - this.CollaboratorsGetter.promises.getInvitedCollaboratorCount = sinon - .stub() - .resolves(this.current_number) - this.CollaboratorsInviteGetter.promises.getInviteCount = sinon - .stub() - .resolves(this.invite_count) - this.callback = sinon.stub().callsFake(() => done()) - this.LimitationsManager.canAddXCollaborators( - this.projectId, - 1, - this.callback - ) - }) - - it('should return true', function () { - this.callback.calledWith(null, true).should.equal(true) - }) - }) - - describe('when the project has fewer collaborators and invites than allowed', function () { - beforeEach(function (done) { - this.current_number = 1 - this.user.features.collaborators = 4 - this.invite_count = 1 - this.CollaboratorsGetter.promises.getInvitedCollaboratorCount = sinon - .stub() - .resolves(this.current_number) - this.CollaboratorsInviteGetter.promises.getInviteCount = sinon - .stub() - .resolves(this.invite_count) - this.callback = sinon.stub().callsFake(() => done()) - this.LimitationsManager.canAddXCollaborators( - this.projectId, - 1, - this.callback - ) - }) - - it('should return true', function () { - this.callback.calledWith(null, true).should.equal(true) - }) - }) - - describe('when the project has fewer collaborators than allowed but I want to add more than allowed', function () { - beforeEach(function (done) { - this.current_number = 1 - this.user.features.collaborators = 2 - this.invite_count = 0 - this.CollaboratorsGetter.promises.getInvitedCollaboratorCount = sinon - .stub() - .resolves(this.current_number) - this.CollaboratorsInviteGetter.promises.getInviteCount = sinon - .stub() - .resolves(this.invite_count) - this.callback = sinon.stub().callsFake(() => done()) - this.LimitationsManager.canAddXCollaborators( - this.projectId, - 2, - this.callback - ) - }) - - it('should return false', function () { - this.callback.calledWith(null, false).should.equal(true) - }) - }) - - describe('when the project has more collaborators than allowed', function () { - beforeEach(function (done) { - this.current_number = 3 - this.user.features.collaborators = 2 - this.invite_count = 0 - this.CollaboratorsGetter.promises.getInvitedCollaboratorCount = sinon - .stub() - .resolves(this.current_number) - this.CollaboratorsInviteGetter.promises.getInviteCount = sinon - .stub() - .resolves(this.invite_count) - this.callback = sinon.stub().callsFake(() => done()) - this.LimitationsManager.canAddXCollaborators( - this.projectId, - 1, - this.callback - ) - }) - - it('should return false', function () { - this.callback.calledWith(null, false).should.equal(true) - }) - }) - - describe('when the project has infinite collaborators', function () { - beforeEach(function (done) { - this.current_number = 100 - this.user.features.collaborators = -1 - this.invite_count = 0 - this.CollaboratorsGetter.promises.getInvitedCollaboratorCount = sinon - .stub() - .resolves(this.current_number) - this.CollaboratorsInviteGetter.promises.getInviteCount = sinon - .stub() - .resolves(this.invite_count) - this.callback = sinon.stub().callsFake(() => done()) - this.LimitationsManager.canAddXCollaborators( - this.projectId, - 1, - this.callback - ) - }) - - it('should return true', function () { - this.callback.calledWith(null, true).should.equal(true) - }) - }) - - describe('when the project has more invites than allowed', function () { - beforeEach(function (done) { - this.current_number = 0 - this.user.features.collaborators = 2 - this.invite_count = 2 - this.CollaboratorsGetter.promises.getInvitedCollaboratorCount = sinon - .stub() - .resolves(this.current_number) - this.CollaboratorsInviteGetter.promises.getInviteCount = sinon - .stub() - .resolves(this.invite_count) - this.callback = sinon.stub().callsFake(() => done()) - this.LimitationsManager.canAddXCollaborators( - this.projectId, - 1, - this.callback - ) - }) - - it('should return false', function () { - this.callback.calledWith(null, false).should.equal(true) - }) - }) - - describe('when the project has more invites and collaborators than allowed', function () { - beforeEach(function (done) { - this.current_number = 1 - this.user.features.collaborators = 2 - this.invite_count = 1 - this.CollaboratorsGetter.promises.getInvitedCollaboratorCount = sinon - .stub() - .resolves(this.current_number) - this.CollaboratorsInviteGetter.promises.getInviteCount = sinon - .stub() - .resolves(this.invite_count) - this.callback = sinon.stub().callsFake(() => done()) - this.LimitationsManager.canAddXCollaborators( - this.projectId, - 1, - this.callback - ) - }) - - it('should return false', function () { - this.callback.calledWith(null, false).should.equal(true) - }) - }) - }) describe('canAddXEditCollaborators', function () { describe('when the project has fewer collaborators than allowed', function () { beforeEach(function (done) { diff --git a/services/web/test/unit/src/TokenAccess/TokenAccessControllerTests.mjs b/services/web/test/unit/src/TokenAccess/TokenAccessControllerTests.mjs index f8b43a2446..277330fdeb 100644 --- a/services/web/test/unit/src/TokenAccess/TokenAccessControllerTests.mjs +++ b/services/web/test/unit/src/TokenAccess/TokenAccessControllerTests.mjs @@ -44,7 +44,6 @@ describe('TokenAccessController', function () { makeTokenUrl: sinon.stub().returns('/'), grantSessionTokenAccess: sinon.stub(), promises: { - addReadAndWriteUserToProject: sinon.stub().resolves(), addReadOnlyUserToProject: sinon.stub().resolves(), getProjectByToken: sinon.stub().resolves(this.project), getV1DocPublishedInfo: sinon.stub().resolves({ allow: true }), diff --git a/services/web/test/unit/src/TokenAccess/TokenAccessHandlerTests.js b/services/web/test/unit/src/TokenAccess/TokenAccessHandlerTests.js index 1fffb4c3fb..8a587437f0 100644 --- a/services/web/test/unit/src/TokenAccess/TokenAccessHandlerTests.js +++ b/services/web/test/unit/src/TokenAccess/TokenAccessHandlerTests.js @@ -152,54 +152,6 @@ describe('TokenAccessHandler', function () { }) }) - describe('addReadAndWriteUserToProject', function () { - beforeEach(function () { - this.Project.updateOne = sinon - .stub() - .returns({ exec: sinon.stub().resolves(null) }) - }) - - it('should call Project.updateOne', async function () { - await this.TokenAccessHandler.promises.addReadAndWriteUserToProject( - this.userId, - this.projectId - ) - - expect(this.Project.updateOne.callCount).to.equal(1) - expect( - this.Project.updateOne.calledWith({ - _id: this.projectId, - }) - ).to.equal(true) - expect(this.Project.updateOne.lastCall.args[1].$addToSet).to.have.keys( - 'tokenAccessReadAndWrite_refs' - ) - sinon.assert.calledWith( - this.Analytics.recordEventForUserInBackground, - this.userId, - 'project-joined', - { mode: 'read-write', projectId: this.projectId.toString() } - ) - }) - - describe('when Project.updateOne produces an error', function () { - beforeEach(function () { - this.Project.updateOne = sinon - .stub() - .returns({ exec: sinon.stub().rejects(new Error('woops')) }) - }) - - it('should produce an error', async function () { - await expect( - this.TokenAccessHandler.promises.addReadAndWriteUserToProject( - this.userId, - this.projectId - ) - ).to.be.rejected - }) - }) - }) - describe('removeReadAndWriteUserFromProject', function () { beforeEach(function () { this.Project.updateOne = sinon