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.

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