usersessiondata.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. return unpickleValue(value);
  144. });
  145. };
  146. function unpickleValue(value) {
  147. if (value === null) {
  148. return null;
  149. } else if (typeof value === 'object') {
  150. // eslint-disable-next-line no-prototype-builtins
  151. if (value.hasOwnProperty('$$class')) {
  152. switch (value.$$class) {
  153. case 'RegExp':
  154. return new RegExp(value.source, value.flags);
  155. case 'Date':
  156. return new Date(value.stringValue);
  157. case 'Object':
  158. return unpickleObject(value);
  159. }
  160. }
  161. }
  162. return value;
  163. }
  164. function unpickleObject(obj) {
  165. const newObject = {};
  166. Object.entries(obj).forEach(([key, value]) => {
  167. newObject[key] = unpickleValue(value);
  168. });
  169. return newObject;
  170. }
  171. SessionData.pickle = value => {
  172. return JSON.stringify(value, (key, value) => {
  173. return pickleValue(value);
  174. });
  175. };
  176. function pickleValue(value) {
  177. if (value === null) {
  178. return null;
  179. } else if (typeof value === 'object') {
  180. switch(value.constructor.name) {
  181. case 'RegExp':
  182. return {
  183. $$class: 'RegExp',
  184. source: value.source,
  185. flags: value.flags,
  186. };
  187. case 'Date':
  188. return {
  189. $$class: 'Date',
  190. stringValue: String(value),
  191. };
  192. case 'Object':
  193. return pickleObject(value);
  194. }
  195. }
  196. return value;
  197. }
  198. function pickleObject(obj) {
  199. const newObject = {};
  200. Object.entries(obj).forEach(([key, value]) => {
  201. newObject[key] = pickleValue(value);
  202. });
  203. return newObject;
  204. }
  205. if (!Meteor.isServer) {
  206. SessionData.getSessionId = () => {
  207. let sessionId = Session.get('sessionId');
  208. if (!sessionId) {
  209. sessionId = `${String(Meteor.userId())}-${String(Math.random())}`;
  210. Session.set('sessionId', sessionId);
  211. }
  212. return sessionId;
  213. };
  214. }
  215. export default SessionData;