Guest User

Untitled

a guest
Jan 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. var util = require("util"),
  2. redis = require("redis"),
  3. cradle = require("cradle");
  4.  
  5. /*------------------------------------------------------------------------------
  6. (public) UserModel
  7.  
  8. + none
  9. - void
  10.  
  11. Set up model for users.
  12. ------------------------------------------------------------------------------*/
  13. var UserModel = module.exports = function UserModel() {
  14. var couchCon = new(cradle.Connection)(
  15. "173.230.137.226", 5984,
  16. {auth: { username: "cotoja", password: "cotoja4004"}}
  17. );
  18. this._couchClient = couchCon.database("stackchat");
  19.  
  20. this._redisClient = redis.createClient(6379, "173.230.137.226");
  21. _redisClient.auth("cotoja1021", function() {});
  22. };
  23.  
  24. /*------------------------------------------------------------------------------
  25. (private) _createCouchUser
  26.  
  27. + obj
  28. + callback
  29. - void
  30.  
  31. Creates new user in CouchDB.
  32. ------------------------------------------------------------------------------*/
  33. UserModel.prototype._createCouchUser = function(userID, obj, callback) {
  34. this._couchClient.save(
  35. userID,
  36. {
  37. name: obj.name,
  38. email: obj.email,
  39. password_hash: obj.password_hash,
  40. website: obj.website,
  41. introduction: obj.introduction,
  42. gravatar_hash: obj.gravatar_hash,
  43. role: obj.role,
  44. can_talk: obj.can_talk,
  45. can_read: obj.can_read
  46. },
  47. function(err, res) {
  48. callback(err, res);
  49. }
  50. );
  51. };
  52.  
  53. /*------------------------------------------------------------------------------
  54. (private) _createRedisUser
  55.  
  56. + obj
  57. + callback
  58. - void
  59.  
  60. Creates new user in Redis.
  61. ------------------------------------------------------------------------------*/
  62. UserModel.prototype._createRedisUser = function(userID, obj, callback) {
  63. var date = new Date();
  64.  
  65. this._redisClient.hmset("user:" + userID,
  66. {
  67. name: obj.name,
  68. email: obj.email,
  69. password_hash: obj.password_hash,
  70. website: obj.website,
  71. introduction: obj.introduction,
  72. gravatar_hash: obj.gravatar_hash,
  73. last_seen: date.getTime(),
  74. last_sent_message: null,
  75. last_spoke_here: null, // this is only for use on the client, has no need on the server, per-se
  76. total_messages_sent: 0, // can be decremented on deletes
  77. total_flags_ever: 0,
  78. banned: null, // may be null or falsy, indicates no ban active
  79. role: obj.role,
  80. can_talk: obj.can_talk,
  81. can_read: obj.can_read
  82. },
  83. function(err, res) {
  84. callback(err, res);
  85. }
  86. );
  87. };
  88.  
  89. /*------------------------------------------------------------------------------
  90. (public) create
  91.  
  92. + obj
  93. + callback
  94. - void
  95.  
  96. Creates new user.
  97. ------------------------------------------------------------------------------*/
  98. UserModel.prototype.create = function(obj, callback) {
  99. var self = this;
  100.  
  101. self._redisClient.hincrby("increment", "users", 1, function(err, res) {
  102. if(err) {
  103. } else {
  104. if(err) {
  105. callback(err, undefined);
  106. } else {
  107. self._createCouchUser(res, obj, function(err2, res2) {
  108. self._createRedisUser(res, obj, function(err3, res3) {
  109.  
  110. });
  111. });
  112. }
  113. }
  114. });
  115. };
Add Comment
Please, Sign In to add comment