all files / firebase/modules/messaging/ RemoteMessage.js

24.39% Statements 10/41
29.41% Branches 5/17
6.25% Functions 1/16
24.39% Lines 10/41
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156                                                                                                                                                                                                                                                                                                   
/**
 * @flow
 * RemoteMessage representation wrapper
 */
import { isObject, generatePushID } from './../../utils';
 
import type {
  NativeInboundRemoteMessage,
  NativeOutboundRemoteMessage,
} from './types';
 
export default class RemoteMessage {
  _collapseKey: string | void;
  _data: { [string]: string };
  _from: string | void;
  _messageId: string;
  _messageType: string | void;
  _sentTime: number | void;
  _to: string;
  _ttl: number;
 
  constructor(inboundMessage?: NativeInboundRemoteMessage) {
    Eif (inboundMessage) {
      this._collapseKey = inboundMessage.collapseKey;
      this._data = inboundMessage.data;
      this._from = inboundMessage.from;
      this._messageId = inboundMessage.messageId;
      this._messageType = inboundMessage.messageType;
      this._sentTime = inboundMessage.sentTime;
    }
    // defaults
    this._data = this._data || {};
    // TODO: Is this the best way to generate an ID?
    this._messageId = this._messageId || generatePushID();
    this._ttl = 3600;
  }
 
  get collapseKey(): ?string {
    return this._collapseKey;
  }
 
  get data(): { [string]: string } {
    return this._data;
  }
 
  get from(): ?string {
    return this._from;
  }
 
  get messageId(): ?string {
    return this._messageId;
  }
 
  get messageType(): ?string {
    return this._messageType;
  }
 
  get sentTime(): ?number {
    return this._sentTime;
  }
 
  get to(): ?string {
    return this._to;
  }
 
  get ttl(): ?number {
    return this._ttl;
  }
 
  /**
   *
   * @param collapseKey
   * @returns {RemoteMessage}
   */
  setCollapseKey(collapseKey: string): RemoteMessage {
    this._collapseKey = collapseKey;
    return this;
  }
 
  /**
   *
   * @param data
   * @returns {RemoteMessage}
   */
  setData(data: { [string]: string } = {}) {
    if (!isObject(data)) {
      throw new Error(
        `RemoteMessage:setData expects an object but got type '${typeof data}'.`
      );
    }
    this._data = data;
    return this;
  }
 
  /**
   *
   * @param messageId
   * @returns {RemoteMessage}
   */
  setMessageId(messageId: string): RemoteMessage {
    this._messageId = messageId;
    return this;
  }
 
  /**
   *
   * @param messageType
   * @returns {RemoteMessage}
   */
  setMessageType(messageType: string): RemoteMessage {
    this._messageType = messageType;
    return this;
  }
 
  /**
   *
   * @param to
   * @returns {RemoteMessage}
   */
  setTo(to: string): RemoteMessage {
    this._to = to;
    return this;
  }
 
  /**
   *
   * @param ttl
   * @returns {RemoteMessage}
   */
  setTtl(ttl: number): RemoteMessage {
    this._ttl = ttl;
    return this;
  }
 
  build(): NativeOutboundRemoteMessage {
    if (!this.data) {
      throw new Error('RemoteMessage: Missing required `data` property');
    } else if (!this.messageId) {
      throw new Error('RemoteMessage: Missing required `messageId` property');
    } else if (!this.to) {
      throw new Error('RemoteMessage: Missing required `to` property');
    } else if (!this.ttl) {
      throw new Error('RemoteMessage: Missing required `ttl` property');
    }
 
    return {
      collapseKey: this._collapseKey,
      data: this._data,
      messageId: this._messageId,
      messageType: this._messageType,
      to: this._to,
      ttl: this._ttl,
    };
  }
}