github.com/prebid/prebid-server/v2@v2.18.0/endpoints/openrtb2/interstitial.go (about)

     1  package openrtb2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/prebid/openrtb/v20/openrtb2"
     7  	"github.com/prebid/prebid-server/v2/config"
     8  	"github.com/prebid/prebid-server/v2/errortypes"
     9  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    10  )
    11  
    12  func processInterstitials(req *openrtb_ext.RequestWrapper) error {
    13  	unmarshalled := true
    14  	for _, imp := range req.GetImp() {
    15  		if imp.Instl == 1 {
    16  			var prebid *openrtb_ext.ExtDevicePrebid
    17  			if unmarshalled {
    18  				if req.Device.Ext == nil {
    19  					// No special interstitial support requested, so bail as there is nothing to do
    20  					return nil
    21  				}
    22  				deviceExt, err := req.GetDeviceExt()
    23  
    24  				if err != nil {
    25  					return err
    26  				}
    27  				prebid = deviceExt.GetPrebid()
    28  				if prebid == nil || prebid.Interstitial == nil {
    29  					// No special interstitial support requested, so bail as there is nothing to do
    30  					return nil
    31  				}
    32  			}
    33  			err := processInterstitialsForImp(imp, prebid, req.Device)
    34  			if err != nil {
    35  				return err
    36  			}
    37  		}
    38  	}
    39  	return nil
    40  }
    41  
    42  func processInterstitialsForImp(imp *openrtb_ext.ImpWrapper, devExtPrebid *openrtb_ext.ExtDevicePrebid, device *openrtb2.Device) error {
    43  	var maxWidth, maxHeight, minWidth, minHeight int64
    44  	if imp.Banner == nil {
    45  		// custom interstitial support is only available for banner requests.
    46  		return nil
    47  	}
    48  	if len(imp.Banner.Format) > 0 {
    49  		maxWidth = imp.Banner.Format[0].W
    50  		maxHeight = imp.Banner.Format[0].H
    51  	}
    52  	if maxWidth < 2 && maxHeight < 2 {
    53  		// This catches size 1x1 as "use device size"
    54  		if device == nil {
    55  			return &errortypes.BadInput{Message: fmt.Sprintf("Unable to read max interstitial size for Imp id=%s (No Device and no Format objects)", imp.ID)}
    56  		}
    57  		maxWidth = device.W
    58  		maxHeight = device.H
    59  	}
    60  	minWidth = (maxWidth * devExtPrebid.Interstitial.MinWidthPerc) / 100
    61  	minHeight = (maxHeight * devExtPrebid.Interstitial.MinHeightPerc) / 100
    62  	imp.Banner.Format = genInterstitialFormat(minWidth, maxWidth, minHeight, maxHeight)
    63  	if len(imp.Banner.Format) == 0 {
    64  		return &errortypes.BadInput{Message: fmt.Sprintf("Unable to set interstitial size list for Imp id=%s (No valid sizes between %dx%d and %dx%d)", imp.ID, minWidth, minHeight, maxWidth, maxHeight)}
    65  	}
    66  	return nil
    67  }
    68  
    69  func genInterstitialFormat(minWidth, maxWidth, minHeight, maxHeight int64) []openrtb2.Format {
    70  	sizes := make([]config.InterstitialSize, 0, 10)
    71  	for _, size := range config.ResolvedInterstitialSizes {
    72  		if int64(size.Width) >= minWidth && int64(size.Width) <= maxWidth && int64(size.Height) >= minHeight && int64(size.Height) <= maxHeight {
    73  			sizes = append(sizes, size)
    74  			if len(sizes) >= 10 {
    75  				// we have enough sizes
    76  				break
    77  			}
    78  		}
    79  	}
    80  	formatList := make([]openrtb2.Format, 0, len(sizes))
    81  	for _, size := range sizes {
    82  		formatList = append(formatList, openrtb2.Format{W: int64(size.Width), H: int64(size.Height)})
    83  	}
    84  	return formatList
    85  }