PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/models/models.js

https://github.com/JeromeParadis/live-notify
JavaScript | 156 lines | 118 code | 12 blank | 26 comment | 25 complexity | 0451c21d9aa53713e68092aa9784a1d2 MD5 | raw file
  1. (function (notify) {
  2. var server = false;
  3. //if (typeof models === 'undefined')
  4. // var models;
  5. if (typeof exports !== 'undefined') {
  6. Backbone = require('server-backbone-redis');
  7. Backbone.debug = true;
  8. models = exports;
  9. server = true;
  10. } else {
  11. if (!models) models = {};
  12. this.models = models;
  13. }
  14. //
  15. //models
  16. //
  17. models.Backbone = Backbone;
  18. models.Event = Backbone.Model.extend({
  19. defaults: {
  20. "id": null,
  21. "user": {},
  22. "foreign_id": null,
  23. "type": "notification",
  24. "created": null,
  25. "was_read": false,
  26. "group_by": null
  27. },
  28. name: "event",
  29. error: function(model, error) {
  30. //
  31. },
  32. debug: function() {
  33. console.log("User: " + this.get("id"));
  34. }
  35. //~ safeClone: function() {
  36. //~ var clone = this.clone();
  37. //~ clone.unset('name');
  38. //~ clone.unset('email');
  39. //~ clone.unset('location');
  40. //~ clone.unset('gender');
  41. //~ return clone;
  42. //~ }
  43. });
  44. models.Events = Backbone.Collection.extend({
  45. model: models.Event
  46. });
  47. models.EventThread = Backbone.Model.extend({
  48. defaults: {
  49. "total": null,
  50. "notifications": null,
  51. },
  52. name: "event",
  53. error: function(model, error) {
  54. //
  55. },
  56. debug: function() {
  57. console.log("User: " + this.get("id"));
  58. },
  59. get_notification: function() {
  60. var notifications = this.get('notifications');
  61. if (notifications) {
  62. var events = notifications.models;
  63. var message = null;
  64. var actors = [];
  65. var objects = null;
  66. var anonymous_count = 0;
  67. for (var i=0; i< events.length; i++) {
  68. var evt = events[i];
  69. if (i==0) {
  70. message = evt.get('message');
  71. objects = evt.get('objects');
  72. }
  73. var actor = evt.get('actor');
  74. if (actor)
  75. actors.push(actor);
  76. else
  77. anonymous_count += 1;
  78. }
  79. if (objects && message) {
  80. for(obj_name in objects) {
  81. if (objects[obj_name].url || false)
  82. message = message.replace('{{' + obj_name + '}}', '<a href="' + objects[obj_name].url + '">' + objects[obj_name].name + '</a>');
  83. else
  84. message = message.replace('{{' + obj_name + '}}', objects[obj_name].name);
  85. }
  86. }
  87. var avatar = null;
  88. var avatar_url = null;
  89. if (actors.length > 0) {
  90. var actor_no = 1;
  91. var actor_text = '<nobr>'
  92. while(actor_no <= actors.length) {
  93. var actor = actors[actor_no-1];
  94. if (actor) {
  95. if (actor_no <= 2)
  96. actor_text += '<a href="' + actor.profile + '">' + actor.first_name + '</a> ';
  97. else {
  98. var nb_others = actors.length - 2;
  99. actor_text += 'and <a href="#">' + nb_others + ' other' + (nb_others > 1 ? 's' : '') + '</a>';
  100. break;
  101. }
  102. }
  103. if (actor_no < 2 && actors.length > 1) {
  104. actor_text += (actors.length == 2) ? ' and ' : ', ';
  105. }
  106. actor_no++;
  107. }
  108. actor_text += '</nobr>'
  109. message = message.replace('{{actor}}',actor_text);
  110. avatar = actors[0].thumbnail || null;
  111. avatar_url = actors[0].profile || null;
  112. }
  113. else
  114. message = message.replace('{{actor}}','Someone');
  115. if (actors.length > 1) {
  116. message = message.replace('{{actor_plural_verb_s}}','');
  117. message = message.replace('{{actor_plural}}','s');
  118. }
  119. else {
  120. message = message.replace('{{actor_plural_verb_s}}','s');
  121. message = message.replace('{{actor_plural}}','');
  122. }
  123. if (anonymous_count > 0)
  124. message = message.replace('{{anonymous}}',(anonymous_count == 1) ? 'Someone' : anonymous_count + ' persons');
  125. return {'avatar': avatar, 'message': message, 'avatar_url': avatar_url};
  126. }
  127. return null;
  128. },
  129. //~ safeClone: function() {
  130. //~ var clone = this.clone();
  131. //~ clone.unset('name');
  132. //~ clone.unset('email');
  133. //~ clone.unset('location');
  134. //~ clone.unset('gender');
  135. //~ return clone;
  136. //~ }
  137. });
  138. models.EventThreads = Backbone.Collection.extend({
  139. model: models.EventThread,
  140. // intitalize: function(models, options) {
  141. // this.comparator = ;
  142. // }
  143. });
  144. })();