| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- const NotificationsHandler = require('./NotificationsHandler')
- const { promisifyAll } = require('@overleaf/promise-utils')
- const request = require('request')
- const settings = require('@overleaf/settings')
- function dropboxDuplicateProjectNames(userId) {
- return {
- key: `dropboxDuplicateProjectNames-${userId}`,
- create(projectName, callback) {
- if (callback == null) {
- callback = function () {}
- }
- NotificationsHandler.createNotification(
- userId,
- this.key,
- 'notification_dropbox_duplicate_project_names',
- { projectName },
- null,
- true,
- callback
- )
- },
- read(callback) {
- if (callback == null) {
- callback = function () {}
- }
- NotificationsHandler.markAsReadWithKey(userId, this.key, callback)
- },
- }
- }
- function dropboxUnlinkedDueToLapsedReconfirmation(userId) {
- return {
- key: 'drobox-unlinked-due-to-lapsed-reconfirmation',
- create(callback) {
- NotificationsHandler.createNotification(
- userId,
- this.key,
- 'notification_dropbox_unlinked_due_to_lapsed_reconfirmation',
- {},
- null,
- true,
- callback
- )
- },
- read(callback) {
- NotificationsHandler.markAsReadWithKey(userId, this.key, callback)
- },
- }
- }
- function featuresUpgradedByAffiliation(affiliation, user) {
- return {
- key: `features-updated-by=${affiliation.institutionId}`,
- create(callback) {
- if (callback == null) {
- callback = function () {}
- }
- const messageOpts = { institutionName: affiliation.institutionName }
- NotificationsHandler.createNotification(
- user._id,
- this.key,
- 'notification_features_upgraded_by_affiliation',
- messageOpts,
- null,
- false,
- callback
- )
- },
- read(callback) {
- if (callback == null) {
- callback = function () {}
- }
- NotificationsHandler.markAsReadWithKey(user._id, this.key, callback)
- },
- }
- }
- function redundantPersonalSubscription(affiliation, user) {
- return {
- key: `redundant-personal-subscription-${affiliation.institutionId}`,
- create(callback) {
- if (callback == null) {
- callback = function () {}
- }
- const messageOpts = { institutionName: affiliation.institutionName }
- NotificationsHandler.createNotification(
- user._id,
- this.key,
- 'notification_personal_subscription_not_required_due_to_affiliation',
- messageOpts,
- null,
- false,
- callback
- )
- },
- read(callback) {
- if (callback == null) {
- callback = function () {}
- }
- NotificationsHandler.markAsReadWithKey(user._id, this.key, callback)
- },
- }
- }
- function projectInvite(invite, project, sendingUser, user) {
- return {
- key: `project-invite-${invite._id}`,
- create(callback) {
- if (callback == null) {
- callback = function () {}
- }
- const messageOpts = {
- userName: sendingUser.first_name,
- projectName: project.name,
- projectId: project._id.toString(),
- token: invite.token,
- }
- NotificationsHandler.createNotification(
- user._id,
- this.key,
- 'notification_project_invite',
- messageOpts,
- invite.expires,
- callback
- )
- },
- read(callback) {
- if (callback == null) {
- callback = function () {}
- }
- NotificationsHandler.markAsReadByKeyOnly(this.key, callback)
- },
- }
- }
- function ipMatcherAffiliation(userId) {
- return {
- create(ip, callback) {
- if (callback == null) {
- callback = function () {}
- }
- if (!settings.apis.v1.url) {
- // service is not configured
- return callback()
- }
- 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,
- },
- function (error, response, body) {
- if (error != null) {
- return callback(error)
- }
- if (response.statusCode !== 200) {
- return 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,
- content: body.enrolment_ad_html,
- portalPath,
- ssoEnabled: body.sso_enabled,
- }
- NotificationsHandler.createNotification(
- userId,
- key,
- 'notification_ip_matched_affiliation',
- messageOpts,
- null,
- false,
- callback
- )
- }
- )
- },
- read(universityId, callback) {
- if (callback == null) {
- callback = function () {}
- }
- const key = `ip-matched-affiliation-${universityId}`
- NotificationsHandler.markAsReadWithKey(userId, key, callback)
- },
- }
- }
- function tpdsFileLimit(userId) {
- return {
- key: `tpdsFileLimit-${userId}`,
- create(projectName, callback) {
- if (callback == null) {
- callback = function () {}
- }
- const messageOpts = {
- projectName,
- }
- NotificationsHandler.createNotification(
- userId,
- this.key,
- 'notification_tpds_file_limit',
- messageOpts,
- null,
- true,
- callback
- )
- },
- read(callback) {
- if (callback == null) {
- callback = function () {}
- }
- NotificationsHandler.markAsReadByKeyOnly(this.key, callback)
- },
- }
- }
- function groupInvitation(userId, subscriptionId, managedUsersEnabled) {
- return {
- key: `groupInvitation-${subscriptionId}-${userId}`,
- create(invite, callback) {
- if (callback == null) {
- callback = function () {}
- }
- const messageOpts = {
- token: invite.token,
- inviterName: invite.inviterName,
- managedUsersEnabled,
- }
- NotificationsHandler.createNotification(
- userId,
- this.key,
- 'notification_group_invitation',
- messageOpts,
- null,
- true,
- callback
- )
- },
- read(callback) {
- if (callback == null) {
- callback = function () {}
- }
- NotificationsHandler.markAsReadByKeyOnly(this.key, callback)
- },
- }
- }
- function personalAndGroupSubscriptions(userId) {
- return {
- key: 'personal-and-group-subscriptions',
- create(callback) {
- if (callback == null) {
- callback = function () {}
- }
- NotificationsHandler.createNotification(
- userId,
- this.key,
- 'notification_personal_and_group_subscriptions',
- {},
- null,
- false,
- callback
- )
- },
- read(callback) {
- if (callback == null) {
- callback = function () {}
- }
- NotificationsHandler.markAsReadByKeyOnly(this.key, callback)
- },
- }
- }
- function ieeeCollabratecRetirement(userId) {
- return {
- key: 'notification-ieee-collabratec-retirement',
- create(callback) {
- NotificationsHandler.createNotification(
- userId,
- this.key,
- 'notification_ieee_collabratec_retirement',
- {},
- new Date('2024-08-01'),
- false,
- callback
- )
- },
- read(callback) {
- NotificationsHandler.markAsReadByKeyOnly(this.key, callback)
- },
- }
- }
- const NotificationsBuilder = {
- // Note: notification keys should be url-safe
- dropboxUnlinkedDueToLapsedReconfirmation,
- dropboxDuplicateProjectNames,
- featuresUpgradedByAffiliation,
- redundantPersonalSubscription,
- projectInvite,
- ipMatcherAffiliation,
- tpdsFileLimit,
- groupInvitation,
- personalAndGroupSubscriptions,
- ieeeCollabratecRetirement,
- }
- NotificationsBuilder.promises = {
- dropboxUnlinkedDueToLapsedReconfirmation: function (userId) {
- return promisifyAll(dropboxUnlinkedDueToLapsedReconfirmation(userId))
- },
- redundantPersonalSubscription: function (affiliation, user) {
- return promisifyAll(redundantPersonalSubscription(affiliation, user))
- },
- dropboxDuplicateProjectNames(userId) {
- return promisifyAll(dropboxDuplicateProjectNames(userId))
- },
- ipMatcherAffiliation: function (userId) {
- return promisifyAll(ipMatcherAffiliation(userId))
- },
- groupInvitation: function (userId, groupId, managedUsersEnabled) {
- return promisifyAll(groupInvitation(userId, groupId, managedUsersEnabled))
- },
- projectInvite(invite, project, sendingUser, user) {
- return promisifyAll(projectInvite(invite, project, sendingUser, user))
- },
- personalAndGroupSubscriptions(userId) {
- return promisifyAll(personalAndGroupSubscriptions(userId))
- },
- ieeeCollabratecRetirement(userId) {
- return promisifyAll(ieeeCollabratecRetirement(userId))
- },
- }
- module.exports = NotificationsBuilder
|