status.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import Avatar from './avatar';
  5. import AvatarOverlay from './avatar_overlay';
  6. import AvatarComposite from './avatar_composite';
  7. import RelativeTimestamp from './relative_timestamp';
  8. import DisplayName from './display_name';
  9. import StatusContent from './status_content';
  10. import StatusActionBar from './status_action_bar';
  11. import AttachmentList from './attachment_list';
  12. import Card from '../features/status/components/card';
  13. import { injectIntl, defineMessages, FormattedMessage } from 'react-intl';
  14. import ImmutablePureComponent from 'react-immutable-pure-component';
  15. import { MediaGallery, Video, Audio } from '../features/ui/util/async-components';
  16. import { HotKeys } from 'react-hotkeys';
  17. import classNames from 'classnames';
  18. import Icon from 'mastodon/components/icon';
  19. import { displayMedia } from '../initial_state';
  20. import PictureInPicturePlaceholder from 'mastodon/components/picture_in_picture_placeholder';
  21. // We use the component (and not the container) since we do not want
  22. // to use the progress bar to show download progress
  23. import Bundle from '../features/ui/components/bundle';
  24. export const textForScreenReader = (intl, status, rebloggedByText = false) => {
  25. const displayName = status.getIn(['account', 'display_name']);
  26. const values = [
  27. displayName.length === 0 ? status.getIn(['account', 'acct']).split('@')[0] : displayName,
  28. status.get('spoiler_text') && status.get('hidden') ? status.get('spoiler_text') : status.get('search_index').slice(status.get('spoiler_text').length),
  29. intl.formatDate(status.get('created_at'), { hour: '2-digit', minute: '2-digit', month: 'short', day: 'numeric' }),
  30. status.getIn(['account', 'acct']),
  31. ];
  32. if (rebloggedByText) {
  33. values.push(rebloggedByText);
  34. }
  35. return values.join(', ');
  36. };
  37. export const defaultMediaVisibility = (status) => {
  38. if (!status) {
  39. return undefined;
  40. }
  41. if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  42. status = status.get('reblog');
  43. }
  44. return (displayMedia !== 'hide_all' && !status.get('sensitive') || displayMedia === 'show_all');
  45. };
  46. const messages = defineMessages({
  47. public_short: { id: 'privacy.public.short', defaultMessage: 'Public' },
  48. unlisted_short: { id: 'privacy.unlisted.short', defaultMessage: 'Unlisted' },
  49. private_short: { id: 'privacy.private.short', defaultMessage: 'Followers-only' },
  50. direct_short: { id: 'privacy.direct.short', defaultMessage: 'Mentioned people only' },
  51. edited: { id: 'status.edited', defaultMessage: 'Edited {date}' },
  52. });
  53. export default @injectIntl
  54. class Status extends ImmutablePureComponent {
  55. static contextTypes = {
  56. router: PropTypes.object,
  57. };
  58. static propTypes = {
  59. status: ImmutablePropTypes.map,
  60. account: ImmutablePropTypes.map,
  61. otherAccounts: ImmutablePropTypes.list,
  62. onClick: PropTypes.func,
  63. onReply: PropTypes.func,
  64. onFavourite: PropTypes.func,
  65. onReblog: PropTypes.func,
  66. onDelete: PropTypes.func,
  67. onDirect: PropTypes.func,
  68. onMention: PropTypes.func,
  69. onPin: PropTypes.func,
  70. onOpenMedia: PropTypes.func,
  71. onOpenVideo: PropTypes.func,
  72. onBlock: PropTypes.func,
  73. onEmbed: PropTypes.func,
  74. onHeightChange: PropTypes.func,
  75. onToggleHidden: PropTypes.func,
  76. onToggleCollapsed: PropTypes.func,
  77. muted: PropTypes.bool,
  78. hidden: PropTypes.bool,
  79. unread: PropTypes.bool,
  80. onMoveUp: PropTypes.func,
  81. onMoveDown: PropTypes.func,
  82. showThread: PropTypes.bool,
  83. getScrollPosition: PropTypes.func,
  84. updateScrollBottom: PropTypes.func,
  85. cacheMediaWidth: PropTypes.func,
  86. cachedMediaWidth: PropTypes.number,
  87. scrollKey: PropTypes.string,
  88. deployPictureInPicture: PropTypes.func,
  89. pictureInPicture: ImmutablePropTypes.contains({
  90. inUse: PropTypes.bool,
  91. available: PropTypes.bool,
  92. }),
  93. };
  94. // Avoid checking props that are functions (and whose equality will always
  95. // evaluate to false. See react-immutable-pure-component for usage.
  96. updateOnProps = [
  97. 'status',
  98. 'account',
  99. 'muted',
  100. 'hidden',
  101. 'unread',
  102. 'pictureInPicture',
  103. ];
  104. state = {
  105. showMedia: defaultMediaVisibility(this.props.status),
  106. statusId: undefined,
  107. };
  108. static getDerivedStateFromProps(nextProps, prevState) {
  109. if (nextProps.status && nextProps.status.get('id') !== prevState.statusId) {
  110. return {
  111. showMedia: defaultMediaVisibility(nextProps.status),
  112. statusId: nextProps.status.get('id'),
  113. };
  114. } else {
  115. return null;
  116. }
  117. }
  118. handleToggleMediaVisibility = () => {
  119. this.setState({ showMedia: !this.state.showMedia });
  120. }
  121. handleClick = e => {
  122. if (e && (e.button !== 0 || e.ctrlKey || e.metaKey)) {
  123. return;
  124. }
  125. if (e) {
  126. e.preventDefault();
  127. }
  128. this.handleHotkeyOpen();
  129. }
  130. handlePrependAccountClick = e => {
  131. this.handleAccountClick(e, false);
  132. }
  133. handleAccountClick = (e, proper = true) => {
  134. if (e && (e.button !== 0 || e.ctrlKey || e.metaKey)) {
  135. return;
  136. }
  137. if (e) {
  138. e.preventDefault();
  139. }
  140. this._openProfile(proper);
  141. }
  142. handleExpandedToggle = () => {
  143. this.props.onToggleHidden(this._properStatus());
  144. }
  145. handleCollapsedToggle = isCollapsed => {
  146. this.props.onToggleCollapsed(this._properStatus(), isCollapsed);
  147. }
  148. renderLoadingMediaGallery () {
  149. return <div className='media-gallery' style={{ height: '110px' }} />;
  150. }
  151. renderLoadingVideoPlayer () {
  152. return <div className='video-player' style={{ height: '110px' }} />;
  153. }
  154. renderLoadingAudioPlayer () {
  155. return <div className='audio-player' style={{ height: '110px' }} />;
  156. }
  157. handleOpenVideo = (options) => {
  158. const status = this._properStatus();
  159. this.props.onOpenVideo(status.get('id'), status.getIn(['media_attachments', 0]), options);
  160. }
  161. handleOpenMedia = (media, index) => {
  162. this.props.onOpenMedia(this._properStatus().get('id'), media, index);
  163. }
  164. handleHotkeyOpenMedia = e => {
  165. const { onOpenMedia, onOpenVideo } = this.props;
  166. const status = this._properStatus();
  167. e.preventDefault();
  168. if (status.get('media_attachments').size > 0) {
  169. if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  170. onOpenVideo(status.get('id'), status.getIn(['media_attachments', 0]), { startTime: 0 });
  171. } else {
  172. onOpenMedia(status.get('id'), status.get('media_attachments'), 0);
  173. }
  174. }
  175. }
  176. handleDeployPictureInPicture = (type, mediaProps) => {
  177. const { deployPictureInPicture } = this.props;
  178. const status = this._properStatus();
  179. deployPictureInPicture(status, type, mediaProps);
  180. }
  181. handleHotkeyReply = e => {
  182. e.preventDefault();
  183. this.props.onReply(this._properStatus(), this.context.router.history);
  184. }
  185. handleHotkeyFavourite = () => {
  186. this.props.onFavourite(this._properStatus());
  187. }
  188. handleHotkeyBoost = e => {
  189. this.props.onReblog(this._properStatus(), e);
  190. }
  191. handleHotkeyMention = e => {
  192. e.preventDefault();
  193. this.props.onMention(this._properStatus().get('account'), this.context.router.history);
  194. }
  195. handleHotkeyOpen = () => {
  196. if (this.props.onClick) {
  197. this.props.onClick();
  198. return;
  199. }
  200. const { router } = this.context;
  201. const status = this._properStatus();
  202. if (!router) {
  203. return;
  204. }
  205. router.history.push(`/@${status.getIn(['account', 'acct'])}/${status.get('id')}`);
  206. }
  207. handleHotkeyOpenProfile = () => {
  208. this._openProfile();
  209. }
  210. _openProfile = (proper = true) => {
  211. const { router } = this.context;
  212. const status = proper ? this._properStatus() : this.props.status;
  213. if (!router) {
  214. return;
  215. }
  216. router.history.push(`/@${status.getIn(['account', 'acct'])}`);
  217. }
  218. handleHotkeyMoveUp = e => {
  219. this.props.onMoveUp(this.props.status.get('id'), e.target.getAttribute('data-featured'));
  220. }
  221. handleHotkeyMoveDown = e => {
  222. this.props.onMoveDown(this.props.status.get('id'), e.target.getAttribute('data-featured'));
  223. }
  224. handleHotkeyToggleHidden = () => {
  225. this.props.onToggleHidden(this._properStatus());
  226. }
  227. handleHotkeyToggleSensitive = () => {
  228. this.handleToggleMediaVisibility();
  229. }
  230. _properStatus () {
  231. const { status } = this.props;
  232. if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  233. return status.get('reblog');
  234. } else {
  235. return status;
  236. }
  237. }
  238. handleRef = c => {
  239. this.node = c;
  240. }
  241. render () {
  242. let media = null;
  243. let statusAvatar, prepend, rebloggedByText;
  244. const { intl, hidden, featured, otherAccounts, unread, showThread, scrollKey, pictureInPicture } = this.props;
  245. let { status, account, ...other } = this.props;
  246. if (status === null) {
  247. return null;
  248. }
  249. const handlers = this.props.muted ? {} : {
  250. reply: this.handleHotkeyReply,
  251. favourite: this.handleHotkeyFavourite,
  252. boost: this.handleHotkeyBoost,
  253. mention: this.handleHotkeyMention,
  254. open: this.handleHotkeyOpen,
  255. openProfile: this.handleHotkeyOpenProfile,
  256. moveUp: this.handleHotkeyMoveUp,
  257. moveDown: this.handleHotkeyMoveDown,
  258. toggleHidden: this.handleHotkeyToggleHidden,
  259. toggleSensitive: this.handleHotkeyToggleSensitive,
  260. openMedia: this.handleHotkeyOpenMedia,
  261. };
  262. if (hidden) {
  263. return (
  264. <HotKeys handlers={handlers}>
  265. <div ref={this.handleRef} className={classNames('status__wrapper', { focusable: !this.props.muted })} tabIndex='0'>
  266. <span>{status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}</span>
  267. <span>{status.get('content')}</span>
  268. </div>
  269. </HotKeys>
  270. );
  271. }
  272. if (status.get('filtered') || status.getIn(['reblog', 'filtered'])) {
  273. const minHandlers = this.props.muted ? {} : {
  274. moveUp: this.handleHotkeyMoveUp,
  275. moveDown: this.handleHotkeyMoveDown,
  276. };
  277. return (
  278. <HotKeys handlers={minHandlers}>
  279. <div className='status__wrapper status__wrapper--filtered focusable' tabIndex='0' ref={this.handleRef}>
  280. <FormattedMessage id='status.filtered' defaultMessage='Filtered' />
  281. </div>
  282. </HotKeys>
  283. );
  284. }
  285. if (featured) {
  286. prepend = (
  287. <div className='status__prepend'>
  288. <div className='status__prepend-icon-wrapper'><Icon id='thumb-tack' className='status__prepend-icon' fixedWidth /></div>
  289. <FormattedMessage id='status.pinned' defaultMessage='Pinned post' />
  290. </div>
  291. );
  292. } else if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  293. const display_name_html = { __html: status.getIn(['account', 'display_name_html']) };
  294. prepend = (
  295. <div className='status__prepend'>
  296. <div className='status__prepend-icon-wrapper'><Icon id='retweet' className='status__prepend-icon' fixedWidth /></div>
  297. <FormattedMessage id='status.reblogged_by' defaultMessage='{name} boosted' values={{ name: <a onClick={this.handlePrependAccountClick} data-id={status.getIn(['account', 'id'])} href={status.getIn(['account', 'url'])} className='status__display-name muted'><bdi><strong dangerouslySetInnerHTML={display_name_html} /></bdi></a> }} />
  298. </div>
  299. );
  300. rebloggedByText = intl.formatMessage({ id: 'status.reblogged_by', defaultMessage: '{name} boosted' }, { name: status.getIn(['account', 'acct']) });
  301. account = status.get('account');
  302. status = status.get('reblog');
  303. }
  304. if (pictureInPicture.get('inUse')) {
  305. media = <PictureInPicturePlaceholder width={this.props.cachedMediaWidth} />;
  306. } else if (status.get('media_attachments').size > 0) {
  307. if (this.props.muted) {
  308. media = (
  309. <AttachmentList
  310. compact
  311. media={status.get('media_attachments')}
  312. />
  313. );
  314. } else if (status.getIn(['media_attachments', 0, 'type']) === 'audio') {
  315. const attachment = status.getIn(['media_attachments', 0]);
  316. media = (
  317. <Bundle fetchComponent={Audio} loading={this.renderLoadingAudioPlayer} >
  318. {Component => (
  319. <Component
  320. src={attachment.get('url')}
  321. alt={attachment.get('description')}
  322. poster={attachment.get('preview_url') || status.getIn(['account', 'avatar_static'])}
  323. backgroundColor={attachment.getIn(['meta', 'colors', 'background'])}
  324. foregroundColor={attachment.getIn(['meta', 'colors', 'foreground'])}
  325. accentColor={attachment.getIn(['meta', 'colors', 'accent'])}
  326. duration={attachment.getIn(['meta', 'original', 'duration'], 0)}
  327. width={this.props.cachedMediaWidth}
  328. height={110}
  329. cacheWidth={this.props.cacheMediaWidth}
  330. deployPictureInPicture={pictureInPicture.get('available') ? this.handleDeployPictureInPicture : undefined}
  331. />
  332. )}
  333. </Bundle>
  334. );
  335. } else if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  336. const attachment = status.getIn(['media_attachments', 0]);
  337. media = (
  338. <Bundle fetchComponent={Video} loading={this.renderLoadingVideoPlayer} >
  339. {Component => (
  340. <Component
  341. preview={attachment.get('preview_url')}
  342. frameRate={attachment.getIn(['meta', 'original', 'frame_rate'])}
  343. blurhash={attachment.get('blurhash')}
  344. src={attachment.get('url')}
  345. alt={attachment.get('description')}
  346. width={this.props.cachedMediaWidth}
  347. height={110}
  348. inline
  349. sensitive={status.get('sensitive')}
  350. onOpenVideo={this.handleOpenVideo}
  351. cacheWidth={this.props.cacheMediaWidth}
  352. deployPictureInPicture={pictureInPicture.get('available') ? this.handleDeployPictureInPicture : undefined}
  353. visible={this.state.showMedia}
  354. onToggleVisibility={this.handleToggleMediaVisibility}
  355. />
  356. )}
  357. </Bundle>
  358. );
  359. } else {
  360. media = (
  361. <Bundle fetchComponent={MediaGallery} loading={this.renderLoadingMediaGallery}>
  362. {Component => (
  363. <Component
  364. media={status.get('media_attachments')}
  365. sensitive={status.get('sensitive')}
  366. height={110}
  367. onOpenMedia={this.handleOpenMedia}
  368. cacheWidth={this.props.cacheMediaWidth}
  369. defaultWidth={this.props.cachedMediaWidth}
  370. visible={this.state.showMedia}
  371. onToggleVisibility={this.handleToggleMediaVisibility}
  372. />
  373. )}
  374. </Bundle>
  375. );
  376. }
  377. } else if (status.get('spoiler_text').length === 0 && status.get('card')) {
  378. media = (
  379. <Card
  380. onOpenMedia={this.handleOpenMedia}
  381. card={status.get('card')}
  382. compact
  383. cacheWidth={this.props.cacheMediaWidth}
  384. defaultWidth={this.props.cachedMediaWidth}
  385. sensitive={status.get('sensitive')}
  386. />
  387. );
  388. }
  389. if (otherAccounts && otherAccounts.size > 0) {
  390. statusAvatar = <AvatarComposite accounts={otherAccounts} size={48} />;
  391. } else if (account === undefined || account === null) {
  392. statusAvatar = <Avatar account={status.get('account')} size={48} />;
  393. } else {
  394. statusAvatar = <AvatarOverlay account={status.get('account')} friend={account} />;
  395. }
  396. const visibilityIconInfo = {
  397. 'public': { icon: 'globe', text: intl.formatMessage(messages.public_short) },
  398. 'unlisted': { icon: 'unlock', text: intl.formatMessage(messages.unlisted_short) },
  399. 'private': { icon: 'lock', text: intl.formatMessage(messages.private_short) },
  400. 'direct': { icon: 'at', text: intl.formatMessage(messages.direct_short) },
  401. };
  402. const visibilityIcon = visibilityIconInfo[status.get('visibility')];
  403. return (
  404. <HotKeys handlers={handlers}>
  405. <div className={classNames('status__wrapper', `status__wrapper-${status.get('visibility')}`, { 'status__wrapper-reply': !!status.get('in_reply_to_id'), unread, focusable: !this.props.muted })} tabIndex={this.props.muted ? null : 0} data-featured={featured ? 'true' : null} aria-label={textForScreenReader(intl, status, rebloggedByText)} ref={this.handleRef}>
  406. {prepend}
  407. <div className={classNames('status', `status-${status.get('visibility')}`, { 'status-reply': !!status.get('in_reply_to_id'), muted: this.props.muted })} data-id={status.get('id')}>
  408. <div className='status__expand' onClick={this.handleClick} role='presentation' />
  409. <div className='status__info'>
  410. <a onClick={this.handleClick} href={status.get('url')} className='status__relative-time' target='_blank' rel='noopener noreferrer'>
  411. <span className='status__visibility-icon'><Icon id={visibilityIcon.icon} title={visibilityIcon.text} /></span>
  412. <RelativeTimestamp timestamp={status.get('created_at')} />{status.get('edited_at') && <abbr title={intl.formatMessage(messages.edited, { date: intl.formatDate(status.get('edited_at'), { hour12: false, year: 'numeric', month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit' }) })}> *</abbr>}
  413. </a>
  414. <a onClick={this.handleAccountClick} href={status.getIn(['account', 'url'])} title={status.getIn(['account', 'acct'])} className='status__display-name' target='_blank' rel='noopener noreferrer'>
  415. <div className='status__avatar'>
  416. {statusAvatar}
  417. </div>
  418. <DisplayName account={status.get('account')} others={otherAccounts} />
  419. </a>
  420. </div>
  421. <StatusContent status={status} onClick={this.handleClick} expanded={!status.get('hidden')} showThread={showThread} onExpandedToggle={this.handleExpandedToggle} collapsable onCollapsedToggle={this.handleCollapsedToggle} />
  422. {media}
  423. <StatusActionBar scrollKey={scrollKey} status={status} account={account} {...other} />
  424. </div>
  425. </div>
  426. </HotKeys>
  427. );
  428. }
  429. }