OpenCollectiveUtils.js 3.3 KB

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