]>
git.r.bdr.sh - rbdr/dasein/blob - lib/handlers/posts.js
3 const Joi
= require('joi');
4 const Pify
= require('pify');
5 const Redis
= require('redis');
6 const UUID
= require('uuid/v4');
10 internals
.kPostsPrefix
= 'posts';
11 internals
.kMaxPostSize
= 255;
13 internals
.kPostsSchema
= Joi
.object().keys({
14 uuid: Joi
.string().required(),
15 content: Joi
.string().max(internals
.kMaxPostSize
).required(),
16 timestamp: Joi
.number().integer().required(),
17 userId: Joi
.string().required(),
18 userName: Joi
.string().required(),
19 userImage: Joi
.string().required()
23 * Handles the HTTP requests for posts related operations
26 * @param {Dasein.tConfiguration} config The configuration to
29 module
.exports
= internals
.PostsHandler
= class PostsHandler
{
32 this._ttl
= config
.ttl
;
33 this._redis
= Redis
.createClient(config
.redis
);
35 // Log an error if it happens.
36 this._redis
.on('error', (err
) => {
43 * Fetches all available posts
46 * @memberof PostsHandler
48 * @return {generator} a koa compatible handler generator function
54 return function * () {
56 if (!this.state
.user
) {
57 return this.throw('Unauthorized', 401);
60 const scan
= Pify(self
._redis
.scan
.bind(self
._redis
));
61 const hgetall
= Pify(self
._redis
.hgetall
.bind(self
._redis
));
65 let currentKeys
= null;
68 [nextCursor
, currentKeys
] = yield scan(nextCursor
|| 0, 'MATCH', `${internals.kPostsPrefix}:*`);
69 keys
= keys
.concat(currentKeys
);
70 } while (nextCursor
> 0);
72 const posts
= yield keys
.map((key
) => hgetall(key
));
74 this.body
= posts
.sort((a
, b
) => b
.timestamp
- a
.timestamp
);
79 * Fetches a single post
82 * @memberof PostsHandler
84 * @return {generator} a koa compatible handler generator function
90 return function * (uuid
) {
92 if (!this.state
.user
) {
93 return this.throw('Unauthorized', 401);
96 const hgetall
= Pify(self
._redis
.hgetall
.bind(self
._redis
));
98 const postKey
= `${internals.kPostsPrefix}:${uuid}`;
100 const post
= yield hgetall(postKey
);
103 this.throw('Post not found', 404);
114 * @memberof PostsHandler
116 * @return {generator} a koa compatible handler generator function
122 return function * () {
124 if (!this.state
.user
) {
125 return this.throw('Unauthorized', 401);
128 const hmset
= Pify(self
._redis
.hmset
.bind(self
._redis
));
129 const hgetall
= Pify(self
._redis
.hgetall
.bind(self
._redis
));
130 const expire
= Pify(self
._redis
.expire
.bind(self
._redis
));
133 const timestamp
= Date
.now();
134 const user
= this.state
.user
;
136 const postKey
= `${internals.kPostsPrefix}:${uuid}`;
140 content: this.request
.body
.content
,
142 userId: user
.screen_name
,
144 userImage: user
.profile_image_url_https
147 yield self
._validate(post
).catch((err
) => {
149 this.throw(err
.message
, 422);
152 yield hmset(postKey
, post
);
153 yield expire(postKey
, self
._ttl
);
155 this.body
= yield hgetall(postKey
);
163 * @memberof PostsHandler
165 * @return {generator} a koa compatible handler generator function
169 return function * () {};
172 // Validates the post schema
176 const validate
= Pify(Joi
.validate
.bind(Joi
));
177 return validate(post
, internals
.kPostsSchema
);