# All files / composer/utils WebSocket.js

25% Statements 7/28
0% Branches 0/6
11.11% Functions 1/9
28% Lines 7/25

    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

    10x

    10x

    10x

    10x

    10x

    10x

    10x

     /* global Pusher */
    /**
     * Scrape urls to retrieve information about them.
     *
     * Returns a Promise that resolves with the JSON response from WebSocket.url as
     * an object literal.
     *
     * Results are cached, and a same request done simultaneously will be
     * queued to return the same info as the already-running request.
     */

    import InternalPusher from 'pusher-js';
    import ServerActionCreators from '../action-creators/ServerActionCreators';
    import AppStore from '../stores/AppStore';
    import { AppEnvironments } from '../AppConstants';

    const handleTranscodedVideo = (data) => {
      ServerActionCreators.videoProcessed({
        uploadId: data.upload_id,
        name: data.media.video.title,
        duration: data.media.video.details.duration,
        durationMs: data.media.video.details.duration_millis,
        size: data.media.video.details.file_size,
        width: data.media.video.details.width,
        height: data.media.video.details.height,
        url: data.media.video.details.transcoded_location,
        originalUrl: data.media.video.details.location,
        thumbnail: data.media.thumbnail,
        availableThumbnails: data.media.video.thumbnails,
      });
    };

    const handleCreatedProfileGroup = ({ id, name, profile_ids }) => {
      ServerActionCreators.profileGroupCreated({
        id,
        name,
        profileIds: profile_ids,
      });
    };

    const handleEditedProfileGroup = ({ id, name, profile_ids }) => {
      ServerActionCreators.profileGroupUpdated({
        id,
        name,
        profileIds: profile_ids,
      });
    };

    const handleDeletedProfileGroup = ({ id }) => {
      ServerActionCreators.profileGroupDeleted({
        id,
      });
    };

    const eventHandlers = new Map([
      ['transcode_done', handleTranscodedVideo],
      ['created_profile_group', handleCreatedProfileGroup],
      ['edited_profile_group', handleEditedProfileGroup],
      ['deleted_profile_group', handleDeletedProfileGroup],
    ]);

    class WebSocket {
      static PUSHER_API_KEY = 'bd9ba9324ece3341976e';
      static PUSHER_CLUSTER = 'mt1';
      static PUSHER_AUTH_ENDPOINT = '/pusher_receiver/auth';

      static init = (() => {
        let hasWebSocketConnectionOpen = false; // Only one connection's necessary

        return () => {
          if (hasWebSocketConnectionOpen) return;

          const userId = AppStore.getUserData().id;

          Pusher.channel_auth_endpoint = WebSocket.PUSHER_AUTH_ENDPOINT;
          const pusher = new Pusher(WebSocket.PUSHER_API_KEY, {
            cluster: WebSocket.PUSHER_CLUSTER,
          });
          const pusherInstance = pusher.subscribe(`private-updates-${userId}`);
          eventHandlers.forEach((handler, event) => pusherInstance.bind(event, handler));
          hasWebSocketConnectionOpen = true;
        };
      })();

      static cleanUp = () => {
        const { appEnvironment } = AppStore.getMetaData();
        const isDashboardEnv = appEnvironment === AppEnvironments.WEB_DASHBOARD;
        let pusherInstance;

        if (isDashboardEnv) {
          pusherInstance = InternalPusher.instances[0];
        } else {
          pusherInstance = window.__pusher;
        }

        if (pusherInstance) {
          eventHandlers.forEach((handler, event) => pusherInstance.unbind(event, handler));
        }
      };
    }

    export default WebSocket;