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.

349 lines
13 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import { connect } from 'react-redux';
  4. import PropTypes from 'prop-types';
  5. import IconButton from './icon_button';
  6. import DropdownMenuContainer from '../containers/dropdown_menu_container';
  7. import { defineMessages, injectIntl } from 'react-intl';
  8. import ImmutablePureComponent from 'react-immutable-pure-component';
  9. import { me, isStaff } from '../initial_state';
  10. import classNames from 'classnames';
  11. const messages = defineMessages({
  12. delete: { id: 'status.delete', defaultMessage: 'Delete' },
  13. redraft: { id: 'status.redraft', defaultMessage: 'Delete & re-draft' },
  14. direct: { id: 'status.direct', defaultMessage: 'Direct message @{name}' },
  15. mention: { id: 'status.mention', defaultMessage: 'Mention @{name}' },
  16. mute: { id: 'account.mute', defaultMessage: 'Mute @{name}' },
  17. block: { id: 'account.block', defaultMessage: 'Block @{name}' },
  18. reply: { id: 'status.reply', defaultMessage: 'Reply' },
  19. comment: {id: 'status.comment', defaultMessage: 'Comment' },
  20. share: { id: 'status.share', defaultMessage: 'Share' },
  21. more: { id: 'status.more', defaultMessage: 'More' },
  22. replyAll: { id: 'status.replyAll', defaultMessage: 'Reply to thread' },
  23. reblog: { id: 'status.reblog', defaultMessage: 'Boost' },
  24. reblog_private: { id: 'status.reblog_private', defaultMessage: 'Boost with original visibility' },
  25. cancel_reblog_private: { id: 'status.cancel_reblog_private', defaultMessage: 'Unboost' },
  26. cannot_reblog: { id: 'status.cannot_reblog', defaultMessage: 'This post cannot be boosted' },
  27. favourite: { id: 'status.favourite', defaultMessage: 'Favourite' },
  28. bookmark: { id: 'status.bookmark', defaultMessage: 'Bookmark' },
  29. removeBookmark: { id: 'status.remove_bookmark', defaultMessage: 'Remove bookmark' },
  30. open: { id: 'status.open', defaultMessage: 'Expand this status' },
  31. report: { id: 'status.report', defaultMessage: 'Report @{name}' },
  32. muteConversation: { id: 'status.mute_conversation', defaultMessage: 'Mute conversation' },
  33. unmuteConversation: { id: 'status.unmute_conversation', defaultMessage: 'Unmute conversation' },
  34. pin: { id: 'status.pin', defaultMessage: 'Pin on profile' },
  35. unpin: { id: 'status.unpin', defaultMessage: 'Unpin from profile' },
  36. embed: { id: 'status.embed', defaultMessage: 'Embed' },
  37. admin_account: { id: 'status.admin_account', defaultMessage: 'Open moderation interface for @{name}' },
  38. admin_status: { id: 'status.admin_status', defaultMessage: 'Open this status in the moderation interface' },
  39. copy: { id: 'status.copy', defaultMessage: 'Copy link to status' },
  40. blockDomain: { id: 'account.block_domain', defaultMessage: 'Block domain {domain}' },
  41. unblockDomain: { id: 'account.unblock_domain', defaultMessage: 'Unblock domain {domain}' },
  42. unmute: { id: 'account.unmute', defaultMessage: 'Unmute @{name}' },
  43. unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
  44. });
  45. const mapStateToProps = (state, { status }) => ({
  46. relationship: state.getIn(['relationships', status.getIn(['account', 'id'])]),
  47. });
  48. export default @connect(mapStateToProps)
  49. @injectIntl
  50. class StatusActionBar extends ImmutablePureComponent {
  51. static contextTypes = {
  52. router: PropTypes.object,
  53. };
  54. static propTypes = {
  55. status: ImmutablePropTypes.map.isRequired,
  56. relationship: ImmutablePropTypes.map,
  57. onReply: PropTypes.func,
  58. onFavourite: PropTypes.func,
  59. onReblog: PropTypes.func,
  60. onDelete: PropTypes.func,
  61. onDirect: PropTypes.func,
  62. onMention: PropTypes.func,
  63. onMute: PropTypes.func,
  64. onUnmute: PropTypes.func,
  65. onBlock: PropTypes.func,
  66. onUnblock: PropTypes.func,
  67. onBlockDomain: PropTypes.func,
  68. onUnblockDomain: PropTypes.func,
  69. onReport: PropTypes.func,
  70. onEmbed: PropTypes.func,
  71. onMuteConversation: PropTypes.func,
  72. onPin: PropTypes.func,
  73. onBookmark: PropTypes.func,
  74. withDismiss: PropTypes.bool,
  75. scrollKey: PropTypes.string,
  76. intl: PropTypes.object.isRequired,
  77. };
  78. // Avoid checking props that are functions (and whose equality will always
  79. // evaluate to false. See react-immutable-pure-component for usage.
  80. updateOnProps = [
  81. 'status',
  82. 'relationship',
  83. 'withDismiss',
  84. ]
  85. handleReplyClick = () => {
  86. if (me) {
  87. this.props.onReply(this.props.status, this.context.router.history);
  88. } else {
  89. this._openInteractionDialog('reply');
  90. }
  91. }
  92. handleShareClick = () => {
  93. navigator.share({
  94. text: this.props.status.get('search_index'),
  95. url: this.props.status.get('url'),
  96. }).catch((e) => {
  97. if (e.name !== 'AbortError') console.error(e);
  98. });
  99. }
  100. handleFavouriteClick = () => {
  101. if (me) {
  102. this.props.onFavourite(this.props.status);
  103. } else {
  104. this._openInteractionDialog('favourite');
  105. }
  106. }
  107. handleReblogClick = e => {
  108. if (me) {
  109. this.props.onReblog(this.props.status, e);
  110. } else {
  111. this._openInteractionDialog('reblog');
  112. }
  113. }
  114. _openInteractionDialog = type => {
  115. window.open(`/interact/${this.props.status.get('id')}?type=${type}`, 'mastodon-intent', 'width=445,height=600,resizable=no,menubar=no,status=no,scrollbars=yes');
  116. }
  117. handleBookmarkClick = () => {
  118. this.props.onBookmark(this.props.status);
  119. }
  120. handleDeleteClick = () => {
  121. this.props.onDelete(this.props.status, this.context.router.history);
  122. }
  123. handleRedraftClick = () => {
  124. this.props.onDelete(this.props.status, this.context.router.history, true);
  125. }
  126. handlePinClick = () => {
  127. this.props.onPin(this.props.status);
  128. }
  129. handleMentionClick = () => {
  130. this.props.onMention(this.props.status.get('account'), this.context.router.history);
  131. }
  132. handleDirectClick = () => {
  133. this.props.onDirect(this.props.status.get('account'), this.context.router.history);
  134. }
  135. handleMuteClick = () => {
  136. const { status, relationship, onMute, onUnmute } = this.props;
  137. const account = status.get('account');
  138. if (relationship && relationship.get('muting')) {
  139. onUnmute(account);
  140. } else {
  141. onMute(account);
  142. }
  143. }
  144. handleBlockClick = () => {
  145. const { status, relationship, onBlock, onUnblock } = this.props;
  146. const account = status.get('account');
  147. if (relationship && relationship.get('blocking')) {
  148. onUnblock(account);
  149. } else {
  150. onBlock(status);
  151. }
  152. }
  153. handleBlockDomain = () => {
  154. const { status, onBlockDomain } = this.props;
  155. const account = status.get('account');
  156. onBlockDomain(account.get('acct').split('@')[1]);
  157. }
  158. handleUnblockDomain = () => {
  159. const { status, onUnblockDomain } = this.props;
  160. const account = status.get('account');
  161. onUnblockDomain(account.get('acct').split('@')[1]);
  162. }
  163. handleOpen = () => {
  164. this.context.router.history.push(`/statuses/${this.props.status.get('id')}`);
  165. }
  166. handleEmbed = () => {
  167. this.props.onEmbed(this.props.status);
  168. }
  169. handleReport = () => {
  170. this.props.onReport(this.props.status);
  171. }
  172. handleConversationMuteClick = () => {
  173. this.props.onMuteConversation(this.props.status);
  174. }
  175. handleCopy = () => {
  176. const url = this.props.status.get('url');
  177. const textarea = document.createElement('textarea');
  178. textarea.textContent = url;
  179. textarea.style.position = 'fixed';
  180. document.body.appendChild(textarea);
  181. try {
  182. textarea.select();
  183. document.execCommand('copy');
  184. } catch (e) {
  185. } finally {
  186. document.body.removeChild(textarea);
  187. }
  188. }
  189. render () {
  190. const { status, relationship, intl, withDismiss, scrollKey } = this.props;
  191. const anonymousAccess = !me;
  192. const publicStatus = ['public', 'unlisted'].includes(status.get('visibility'));
  193. const mutingConversation = status.get('muted');
  194. const account = status.get('account');
  195. const writtenByMe = status.getIn(['account', 'id']) === me;
  196. let menu = [];
  197. menu.push({ text: intl.formatMessage(messages.open), action: this.handleOpen });
  198. if (publicStatus) {
  199. menu.push({ text: intl.formatMessage(messages.copy), action: this.handleCopy });
  200. menu.push({ text: intl.formatMessage(messages.embed), action: this.handleEmbed });
  201. }
  202. menu.push(null);
  203. menu.push({ text: intl.formatMessage(status.get('bookmarked') ? messages.removeBookmark : messages.bookmark), action: this.handleBookmarkClick });
  204. if (writtenByMe && publicStatus) {
  205. menu.push({ text: intl.formatMessage(status.get('pinned') ? messages.unpin : messages.pin), action: this.handlePinClick });
  206. }
  207. menu.push(null);
  208. if (writtenByMe || withDismiss) {
  209. menu.push({ text: intl.formatMessage(mutingConversation ? messages.unmuteConversation : messages.muteConversation), action: this.handleConversationMuteClick });
  210. menu.push(null);
  211. }
  212. if (writtenByMe) {
  213. menu.push({ text: intl.formatMessage(messages.delete), action: this.handleDeleteClick });
  214. menu.push({ text: intl.formatMessage(messages.redraft), action: this.handleRedraftClick });
  215. } else {
  216. menu.push({ text: intl.formatMessage(messages.mention, { name: account.get('username') }), action: this.handleMentionClick });
  217. menu.push({ text: intl.formatMessage(messages.direct, { name: account.get('username') }), action: this.handleDirectClick });
  218. menu.push(null);
  219. if (relationship && relationship.get('muting')) {
  220. menu.push({ text: intl.formatMessage(messages.unmute, { name: account.get('username') }), action: this.handleMuteClick });
  221. } else {
  222. menu.push({ text: intl.formatMessage(messages.mute, { name: account.get('username') }), action: this.handleMuteClick });
  223. }
  224. if (relationship && relationship.get('blocking')) {
  225. menu.push({ text: intl.formatMessage(messages.unblock, { name: account.get('username') }), action: this.handleBlockClick });
  226. } else {
  227. menu.push({ text: intl.formatMessage(messages.block, { name: account.get('username') }), action: this.handleBlockClick });
  228. }
  229. menu.push({ text: intl.formatMessage(messages.report, { name: account.get('username') }), action: this.handleReport });
  230. if (account.get('acct') !== account.get('username')) {
  231. const domain = account.get('acct').split('@')[1];
  232. menu.push(null);
  233. if (relationship && relationship.get('domain_blocking')) {
  234. menu.push({ text: intl.formatMessage(messages.unblockDomain, { domain }), action: this.handleUnblockDomain });
  235. } else {
  236. menu.push({ text: intl.formatMessage(messages.blockDomain, { domain }), action: this.handleBlockDomain });
  237. }
  238. }
  239. if (isStaff) {
  240. menu.push(null);
  241. menu.push({ text: intl.formatMessage(messages.admin_account, { name: account.get('username') }), href: `/admin/accounts/${status.getIn(['account', 'id'])}` });
  242. menu.push({ text: intl.formatMessage(messages.admin_status), href: `/admin/accounts/${status.getIn(['account', 'id'])}/statuses/${status.get('id')}` });
  243. }
  244. }
  245. let replyIcon;
  246. let replyTitle;
  247. if (status.get('in_reply_to_id', null) === null) {
  248. replyIcon = 'comment';
  249. replyTitle = intl.formatMessage(messages.comment);
  250. } else {
  251. replyIcon = 'reply';
  252. replyTitle = intl.formatMessage(messages.reply);
  253. }
  254. const reblogPrivate = status.getIn(['account', 'id']) === me && status.get('visibility') === 'private';
  255. let reblogTitle = '';
  256. if (status.get('reblogged')) {
  257. reblogTitle = intl.formatMessage(messages.cancel_reblog_private);
  258. } else if (publicStatus) {
  259. reblogTitle = intl.formatMessage(messages.reblog);
  260. } else if (reblogPrivate) {
  261. reblogTitle = intl.formatMessage(messages.reblog_private);
  262. } else {
  263. reblogTitle = intl.formatMessage(messages.cannot_reblog);
  264. }
  265. const shareButton = ('share' in navigator) && publicStatus && (
  266. <IconButton className='status__action-bar-button' title={intl.formatMessage(messages.share)} icon='share-alt' onClick={this.handleShareClick} />
  267. );
  268. return (
  269. <div className='status__action-bar'>
  270. <IconButton className='status__action-bar-button' title={replyTitle} icon={status.get('in_reply_to_account_id') === status.getIn(['account', 'id']) ? 'comment-o' : replyIcon} onClick={this.handleReplyClick} counter={status.get('replies_count')} />
  271. <IconButton className={classNames('status__action-bar-button', { reblogPrivate })} disabled={!publicStatus && !reblogPrivate} active={status.get('reblogged')} pressed={status.get('reblogged')} title={reblogTitle} icon='retweet' onClick={this.handleReblogClick} counter={status.get('reblogs_count')} />
  272. <IconButton className='status__action-bar-button star-icon' animate active={status.get('favourited')} pressed={status.get('favourited')} title={intl.formatMessage(messages.favourite)} icon='heart' onClick={this.handleFavouriteClick} counter={status.get('favourites_count')} />
  273. {shareButton}
  274. <div className='status__action-bar-dropdown'>
  275. <DropdownMenuContainer
  276. scrollKey={scrollKey}
  277. disabled={anonymousAccess}
  278. status={status}
  279. items={menu}
  280. icon='ellipsis-h'
  281. size={18}
  282. direction='right'
  283. title={intl.formatMessage(messages.more)}
  284. />
  285. </div>
  286. </div>
  287. );
  288. }
  289. }