All files / src ThreadSettings.js

100% Statements 38/38
100% Branches 40/40
100% Functions 4/4
100% Lines 38/38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126              1x   1x                     1x   1x                                         12x 1x     11x 1x     10x 1x     9x 1x     8x 1x     7x 1x     6x 6x 6x 6x 6x 6x   6x         6x 4x     6x 2x     6x 1x     6x 2x     6x           3x 1x     2x 1x     1x   1x                            
import {
  WEBVIEW_HEIGHT_RATIOS,
  PERSISTENT_MENU_TYPES,
} from './constants';
 
class GreetingText {
  constructor(text) {
    this.text = text;
 
    return {
      setting_type: 'greeting',
      greeting: {
        text: this.text,
      },
    };
  }
}
 
class GetStartedButton {
  constructor(payload) {
    this.payload = payload;
 
    return {
      setting_type: 'call_to_actions',
      thread_state: 'new_thread',
      call_to_actions: [
        {
          payload: this.payload,
        },
      ],
    };
  }
}
 
class PersistentMenuItem {
  constructor({
    type,
    title,
    url = '',
    payload = '',
    webview_height_ratio = '',
    messenger_extensions = false,
  }) {
    if (PERSISTENT_MENU_TYPES.indexOf(type) === -1) {
      throw new Error('Invalid type provided.');
    }
 
    if (title.length > 30) {
      throw new Error('Title cannot be longer 30 characters.');
    }
 
    if (payload && payload.length > 1000) {
      throw new Error('Payload cannot be longer 1000 characters.');
    }
 
    if (type === 'web_url' && !url) {
      throw new Error('`url` must be supplied for `web_url` type menu items.');
    }
 
    if (type === 'postback' && !payload) {
      throw new Error('`payload` must be supplied for `postback` type menu items.');
    }
 
    if (webview_height_ratio && WEBVIEW_HEIGHT_RATIOS.indexOf(webview_height_ratio) === -1) {
      throw new Error('Invalid `webview_height_ratio` provided.');
    }
 
    this.type = type;
    this.title = title;
    this.url = url;
    this.payload = payload;
    this.webview_height_ratio = webview_height_ratio;
    this.messenger_extensions = Boolean(messenger_extensions);
 
    const res = {
      type: this.type,
      title: this.title,
    };
 
    if (this.url && this.type === 'web_url') {
      res.url = this.url;
    }
 
    if (this.payload && this.type === 'postback') {
      res.payload = this.payload;
    }
 
    if (this.webview_height_ratio) {
      res.webview_height_ratio = this.webview_height_ratio;
    }
 
    if (this.messenger_extensions) {
      res.messenger_extensions = this.messenger_extensions;
    }
 
    return res;
  }
}
 
class PersistentMenu {
  constructor(menuItems) {
    if (!Array.isArray(menuItems)) {
      throw new Error('You must pass an array of PersistentMenuItem objects.');
    }
 
    if (menuItems.length > 5) {
      throw new Error('You cannot have more than 5 menu items.');
    }
 
    this.menuItems = menuItems;
 
    return {
      setting_type: 'call_to_actions',
      thread_state: 'existing_thread',
      call_to_actions: this.menuItems,
    };
  }
}
 
export {
  GreetingText,
  GetStartedButton,
  PersistentMenuItem,
  PersistentMenu,
};