@ -1,9 +1,11 @@
// dtp-sites -cli.js
// Copyright (C) 2021 Digital Telepresence , LLC
// dtp-webapp -cli.js
// Copryright (C) DTP Technologies , LLC
// License: Apache-2.0
'use strict' ;
const DTP_COMPONENT_NAME = 'webapp-cli' ;
require ( 'dotenv' ) . config ( ) ;
const path = require ( 'path' ) ;
@ -16,17 +18,12 @@ const {
SiteLog ,
} = require ( path . join ( __ dirname , 'lib' , 'site-lib' ) ) ;
module . pkg = require ( path . join ( __ dirname , 'package.json' ) ) ;
module . rootPath = __ dirname ;
module . pkg = require ( path . join ( module . rootPath , 'package.json' ) ) ;
module . config = {
componentName : 'sites-cli' ,
root : __ dirname ,
site : {
name : process . env . DTP_SITE_NAME ,
description : process . env . DTP_SITE_DESCRIPTION ,
domain : process . env . DTP_SITE_DOMAIN ,
domainKey : process . env . DTP_SITE_DOMAIN_KEY ,
company : process . env . DTP_SITE_COMPANY || 'Digital Telepresence, LLC' ,
} ,
componentName : DTP_COMPONENT_NAME ,
root : module . rootPath ,
site : require ( path . join ( module . rootPath , 'config' , 'site' ) ) ,
} ;
module . log = new SiteLog ( module , module . config . componentName ) ;
@ -79,12 +76,6 @@ module.revokePermission = async (target, permission) => {
}
} ;
module . dvrIngest = async ( episodeId ) => {
const jobQueue = module . services . jobQueue . getJobQueue ( 'dvr-ingest' , module . config . jobQueues [ 'dvr-ingest' ] ) ;
const job = await jobQueue . add ( { episodeId } ) ;
module . log . info ( 'job created' , { id : job . id } ) ;
} ;
module . deleteOtpAccount = async ( target ) => {
const { otpAuth : otpAuthService } = module . services ;
const User = mongoose . model ( 'User' ) ;
@ -116,6 +107,67 @@ module.resetIndexes = async ( ) => {
}
} ;
module . resetPassword = async ( username ) => {
const { user : userService } = module . services ;
const User = mongoose . model ( 'User' ) ;
const user = await User
. findOne ( {
username_lc : username . trim ( ) . toLowerCase ( ) ,
} )
. lean ( ) ;
if ( ! user ) {
throw new Error ( 'user not found' ) ;
}
const password = generatePassword ( 8 , true ) ;
module . log . info ( 'updating user password' , { username , password } ) ;
await userService . updatePassword ( user , password ) ;
} ;
module . sendWelcomeEmail = async ( username ) => {
const User = mongoose . model ( 'User' ) ;
if ( username === 'all' ) {
await User
. find ( )
. select ( '+flags +optIn' )
. cursor ( )
. eachAsync ( module . sendUserWelcomeEmail , 4 ) ;
} else {
const user = await User
. findOne ( {
username_lc : username . trim ( ) . toLowerCase ( ) ,
} )
. select ( '+flags +optIn' )
. lean ( ) ;
if ( ! user ) {
throw new Error ( 'user not found' ) ;
}
await module . sendUserWelcomeEmail ( user ) ;
}
} ;
module . sendUserWelcomeEmail = async ( user ) => {
const { user : userService } = module . services ;
if ( user . flags . isEmailVerified ) {
module . log . alert ( 'user is email-verified (aboring welcome email)' , {
user : user . _ id ,
username : user . username ,
} ) ;
return ;
}
module . log . info ( 'sending welcome email' , {
user : user . _ id ,
username : user . username ,
email : user . email ,
} ) ;
await userService . sendWelcomeEmail ( user ) ;
} ;
/ *
* SERVER INIT
* /
@ -130,21 +182,21 @@ module.resetIndexes = async ( ) => {
short : 'a' ,
type : 'string' ,
description : 'The action to perform' ,
example : 'sites -cli --action=grant --permission=moderator email email ...' ,
example : 'dtp-webapp -cli --action=grant --permission=moderator email email ...' ,
} ,
{
name : 'permission' ,
short : 'p' ,
type : 'string' ,
description : 'The permission(s) being added or removed' ,
example : 'sites -cli --action=grant --permission=admin email email ...'
example : 'dtp-webapp -cli --action=grant --permission=admin email email ...'
} ,
] ) ;
try {
await SitePlatform . startPlatform ( module ) ;
} catch ( error ) {
module . log . error ( 'failed to start DTP Sites platform' , { error } ) ;
module . log . error ( ` failed to start DTP ${ DTP_COMPONENT_NAME } platform ` , { error } ) ;
return ;
}
@ -162,10 +214,6 @@ module.resetIndexes = async ( ) => {
await module . revokePermission ( target , module . app . options . permission ) ;
break ;
case 'dvr-ingest' :
await module . dvrIngest ( target ) ;
break ;
case 'make-bucket' :
await module . makeBucket ( target ) ;
break ;
@ -174,10 +222,18 @@ module.resetIndexes = async ( ) => {
await module . deleteOtpAccount ( target ) ;
break ;
case 'reset-password' :
await module . resetPassword ( target ) ;
break ;
case 'reset-indexes' :
await module . resetIndexes ( ) ;
break ;
case 'welcome-email' :
await module . sendWelcomeEmail ( target ) ;
break ;
default :
throw new Error ( ` invalid action: ${ module . app . options . action } ` ) ;
}