# All files / composer/components LinkAttachment.jsx

0% Statements 0/41
0% Branches 0/32
0% Functions 0/9
0% Lines 0/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197

     /**
     * Component that displays a link attachment
     */

    import React from 'react';
    import PropTypes from 'prop-types';
    import AppActionCreators from '../action-creators/AppActionCreators';
    import ComposerActionCreators from '../action-creators/ComposerActionCreators';
    import LinkAttachmentTextEditor from '../components/LinkAttachmentTextEditor';
    import LinkAttachmentThumbnailEditor from '../components/LinkAttachmentThumbnailEditor';
    import LinkAttachmentThumbnail from '../components/LinkAttachmentThumbnail';
    import CloseButton from '../components/CloseButton';
    import A from '../components/A';
    import { AttachmentTypes, LinkAttachmentTextFieldTypes } from '../AppConstants';
    import styles from './css/LinkAttachment.css';
    import { getAbsoluteUrl, getDomainFromUrl } from '../utils/StringUtils';

    class LinkAttachment extends React.Component {
      static propTypes = {
        appState: PropTypes.object.isRequired,
        draftId: PropTypes.string.isRequired,
        link: PropTypes.object.isRequired,
        service: PropTypes.object,
        visibleNotifications: PropTypes.array,
        filesUploadProgress: PropTypes.instanceOf(Map),
        selectedProfiles: PropTypes.array,
      };

      onCloseButtonClick = () => {
        ComposerActionCreators.toggleAttachment(this.props.draftId, AttachmentTypes.LINK);

        AppActionCreators.trackUserAction(['composer', 'attachment', 'disabled'], {
          attachment_type: AttachmentTypes.LINK,
          button_clicked: 'close_button',
        });
      };

      // Facebook has specific logic to allow editing of link attachments, not everyone
      // on the network can edit all attached links. For other social networks, we defer
      // to their config with `service.canEditLinkAttachment`
      canEditLinkAttachment = () => {
        const { appState, selectedProfiles, link, service } = this.props;

        if (service.name !== 'facebook' || !service.canEditLinkAttachment) {
          return {
            canEdit: service.canEditLinkAttachment,
            isResultCertain: true,
          };
        }

        // If only Facebook Profiles/Groups are selected (not Pages), prevent editing
        const hasOnlyNonPagesSelected = selectedProfiles.every((profile) => profile.serviceType !== 'page');
        if (hasOnlyNonPagesSelected && !link.wasEdited) {
          return {
            canEdit: false,
            isResultCertain: true,
          };
        }

        // If Facebook Profiles or Groups are selected, default to the generic info message
        // that mentions their unability to edit link previews
        const hasNonPagesSelected = selectedProfiles.some((profile) => profile.serviceType !== 'page');
        if (hasNonPagesSelected) {
          return {
            canEdit: true,
            isResultCertain: false,
            hasNonPages: true,
          };
        }

        // Don't check more than 5 profiles to save on Facebook API request quota
        const maxPagesToCheckCount = 5;

        // If unsure, allow editing straight away
        if (selectedProfiles.length > maxPagesToCheckCount) {
          return {
            canEdit: true,
            isResultCertain: false,
          };
        }

        const selectedProfilesIds = selectedProfiles.map(({ id }) => id);
        const linkDomainOwnershipData = appState.domainsOwnedByFacebookPages.get(link.url);

        // Default to true while we retrieve more data
        if (linkDomainOwnershipData === undefined) {
          return {
            canEdit: true,
            isResultCertain: false,
          };
        }

        const doAllSelectedProfilesOwnDomain =
          selectedProfilesIds.every((id) => linkDomainOwnershipData.get(id) === true);

        if (doAllSelectedProfilesOwnDomain) {
          return {
            canEdit: true,
            isResultCertain: true,
          };
        }

        const doSomeSelectedProfilesOwnDomain =
          selectedProfilesIds.some((id) => linkDomainOwnershipData.get(id) === true);
        const isSomeOwnershipDataBeingLoaded =
          selectedProfilesIds.some((id) => linkDomainOwnershipData.get(id) === null);

        if (doSomeSelectedProfilesOwnDomain || isSomeOwnershipDataBeingLoaded || link.wasEdited) {
          return {
            canEdit: true,
            isResultCertain: false,
          };
        }

        return {
          canEdit: false,
          isResultCertain: true,
        };
      };

      render() {
        const { link, draftId, service, visibleNotifications, filesUploadProgress } = this.props;
        const hasTitle = link.title !== null;
        const hasDescription = link.description !== null;
        const areUploadsInProgress = filesUploadProgress.size > 0;
        const absoluteUrl = getAbsoluteUrl(link.url);
        const domainOnlyUrl =
          link.url.replace('http://', '')
          .replace('www.', '')
          .replace('https://', '')
          .split(/[/?#]/)[0];

        const { canEdit: canEditLinkAttachment, isResultCertain, hasNonPages = false } = this.canEditLinkAttachment();
        const showFacebookLinkEditingMessage = !isResultCertain;

        return (
          <div>
            {showFacebookLinkEditingMessage &&
              <p className={styles.facebookLinkEditingMessage}>
                {hasNonPages ?
                  `It looks like you've got a few Facebook accounts selected. Please note that Profiles
                  and Groups are not allowed to make changes to link previews, and some Pages may not
                  either.` :
                  `Heads up! We're unsure if all the Facebook Pages you've selected are allowed to make
                  changes to link previews.`}{' '}
                Please bear in mind that if you do make changes, the link attachment may look different once
                published on Facebook. You can read more about{' '}
                <A href="https://faq.buffer.com/article/255-can-i-customize-images-for-my-link-attachments" target="_blank">
                Facebook's current link editing policies here</A>.
              </p>}

            <div className={styles.linkAttachment}>
              <LinkAttachmentThumbnail
                thumbnail={link.thumbnail}
                isUploadInProgress={areUploadsInProgress}
              />

              {canEditLinkAttachment &&
                <LinkAttachmentThumbnailEditor
                  draftId={draftId}
                  selectedThumbnail={link.thumbnail}
                  availableThumbnails={link.availableThumbnails}
                  visibleNotifications={visibleNotifications}
                  service={service}
                  filesUploadProgress={filesUploadProgress}
                />}

              <span className={styles.linkDetailsContainer}>
                {hasTitle ?
                  <LinkAttachmentTextEditor
                    type={LinkAttachmentTextFieldTypes.TITLE}
                    value={link.title} draftId={draftId} canBeEdited={canEditLinkAttachment}
                  /> :
                  <p className={styles.loadingMessage} />}

                <A className={styles.url} href={absoluteUrl} target="_blank">{domainOnlyUrl}</A>

                {hasDescription &&
                  <LinkAttachmentTextEditor
                    type={LinkAttachmentTextFieldTypes.DESCRIPTION}
                    value={link.description} draftId={draftId} canBeEdited={canEditLinkAttachment}
                  />}
              </span>

              <CloseButton
                className={styles.closeButton}
                onClick={this.onCloseButtonClick}
                data-tip="Disable Link Attachment"
              />
            </div>
          </div>
        );
      }
    }

    export default LinkAttachment;