25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

85 lines
1.7 KiB

  1. define([
  2. 'google-spreadsheet',
  3. './creds',
  4. './sheetsConfig'
  5. ], function (
  6. googleSheets,
  7. creds,
  8. sheetsConfig
  9. ) {
  10. return {
  11. doc: null,
  12. sheet: null,
  13. records: null,
  14. init: function () {
  15. if (sheetsConfig.roles) {
  16. this.update = function () {};
  17. this.onGetRows(null, sheetsConfig.roles);
  18. return;
  19. }
  20. this.doc = new googleSheets(sheetsConfig.sheetId);
  21. this.doc.useServiceAccountAuth(creds, this.onAuth.bind(this));
  22. },
  23. onAuth: function () {
  24. this.doc.getInfo(this.onGetInfo.bind(this));
  25. },
  26. onGetInfo: function () {
  27. if (!this.doc.worksheets) {
  28. setTimeout(this.onAuth.bind(this), 300000);
  29. return;
  30. }
  31. this.sheet = this.doc.worksheets[0];
  32. this.update();
  33. },
  34. getRecord: function (name) {
  35. return (this.records || []).find(r => (r.username == name));
  36. },
  37. onGetRows: function (err, rows) {
  38. if (rows) {
  39. try {
  40. var records = (rows || []).map(function (r) {
  41. var o = {};
  42. Object.keys(r).forEach(function (p) {
  43. if (['id', 'app:edited', '_links', '_xml', 'save', 'del'].indexOf(p) > -1)
  44. return;
  45. o[p] = r[p];
  46. });
  47. o.messageStyle = o.messagestyle;
  48. delete o.messagestyle;
  49. o.messagePrefix = o.messageprefix;
  50. delete o.messageprefix;
  51. if (typeof (o.items) == 'string')
  52. o.items = JSON.parse(o.items || "[]");
  53. if (typeof (o.skins) == 'string')
  54. o.skins = JSON.parse(o.skins || "[]");
  55. return o;
  56. });
  57. this.records = records;
  58. } catch (e) {
  59. console.log('Sheets in error state');
  60. }
  61. }
  62. setTimeout(this.update.bind(this), 300000)
  63. },
  64. update: function () {
  65. this.sheet.getRows({}, this.onGetRows.bind(this));
  66. }
  67. };
  68. });