OpenCollectiveUtils.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import CacheUtils from "./CacheUtils.js"
  2. const OPEN_COLLECTIVE_ENDPOINT= ()=>"https://api.opencollective.com/graphql/v2";
  3. const OPEN_COLLECTIVE_BACKER_QUERY = (org) => `
  4. query collective ($offset: Int, $limit: Int){
  5. collective(slug: "${org}") {
  6. name
  7. backers: members(role: BACKER, offset: $offset, limit: $limit) {
  8. limit
  9. totalCount
  10. nodes {
  11. publicMessage
  12. totalDonations {
  13. value
  14. currency
  15. valueInCents
  16. }
  17. account {
  18. slug
  19. type
  20. ... on Individual {
  21. email
  22. name
  23. website
  24. imageUrl
  25. }
  26. ... on Organization {
  27. admins: members(role: ADMIN) {
  28. nodes {
  29. account {
  30. ... on Individual {
  31. email
  32. name
  33. website
  34. imageUrl
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. `;
  46. export default class OpenCollectiveUtils {
  47. static async getBackers(collective) {
  48. const cacheKey = "ocollective/" + collective + "/backers";
  49. let backers = CacheUtils.get(cacheKey);
  50. let cached=true;
  51. if (!backers) {
  52. cached=false;
  53. const endPoint = OPEN_COLLECTIVE_ENDPOINT(collective);
  54. const query = OPEN_COLLECTIVE_BACKER_QUERY(collective);
  55. backers = await fetch(endPoint, {
  56. method: 'POST',
  57. headers: {
  58. 'Content-Type': 'application/json',
  59. 'Accept': 'application/json',
  60. },
  61. body: JSON.stringify({
  62. query: query,
  63. variables: {
  64. offset: 0,
  65. limit: 1000
  66. },
  67. })
  68. }).then((res) =>{
  69. if(res.ok&&res.status==200)return res.json() ;
  70. else throw "Failed to fetch!";
  71. });
  72. if (!backers) throw "Undefined backers?";
  73. CacheUtils.set(cacheKey, backers);
  74. }
  75. return [backers,cached];
  76. }
  77. static async getBackersFlat(collective,modifier) {
  78. const [res,cached]=await this.getBackers(collective);
  79. const backers = [];
  80. const backersObj = res.data.collective.backers.nodes;
  81. for (let i in backersObj) {
  82. const entry = {};
  83. const backer = backersObj[i];
  84. entry.message = backer.publicMessage;
  85. entry.value = backer.totalDonations.value;
  86. entry.currency = backer.totalDonations.currency;
  87. let account = backer.account;
  88. entry.accountUrl = "https://opencollective.com/" + account.slug;
  89. const subAccounts = [];
  90. if (account.type == "ORGANIZATION") {
  91. account.admins.nodes.forEach(admin => subAccounts.push(admin.account));
  92. } else {
  93. subAccounts.push(account);
  94. }
  95. subAccounts.forEach(account => {
  96. const subEntry = { ...entry };
  97. subEntry.name = account.name;
  98. subEntry.website = account.website;
  99. subEntry.avatar = account.imageUrl;
  100. backers.push(subEntry);
  101. });
  102. }
  103. if(!cached&&modifier)backers=modifier(backers);
  104. return backers;
  105. }
  106. }