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.

391 lines
8.8 KiB

8 years ago
  1. import api from 'flavours/glitch/util/api';
  2. export const REBLOG_REQUEST = 'REBLOG_REQUEST';
  3. export const REBLOG_SUCCESS = 'REBLOG_SUCCESS';
  4. export const REBLOG_FAIL = 'REBLOG_FAIL';
  5. export const FAVOURITE_REQUEST = 'FAVOURITE_REQUEST';
  6. export const FAVOURITE_SUCCESS = 'FAVOURITE_SUCCESS';
  7. export const FAVOURITE_FAIL = 'FAVOURITE_FAIL';
  8. export const UNREBLOG_REQUEST = 'UNREBLOG_REQUEST';
  9. export const UNREBLOG_SUCCESS = 'UNREBLOG_SUCCESS';
  10. export const UNREBLOG_FAIL = 'UNREBLOG_FAIL';
  11. export const UNFAVOURITE_REQUEST = 'UNFAVOURITE_REQUEST';
  12. export const UNFAVOURITE_SUCCESS = 'UNFAVOURITE_SUCCESS';
  13. export const UNFAVOURITE_FAIL = 'UNFAVOURITE_FAIL';
  14. export const REBLOGS_FETCH_REQUEST = 'REBLOGS_FETCH_REQUEST';
  15. export const REBLOGS_FETCH_SUCCESS = 'REBLOGS_FETCH_SUCCESS';
  16. export const REBLOGS_FETCH_FAIL = 'REBLOGS_FETCH_FAIL';
  17. export const FAVOURITES_FETCH_REQUEST = 'FAVOURITES_FETCH_REQUEST';
  18. export const FAVOURITES_FETCH_SUCCESS = 'FAVOURITES_FETCH_SUCCESS';
  19. export const FAVOURITES_FETCH_FAIL = 'FAVOURITES_FETCH_FAIL';
  20. export const PIN_REQUEST = 'PIN_REQUEST';
  21. export const PIN_SUCCESS = 'PIN_SUCCESS';
  22. export const PIN_FAIL = 'PIN_FAIL';
  23. export const UNPIN_REQUEST = 'UNPIN_REQUEST';
  24. export const UNPIN_SUCCESS = 'UNPIN_SUCCESS';
  25. export const UNPIN_FAIL = 'UNPIN_FAIL';
  26. export const BOOKMARK_REQUEST = 'BOOKMARK_REQUEST';
  27. export const BOOKMARK_SUCCESS = 'BOOKMARKED_SUCCESS';
  28. export const BOOKMARK_FAIL = 'BOOKMARKED_FAIL';
  29. export const UNBOOKMARK_REQUEST = 'UNBOOKMARKED_REQUEST';
  30. export const UNBOOKMARK_SUCCESS = 'UNBOOKMARKED_SUCCESS';
  31. export const UNBOOKMARK_FAIL = 'UNBOOKMARKED_FAIL';
  32. export function reblog(status) {
  33. return function (dispatch, getState) {
  34. dispatch(reblogRequest(status));
  35. api(getState).post(`/api/v1/statuses/${status.get('id')}/reblog`).then(function (response) {
  36. // The reblog API method returns a new status wrapped around the original. In this case we are only
  37. // interested in how the original is modified, hence passing it skipping the wrapper
  38. dispatch(reblogSuccess(status, response.data.reblog));
  39. }).catch(function (error) {
  40. dispatch(reblogFail(status, error));
  41. });
  42. };
  43. };
  44. export function unreblog(status) {
  45. return (dispatch, getState) => {
  46. dispatch(unreblogRequest(status));
  47. api(getState).post(`/api/v1/statuses/${status.get('id')}/unreblog`).then(response => {
  48. dispatch(unreblogSuccess(status, response.data));
  49. }).catch(error => {
  50. dispatch(unreblogFail(status, error));
  51. });
  52. };
  53. };
  54. export function reblogRequest(status) {
  55. return {
  56. type: REBLOG_REQUEST,
  57. status: status,
  58. };
  59. };
  60. export function reblogSuccess(status, response) {
  61. return {
  62. type: REBLOG_SUCCESS,
  63. status: status,
  64. response: response,
  65. };
  66. };
  67. export function reblogFail(status, error) {
  68. return {
  69. type: REBLOG_FAIL,
  70. status: status,
  71. error: error,
  72. };
  73. };
  74. export function unreblogRequest(status) {
  75. return {
  76. type: UNREBLOG_REQUEST,
  77. status: status,
  78. };
  79. };
  80. export function unreblogSuccess(status, response) {
  81. return {
  82. type: UNREBLOG_SUCCESS,
  83. status: status,
  84. response: response,
  85. };
  86. };
  87. export function unreblogFail(status, error) {
  88. return {
  89. type: UNREBLOG_FAIL,
  90. status: status,
  91. error: error,
  92. };
  93. };
  94. export function favourite(status) {
  95. return function (dispatch, getState) {
  96. dispatch(favouriteRequest(status));
  97. api(getState).post(`/api/v1/statuses/${status.get('id')}/favourite`).then(function (response) {
  98. dispatch(favouriteSuccess(status, response.data));
  99. }).catch(function (error) {
  100. dispatch(favouriteFail(status, error));
  101. });
  102. };
  103. };
  104. export function unfavourite(status) {
  105. return (dispatch, getState) => {
  106. dispatch(unfavouriteRequest(status));
  107. api(getState).post(`/api/v1/statuses/${status.get('id')}/unfavourite`).then(response => {
  108. dispatch(unfavouriteSuccess(status, response.data));
  109. }).catch(error => {
  110. dispatch(unfavouriteFail(status, error));
  111. });
  112. };
  113. };
  114. export function favouriteRequest(status) {
  115. return {
  116. type: FAVOURITE_REQUEST,
  117. status: status,
  118. };
  119. };
  120. export function favouriteSuccess(status, response) {
  121. return {
  122. type: FAVOURITE_SUCCESS,
  123. status: status,
  124. response: response,
  125. };
  126. };
  127. export function favouriteFail(status, error) {
  128. return {
  129. type: FAVOURITE_FAIL,
  130. status: status,
  131. error: error,
  132. };
  133. };
  134. export function unfavouriteRequest(status) {
  135. return {
  136. type: UNFAVOURITE_REQUEST,
  137. status: status,
  138. };
  139. };
  140. export function unfavouriteSuccess(status, response) {
  141. return {
  142. type: UNFAVOURITE_SUCCESS,
  143. status: status,
  144. response: response,
  145. };
  146. };
  147. export function unfavouriteFail(status, error) {
  148. return {
  149. type: UNFAVOURITE_FAIL,
  150. status: status,
  151. error: error,
  152. };
  153. };
  154. export function bookmark(status) {
  155. return function (dispatch, getState) {
  156. dispatch(bookmarkRequest(status));
  157. api(getState).post(`/api/v1/statuses/${status.get('id')}/bookmark`).then(function (response) {
  158. dispatch(bookmarkSuccess(status, response.data));
  159. }).catch(function (error) {
  160. dispatch(bookmarkFail(status, error));
  161. });
  162. };
  163. };
  164. export function unbookmark(status) {
  165. return (dispatch, getState) => {
  166. dispatch(unbookmarkRequest(status));
  167. api(getState).post(`/api/v1/statuses/${status.get('id')}/unbookmark`).then(response => {
  168. dispatch(unbookmarkSuccess(status, response.data));
  169. }).catch(error => {
  170. dispatch(unbookmarkFail(status, error));
  171. });
  172. };
  173. };
  174. export function bookmarkRequest(status) {
  175. return {
  176. type: BOOKMARK_REQUEST,
  177. status: status,
  178. };
  179. };
  180. export function bookmarkSuccess(status, response) {
  181. return {
  182. type: BOOKMARK_SUCCESS,
  183. status: status,
  184. response: response,
  185. };
  186. };
  187. export function bookmarkFail(status, error) {
  188. return {
  189. type: BOOKMARK_FAIL,
  190. status: status,
  191. error: error,
  192. };
  193. };
  194. export function unbookmarkRequest(status) {
  195. return {
  196. type: UNBOOKMARK_REQUEST,
  197. status: status,
  198. };
  199. };
  200. export function unbookmarkSuccess(status, response) {
  201. return {
  202. type: UNBOOKMARK_SUCCESS,
  203. status: status,
  204. response: response,
  205. };
  206. };
  207. export function unbookmarkFail(status, error) {
  208. return {
  209. type: UNBOOKMARK_FAIL,
  210. status: status,
  211. error: error,
  212. };
  213. };
  214. export function fetchReblogs(id) {
  215. return (dispatch, getState) => {
  216. dispatch(fetchReblogsRequest(id));
  217. api(getState).get(`/api/v1/statuses/${id}/reblogged_by`).then(response => {
  218. dispatch(fetchReblogsSuccess(id, response.data));
  219. }).catch(error => {
  220. dispatch(fetchReblogsFail(id, error));
  221. });
  222. };
  223. };
  224. export function fetchReblogsRequest(id) {
  225. return {
  226. type: REBLOGS_FETCH_REQUEST,
  227. id,
  228. };
  229. };
  230. export function fetchReblogsSuccess(id, accounts) {
  231. return {
  232. type: REBLOGS_FETCH_SUCCESS,
  233. id,
  234. accounts,
  235. };
  236. };
  237. export function fetchReblogsFail(id, error) {
  238. return {
  239. type: REBLOGS_FETCH_FAIL,
  240. error,
  241. };
  242. };
  243. export function fetchFavourites(id) {
  244. return (dispatch, getState) => {
  245. dispatch(fetchFavouritesRequest(id));
  246. api(getState).get(`/api/v1/statuses/${id}/favourited_by`).then(response => {
  247. dispatch(fetchFavouritesSuccess(id, response.data));
  248. }).catch(error => {
  249. dispatch(fetchFavouritesFail(id, error));
  250. });
  251. };
  252. };
  253. export function fetchFavouritesRequest(id) {
  254. return {
  255. type: FAVOURITES_FETCH_REQUEST,
  256. id,
  257. };
  258. };
  259. export function fetchFavouritesSuccess(id, accounts) {
  260. return {
  261. type: FAVOURITES_FETCH_SUCCESS,
  262. id,
  263. accounts,
  264. };
  265. };
  266. export function fetchFavouritesFail(id, error) {
  267. return {
  268. type: FAVOURITES_FETCH_FAIL,
  269. error,
  270. };
  271. };
  272. export function pin(status) {
  273. return (dispatch, getState) => {
  274. dispatch(pinRequest(status));
  275. api(getState).post(`/api/v1/statuses/${status.get('id')}/pin`).then(response => {
  276. dispatch(pinSuccess(status, response.data));
  277. }).catch(error => {
  278. dispatch(pinFail(status, error));
  279. });
  280. };
  281. };
  282. export function pinRequest(status) {
  283. return {
  284. type: PIN_REQUEST,
  285. status,
  286. };
  287. };
  288. export function pinSuccess(status, response) {
  289. return {
  290. type: PIN_SUCCESS,
  291. status,
  292. response,
  293. };
  294. };
  295. export function pinFail(status, error) {
  296. return {
  297. type: PIN_FAIL,
  298. status,
  299. error,
  300. };
  301. };
  302. export function unpin (status) {
  303. return (dispatch, getState) => {
  304. dispatch(unpinRequest(status));
  305. api(getState).post(`/api/v1/statuses/${status.get('id')}/unpin`).then(response => {
  306. dispatch(unpinSuccess(status, response.data));
  307. }).catch(error => {
  308. dispatch(unpinFail(status, error));
  309. });
  310. };
  311. };
  312. export function unpinRequest(status) {
  313. return {
  314. type: UNPIN_REQUEST,
  315. status,
  316. };
  317. };
  318. export function unpinSuccess(status, response) {
  319. return {
  320. type: UNPIN_SUCCESS,
  321. status,
  322. response,
  323. };
  324. };
  325. export function unpinFail(status, error) {
  326. return {
  327. type: UNPIN_FAIL,
  328. status,
  329. error,
  330. };
  331. };