]>
git.r.bdr.sh - rbdr/dasein/blob - lib/handlers/posts.js
425fb5e76fddb573ae2282978961998cb1de968c
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
));
63 const cursor
= parseInt(this.request
.query
.cursor
) || 0;
64 const [nextCursor
, keys
] = yield scan(cursor
, 'MATCH', `${internals.kPostsPrefix}:*`);
67 this.append('Link', `<${this.request.origin}${this.request.path}?cursor=${nextCursor}>; rel="next"`);
70 const posts
= yield keys
.map((key
) => hgetall(key
));
77 * Fetches a single post
80 * @memberof PostsHandler
82 * @return {generator} a koa compatible handler generator function
88 return function * (uuid
) {
90 if (!this.state
.user
) {
91 return this.throw('Unauthorized', 401);
94 const hgetall
= Pify(self
._redis
.hgetall
.bind(self
._redis
));
96 const postKey
= `${internals.kPostsPrefix}:${uuid}`;
98 const post
= yield hgetall(postKey
);
101 this.throw('Post not found', 404);
112 * @memberof PostsHandler
114 * @return {generator} a koa compatible handler generator function
120 return function * () {
122 if (!this.state
.user
) {
123 return this.throw('Unauthorized', 401);
126 const hmset
= Pify(self
._redis
.hmset
.bind(self
._redis
));
127 const hgetall
= Pify(self
._redis
.hgetall
.bind(self
._redis
));
128 const expire
= Pify(self
._redis
.expire
.bind(self
._redis
));
131 const timestamp
= Date
.now();
132 const user
= this.state
.user
;
134 const postKey
= `${internals.kPostsPrefix}:${uuid}`;
138 content: this.request
.body
.content
,
140 userId: user
.screen_name
,
142 userImage: user
.profile_image_url_https
145 yield self
._validate(post
).catch((err
) => {
147 this.throw(err
.message
, 422);
150 yield hmset(postKey
, post
);
151 yield expire(postKey
, self
._ttl
);
153 this.body
= yield hgetall(postKey
);
161 * @memberof PostsHandler
163 * @return {generator} a koa compatible handler generator function
167 return function * () {};
170 // Validates the post schema
174 const validate
= Pify(Joi
.validate
.bind(Joi
));
175 return validate(post
, internals
.kPostsSchema
);