forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from lnanase/add_announcement_manager
お知らせ動的対応(二コフレver)
- Loading branch information
Showing
38 changed files
with
467 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class AnnouncementsController < BaseController | ||
before_action :set_announcement, only: [:edit, :update, :destroy] | ||
after_action :publish_updates, only: [:create, :update, :destroy] | ||
|
||
def index | ||
@announcements = Announcement.all | ||
end | ||
|
||
def new | ||
@announcement = Announcement.new | ||
fill_links! | ||
end | ||
|
||
def edit | ||
fill_links! | ||
end | ||
|
||
def create | ||
@announcement = Announcement.new(announcement_params) | ||
if @announcement.save | ||
redirect_to :admin_announcements | ||
else | ||
fill_links! | ||
render :new | ||
end | ||
end | ||
|
||
def update | ||
if @announcement.update(announcement_params) | ||
redirect_to :admin_announcements | ||
else | ||
fill_links! | ||
render :edit | ||
end | ||
end | ||
|
||
def destroy | ||
@announcement.destroy | ||
redirect_to admin_announcements_path | ||
end | ||
|
||
private | ||
|
||
def set_announcement | ||
@announcement = Announcement.find(params[:id]) | ||
end | ||
|
||
def announcement_params | ||
params.require(:announcement).permit(:body, :order, links_attributes: [:id, :text, :url]).tap do |x| | ||
x[:links_attributes].each do |n, link| | ||
link[:_destroy] = 1 if link[:id].present? && link[:url].blank? && link[:text].blank? | ||
end | ||
end | ||
end | ||
|
||
def fill_links! | ||
@announcement.links << (3 - @announcement.links.length).times.map { AnnouncementLink.new } | ||
end | ||
|
||
def publish_updates | ||
AnnouncementPublishWorker.perform_async | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const TOGGLE_ANNOUNCEMENTS = 'TOGGLE_ANNOUNCEMENTS'; | ||
export const UPDATE_ANNOUNCEMENTS = 'UPDATE_ANNOUNCEMENTS'; | ||
|
||
export function toggleAnnouncements() { | ||
return { | ||
type: TOGGLE_ANNOUNCEMENTS, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { connectStream } from '../stream'; | ||
import { UPDATE_ANNOUNCEMENTS } from './announcements'; | ||
|
||
export function connectCommandStream(pollingRefresh = null) { | ||
return connectStream('commands', pollingRefresh, (dispatch) => ({ | ||
onReceive(data) { | ||
switch(data.event) { | ||
case 'announcements': | ||
dispatch({ | ||
type: UPDATE_ANNOUNCEMENTS, | ||
data: JSON.parse(data.payload), | ||
}); | ||
break; | ||
default: | ||
return; | ||
} | ||
}, | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 41 additions & 81 deletions
122
app/javascript/mastodon/features/compose/components/announcements.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,56 @@ | ||
import React from 'react'; | ||
import Immutable from 'immutable'; | ||
import PropTypes from 'prop-types'; | ||
import ImmutablePropTypes from 'react-immutable-proptypes'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import Link from 'react-router-dom/Link'; | ||
import { defineMessages, injectIntl } from 'react-intl'; | ||
import IconButton from '../../../components/announcement_icon_button'; | ||
import Motion from 'react-motion/lib/Motion'; | ||
import spring from 'react-motion/lib/spring'; | ||
|
||
const Collapsable = ({ fullHeight, minHeight, isVisible, children }) => ( | ||
<Motion defaultStyle={{ height: isVisible ? fullHeight : minHeight }} style={{ height: spring(!isVisible ? minHeight : fullHeight) }}> | ||
{({ height }) => | ||
<div style={{ height: `${height}px`, overflow: 'hidden' }}> | ||
{children} | ||
</div> | ||
} | ||
</Motion> | ||
); | ||
|
||
Collapsable.propTypes = { | ||
fullHeight: PropTypes.number.isRequired, | ||
minHeight: PropTypes.number.isRequired, | ||
isVisible: PropTypes.bool.isRequired, | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
const messages = defineMessages({ | ||
toggle_visible: { id: 'media_gallery.toggle_visible', defaultMessage: 'Toggle visibility' }, | ||
welcome: { id: 'welcome.message', defaultMessage: 'Welcome to {domain}!' }, | ||
}); | ||
|
||
const hashtags = Immutable.fromJS([ | ||
'Pの自己紹介', | ||
'みんなのP名刺', | ||
]); | ||
|
||
class Announcements extends React.PureComponent { | ||
export default class Announcements extends React.PureComponent { | ||
|
||
static propTypes = { | ||
intl: PropTypes.object.isRequired, | ||
homeSize: PropTypes.number, | ||
isLoading: PropTypes.bool, | ||
}; | ||
|
||
state = { | ||
show: false, | ||
isLoaded: false, | ||
visible: PropTypes.bool.isRequired, | ||
onToggle: PropTypes.func.isRequired, | ||
announcements: ImmutablePropTypes.list.isRequired, | ||
}; | ||
|
||
onClick = () => { | ||
this.setState({ show: !this.state.show }); | ||
} | ||
nl2br (text) { | ||
return text.split(/(\n)/g).map((line, i) => { | ||
if (line.match(/(\n)/g)) { | ||
return React.createElement('br', { key: i }); | ||
} | ||
return line; | ||
}); | ||
} | ||
|
||
render () { | ||
const { intl } = this.props; | ||
const { visible, onToggle, announcements } = this.props; | ||
const caretClass = visible ? 'fa fa-caret-down' : 'fa fa-caret-up'; | ||
|
||
return ( | ||
<ul className='announcements'> | ||
<li> | ||
<Collapsable isVisible={this.state.show} fullHeight={300} minHeight={20} > | ||
<div className='announcements__body'> | ||
<p>{ this.nl2br(intl.formatMessage(messages.welcome, { domain: document.title }))}</p> | ||
{hashtags.map((hashtag, i) => | ||
<Link key={i} to={`/timelines/tag/${hashtag}`} tabIndex={this.state.show ? undefined : -1}> | ||
#{hashtag} | ||
</Link> | ||
)} | ||
</div> | ||
</Collapsable> | ||
<div className='announcements__icon'> | ||
<IconButton title={intl.formatMessage(messages.toggle_visible)} icon='caret-up' onClick={this.onClick} size={20} animate active={this.state.show} /> | ||
</div> | ||
</li> | ||
</ul> | ||
<div className='announcements'> | ||
<div className='compose__extra__header'> | ||
<i className='fa fa-bell' /> | ||
<FormattedMessage id='announcement.title' defaultMessage='information' /> | ||
<button className='compose__extra__header__icon' onClick={onToggle} > | ||
<i className={caretClass} /> | ||
</button> | ||
</div> | ||
{ visible && ( | ||
<ul> | ||
{announcements.map((announcement, idx) => ( | ||
<li key={idx}> | ||
<div className='announcements__body'> | ||
<p dangerouslySetInnerHTML={{ __html: announcement.get('body') }} /> | ||
<div className='links'> | ||
{announcement.get('links').map((link, i) => { | ||
if (link.get('url').indexOf('/') === 0) { | ||
return ( | ||
<Link to={link.get('url')} key={i}>{link.get('text')}</Link> | ||
); | ||
} else { | ||
return ( | ||
<a href={link.get('url')} target='_blank' key={i}>{link.get('text')}</a> | ||
); | ||
} | ||
})} | ||
</div> | ||
</div> | ||
</li> | ||
))} | ||
</ul> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
componentWillReceiveProps (nextProps) { | ||
if (!this.state.isLoaded) { | ||
if (!nextProps.isLoading && (nextProps.homeSize === 0 || this.props.homeSize !== nextProps.homeSize)) { | ||
this.setState({ show: nextProps.homeSize < 5, isLoaded: true }); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
export default injectIntl(Announcements); |
19 changes: 12 additions & 7 deletions
19
app/javascript/mastodon/features/compose/containers/announcements_container.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import { connect } from 'react-redux'; | ||
import Announcements from '../components/announcements'; | ||
import { toggleAnnouncements } from '../../../actions/announcements'; | ||
|
||
const mapStateToProps = state => { | ||
return { | ||
homeSize: state.getIn(['timelines', 'home', 'items']).size, | ||
isLoading: state.getIn(['timelines', 'home', 'isLoading']), | ||
}; | ||
}; | ||
const mapStateToProps = (state) => ({ | ||
visible: state.getIn(['announcements', 'visible']), | ||
announcements: state.getIn(['announcements', 'list']), | ||
}); | ||
|
||
export default connect(mapStateToProps)(Announcements); | ||
const mapDispatchToProps = (dispatch) => ({ | ||
onToggle () { | ||
dispatch(toggleAnnouncements()); | ||
}, | ||
}); | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(Announcements); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { STORE_HYDRATE } from '../actions/store'; | ||
import Immutable from 'immutable'; | ||
import { | ||
TOGGLE_ANNOUNCEMENTS, | ||
UPDATE_ANNOUNCEMENTS, | ||
} from '../actions/announcements'; | ||
|
||
const initialState = Immutable.Map({ | ||
visible: true, | ||
list: Immutable.List(), | ||
}); | ||
|
||
export default function announcements(state = initialState, action) { | ||
switch(action.type) { | ||
case STORE_HYDRATE: | ||
return state.set('list', action.state.get('announcements')); | ||
case UPDATE_ANNOUNCEMENTS: | ||
return state.set('list', Immutable.fromJS(action.data)); | ||
case TOGGLE_ANNOUNCEMENTS: | ||
return state.set('visible', !state.get('visible')); | ||
default: | ||
return state; | ||
} | ||
}; |
Oops, something went wrong.