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.
 
 
 
 

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