|
|
@@ -1,30 +1,28 @@
|
|
|
const NotificationsHandler = require('./NotificationsHandler')
|
|
|
-const { promisifyAll } = require('@overleaf/promise-utils')
|
|
|
-const request = require('request')
|
|
|
+const { callbackifyAll } = require('@overleaf/promise-utils')
|
|
|
+const { fetchJson } = require('@overleaf/fetch-utils')
|
|
|
const settings = require('@overleaf/settings')
|
|
|
+const path = require('path')
|
|
|
+const { ObjectId } = require('mongodb-legacy')
|
|
|
|
|
|
function dropboxDuplicateProjectNames(userId) {
|
|
|
return {
|
|
|
key: `dropboxDuplicateProjectNames-${userId}`,
|
|
|
- create(projectName, callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
- NotificationsHandler.createNotification(
|
|
|
+ async create(projectName) {
|
|
|
+ return await NotificationsHandler.promises.createNotification(
|
|
|
userId,
|
|
|
this.key,
|
|
|
'notification_dropbox_duplicate_project_names',
|
|
|
{ projectName },
|
|
|
null,
|
|
|
- true,
|
|
|
- callback
|
|
|
+ true
|
|
|
)
|
|
|
},
|
|
|
- read(callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
- NotificationsHandler.markAsReadWithKey(userId, this.key, callback)
|
|
|
+ async read() {
|
|
|
+ return await NotificationsHandler.promises.markAsReadWithKey(
|
|
|
+ userId,
|
|
|
+ this.key
|
|
|
+ )
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
@@ -32,19 +30,21 @@ function dropboxDuplicateProjectNames(userId) {
|
|
|
function dropboxUnlinkedDueToLapsedReconfirmation(userId) {
|
|
|
return {
|
|
|
key: 'drobox-unlinked-due-to-lapsed-reconfirmation',
|
|
|
- create(callback) {
|
|
|
- NotificationsHandler.createNotification(
|
|
|
+ async create() {
|
|
|
+ return await NotificationsHandler.promises.createNotification(
|
|
|
userId,
|
|
|
this.key,
|
|
|
'notification_dropbox_unlinked_due_to_lapsed_reconfirmation',
|
|
|
{},
|
|
|
null,
|
|
|
- true,
|
|
|
- callback
|
|
|
+ true
|
|
|
)
|
|
|
},
|
|
|
- read(callback) {
|
|
|
- NotificationsHandler.markAsReadWithKey(userId, this.key, callback)
|
|
|
+ async read() {
|
|
|
+ return await NotificationsHandler.promises.markAsReadWithKey(
|
|
|
+ userId,
|
|
|
+ this.key
|
|
|
+ )
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
@@ -52,26 +52,22 @@ function dropboxUnlinkedDueToLapsedReconfirmation(userId) {
|
|
|
function featuresUpgradedByAffiliation(affiliation, user) {
|
|
|
return {
|
|
|
key: `features-updated-by=${affiliation.institutionId}`,
|
|
|
- create(callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
+ async create() {
|
|
|
const messageOpts = { institutionName: affiliation.institutionName }
|
|
|
- NotificationsHandler.createNotification(
|
|
|
+ return await NotificationsHandler.promises.createNotification(
|
|
|
user._id,
|
|
|
this.key,
|
|
|
'notification_features_upgraded_by_affiliation',
|
|
|
messageOpts,
|
|
|
null,
|
|
|
- false,
|
|
|
- callback
|
|
|
+ false
|
|
|
)
|
|
|
},
|
|
|
- read(callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
- NotificationsHandler.markAsReadWithKey(user._id, this.key, callback)
|
|
|
+ async read() {
|
|
|
+ return await NotificationsHandler.promises.markAsReadWithKey(
|
|
|
+ user._id,
|
|
|
+ this.key
|
|
|
+ )
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
@@ -79,26 +75,22 @@ function featuresUpgradedByAffiliation(affiliation, user) {
|
|
|
function redundantPersonalSubscription(affiliation, user) {
|
|
|
return {
|
|
|
key: `redundant-personal-subscription-${affiliation.institutionId}`,
|
|
|
- create(callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
+ async create() {
|
|
|
const messageOpts = { institutionName: affiliation.institutionName }
|
|
|
- NotificationsHandler.createNotification(
|
|
|
+ return await NotificationsHandler.promises.createNotification(
|
|
|
user._id,
|
|
|
this.key,
|
|
|
'notification_personal_subscription_not_required_due_to_affiliation',
|
|
|
messageOpts,
|
|
|
null,
|
|
|
- false,
|
|
|
- callback
|
|
|
+ false
|
|
|
)
|
|
|
},
|
|
|
- read(callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
- NotificationsHandler.markAsReadWithKey(user._id, this.key, callback)
|
|
|
+ async read() {
|
|
|
+ return await NotificationsHandler.promises.markAsReadWithKey(
|
|
|
+ user._id,
|
|
|
+ this.key
|
|
|
+ )
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
@@ -106,90 +98,74 @@ function redundantPersonalSubscription(affiliation, user) {
|
|
|
function projectInvite(invite, project, sendingUser, user) {
|
|
|
return {
|
|
|
key: `project-invite-${invite._id}`,
|
|
|
- create(callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
+ async create() {
|
|
|
const messageOpts = {
|
|
|
userName: sendingUser.first_name,
|
|
|
projectName: project.name,
|
|
|
projectId: project._id.toString(),
|
|
|
token: invite.token,
|
|
|
}
|
|
|
- NotificationsHandler.createNotification(
|
|
|
+ return await NotificationsHandler.promises.createNotification(
|
|
|
user._id,
|
|
|
this.key,
|
|
|
'notification_project_invite',
|
|
|
messageOpts,
|
|
|
- invite.expires,
|
|
|
- callback
|
|
|
+ invite.expires
|
|
|
)
|
|
|
},
|
|
|
- read(callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
- NotificationsHandler.markAsReadByKeyOnly(this.key, callback)
|
|
|
+ async read() {
|
|
|
+ return await NotificationsHandler.promises.markAsReadByKeyOnly(this.key)
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
|
|
|
function ipMatcherAffiliation(userId) {
|
|
|
return {
|
|
|
- create(ip, callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
+ async create(ip) {
|
|
|
if (!settings.apis.v1.url) {
|
|
|
// service is not configured
|
|
|
- return callback()
|
|
|
+ return
|
|
|
}
|
|
|
- request(
|
|
|
- {
|
|
|
- method: 'GET',
|
|
|
- url: `${settings.apis.v1.url}/api/v2/users/${userId}/ip_matcher`,
|
|
|
- auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass },
|
|
|
- body: { ip },
|
|
|
- json: true,
|
|
|
- timeout: settings.apis.v1.timeout,
|
|
|
+ if (!ObjectId.isValid(userId)) {
|
|
|
+ throw new Error('invalid user id')
|
|
|
+ }
|
|
|
+ const url = new URL(settings.apis.v1.url)
|
|
|
+ url.pathname = path.posix.join('/api/v2/users', userId, 'ip_matcher')
|
|
|
+ url.searchParams.set('ip', ip)
|
|
|
+
|
|
|
+ const body = await fetchJson(url, {
|
|
|
+ method: 'GET',
|
|
|
+ basicAuth: {
|
|
|
+ user: settings.apis.v1.user,
|
|
|
+ password: settings.apis.v1.pass,
|
|
|
},
|
|
|
- function (error, response, body) {
|
|
|
- if (error != null) {
|
|
|
- return callback(error)
|
|
|
- }
|
|
|
- if (response.statusCode !== 200 || !body) {
|
|
|
- return callback()
|
|
|
- }
|
|
|
+ signal: AbortSignal.timeout(settings.apis.v1.timeout || 10_000),
|
|
|
+ })
|
|
|
+ if (!body) return
|
|
|
|
|
|
- const key = `ip-matched-affiliation-${body.id}`
|
|
|
- const portalPath = body.portal_slug
|
|
|
- ? `/${body.is_university ? 'edu' : 'org'}/${body.portal_slug}`
|
|
|
- : undefined
|
|
|
- const messageOpts = {
|
|
|
- university_name: body.name,
|
|
|
- institutionId: body.id,
|
|
|
- portalPath,
|
|
|
- ssoEnabled: body.sso_enabled,
|
|
|
- }
|
|
|
- NotificationsHandler.createNotification(
|
|
|
- userId,
|
|
|
- key,
|
|
|
- 'notification_ip_matched_affiliation',
|
|
|
- messageOpts,
|
|
|
- null,
|
|
|
- false,
|
|
|
- callback
|
|
|
- )
|
|
|
- }
|
|
|
+ const key = `ip-matched-affiliation-${body.id}`
|
|
|
+ const portalPath = body.portal_slug
|
|
|
+ ? `/${body.is_university ? 'edu' : 'org'}/${body.portal_slug}`
|
|
|
+ : undefined
|
|
|
+ const messageOpts = {
|
|
|
+ university_name: body.name,
|
|
|
+ institutionId: body.id,
|
|
|
+ portalPath,
|
|
|
+ ssoEnabled: body.sso_enabled,
|
|
|
+ }
|
|
|
+ return await NotificationsHandler.promises.createNotification(
|
|
|
+ userId,
|
|
|
+ key,
|
|
|
+ 'notification_ip_matched_affiliation',
|
|
|
+ messageOpts,
|
|
|
+ null,
|
|
|
+ false
|
|
|
)
|
|
|
},
|
|
|
|
|
|
- read(universityId, callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
+ async read(universityId) {
|
|
|
const key = `ip-matched-affiliation-${universityId}`
|
|
|
- NotificationsHandler.markAsReadWithKey(userId, key, callback)
|
|
|
+ return await NotificationsHandler.promises.markAsReadWithKey(userId, key)
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
@@ -197,29 +173,22 @@ function ipMatcherAffiliation(userId) {
|
|
|
function tpdsFileLimit(userId) {
|
|
|
return {
|
|
|
key: `tpdsFileLimit-${userId}`,
|
|
|
- create(projectName, projectId, callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
+ async create(projectName, projectId) {
|
|
|
const messageOpts = {
|
|
|
projectName,
|
|
|
projectId,
|
|
|
}
|
|
|
- NotificationsHandler.createNotification(
|
|
|
+ return await NotificationsHandler.promises.createNotification(
|
|
|
userId,
|
|
|
this.key,
|
|
|
'notification_tpds_file_limit',
|
|
|
messageOpts,
|
|
|
null,
|
|
|
- true,
|
|
|
- callback
|
|
|
+ true
|
|
|
)
|
|
|
},
|
|
|
- read(callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
- NotificationsHandler.markAsReadByKeyOnly(this.key, callback)
|
|
|
+ async read() {
|
|
|
+ return await NotificationsHandler.promises.markAsReadByKeyOnly(this.key)
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
@@ -227,30 +196,23 @@ function tpdsFileLimit(userId) {
|
|
|
function groupInvitation(userId, subscriptionId, managedUsersEnabled) {
|
|
|
return {
|
|
|
key: `groupInvitation-${subscriptionId}-${userId}`,
|
|
|
- create(invite, callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
+ async create(invite) {
|
|
|
const messageOpts = {
|
|
|
token: invite.token,
|
|
|
inviterName: invite.inviterName,
|
|
|
managedUsersEnabled,
|
|
|
}
|
|
|
- NotificationsHandler.createNotification(
|
|
|
+ return await NotificationsHandler.promises.createNotification(
|
|
|
userId,
|
|
|
this.key,
|
|
|
'notification_group_invitation',
|
|
|
messageOpts,
|
|
|
null,
|
|
|
- true,
|
|
|
- callback
|
|
|
+ true
|
|
|
)
|
|
|
},
|
|
|
- read(callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
- NotificationsHandler.markAsReadByKeyOnly(this.key, callback)
|
|
|
+ async read() {
|
|
|
+ return await NotificationsHandler.promises.markAsReadByKeyOnly(this.key)
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
@@ -258,67 +220,63 @@ function groupInvitation(userId, subscriptionId, managedUsersEnabled) {
|
|
|
function personalAndGroupSubscriptions(userId) {
|
|
|
return {
|
|
|
key: 'personal-and-group-subscriptions',
|
|
|
- create(callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
- NotificationsHandler.createNotification(
|
|
|
+ async create() {
|
|
|
+ return await NotificationsHandler.promises.createNotification(
|
|
|
userId,
|
|
|
this.key,
|
|
|
'notification_personal_and_group_subscriptions',
|
|
|
{},
|
|
|
null,
|
|
|
- false,
|
|
|
- callback
|
|
|
+ false
|
|
|
)
|
|
|
},
|
|
|
- read(callback) {
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
- NotificationsHandler.markAsReadByKeyOnly(this.key, callback)
|
|
|
+ async read() {
|
|
|
+ return await NotificationsHandler.promises.markAsReadByKeyOnly(this.key)
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
|
|
|
const NotificationsBuilder = {
|
|
|
// Note: notification keys should be url-safe
|
|
|
- dropboxUnlinkedDueToLapsedReconfirmation,
|
|
|
- dropboxDuplicateProjectNames,
|
|
|
- featuresUpgradedByAffiliation,
|
|
|
- redundantPersonalSubscription,
|
|
|
- projectInvite,
|
|
|
- ipMatcherAffiliation,
|
|
|
- tpdsFileLimit,
|
|
|
- groupInvitation,
|
|
|
- personalAndGroupSubscriptions,
|
|
|
-}
|
|
|
-
|
|
|
-NotificationsBuilder.promises = {
|
|
|
- dropboxUnlinkedDueToLapsedReconfirmation: function (userId) {
|
|
|
- return promisifyAll(dropboxUnlinkedDueToLapsedReconfirmation(userId))
|
|
|
- },
|
|
|
- redundantPersonalSubscription: function (affiliation, user) {
|
|
|
- return promisifyAll(redundantPersonalSubscription(affiliation, user))
|
|
|
+ dropboxUnlinkedDueToLapsedReconfirmation(userId) {
|
|
|
+ return callbackifyAll(dropboxUnlinkedDueToLapsedReconfirmation(userId))
|
|
|
},
|
|
|
dropboxDuplicateProjectNames(userId) {
|
|
|
- return promisifyAll(dropboxDuplicateProjectNames(userId))
|
|
|
- },
|
|
|
- featuresUpgradedByAffiliation: function (affiliation, user) {
|
|
|
- return promisifyAll(featuresUpgradedByAffiliation(affiliation, user))
|
|
|
+ return callbackifyAll(dropboxDuplicateProjectNames(userId))
|
|
|
},
|
|
|
- ipMatcherAffiliation: function (userId) {
|
|
|
- return promisifyAll(ipMatcherAffiliation(userId))
|
|
|
+ featuresUpgradedByAffiliation(affiliation, user) {
|
|
|
+ return callbackifyAll(featuresUpgradedByAffiliation(affiliation, user))
|
|
|
},
|
|
|
- groupInvitation: function (userId, groupId, managedUsersEnabled) {
|
|
|
- return promisifyAll(groupInvitation(userId, groupId, managedUsersEnabled))
|
|
|
+ redundantPersonalSubscription(affiliation, user) {
|
|
|
+ return callbackifyAll(redundantPersonalSubscription(affiliation, user))
|
|
|
},
|
|
|
projectInvite(invite, project, sendingUser, user) {
|
|
|
- return promisifyAll(projectInvite(invite, project, sendingUser, user))
|
|
|
+ return callbackifyAll(projectInvite(invite, project, sendingUser, user))
|
|
|
+ },
|
|
|
+ ipMatcherAffiliation(userId) {
|
|
|
+ return callbackifyAll(ipMatcherAffiliation(userId))
|
|
|
+ },
|
|
|
+ tpdsFileLimit(userId) {
|
|
|
+ return callbackifyAll(tpdsFileLimit(userId))
|
|
|
+ },
|
|
|
+ groupInvitation(userId, groupId, managedUsersEnabled) {
|
|
|
+ return callbackifyAll(groupInvitation(userId, groupId, managedUsersEnabled))
|
|
|
},
|
|
|
personalAndGroupSubscriptions(userId) {
|
|
|
- return promisifyAll(personalAndGroupSubscriptions(userId))
|
|
|
+ return callbackifyAll(personalAndGroupSubscriptions(userId))
|
|
|
},
|
|
|
}
|
|
|
|
|
|
+NotificationsBuilder.promises = {
|
|
|
+ dropboxUnlinkedDueToLapsedReconfirmation,
|
|
|
+ redundantPersonalSubscription,
|
|
|
+ dropboxDuplicateProjectNames,
|
|
|
+ featuresUpgradedByAffiliation,
|
|
|
+ ipMatcherAffiliation,
|
|
|
+ groupInvitation,
|
|
|
+ projectInvite,
|
|
|
+ personalAndGroupSubscriptions,
|
|
|
+ tpdsFileLimit,
|
|
|
+}
|
|
|
+
|
|
|
module.exports = NotificationsBuilder
|