2020-06-23 04:45:38 -04:00
|
|
|
import _ from 'lodash'
|
2020-05-19 05:02:56 -04:00
|
|
|
import App from '../../../base'
|
2021-04-14 09:17:21 -04:00
|
|
|
App.controller('ShareProjectModalController', function (
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope,
|
|
|
|
$modalInstance,
|
|
|
|
$timeout,
|
|
|
|
projectMembers,
|
|
|
|
projectInvites,
|
|
|
|
$modal,
|
|
|
|
$http,
|
|
|
|
ide,
|
|
|
|
validateCaptcha,
|
|
|
|
validateCaptchaV3,
|
|
|
|
settings,
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
eventTracking
|
|
|
|
) {
|
|
|
|
$scope.inputs = {
|
|
|
|
privileges: 'readAndWrite',
|
|
|
|
contacts: []
|
|
|
|
}
|
|
|
|
$scope.state = {
|
|
|
|
error: null,
|
|
|
|
errorReason: null,
|
|
|
|
inflight: false,
|
|
|
|
startedFreeTrial: false,
|
|
|
|
invites: []
|
|
|
|
}
|
|
|
|
|
|
|
|
$modalInstance.opened.then(() =>
|
|
|
|
$timeout(() => $scope.$broadcast('open'), 200)
|
|
|
|
)
|
|
|
|
|
|
|
|
const INFINITE_COLLABORATORS = -1
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.refreshCanAddCollaborators = function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
const allowedNoOfMembers = $scope.project.features.collaborators
|
|
|
|
$scope.canAddCollaborators =
|
|
|
|
$scope.project.members.length + $scope.project.invites.length <
|
|
|
|
allowedNoOfMembers || allowedNoOfMembers === INFINITE_COLLABORATORS
|
|
|
|
}
|
|
|
|
$scope.refreshCanAddCollaborators()
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.$watch('canAddCollaborators', function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (!$scope.canAddCollaborators) {
|
|
|
|
eventTracking.send(
|
|
|
|
'subscription-funnel',
|
|
|
|
'editor-click-feature',
|
|
|
|
'projectMembers'
|
|
|
|
)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
$scope.$watch(
|
|
|
|
'(project.members.length + project.invites.length)',
|
|
|
|
_noOfMembers => $scope.refreshCanAddCollaborators()
|
|
|
|
)
|
|
|
|
|
|
|
|
$scope.autocompleteContacts = []
|
2020-07-24 04:54:55 -04:00
|
|
|
if ($scope.isRestrictedTokenMember) {
|
|
|
|
// Restricted token members are users who join via a read-only link.
|
|
|
|
// They will not be able to invite any users, so skip the lookup of
|
|
|
|
// their contacts. This request would result in a 403 for anonymous
|
|
|
|
// users, which in turn would redirect them to the /login.
|
|
|
|
} else {
|
|
|
|
$http.get('/user/contacts').then(processContactsResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
function processContactsResponse(response) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const { data } = response
|
|
|
|
$scope.autocompleteContacts = data.contacts || []
|
|
|
|
for (let contact of $scope.autocompleteContacts) {
|
|
|
|
if (contact.type === 'user') {
|
|
|
|
if (
|
|
|
|
contact.first_name === contact.email.split('@')[0] &&
|
|
|
|
!contact.last_name
|
|
|
|
) {
|
|
|
|
// User has not set their proper name so use email as canonical display property
|
|
|
|
contact.display = contact.email
|
|
|
|
} else {
|
|
|
|
contact.name = `${contact.first_name} ${contact.last_name}`
|
|
|
|
contact.display = `${contact.name} <${contact.email}>`
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Must be a group
|
|
|
|
contact.display = contact.name
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-07-24 04:54:55 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const getCurrentMemberEmails = () =>
|
|
|
|
($scope.project.members || []).map(u => u.email)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const getCurrentInviteEmails = () =>
|
|
|
|
($scope.project.invites || []).map(u => u.email)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.filterAutocompleteUsers = function ($query) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const currentMemberEmails = getCurrentMemberEmails()
|
2021-04-14 09:17:21 -04:00
|
|
|
return $scope.autocompleteContacts.filter(function (contact) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (
|
|
|
|
contact.email != null &&
|
|
|
|
currentMemberEmails.includes(contact.email)
|
|
|
|
) {
|
|
|
|
return false
|
2019-05-14 09:02:14 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
for (let text of [contact.name, contact.email]) {
|
|
|
|
if (
|
|
|
|
text != null &&
|
|
|
|
text.toLowerCase().indexOf($query.toLowerCase()) > -1
|
|
|
|
) {
|
|
|
|
return true
|
2019-10-15 09:10:56 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return false
|
2019-10-15 09:10:56 -04:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.addMembers = function () {
|
|
|
|
const addMembers = function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
if ($scope.inputs.contacts.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const members = $scope.inputs.contacts
|
|
|
|
$scope.inputs.contacts = []
|
|
|
|
$scope.clearError()
|
|
|
|
$scope.state.inflight = true
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if ($scope.project.invites == null) {
|
|
|
|
$scope.project.invites = []
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
|
|
|
const currentMemberEmails = getCurrentMemberEmails()
|
2020-05-19 05:02:56 -04:00
|
|
|
const currentInviteEmails = getCurrentInviteEmails()
|
|
|
|
addNextMember()
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
function addNextMember() {
|
|
|
|
let email
|
|
|
|
if (members.length === 0 || !$scope.canAddCollaborators) {
|
|
|
|
$scope.state.inflight = false
|
|
|
|
$scope.$apply()
|
2018-11-05 05:06:39 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const member = members.shift()
|
|
|
|
if (member.type === 'user') {
|
|
|
|
email = member.email
|
|
|
|
} else {
|
|
|
|
// Not an auto-complete object, so email == display
|
|
|
|
email = member.display
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
email = email.toLowerCase()
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (currentMemberEmails.includes(email)) {
|
|
|
|
// Skip this existing member
|
|
|
|
return addNextMember()
|
|
|
|
}
|
|
|
|
// do v3 captcha to collect data only
|
|
|
|
validateCaptchaV3('invite')
|
|
|
|
// do v2 captcha
|
|
|
|
const ExposedSettings = window.ExposedSettings
|
2021-04-14 09:17:21 -04:00
|
|
|
validateCaptcha(function (response) {
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.grecaptchaResponse = response
|
|
|
|
const invites = $scope.project.invites || []
|
|
|
|
const invite = _.find(invites, invite => invite.email === email)
|
|
|
|
let request
|
|
|
|
if (currentInviteEmails.includes(email) && invite) {
|
|
|
|
request = projectInvites.resendInvite(invite._id)
|
2018-11-05 05:06:39 -05:00
|
|
|
} else {
|
2020-05-19 05:02:56 -04:00
|
|
|
request = projectInvites.sendInvite(
|
|
|
|
email,
|
|
|
|
$scope.inputs.privileges,
|
|
|
|
$scope.grecaptchaResponse
|
|
|
|
)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
request
|
2021-04-14 09:17:21 -04:00
|
|
|
.then(function (response) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const { data } = response
|
|
|
|
if (data.error) {
|
|
|
|
$scope.setError(data.error)
|
|
|
|
$scope.state.inflight = false
|
|
|
|
} else {
|
|
|
|
if (data.invite) {
|
|
|
|
const { invite } = data
|
|
|
|
$scope.project.invites.push(invite)
|
2019-04-08 11:04:44 -04:00
|
|
|
} else {
|
2020-05-19 05:02:56 -04:00
|
|
|
const users =
|
|
|
|
data.users != null
|
|
|
|
? data.users
|
|
|
|
: data.user != null
|
2020-12-15 05:23:54 -05:00
|
|
|
? [data.user]
|
|
|
|
: []
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.project.members.push(...users)
|
2019-04-08 11:04:44 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
setTimeout(
|
|
|
|
() =>
|
|
|
|
// Give $scope a chance to update $scope.canAddCollaborators
|
|
|
|
// with new collaborator information.
|
|
|
|
addNextMember(),
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
0
|
|
|
|
)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
.catch(function (httpResponse) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const { data } = httpResponse
|
|
|
|
$scope.state.inflight = false
|
|
|
|
$scope.setError(data.errorReason)
|
|
|
|
})
|
|
|
|
}, ExposedSettings.recaptchaDisabled.invite)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$timeout(addMembers, 50) // Give email list a chance to update
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.removeMember = function (member) {
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.monitorRequest(
|
2021-04-14 09:17:21 -04:00
|
|
|
projectMembers.removeMember(member).then(function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
const index = $scope.project.members.indexOf(member)
|
|
|
|
if (index === -1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$scope.project.members.splice(index, 1)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.revokeInvite = function (invite) {
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.monitorRequest(
|
2021-04-14 09:17:21 -04:00
|
|
|
projectInvites.revokeInvite(invite._id).then(function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
const index = $scope.project.invites.indexOf(invite)
|
|
|
|
if (index === -1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$scope.project.invites.splice(index, 1)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.resendInvite = function (invite, event) {
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.monitorRequest(
|
|
|
|
projectInvites
|
|
|
|
.resendInvite(invite._id)
|
2021-04-14 09:17:21 -04:00
|
|
|
.then(function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
event.target.blur()
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
.catch(function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
event.target.blur()
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.makeTokenBased = function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.project.publicAccesLevel = 'tokenBased'
|
|
|
|
settings.saveProjectAdminSettings({ publicAccessLevel: 'tokenBased' })
|
|
|
|
eventTracking.sendMB('project-make-token-based')
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.makePrivate = function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.project.publicAccesLevel = 'private'
|
|
|
|
settings.saveProjectAdminSettings({ publicAccessLevel: 'private' })
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.$watch('project.tokens.readAndWrite', function (token) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (token != null) {
|
|
|
|
$scope.readAndWriteTokenLink = `${location.origin}/${token}`
|
|
|
|
} else {
|
|
|
|
$scope.readAndWriteTokenLink = null
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.$watch('project.tokens.readOnly', function (token) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (token != null) {
|
|
|
|
$scope.readOnlyTokenLink = `${location.origin}/read/${token}`
|
|
|
|
} else {
|
|
|
|
$scope.readOnlyTokenLink = null
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.done = () => $modalInstance.close()
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.cancel = () => $modalInstance.dismiss()
|
2019-10-15 09:10:56 -04:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.monitorRequest = function monitorRequest(request) {
|
|
|
|
$scope.clearError()
|
|
|
|
$scope.state.inflight = true
|
|
|
|
return request
|
|
|
|
.then(() => {
|
|
|
|
$scope.state.inflight = false
|
|
|
|
$scope.clearError()
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
$scope.state.inflight = false
|
|
|
|
$scope.setError(err.data && err.data.error)
|
|
|
|
})
|
|
|
|
}
|
2019-10-15 09:10:56 -04:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.clearError = function clearError() {
|
|
|
|
$scope.state.error = false
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.setError = function setError(reason) {
|
|
|
|
$scope.state.error = true
|
|
|
|
$scope.state.errorReason = reason
|
|
|
|
}
|
2019-10-15 09:10:56 -04:00
|
|
|
})
|