usersessiondata.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. debug: {
  80. type: String,
  81. optional: true,
  82. },
  83. 'errors.$': {
  84. type: new SimpleSchema({
  85. tag: {
  86. /**
  87. * i18n tag
  88. */
  89. type: String,
  90. optional: false,
  91. },
  92. value: {
  93. /**
  94. * value for the tag
  95. */
  96. type: String,
  97. optional: true,
  98. defaultValue: null,
  99. },
  100. color: {
  101. type: Boolean,
  102. optional: true,
  103. defaultValue: false,
  104. },
  105. }),
  106. },
  107. createdAt: {
  108. /**
  109. * creation date of the team
  110. */
  111. type: Date,
  112. // eslint-disable-next-line consistent-return
  113. autoValue() {
  114. if (this.isInsert) {
  115. return new Date();
  116. } else if (this.isUpsert) {
  117. return { $setOnInsert: new Date() };
  118. } else {
  119. this.unset();
  120. }
  121. },
  122. },
  123. modifiedAt: {
  124. type: Date,
  125. denyUpdate: false,
  126. // eslint-disable-next-line consistent-return
  127. autoValue() {
  128. if (this.isInsert || this.isUpsert || this.isUpdate) {
  129. return new Date();
  130. } else {
  131. this.unset();
  132. }
  133. },
  134. },
  135. }),
  136. );
  137. SessionData.helpers({
  138. getSelector() {
  139. return SessionData.unpickle(this.selector);
  140. },
  141. getProjection() {
  142. return SessionData.unpickle(this.projection);
  143. },
  144. });
  145. SessionData.unpickle = pickle => {
  146. return JSON.parse(pickle, (key, value) => {
  147. return unpickleValue(value);
  148. });
  149. };
  150. function unpickleValue(value) {
  151. if (value === null) {
  152. return null;
  153. } else if (typeof value === 'object') {
  154. // eslint-disable-next-line no-prototype-builtins
  155. if (value.hasOwnProperty('$$class')) {
  156. switch (value.$$class) {
  157. case 'RegExp':
  158. return new RegExp(value.source, value.flags);
  159. case 'Date':
  160. return new Date(value.stringValue);
  161. case 'Object':
  162. return unpickleObject(value);
  163. }
  164. }
  165. }
  166. return value;
  167. }
  168. function unpickleObject(obj) {
  169. const newObject = {};
  170. Object.entries(obj).forEach(([key, value]) => {
  171. newObject[key] = unpickleValue(value);
  172. });
  173. return newObject;
  174. }
  175. SessionData.pickle = value => {
  176. return JSON.stringify(value, (key, value) => {
  177. return pickleValue(value);
  178. }, 2);
  179. };
  180. function pickleValue(value) {
  181. if (value === null) {
  182. return null;
  183. } else if (typeof value === 'object') {
  184. switch (value.constructor.name) {
  185. case 'RegExp':
  186. return {
  187. $$class: 'RegExp',
  188. source: value.source,
  189. flags: value.flags,
  190. };
  191. case 'Date':
  192. return {
  193. $$class: 'Date',
  194. stringValue: String(value),
  195. };
  196. case 'Object':
  197. return pickleObject(value);
  198. }
  199. }
  200. return value;
  201. }
  202. function pickleObject(obj) {
  203. const newObject = {};
  204. Object.entries(obj).forEach(([key, value]) => {
  205. newObject[key] = pickleValue(value);
  206. });
  207. return newObject;
  208. }
  209. if (!Meteor.isServer) {
  210. SessionData.getSessionId = () => {
  211. let sessionId = Session.get('sessionId');
  212. if (!sessionId) {
  213. sessionId = `${String(Meteor.userId())}-${String(Math.random())}`;
  214. Session.set('sessionId', sessionId);
  215. }
  216. return sessionId;
  217. };
  218. }
  219. export default SessionData;