serializers/post - run promises in parallel
now w/ opts.detail, returns my_reaction field as 'null' w/ no reaction (before: field appears w/ some reaction)
This commit is contained in:
		
							parent
							
								
									78487934c7
								
							
						
					
					
						commit
						11190f56ad
					
				|  | @ -95,6 +95,7 @@ | |||
|     "webpack": "3.8.1" | ||||
|   }, | ||||
|   "dependencies": { | ||||
|     "@prezzemolo/rap": "^0.1.0", | ||||
|     "accesses": "2.5.0", | ||||
|     "animejs": "2.2.0", | ||||
|     "autwh": "0.0.1", | ||||
|  |  | |||
|  | @ -12,6 +12,7 @@ import serializeChannel from './channel'; | |||
| import serializeUser from './user'; | ||||
| import serializeDriveFile from './drive-file'; | ||||
| import parse from '../common/text'; | ||||
| import rap from '@prezzemolo/rap' | ||||
| 
 | ||||
| /** | ||||
|  * Serialize a post | ||||
|  | @ -70,21 +71,21 @@ const self = ( | |||
| 	} | ||||
| 
 | ||||
| 	// Populate user
 | ||||
| 	_post.user = await serializeUser(_post.user_id, meId); | ||||
| 	_post.user = serializeUser(_post.user_id, meId); | ||||
| 
 | ||||
| 	// Populate app
 | ||||
| 	if (_post.app_id) { | ||||
| 		_post.app = await serializeApp(_post.app_id); | ||||
| 		_post.app = serializeApp(_post.app_id); | ||||
| 	} | ||||
| 
 | ||||
| 	// Populate channel
 | ||||
| 	if (_post.channel_id) { | ||||
| 		_post.channel = await serializeChannel(_post.channel_id); | ||||
| 		_post.channel = serializeChannel(_post.channel_id); | ||||
| 	} | ||||
| 
 | ||||
| 	// Populate media
 | ||||
| 	if (_post.media_ids) { | ||||
| 		_post.media = await Promise.all(_post.media_ids.map(fileId => | ||||
| 		_post.media = Promise.all(_post.media_ids.map(fileId => | ||||
| 			serializeDriveFile(fileId) | ||||
| 		)); | ||||
| 	} | ||||
|  | @ -92,82 +93,97 @@ const self = ( | |||
| 	// When requested a detailed post data
 | ||||
| 	if (opts.detail) { | ||||
| 		// Get previous post info
 | ||||
| 		const prev = await Post.findOne({ | ||||
| 			user_id: _post.user_id, | ||||
| 			_id: { | ||||
| 				$lt: id | ||||
| 			} | ||||
| 		}, { | ||||
| 			fields: { | ||||
| 				_id: true | ||||
| 			}, | ||||
| 			sort: { | ||||
| 				_id: -1 | ||||
| 			} | ||||
| 		}); | ||||
| 		_post.prev = prev ? prev._id : null; | ||||
| 		_post.prev = (async () => { | ||||
| 			const prev = Post.findOne({ | ||||
| 				user_id: _post.user_id, | ||||
| 				_id: { | ||||
| 					$lt: id | ||||
| 				} | ||||
| 			}, { | ||||
| 				fields: { | ||||
| 					_id: true | ||||
| 				}, | ||||
| 				sort: { | ||||
| 					_id: -1 | ||||
| 				} | ||||
| 			}); | ||||
| 			return prev ? prev._id : null; | ||||
| 		})() | ||||
| 
 | ||||
| 		// Get next post info
 | ||||
| 		const next = await Post.findOne({ | ||||
| 			user_id: _post.user_id, | ||||
| 			_id: { | ||||
| 				$gt: id | ||||
| 			} | ||||
| 		}, { | ||||
| 			fields: { | ||||
| 				_id: true | ||||
| 			}, | ||||
| 			sort: { | ||||
| 				_id: 1 | ||||
| 			} | ||||
| 		}); | ||||
| 		_post.next = next ? next._id : null; | ||||
| 		_post.next = (async () => { | ||||
| 			const next = await Post.findOne({ | ||||
| 				user_id: _post.user_id, | ||||
| 				_id: { | ||||
| 					$gt: id | ||||
| 				} | ||||
| 			}, { | ||||
| 				fields: { | ||||
| 					_id: true | ||||
| 				}, | ||||
| 				sort: { | ||||
| 					_id: 1 | ||||
| 				} | ||||
| 			}); | ||||
| 			return next ? next._id : null; | ||||
| 		})() | ||||
| 
 | ||||
| 		if (_post.reply_id) { | ||||
| 			// Populate reply to post
 | ||||
| 			_post.reply = await self(_post.reply_id, meId, { | ||||
| 			_post.reply = self(_post.reply_id, meId, { | ||||
| 				detail: false | ||||
| 			}); | ||||
| 		} | ||||
| 
 | ||||
| 		if (_post.repost_id) { | ||||
| 			// Populate repost
 | ||||
| 			_post.repost = await self(_post.repost_id, meId, { | ||||
| 			_post.repost = self(_post.repost_id, meId, { | ||||
| 				detail: _post.text == null | ||||
| 			}); | ||||
| 		} | ||||
| 
 | ||||
| 		// Poll
 | ||||
| 		if (meId && _post.poll) { | ||||
| 			const vote = await Vote | ||||
| 				.findOne({ | ||||
| 					user_id: meId, | ||||
| 					post_id: id | ||||
| 				}); | ||||
| 			_post.poll = (async (poll) => { | ||||
| 				const vote = await Vote | ||||
| 					.findOne({ | ||||
| 						user_id: meId, | ||||
| 						post_id: id | ||||
| 					}); | ||||
| 
 | ||||
| 			if (vote != null) { | ||||
| 				const myChoice = _post.poll.choices | ||||
| 					.filter(c => c.id == vote.choice)[0]; | ||||
| 				if (vote != null) { | ||||
| 					const myChoice = poll.choices | ||||
| 						.filter(c => c.id == vote.choice)[0]; | ||||
| 
 | ||||
| 				myChoice.is_voted = true; | ||||
| 			} | ||||
| 					myChoice.is_voted = true; | ||||
| 				} | ||||
| 
 | ||||
| 				return poll | ||||
| 			})(_post.poll) | ||||
| 		} | ||||
| 
 | ||||
| 		// Fetch my reaction
 | ||||
| 		if (meId) { | ||||
| 			const reaction = await Reaction | ||||
| 				.findOne({ | ||||
| 					user_id: meId, | ||||
| 					post_id: id, | ||||
| 					deleted_at: { $exists: false } | ||||
| 				}); | ||||
| 			_post.my_reaction = (async () => { | ||||
| 				const reaction = await Reaction | ||||
| 					.findOne({ | ||||
| 						user_id: meId, | ||||
| 						post_id: id, | ||||
| 						deleted_at: { $exists: false } | ||||
| 					}); | ||||
| 
 | ||||
| 			if (reaction) { | ||||
| 				_post.my_reaction = reaction.reaction; | ||||
| 			} | ||||
| 				if (reaction) { | ||||
| 					return reaction.reaction; | ||||
| 				} | ||||
| 
 | ||||
| 				return null | ||||
| 			})(); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	// resolve promises in _post object
 | ||||
| 	_post = await rap(_post) | ||||
| 
 | ||||
| 	resolve(_post); | ||||
| }); | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue