[Glitch] Add streaming API updates for announcements being modified or deleted
Port b9d74d407673a6dbdc87c3310618b22c85358c85 to glitch-soc Signed-off-by: Thibaut Girka <thib@sitedethib.com>
This commit is contained in:
		
							parent
							
								
									8a8936725e
								
							
						
					
					
						commit
						e36d0a98cc
					
				@ -5,6 +5,7 @@ export const ANNOUNCEMENTS_FETCH_REQUEST = 'ANNOUNCEMENTS_FETCH_REQUEST';
 | 
				
			|||||||
export const ANNOUNCEMENTS_FETCH_SUCCESS = 'ANNOUNCEMENTS_FETCH_SUCCESS';
 | 
					export const ANNOUNCEMENTS_FETCH_SUCCESS = 'ANNOUNCEMENTS_FETCH_SUCCESS';
 | 
				
			||||||
export const ANNOUNCEMENTS_FETCH_FAIL    = 'ANNOUNCEMENTS_FETCH_FAIL';
 | 
					export const ANNOUNCEMENTS_FETCH_FAIL    = 'ANNOUNCEMENTS_FETCH_FAIL';
 | 
				
			||||||
export const ANNOUNCEMENTS_UPDATE        = 'ANNOUNCEMENTS_UPDATE';
 | 
					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_REQUEST = 'ANNOUNCEMENTS_REACTION_ADD_REQUEST';
 | 
				
			||||||
export const ANNOUNCEMENTS_REACTION_ADD_SUCCESS = 'ANNOUNCEMENTS_REACTION_ADD_SUCCESS';
 | 
					export const ANNOUNCEMENTS_REACTION_ADD_SUCCESS = 'ANNOUNCEMENTS_REACTION_ADD_SUCCESS';
 | 
				
			||||||
@ -139,8 +140,11 @@ export const updateReaction = reaction => ({
 | 
				
			|||||||
  reaction,
 | 
					  reaction,
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function toggleShowAnnouncements() {
 | 
					export const toggleShowAnnouncements = () => ({
 | 
				
			||||||
  return dispatch => {
 | 
					  type: ANNOUNCEMENTS_TOGGLE_SHOW,
 | 
				
			||||||
    dispatch({ type: ANNOUNCEMENTS_TOGGLE_SHOW });
 | 
					});
 | 
				
			||||||
  };
 | 
					
 | 
				
			||||||
}
 | 
					export const deleteAnnouncement = id => ({
 | 
				
			||||||
 | 
					  type: ANNOUNCEMENTS_DELETE,
 | 
				
			||||||
 | 
					  id,
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -8,7 +8,12 @@ import {
 | 
				
			|||||||
} from './timelines';
 | 
					} from './timelines';
 | 
				
			||||||
import { updateNotifications, expandNotifications } from './notifications';
 | 
					import { updateNotifications, expandNotifications } from './notifications';
 | 
				
			||||||
import { updateConversations } from './conversations';
 | 
					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 { fetchFilters } from './filters';
 | 
				
			||||||
import { getLocale } from 'mastodon/locales';
 | 
					import { getLocale } from 'mastodon/locales';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -51,6 +56,9 @@ export function connectTimelineStream (timelineId, path, pollingRefresh = null,
 | 
				
			|||||||
        case 'announcement.reaction':
 | 
					        case 'announcement.reaction':
 | 
				
			||||||
          dispatch(updateAnnouncementsReaction(JSON.parse(data.payload)));
 | 
					          dispatch(updateAnnouncementsReaction(JSON.parse(data.payload)));
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
 | 
					        case 'announcement.delete':
 | 
				
			||||||
 | 
					          dispatch(deleteAnnouncement(data.payload));
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
				
			|||||||
@ -27,7 +27,8 @@ export default class AnimatedNumber extends React.PureComponent {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const styles = [{
 | 
					    const styles = [{
 | 
				
			||||||
      key: value,
 | 
					      key: `${value}`,
 | 
				
			||||||
 | 
					      data: value,
 | 
				
			||||||
      style: { y: spring(0, { damping: 35, stiffness: 400 }) },
 | 
					      style: { y: spring(0, { damping: 35, stiffness: 400 }) },
 | 
				
			||||||
    }];
 | 
					    }];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -35,8 +36,8 @@ export default class AnimatedNumber extends React.PureComponent {
 | 
				
			|||||||
      <TransitionMotion styles={styles} willEnter={this.willEnter} willLeave={this.willLeave}>
 | 
					      <TransitionMotion styles={styles} willEnter={this.willEnter} willLeave={this.willLeave}>
 | 
				
			||||||
        {items => (
 | 
					        {items => (
 | 
				
			||||||
          <span className='animated-number'>
 | 
					          <span className='animated-number'>
 | 
				
			||||||
            {items.map(({ key, style }) => (
 | 
					            {items.map(({ key, data, style }) => (
 | 
				
			||||||
              <span key={key} style={{ position: style.y > 0 ? 'absolute' : 'static', transform: `translateY(${style.y * 100}%)` }}><FormattedNumber value={key} /></span>
 | 
					              <span key={key} style={{ position: style.y > 0 ? 'absolute' : 'static', transform: `translateY(${style.y * 100}%)` }}><FormattedNumber value={data} /></span>
 | 
				
			||||||
            ))}
 | 
					            ))}
 | 
				
			||||||
          </span>
 | 
					          </span>
 | 
				
			||||||
        )}
 | 
					        )}
 | 
				
			||||||
 | 
				
			|||||||
@ -367,11 +367,13 @@ class Announcements extends ImmutablePureComponent {
 | 
				
			|||||||
            ))}
 | 
					            ))}
 | 
				
			||||||
          </ReactSwipeableViews>
 | 
					          </ReactSwipeableViews>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          <div className='announcements__pagination'>
 | 
					          {announcements.size > 1 && (
 | 
				
			||||||
            <IconButton disabled={announcements.size === 1} title={intl.formatMessage(messages.previous)} icon='chevron-left' onClick={this.handlePrevClick} size={13} />
 | 
					            <div className='announcements__pagination'>
 | 
				
			||||||
            <span>{index + 1} / {announcements.size}</span>
 | 
					              <IconButton disabled={announcements.size === 1} title={intl.formatMessage(messages.previous)} icon='chevron-left' onClick={this.handlePrevClick} size={13} />
 | 
				
			||||||
            <IconButton disabled={announcements.size === 1} title={intl.formatMessage(messages.next)} icon='chevron-right' onClick={this.handleNextClick} size={13} />
 | 
					              <span>{index + 1} / {announcements.size}</span>
 | 
				
			||||||
          </div>
 | 
					              <IconButton disabled={announcements.size === 1} title={intl.formatMessage(messages.next)} icon='chevron-right' onClick={this.handleNextClick} size={13} />
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					          )}
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
      </div>
 | 
					      </div>
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
				
			|||||||
@ -9,6 +9,7 @@ import {
 | 
				
			|||||||
  ANNOUNCEMENTS_REACTION_REMOVE_REQUEST,
 | 
					  ANNOUNCEMENTS_REACTION_REMOVE_REQUEST,
 | 
				
			||||||
  ANNOUNCEMENTS_REACTION_REMOVE_FAIL,
 | 
					  ANNOUNCEMENTS_REACTION_REMOVE_FAIL,
 | 
				
			||||||
  ANNOUNCEMENTS_TOGGLE_SHOW,
 | 
					  ANNOUNCEMENTS_TOGGLE_SHOW,
 | 
				
			||||||
 | 
					  ANNOUNCEMENTS_DELETE,
 | 
				
			||||||
} from '../actions/announcements';
 | 
					} from '../actions/announcements';
 | 
				
			||||||
