usersessiondata.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. projection: {
  65. type: String,
  66. optional: true,
  67. blackbox: true,
  68. defaultValue: {},
  69. },
  70. errorMessages: {
  71. type: [String],
  72. optional: true,
  73. },
  74. errors: {
  75. type: [Object],
  76. optional: true,
  77. defaultValue: [],
  78. },
  79. 'errors.$': {
  80. type: new SimpleSchema({
  81. tag: {
  82. /**
  83. * i18n tag
  84. */
  85. type: String,
  86. optional: false,
  87. },
  88. value: {
  89. /**
  90. * value for the tag
  91. */
  92. type: String,
  93. optional: true,
  94. defaultValue: null,
  95. },
  96. color: {
  97. type: Boolean,
  98. optional: true,
  99. defaultValue: false,
  100. },
  101. }),
  102. },
  103. createdAt: {
  104. /**
  105. * creation date of the team
  106. */
  107. type: Date,
  108. // eslint-disable-next-line consistent-return
  109. autoValue() {
  110. if (this.isInsert) {
  111. return new Date();
  112. } else if (this.isUpsert) {
  113. return { $setOnInsert: new Date() };
  114. } else {
  115. this.unset();
  116. }
  117. },
  118. },
  119. modifiedAt: {
  120. type: Date,
  121. denyUpdate: false,
  122. // eslint-disable-next-line consistent-return
  123. autoValue() {
  124. if (this.isInsert || this.isUpsert || this.isUpdate) {
  125. return new Date();
  126. } else {
  127. this.unset();
  128. }
  129. },
  130. },
  131. }),
  132. );
  133. SessionData.helpers({
  134. getSelector() {
  135. return SessionData.unpickle(this.selector);
  136. },
  137. getProjection() {
  138. return SessionData.unpickle(this.projection);
  139. },
  140. });
  141. SessionData.unpickle = pickle => {
  142. return JSON.parse(pickle, (key, value) => {
  143. if (value === null) {
  144. return null;
  145. } else if (typeof value === 'object') {
  146. // eslint-disable-next-line no-prototype-builtins
  147. if (value.hasOwnProperty('$$class')) {
  148. if (value.$$class === 'RegExp') {
  149. return new RegExp(value.source, value.flags);
  150. }
  151. }
  152. }
  153. return value;
  154. });
  155. };
  156. SessionData.pickle = value => {
  157. return JSON.stringify(value, (key, value) => {
  158. if (value === null) {
  159. return null;
  160. } else if (typeof value === 'object') {
  161. if (value.constructor.name === 'RegExp') {
  162. return {
  163. $$class: 'RegExp',
  164. source: value.source,
  165. flags: value.flags,
  166. };
  167. }
  168. }
  169. return value;
  170. });
  171. };
  172. if (!Meteor.isServer) {
  173. SessionData.getSessionId = () => {
  174. let sessionId = Session.get('sessionId');
  175. if (!sessionId) {
  176. sessionId = `${String(Meteor.userId())}-${String(Math.random())}`;
  177. Session.set('sessionId', sessionId);
  178. }
  179. return sessionId;
  180. };
  181. }
  182. export default SessionData;