@announcement = Announcement.new(resource_params)
if @announcement.save
+ PublishScheduledAnnouncementWorker.perform_async(@announcement.id) if @announcement.published?
log_action :create, @announcement
redirect_to admin_announcements_path
else
authorize :announcement, :update?
if @announcement.update(resource_params)
+ PublishScheduledAnnouncementWorker.perform_async(@announcement.id) if @announcement.published?
log_action :update, @announcement
redirect_to admin_announcements_path
else
def destroy
authorize :announcement, :destroy?
@announcement.destroy!
+ UnpublishAnnouncementWorker.perform_async(@announcement.id) if @announcement.published?
log_action :destroy, @announcement
redirect_to admin_announcements_path
end
export const ANNOUNCEMENTS_FETCH_SUCCESS = 'ANNOUNCEMENTS_FETCH_SUCCESS';
export const ANNOUNCEMENTS_FETCH_FAIL = 'ANNOUNCEMENTS_FETCH_FAIL';
export const ANNOUNCEMENTS_UPDATE = 'ANNOUNCEMENTS_UPDATE';
+export const ANNOUNCEMENTS_DELETE = 'ANNOUNCEMENTS_DELETE';
export const ANNOUNCEMENTS_REACTION_ADD_REQUEST = 'ANNOUNCEMENTS_REACTION_ADD_REQUEST';
export const ANNOUNCEMENTS_REACTION_ADD_SUCCESS = 'ANNOUNCEMENTS_REACTION_ADD_SUCCESS';
reaction,
});
-export function toggleShowAnnouncements() {
- return dispatch => {
- dispatch({ type: ANNOUNCEMENTS_TOGGLE_SHOW });
- };
-}
+export const toggleShowAnnouncements = () => ({
+ type: ANNOUNCEMENTS_TOGGLE_SHOW,
+});
+
+export const deleteAnnouncement = id => ({
+ type: ANNOUNCEMENTS_DELETE,
+ id,
+});
} from './timelines';
import { updateNotifications, expandNotifications } from './notifications';
import { updateConversations } from './conversations';
-import { fetchAnnouncements, updateAnnouncements, updateReaction as updateAnnouncementsReaction } from './announcements';
+import {
+ fetchAnnouncements,
+ updateAnnouncements,
+ updateReaction as updateAnnouncementsReaction,
+ deleteAnnouncement,
+} from './announcements';
import { fetchFilters } from './filters';
import { getLocale } from '../locales';
case 'announcement.reaction':
dispatch(updateAnnouncementsReaction(JSON.parse(data.payload)));
break;
+ case 'announcement.delete':
+ dispatch(deleteAnnouncement(data.payload));
+ break;
}
},
};
}
const styles = [{
- key: value,
+ key: `${value}`,
+ data: value,
style: { y: spring(0, { damping: 35, stiffness: 400 }) },
}];
<TransitionMotion styles={styles} willEnter={this.willEnter} willLeave={this.willLeave}>
{items => (
<span className='animated-number'>
- {items.map(({ key, style }) => (
- <span key={key} style={{ position: style.y > 0 ? 'absolute' : 'static', transform: `translateY(${style.y * 100}%)` }}><FormattedNumber value={key} /></span>
+ {items.map(({ key, data, style }) => (
+ <span key={key} style={{ position: style.y > 0 ? 'absolute' : 'static', transform: `translateY(${style.y * 100}%)` }}><FormattedNumber value={data} /></span>
))}
</span>
)}
))}
</ReactSwipeableViews>
- <div className='announcements__pagination'>
- <IconButton disabled={announcements.size === 1} title={intl.formatMessage(messages.previous)} icon='chevron-left' onClick={this.handlePrevClick} size={13} />
- <span>{index + 1} / {announcements.size}</span>
- <IconButton disabled={announcements.size === 1} title={intl.formatMessage(messages.next)} icon='chevron-right' onClick={this.handleNextClick} size={13} />
- </div>
+ {announcements.size > 1 && (
+ <div className='announcements__pagination'>
+ <IconButton disabled={announcements.size === 1} title={intl.formatMessage(messages.previous)} icon='chevron-left' onClick={this.handlePrevClick} size={13} />
+ <span>{index + 1} / {announcements.size}</span>
+ <IconButton disabled={announcements.size === 1} title={intl.formatMessage(messages.next)} icon='chevron-right' onClick={this.handleNextClick} size={13} />
+ </div>
+ )}
</div>
</div>
);
ANNOUNCEMENTS_REACTION_REMOVE_REQUEST,
ANNOUNCEMENTS_REACTION_REMOVE_FAIL,
ANNOUNCEMENTS_TOGGLE_SHOW,
+ ANNOUNCEMENTS_DELETE,
} from '../actions/announcements';
import { Map as ImmutableMap, List as ImmutableList, Set as ImmutableSet, fromJS } from 'immutable';
const updateReaction = (state, id, name, updater) => state.update('items', list => list.map(announcement => {
if (announcement.get('id') === id) {
return announcement.update('reactions', reactions => {
- if (reactions.find(reaction => reaction.get('name') === name)) {
- return reactions.map(reaction => {
- if (reaction.get('name') === name) {
- return updater(reaction);
- }
-
- return reaction;
- });
+ const idx = reactions.findIndex(reaction => reaction.get('name') === name);
+
+ if (idx > -1) {
+ return reactions.update(idx, reaction => updater(reaction));
}
return reactions.push(updater(fromJS({ name, count: 0 })));
const removeReaction = (state, id, name) => updateReaction(state, id, name, x => x.set('me', false).update('count', y => y - 1));
const addUnread = (state, items) => {
- if (state.get('show')) return state;
+ if (state.get('show')) {
+ return state;
+ }
const newIds = ImmutableSet(items.map(x => x.get('id')));
const oldIds = ImmutableSet(state.get('items').map(x => x.get('id')));
+
return state.update('unread', unread => unread.union(newIds.subtract(oldIds)));
};
+const sortAnnouncements = list => list.sortBy(x => x.get('starts_at') || x.get('published_at'));
+
+const updateAnnouncement = (state, announcement) => {
+ const idx = state.get('items').findIndex(x => x.get('id') === announcement.get('id'));
+
+ state = addUnread(state, [announcement]);
+
+ if (idx > -1) {
+ // Deep merge is used because announcements from the streaming API do not contain
+ // personalized data about which reactions have been selected by the given user,
+ // and that is information we want to preserve
+ return state.update('items', list => sortAnnouncements(list.update(idx, x => x.mergeDeep(announcement))));
+ }
+
+ return state.update('items', list => sortAnnouncements(list.unshift(announcement)));
+};
+
export default function announcementsReducer(state = initialState, action) {
switch(action.type) {
case ANNOUNCEMENTS_TOGGLE_SHOW:
case ANNOUNCEMENTS_FETCH_SUCCESS:
return state.withMutations(map => {
const items = fromJS(action.announcements);
+
map.set('unread', ImmutableSet());
- addUnread(map, items);
map.set('items', items);
map.set('isLoading', false);
+
+ addUnread(map, items);
});
case ANNOUNCEMENTS_FETCH_FAIL:
return state.set('isLoading', false);
case ANNOUNCEMENTS_UPDATE:
- return addUnread(state, [fromJS(action.announcement)]).update('items', list => list.unshift(fromJS(action.announcement)).sortBy(announcement => announcement.get('starts_at')));
+ return updateAnnouncement(state, fromJS(action.announcement));
case ANNOUNCEMENTS_REACTION_UPDATE:
return updateReactionCount(state, action.reaction);
case ANNOUNCEMENTS_REACTION_ADD_REQUEST:
case ANNOUNCEMENTS_REACTION_REMOVE_REQUEST:
case ANNOUNCEMENTS_REACTION_ADD_FAIL:
return removeReaction(state, action.id, action.name);
+ case ANNOUNCEMENTS_DELETE:
+ return state.update('unread', set => set.delete(action.id)).update('items', list => {
+ const idx = list.findIndex(x => x.get('id') === action.id);
+
+ if (idx > -1) {
+ return list.delete(idx);
+ }
+
+ return list;
+ });
default:
return state;
}
# Must be <= MAX_ITEMS or the tracking sets will grow forever
REBLOG_FALLOFF = 40
+ def with_active_accounts(&block)
+ Account.joins(:user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago).find_each(&block)
+ end
+
def key(type, id, subtype = nil)
return "feed:#{type}:#{id}" unless subtype
#
class Announcement < ApplicationRecord
- after_commit :queue_publish, on: :create
-
scope :unpublished, -> { where(published: false) }
scope :published, -> { where(published: true) }
scope :without_muted, ->(account) { joins("LEFT OUTER JOIN announcement_mutes ON announcement_mutes.announcement_id = announcements.id AND announcement_mutes.account_id = #{account.id}").where('announcement_mutes.id IS NULL') }
validates :ends_at, presence: true, if: -> { starts_at.present? }
before_validation :set_all_day
- before_validation :set_starts_at, on: :create
- before_validation :set_ends_at, on: :create
+ before_validation :set_published, on: :create
+
+ def published_at
+ scheduled_at || created_at
+ end
def time_range?
starts_at.present? && ends_at.present?
self.all_day = false if starts_at.blank? || ends_at.blank?
end
- def set_starts_at
- self.starts_at = starts_at.change(hour: 0, min: 0, sec: 0) if all_day? && starts_at.present?
- end
-
- def set_ends_at
- self.ends_at = ends_at.change(hour: 23, min: 59, sec: 59) if all_day? && ends_at.present?
- end
-
- def queue_publish
- PublishScheduledAnnouncementWorker.perform_async(id) if scheduled_at.blank?
+ def set_published
+ self.published = true if scheduled_at.blank? || scheduled_at.past?
end
end
# frozen_string_literal: true
class REST::AnnouncementSerializer < ActiveModel::Serializer
- attributes :id, :content, :starts_at, :ends_at, :all_day
+ attributes :id, :content, :starts_at, :ends_at, :all_day,
+ :published_at, :updated_at
has_many :mentions
has_many :tags, serializer: REST::StatusSerializer::TagSerializer
payload = InlineRenderer.render(reaction, nil, :reaction).tap { |h| h[:announcement_id] = announcement_id.to_s }
payload = Oj.dump(event: :'announcement.reaction', payload: payload)
- Account.joins(:user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago).find_each do |account|
+ FeedManager.instance.with_active_accounts do |account|
redis.publish("timeline:#{account.id}", payload) if redis.exists("subscribed:timeline:#{account.id}")
end
rescue ActiveRecord::RecordNotFound
payload = InlineRenderer.render(announcement, nil, :announcement)
payload = Oj.dump(event: :announcement, payload: payload)
- Account.joins(:user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago).find_each do |account|
+ FeedManager.instance.with_active_accounts do |account|
redis.publish("timeline:#{account.id}", payload) if redis.exists("subscribed:timeline:#{account.id}")
end
end
--- /dev/null
+# frozen_string_literal: true
+
+class UnpublishAnnouncementWorker
+ include Sidekiq::Worker
+ include Redisable
+
+ def perform(announcement_id)
+ payload = Oj.dump(event: :'announcement.delete', payload: announcement_id.to_s)
+
+ FeedManager.instance.with_active_accounts do |account|
+ redis.publish("timeline:#{account.id}", payload) if redis.exists("subscribed:timeline:#{account.id}")
+ end
+ end
+end