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.

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