lib/qpm_media.js
function getRandomUnseenImage(params, config, conn) {

  // defaults
  config = config || require('../config/config.js')

  params = params || {}

  var max = config.db.max || 0
  var optimization = config.optimization || 0
  var offset = Math.floor(Math.random() * max)

  params.optimization = optimization
  params.offset = offset

  // main
  return new Promise((resolve, reject) => {

    if (!conn) {
      var conn = wc_db.getConnection(config.db)
    }

    var safeClause = ''
    if (params.safe !== undefined && params.safe !== null) {
      if (parseInt(params.safe) === 0) {
        safeClause = ''
      } else {
        safeClause = ' and a.safe = :safe '
      }
    } else {
      safeClause = ' and a.safe = 1 '
    }

    if (params.reviewer) {
      var sql = "SELECT * from Media m where m.uri not in (select r.uri from Rating r join User u on u.id = r.reviewer_id) and contentType = 'image' and lastSeen is NULL " + safeClause + " and FLOOR( m.id / :optimization ) = FLOOR( RAND() * :optimization ) and m.id >= :offset LIMIT 1;"
    } else {
      var sql = "SELECT * from Media m where contentType = 'image' and lastSeen is NULL order by RAND() LIMIT 1;"
    }

    debug('getRandomUnseenImage', sql, params)

    conn.query(sql, { replacements: { "optimization" : params.optimization, "offset" : params.offset } }).then(function(ret){
      return resolve({"ret" : ret, "conn" : conn})
    }).catch(function(err) {
      return reject({"err" : err, "conn" : conn})
    })

  })

}
function getRandomUnseenImage(params, config, conn) {

  // defaults
  config = config || require('../config/config.js')

  params = params || {}

  var max = config.db.max || 0
  var optimization = config.optimization || 0
  var offset = Math.floor(Math.random() * max)

  params.optimization = optimization
  params.offset = offset

  // main
  return new Promise((resolve, reject) => {

    if (!conn) {
      var conn = wc_db.getConnection(config.db)
    }

    var safeClause = ''
    if (params.safe !== undefined && params.safe !== null) {
      if (parseInt(params.safe) === 0) {
        safeClause = ''
      } else {
        safeClause = ' and a.safe = :safe '
      }
    } else {
      safeClause = ' and a.safe = 1 '
    }

    if (params.reviewer) {
      var sql = "SELECT * from Media m where m.uri not in (select r.uri from Rating r join User u on u.id = r.reviewer_id) and contentType = 'image' and lastSeen is NULL " + safeClause + " and FLOOR( m.id / :optimization ) = FLOOR( RAND() * :optimization ) and m.id >= :offset LIMIT 1;"
    } else {
      var sql = "SELECT * from Media m where contentType = 'image' and lastSeen is NULL order by RAND() LIMIT 1;"
    }

    debug('getRandomUnseenImage', sql, params)

    conn.query(sql, { replacements: { "optimization" : params.optimization, "offset" : params.offset } }).then(function(ret){
      return resolve({"ret" : ret, "conn" : conn})
    }).catch(function(err) {
      return reject({"err" : err, "conn" : conn})
    })

  })

}
function getRandomUnseenImage ( params , config , conn ) { // defaults config = config || require ( '../config/config.js' ) params = params || { } var max = config . db . max || 0 var optimization = config . optimization || 0 var offset = Math . floor ( Math . random ( ) * max ) params . optimization = optimization params . offset = offset // main return new Promise ( ( resolve , reject ) => { if ( ! conn ) { var conn = wc_db . getConnection ( config . db ) } var safeClause = '' if ( params . safe !== undefined && params . safe !== null ) { if ( parseInt ( params . safe ) === 0 ) { safeClause = '' } else { safeClause = ' and a.safe = :safe ' } } else { safeClause = ' and a.safe = 1 ' } if ( params . reviewer ) { var sql = "SELECT * from Media m where m.uri not in (select r.uri from Rating r join User u on u.id = r.reviewer_id) and contentType = 'image' and lastSeen is NULL " + safeClause + " and FLOOR( m.id / :optimization ) = FLOOR( RAND() * :optimization ) and m.id >= :offset LIMIT 1;" } else { var sql = "SELECT * from Media m where contentType = 'image' and lastSeen is NULL order by RAND() LIMIT 1;" } debug ( 'getRandomUnseenImage' , sql , params ) conn . query ( sql , { replacements : { "optimization" : params . optimization , "offset" : params . offset } } ) . then ( function ( ret ) { return resolve ( { "ret" : ret , "conn" : conn } ) } ) . catch ( function ( err ) { return reject ( { "err" : err , "conn" : conn } ) } ) } ) }
Get a random unseen image
@param {Object} rating  The rating to add.
@param {Object} config  The optional config.
@param {Object} conn    The optional db connection.
@return {Object} Promise with success or fail.
Get a random unseen image