The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

389 lines
14 KiB

  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, FormattedMessage } from 'react-intl';
  14. import ImmutablePureComponent from 'react-immutable-pure-component';
  15. import { MediaGallery, Video } 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 PollContainer from 'mastodon/containers/poll_container';
  20. // We use the component (and not the container) since we do not want
  21. // to use the progress bar to show download progress
  22. import Bundle from '../features/ui/components/bundle';
  23. export const textForScreenReader = (intl, status, rebloggedByText = false) => {
  24. const displayName = status.getIn(['account', 'display_name']);
  25. const values = [
  26. displayName.length === 0 ? status.getIn(['account', 'acct']).split('@')[0] : displayName,
  27. status.get('spoiler_text') && status.get('hidden') ? status.get('spoiler_text') : status.get('search_index').slice(status.get('spoiler_text').length),
  28. intl.formatDate(status.get('created_at'), { hour: '2-digit', minute: '2-digit', month: 'short', day: 'numeric' }),
  29. status.getIn(['account', 'acct']),
  30. ];
  31. if (rebloggedByText) {
  32. values.push(rebloggedByText);
  33. }
  34. return values.join(', ');
  35. };
  36. export default @injectIntl
  37. class Status extends ImmutablePureComponent {
  38. static contextTypes = {
  39. router: PropTypes.object,
  40. };
  41. static propTypes = {
  42. status: ImmutablePropTypes.map,
  43. account: ImmutablePropTypes.map,
  44. otherAccounts: ImmutablePropTypes.list,
  45. onClick: PropTypes.func,
  46. onReply: PropTypes.func,
  47. onFavourite: PropTypes.func,
  48. onReblog: PropTypes.func,
  49. onDelete: PropTypes.func,
  50. onDirect: PropTypes.func,
  51. onMention: PropTypes.func,
  52. onPin: PropTypes.func,
  53. onOpenMedia: PropTypes.func,
  54. onOpenVideo: PropTypes.func,
  55. onBlock: PropTypes.func,
  56. onEmbed: PropTypes.func,
  57. onHeightChange: PropTypes.func,
  58. onToggleHidden: PropTypes.func,
  59. muted: PropTypes.bool,
  60. hidden: PropTypes.bool,
  61. unread: PropTypes.bool,
  62. onMoveUp: PropTypes.func,
  63. onMoveDown: PropTypes.func,
  64. showThread: PropTypes.bool,
  65. getScrollPosition: PropTypes.func,
  66. updateScrollBottom: PropTypes.func,
  67. cacheMediaWidth: PropTypes.func,
  68. cachedMediaWidth: PropTypes.number,
  69. };
  70. // Avoid checking props that are functions (and whose equality will always
  71. // evaluate to false. See react-immutable-pure-component for usage.
  72. updateOnProps = [
  73. 'status',
  74. 'account',
  75. 'muted',
  76. 'hidden',
  77. ];
  78. // Track height changes we know about to compensate scrolling
  79. componentDidMount () {
  80. this.didShowCard = !this.props.muted && !this.props.hidden && this.props.status && this.props.status.get('card');
  81. }
  82. getSnapshotBeforeUpdate () {
  83. if (this.props.getScrollPosition) {
  84. return this.props.getScrollPosition();
  85. } else {
  86. return null;
  87. }
  88. }
  89. // Compensate height changes
  90. componentDidUpdate (prevProps, prevState, snapshot) {
  91. const doShowCard = !this.props.muted && !this.props.hidden && this.props.status && this.props.status.get('card');
  92. if (doShowCard && !this.didShowCard) {
  93. this.didShowCard = true;
  94. if (snapshot !== null && this.props.updateScrollBottom) {
  95. if (this.node && this.node.offsetTop < snapshot.top) {
  96. this.props.updateScrollBottom(snapshot.height - snapshot.top);
  97. }
  98. }
  99. }
  100. }
  101. componentWillUnmount() {
  102. if (this.node && this.props.getScrollPosition) {
  103. const position = this.props.getScrollPosition();
  104. if (position !== null && this.node.offsetTop < position.top) {
  105. requestAnimationFrame(() => {
  106. this.props.updateScrollBottom(position.height - position.top);
  107. });
  108. }
  109. }
  110. }
  111. handleClick = () => {
  112. if (this.props.onClick) {
  113. this.props.onClick();
  114. return;
  115. }
  116. if (!this.context.router) {
  117. return;
  118. }
  119. const { status } = this.props;
  120. this.context.router.history.push(`/statuses/${status.getIn(['reblog', 'id'], status.get('id'))}`);
  121. }
  122. handleAccountClick = (e) => {
  123. if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  124. const id = e.currentTarget.getAttribute('data-id');
  125. e.preventDefault();
  126. this.context.router.history.push(`/accounts/${id}`);
  127. }
  128. }
  129. handleExpandedToggle = () => {
  130. this.props.onToggleHidden(this._properStatus());
  131. };
  132. renderLoadingMediaGallery () {
  133. return <div className='media_gallery' style={{ height: '110px' }} />;
  134. }
  135. renderLoadingVideoPlayer () {
  136. return <div className='media-spoiler-video' style={{ height: '110px' }} />;
  137. }
  138. handleOpenVideo = (media, startTime) => {
  139. this.props.onOpenVideo(media, startTime);
  140. }
  141. handleHotkeyReply = e => {
  142. e.preventDefault();
  143. this.props.onReply(this._properStatus(), this.context.router.history);
  144. }
  145. handleHotkeyFavourite = () => {
  146. this.props.onFavourite(this._properStatus());
  147. }
  148. handleHotkeyBoost = e => {
  149. this.props.onReblog(this._properStatus(), e);
  150. }
  151. handleHotkeyMention = e => {
  152. e.preventDefault();
  153. this.props.onMention(this._properStatus().get('account'), this.context.router.history);
  154. }
  155. handleHotkeyOpen = () => {
  156. this.context.router.history.push(`/statuses/${this._properStatus().get('id')}`);
  157. }
  158. handleHotkeyOpenProfile = () => {
  159. this.context.router.history.push(`/accounts/${this._properStatus().getIn(['account', 'id'])}`);
  160. }
  161. handleHotkeyMoveUp = e => {
  162. this.props.onMoveUp(this.props.status.get('id'), e.target.getAttribute('data-featured'));
  163. }
  164. handleHotkeyMoveDown = e => {
  165. this.props.onMoveDown(this.props.status.get('id'), e.target.getAttribute('data-featured'));
  166. }
  167. handleHotkeyToggleHidden = () => {
  168. this.props.onToggleHidden(this._properStatus());
  169. }
  170. _properStatus () {
  171. const { status } = this.props;
  172. if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  173. return status.get('reblog');
  174. } else {
  175. return status;
  176. }
  177. }
  178. handleRef = c => {
  179. this.node = c;
  180. }
  181. render () {
  182. let media = null;
  183. let statusAvatar, prepend, rebloggedByText;
  184. const { intl, hidden, featured, otherAccounts, unread, showThread } = this.props;
  185. let { status, account, ...other } = this.props;
  186. if (status === null) {
  187. return null;
  188. }
  189. if (hidden) {
  190. return (
  191. <div ref={this.handleRef}>
  192. {status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}
  193. {status.get('content')}
  194. </div>
  195. );
  196. }
  197. if (status.get('filtered') || status.getIn(['reblog', 'filtered'])) {
  198. const minHandlers = this.props.muted ? {} : {
  199. moveUp: this.handleHotkeyMoveUp,
  200. moveDown: this.handleHotkeyMoveDown,
  201. };
  202. return (
  203. <HotKeys handlers={minHandlers}>
  204. <div className='status__wrapper status__wrapper--filtered focusable' tabIndex='0' ref={this.handleRef}>
  205. <FormattedMessage id='status.filtered' defaultMessage='Filtered' />
  206. </div>
  207. </HotKeys>
  208. );
  209. }
  210. if (featured) {
  211. prepend = (
  212. <div className='status__prepend'>
  213. <div className='status__prepend-icon-wrapper'><Icon id='thumb-tack' className='status__prepend-icon' fixedWidth /></div>
  214. <FormattedMessage id='status.pinned' defaultMessage='Pinned toot' />
  215. </div>
  216. );
  217. } else if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
  218. const display_name_html = { __html: status.getIn(['account', 'display_name_html']) };
  219. prepend = (
  220. <div className='status__prepend'>
  221. <div className='status__prepend-icon-wrapper'><Icon id='retweet' className='status__prepend-icon' fixedWidth /></div>
  222. <FormattedMessage id='status.reblogged_by' defaultMessage='{name} boosted' values={{ name: <a onClick={this.handleAccountClick} data-id={status.getIn(['account', 'id'])} href={status.getIn(['account', 'url'])} className='status__display-name muted'><bdi><strong dangerouslySetInnerHTML={display_name_html} /></bdi></a> }} />
  223. </div>
  224. );
  225. rebloggedByText = intl.formatMessage({ id: 'status.reblogged_by', defaultMessage: '{name} boosted' }, { name: status.getIn(['account', 'acct']) });
  226. account = status.get('account');
  227. status = status.get('reblog');
  228. }
  229. if (status.get('poll')) {
  230. media = <PollContainer pollId={status.get('poll')} />;
  231. } else if (status.get('media_attachments').size > 0) {
  232. if (this.props.muted) {
  233. media = (
  234. <AttachmentList
  235. compact
  236. media={status.get('media_attachments')}
  237. />
  238. );
  239. } else if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
  240. const video = status.getIn(['media_attachments', 0]);
  241. media = (
  242. <Bundle fetchComponent={Video} loading={this.renderLoadingVideoPlayer} >
  243. {Component => (
  244. <Component
  245. preview={video.get('preview_url')}
  246. blurhash={video.get('blurhash')}
  247. src={video.get('url')}
  248. alt={video.get('description')}
  249. width={this.props.cachedMediaWidth}
  250. height={110}
  251. inline
  252. sensitive={status.get('sensitive')}
  253. onOpenVideo={this.handleOpenVideo}
  254. cacheWidth={this.props.cacheMediaWidth}
  255. />
  256. )}
  257. </Bundle>
  258. );
  259. } else {
  260. media = (
  261. <Bundle fetchComponent={MediaGallery} loading={this.renderLoadingMediaGallery}>
  262. {Component => (
  263. <Component
  264. media={status.get('media_attachments')}
  265. sensitive={status.get('sensitive')}
  266. height={110}
  267. onOpenMedia={this.props.onOpenMedia}
  268. cacheWidth={this.props.cacheMediaWidth}
  269. defaultWidth={this.props.cachedMediaWidth}
  270. />
  271. )}
  272. </Bundle>
  273. );
  274. }
  275. } else if (status.get('spoiler_text').length === 0 && status.get('card')) {
  276. media = (
  277. <Card
  278. onOpenMedia={this.props.onOpenMedia}
  279. card={status.get('card')}
  280. compact
  281. cacheWidth={this.props.cacheMediaWidth}
  282. defaultWidth={this.props.cachedMediaWidth}
  283. />
  284. );
  285. }
  286. if (otherAccounts && otherAccounts.size > 0) {
  287. statusAvatar = <AvatarComposite accounts={otherAccounts} size={48} />;
  288. } else if (account === undefined || account === null) {
  289. statusAvatar = <Avatar account={status.get('account')} size={48} />;
  290. } else {
  291. statusAvatar = <AvatarOverlay account={status.get('account')} friend={account} />;
  292. }
  293. const handlers = this.props.muted ? {} : {
  294. reply: this.handleHotkeyReply,
  295. favourite: this.handleHotkeyFavourite,
  296. boost: this.handleHotkeyBoost,
  297. mention: this.handleHotkeyMention,
  298. open: this.handleHotkeyOpen,
  299. openProfile: this.handleHotkeyOpenProfile,
  300. moveUp: this.handleHotkeyMoveUp,
  301. moveDown: this.handleHotkeyMoveDown,
  302. toggleHidden: this.handleHotkeyToggleHidden,
  303. };
  304. return (
  305. <HotKeys handlers={handlers}>
  306. <div className={classNames('status__wrapper', `status__wrapper-${status.get('visibility')}`, { 'status__wrapper-reply': !!status.get('in_reply_to_id'), read: unread === false, focusable: !this.props.muted })} tabIndex={this.props.muted ? null : 0} data-featured={featured ? 'true' : null} aria-label={textForScreenReader(intl, status, rebloggedByText)} ref={this.handleRef}>
  307. {prepend}
  308. <div className={classNames('status', `status-${status.get('visibility')}`, { 'status-reply': !!status.get('in_reply_to_id'), muted: this.props.muted, read: unread === false })} data-id={status.get('id')}>
  309. <div className='status__info'>
  310. <a href={status.get('url')} className='status__relative-time' target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} /></a>
  311. <a onClick={this.handleAccountClick} target='_blank' data-id={status.getIn(['account', 'id'])} href={status.getIn(['account', 'url'])} title={status.getIn(['account', 'acct'])} className='status__display-name'>
  312. <div className='status__avatar'>
  313. {statusAvatar}
  314. </div>
  315. <DisplayName account={status.get('account')} others={otherAccounts} />
  316. </a>
  317. </div>
  318. <StatusContent status={status} onClick={this.handleClick} expanded={!status.get('hidden')} onExpandedToggle={this.handleExpandedToggle} collapsable />
  319. {media}
  320. {showThread && status.get('in_reply_to_id') && status.get('in_reply_to_account_id') === status.getIn(['account', 'id']) && (
  321. <button className='status__content__read-more-button' onClick={this.handleClick}>
  322. <FormattedMessage id='status.show_thread' defaultMessage='Show thread' />
  323. </button>
  324. )}
  325. <StatusActionBar status={status} account={account} {...other} />
  326. </div>
  327. </div>
  328. </HotKeys>
  329. );
  330. }
  331. }