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.

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