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.
 
 
 
 

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