usersessiondata.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. SessionData = new Mongo.Collection('sessiondata');
  2. /**
  3. * A UserSessionData in Wekan. Organization in Trello.
  4. */
  5. SessionData.attachSchema(
  6. new SimpleSchema({
  7. _id: {
  8. /**
  9. * the organization id
  10. */
  11. type: Number,
  12. optional: true,
  13. // eslint-disable-next-line consistent-return
  14. autoValue() {
  15. if (this.isInsert && !this.isSet) {
  16. return incrementCounter('counters', 'orgId', 1);
  17. }
  18. },
  19. },
  20. userId: {
  21. /**
  22. * userId of the user
  23. */
  24. type: String,
  25. optional: false,
  26. },
  27. sessionId: {
  28. /**
  29. * unique session ID
  30. */
  31. type: String,
  32. optional: false,
  33. },
  34. totalHits: {
  35. /**
  36. * total number of hits in the last report query
  37. */
  38. type: Number,
  39. optional: true,
  40. },
  41. resultsCount: {
  42. /**
  43. * number of results returned
  44. */
  45. type: Number,
  46. optional: true,
  47. },
  48. lastHit: {
  49. /**
  50. * the last hit returned from a report query
  51. */
  52. type: Number,
  53. optional: true,
  54. },
  55. cards: {
  56. type: [String],
  57. optional: true,
  58. },
  59. selector: {
  60. type: String,
  61. optional: true,
  62. blackbox: true,
  63. },
  64. errorMessages: {
  65. type: [String],
  66. optional: true,
  67. },
  68. errors: {
  69. type: [Object],
  70. optional: true,
  71. defaultValue: [],
  72. },
  73. 'errors.$': {
  74. type: new SimpleSchema({
  75. tag: {
  76. /**
  77. * i18n tag
  78. */
  79. type: String,
  80. optional: false,
  81. },
  82. value: {
  83. /**
  84. * value for the tag
  85. */
  86. type: String,
  87. optional: true,
  88. defaultValue: null,
  89. },
  90. color: {
  91. type: Boolean,
  92. optional: true,
  93. defaultValue: false,
  94. },
  95. }),
  96. },
  97. createdAt: {
  98. /**
  99. * creation date of the team
  100. */
  101. type: Date,
  102. // eslint-disable-next-line consistent-return
  103. autoValue() {
  104. if (this.isInsert) {
  105. return new Date();
  106. } else if (this.isUpsert) {
  107. return { $setOnInsert: new Date() };
  108. } else {
  109. this.unset();
  110. }
  111. },
  112. },
  113. modifiedAt: {
  114. type: Date,
  115. denyUpdate: false,
  116. // eslint-disable-next-line consistent-return
  117. autoValue() {
  118. if (this.isInsert || this.isUpsert || this.isUpdate) {
  119. return new Date();
  120. } else {
  121. this.unset();
  122. }
  123. },
  124. },
  125. }),
  126. );
  127. SessionData.helpers({
  128. getSelector() {
  129. return SessionData.unpickle(this.selector);
  130. },
  131. });
  132. SessionData.unpickle = pickle => {
  133. return JSON.parse(pickle, (key, value) => {
  134. if (typeof value === 'object') {
  135. if (value.hasOwnProperty('$$class')) {
  136. if (value.$$class === 'RegExp') {
  137. return new RegExp(value.source, value.flags);
  138. }
  139. }
  140. }
  141. return value;
  142. });
  143. };
  144. SessionData.pickle = value => {
  145. return JSON.stringify(value, (key, value) => {
  146. if (typeof value === 'object') {
  147. if (value.constructor.name === 'RegExp') {
  148. return {
  149. $$class: 'RegExp',
  150. source: value.source,
  151. flags: value.flags,
  152. };
  153. }
  154. }
  155. return value;
  156. });
  157. };
  158. if (!Meteor.isServer) {
  159. SessionData.getSessionId = () => {
  160. let sessionId = Session.get('sessionId');
  161. if (!sessionId) {
  162. sessionId = `${String(Meteor.userId())}-${String(Math.random())}`;
  163. Session.set('sessionId', sessionId);
  164. }
  165. return sessionId;
  166. };
  167. }
  168. export default SessionData;