|
2 | 2 |
|
3 | 3 | from __future__ import unicode_literals
|
4 | 4 |
|
| 5 | +import collections |
5 | 6 | import re
|
6 | 7 | import time
|
7 | 8 | import functools
|
@@ -112,6 +113,7 @@ class API(object):
|
112 | 113 | ('count', ('GET', '/count')),
|
113 | 114 | ('counts', ('POST', '/count')),
|
114 | 115 | ('feed', ('GET', '/feed')),
|
| 116 | + ('latest', ('GET', '/latest')), |
115 | 117 | ('view', ('GET', '/id/<int:id>')),
|
116 | 118 | ('edit', ('PUT', '/id/<int:id>')),
|
117 | 119 | ('delete', ('DELETE', '/id/<int:id>')),
|
@@ -1136,3 +1138,89 @@ def admin(self, env, req):
|
1136 | 1138 | counts=comment_mode_count,
|
1137 | 1139 | order_by=order_by, asc=asc,
|
1138 | 1140 | isso_host_script=isso_host_script)
|
| 1141 | + """ |
| 1142 | + @api {get} /latest latest |
| 1143 | + @apiGroup Comment |
| 1144 | + @apiDescription |
| 1145 | + Get the latest comments from the system, no matter which thread |
| 1146 | +
|
| 1147 | + @apiParam {number} limit |
| 1148 | + The quantity of last comments to retrieve |
| 1149 | +
|
| 1150 | + @apiExample {curl} Get the latest 5 comments |
| 1151 | + curl 'https://comments.example.com/latest?limit=5' |
| 1152 | +
|
| 1153 | + @apiUse commentResponse |
| 1154 | +
|
| 1155 | + @apiSuccessExample Example result: |
| 1156 | + [ |
| 1157 | + { |
| 1158 | + "website": null, |
| 1159 | + "uri": "/some", |
| 1160 | + "author": null, |
| 1161 | + "parent": null, |
| 1162 | + "created": 1464912312.123416, |
| 1163 | + "text": " <p>I want to use MySQL</p>", |
| 1164 | + "dislikes": 0, |
| 1165 | + "modified": null, |
| 1166 | + "mode": 1, |
| 1167 | + "id": 3, |
| 1168 | + "likes": 1 |
| 1169 | + }, |
| 1170 | + { |
| 1171 | + "website": null, |
| 1172 | + "uri": "/other", |
| 1173 | + "author": null, |
| 1174 | + "parent": null, |
| 1175 | + "created": 1464914341.312426, |
| 1176 | + "text": " <p>I want to use MySQL</p>", |
| 1177 | + "dislikes": 0, |
| 1178 | + "modified": null, |
| 1179 | + "mode": 1, |
| 1180 | + "id": 4, |
| 1181 | + "likes": 0 |
| 1182 | + } |
| 1183 | + ] |
| 1184 | + """ |
| 1185 | + |
| 1186 | + def latest(self, environ, request): |
| 1187 | + # if the feature is not allowed, don't present the endpoint |
| 1188 | + if not self.conf.getboolean("latest-enabled"): |
| 1189 | + return NotFound() |
| 1190 | + |
| 1191 | + # get and check the limit |
| 1192 | + bad_limit_msg = "Query parameter 'limit' is mandatory (integer, >0)" |
| 1193 | + try: |
| 1194 | + limit = int(request.args['limit']) |
| 1195 | + except (KeyError, ValueError): |
| 1196 | + return BadRequest(bad_limit_msg) |
| 1197 | + if limit <= 0: |
| 1198 | + return BadRequest(bad_limit_msg) |
| 1199 | + |
| 1200 | + # retrieve the latest N comments from the DB |
| 1201 | + all_comments_gen = self.comments.fetchall(limit=None, order_by='created', mode='1') |
| 1202 | + comments = collections.deque(all_comments_gen, maxlen=limit) |
| 1203 | + |
| 1204 | + # prepare a special set of fields (except text which is rendered specifically) |
| 1205 | + fields = { |
| 1206 | + 'author', |
| 1207 | + 'created', |
| 1208 | + 'dislikes', |
| 1209 | + 'id', |
| 1210 | + 'likes', |
| 1211 | + 'mode', |
| 1212 | + 'modified', |
| 1213 | + 'parent', |
| 1214 | + 'text', |
| 1215 | + 'uri', |
| 1216 | + 'website', |
| 1217 | + } |
| 1218 | + |
| 1219 | + # process the retrieved comments and build results |
| 1220 | + result = [] |
| 1221 | + for comment in comments: |
| 1222 | + processed = {key: comment[key] for key in fields} |
| 1223 | + processed['text'] = self.isso.render(comment['text']) |
| 1224 | + result.append(processed) |
| 1225 | + |
| 1226 | + return JSON(result, 200) |
0 commit comments