The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

125 lines
3.3 KiB

  1. import api, { getLinks } from '../api'
  2. import Immutable from 'immutable';
  3. import { fetchRelationships } from './accounts';
  4. export const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE';
  5. export const NOTIFICATIONS_REFRESH_REQUEST = 'NOTIFICATIONS_REFRESH_REQUEST';
  6. export const NOTIFICATIONS_REFRESH_SUCCESS = 'NOTIFICATIONS_REFRESH_SUCCESS';
  7. export const NOTIFICATIONS_REFRESH_FAIL = 'NOTIFICATIONS_REFRESH_FAIL';
  8. export const NOTIFICATIONS_EXPAND_REQUEST = 'NOTIFICATIONS_EXPAND_REQUEST';
  9. export const NOTIFICATIONS_EXPAND_SUCCESS = 'NOTIFICATIONS_EXPAND_SUCCESS';
  10. export const NOTIFICATIONS_EXPAND_FAIL = 'NOTIFICATIONS_EXPAND_FAIL';
  11. const fetchRelatedRelationships = (dispatch, notifications) => {
  12. const accountIds = notifications.filter(item => item.type === 'follow').map(item => item.account.id);
  13. if (accountIds > 0) {
  14. dispatch(fetchRelationships(accountIds));
  15. }
  16. };
  17. export function updateNotifications(notification) {
  18. return dispatch => {
  19. dispatch({
  20. type: NOTIFICATIONS_UPDATE,
  21. notification,
  22. account: notification.account,
  23. status: notification.status
  24. });
  25. fetchRelatedRelationships(dispatch, [notification]);
  26. };
  27. };
  28. export function refreshNotifications() {
  29. return (dispatch, getState) => {
  30. dispatch(refreshNotificationsRequest());
  31. const params = {};
  32. const ids = getState().getIn(['notifications', 'items']);
  33. if (ids.size > 0) {
  34. params.since_id = ids.first().get('id');
  35. }
  36. api(getState).get('/api/v1/notifications', { params }).then(response => {
  37. const next = getLinks(response).refs.find(link => link.rel === 'next');
  38. dispatch(refreshNotificationsSuccess(response.data, next ? next.uri : null));
  39. fetchRelatedRelationships(dispatch, response.data);
  40. }).catch(error => {
  41. dispatch(refreshNotificationsFail(error));
  42. });
  43. };
  44. };
  45. export function refreshNotificationsRequest() {
  46. return {
  47. type: NOTIFICATIONS_REFRESH_REQUEST
  48. };
  49. };
  50. export function refreshNotificationsSuccess(notifications, next) {
  51. return {
  52. type: NOTIFICATIONS_REFRESH_SUCCESS,
  53. notifications,
  54. accounts: notifications.map(item => item.account),
  55. statuses: notifications.map(item => item.status),
  56. next
  57. };
  58. };
  59. export function refreshNotificationsFail(error) {
  60. return {
  61. type: NOTIFICATIONS_REFRESH_FAIL,
  62. error
  63. };
  64. };
  65. export function expandNotifications() {
  66. return (dispatch, getState) => {
  67. const url = getState().getIn(['notifications', 'next'], null);
  68. if (url === null) {
  69. return;
  70. }
  71. dispatch(expandNotificationsRequest());
  72. api(getState).get(url).then(response => {
  73. const next = getLinks(response).refs.find(link => link.rel === 'next');
  74. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null));
  75. fetchRelatedRelationships(dispatch, response.data);
  76. }).catch(error => {
  77. dispatch(expandNotificationsFail(error));
  78. });
  79. };
  80. };
  81. export function expandNotificationsRequest() {
  82. return {
  83. type: NOTIFICATIONS_EXPAND_REQUEST
  84. };
  85. };
  86. export function expandNotificationsSuccess(notifications, next) {
  87. return {
  88. type: NOTIFICATIONS_EXPAND_SUCCESS,
  89. notifications,
  90. accounts: notifications.map(item => item.account),
  91. statuses: notifications.map(item => item.status),
  92. next
  93. };
  94. };
  95. export function expandNotificationsFail(error) {
  96. return {
  97. type: NOTIFICATIONS_EXPAND_FAIL,
  98. error
  99. };
  100. };