闭社主体 forked from https://github.com/tootsuite/mastodon
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.

289 lines
11 KiB

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