import { Map as ImmutableMap, List as ImmutableList, Set as ImmutableSet, fromJS } from 'immutable';
 | 
					import { Map as ImmutableMap, List as ImmutableList, Set as ImmutableSet, fromJS } from 'immutable';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -22,14 +23,10 @@ const initialState = ImmutableMap({
 | 
				
			|||||||
const updateReaction = (state, id, name, updater) => state.update('items', list => list.map(announcement => {
 | 
					const updateReaction = (state, id, name, updater) => state.update('items', list => list.map(announcement => {
 | 
				
			||||||
  if (announcement.get('id') === id) {
 | 
					  if (announcement.get('id') === id) {
 | 
				
			||||||
    return announcement.update('reactions', reactions => {
 | 
					    return announcement.update('reactions', reactions => {
 | 
				
			||||||
      if (reactions.find(reaction => reaction.get('name') === name)) {
 | 
					      const idx = reactions.findIndex(reaction => reaction.get('name') === name);
 | 
				
			||||||
        return reactions.map(reaction => {
 | 
					 | 
				
			||||||
          if (reaction.get('name') === name) {
 | 
					 | 
				
			||||||
            return updater(reaction);
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
          return reaction;
 | 
					      if (idx > -1) {
 | 
				
			||||||
        });
 | 
					        return reactions.update(idx, reaction => updater(reaction));
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return reactions.push(updater(fromJS({ name, count: 0 })));
 | 
					      return reactions.push(updater(fromJS({ name, count: 0 })));
 | 
				
			||||||
@ -46,13 +43,33 @@ const addReaction = (state, id, name) => updateReaction(state, id, name, x => x.
 | 
				
			|||||||
const removeReaction = (state, id, name) => updateReaction(state, id, name, x => x.set('me', false).update('count', y => y - 1));
 | 
					const removeReaction = (state, id, name) => updateReaction(state, id, name, x => x.set('me', false).update('count', y => y - 1));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const addUnread = (state, items) => {
 | 
					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 newIds = ImmutableSet(items.map(x => x.get('id')));
 | 
				
			||||||
  const oldIds = ImmutableSet(state.get('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)));
 | 
					  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) {
 | 
					export default function announcementsReducer(state = initialState, action) {
 | 
				
			||||||
  switch(action.type) {
 | 
					  switch(action.type) {
 | 
				
			||||||
  case ANNOUNCEMENTS_TOGGLE_SHOW:
 | 
					  case ANNOUNCEMENTS_TOGGLE_SHOW:
 | 
				
			||||||
@ -65,15 +82,17 @@ export default function announcementsReducer(state = initialState, action) {
 | 
				
			|||||||
  case ANNOUNCEMENTS_FETCH_SUCCESS:
 | 
					  case ANNOUNCEMENTS_FETCH_SUCCESS:
 | 
				
			||||||
    return state.withMutations(map => {
 | 
					    return state.withMutations(map => {
 | 
				
			||||||
      const items = fromJS(action.announcements);
 | 
					      const items = fromJS(action.announcements);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      map.set('unread', ImmutableSet());
 | 
					      map.set('unread', ImmutableSet());
 | 
				
			||||||
      addUnread(map, items);
 | 
					 | 
				
			||||||
      map.set('items', items);
 | 
					      map.set('items', items);
 | 
				
			||||||
      map.set('isLoading', false);
 | 
					      map.set('isLoading', false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      addUnread(map, items);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  case ANNOUNCEMENTS_FETCH_FAIL:
 | 
					  case ANNOUNCEMENTS_FETCH_FAIL:
 | 
				
			||||||
    return state.set('isLoading', false);
 | 
					    return state.set('isLoading', false);
 | 
				
			||||||
  case ANNOUNCEMENTS_UPDATE:
 | 
					  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:
 | 
					  case ANNOUNCEMENTS_REACTION_UPDATE:
 | 
				
			||||||
    return updateReactionCount(state, action.reaction);
 | 
					    return updateReactionCount(state, action.reaction);
 | 
				
			||||||
  case ANNOUNCEMENTS_REACTION_ADD_REQUEST:
 | 
					  case ANNOUNCEMENTS_REACTION_ADD_REQUEST:
 | 
				
			||||||
@ -82,6 +101,16 @@ export default function announcementsReducer(state = initialState, action) {
 | 
				
			|||||||
  case ANNOUNCEMENTS_REACTION_REMOVE_REQUEST:
 | 
					  case ANNOUNCEMENTS_REACTION_REMOVE_REQUEST:
 | 
				
			||||||
  case ANNOUNCEMENTS_REACTION_ADD_FAIL:
 | 
					  case ANNOUNCEMENTS_REACTION_ADD_FAIL:
 | 
				
			||||||
    return removeReaction(state, action.id, action.name);
 | 
					    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:
 | 
					  default:
 | 
				
			||||||
    return state;
 | 
					    return state;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user