comment.go 9.13 KB
Newer Older
zauberstuhl's avatar
zauberstuhl committed
1 2
package controllers
//
zauberstuhl's avatar
zauberstuhl committed
3
// GangGo API Library
zauberstuhl's avatar
zauberstuhl committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import (
  "github.com/revel/revel"
22 23
  "gopkg.in/ganggo/ganggo.v0/app/models"
  "gopkg.in/ganggo/ganggo.v0/app/jobs"
24
  "gopkg.in/ganggo/api.v0/app/helpers"
25 26
  // XXX only for uuid
  uuid "gopkg.in/ganggo/ganggo.v0/app/helpers"
27
  federation "gopkg.in/ganggo/federation.v0"
zauberstuhl's avatar
zauberstuhl committed
28
  "strconv"
29
  "net/http"
zauberstuhl's avatar
zauberstuhl committed
30 31
)

32 33 34 35 36
/**
 * @apiDefine Comments Comments endpoint
 *
 * Viewing, creating and deleting Comment structures
 */
37
type ApiComment struct {
zauberstuhl's avatar
zauberstuhl committed
38
  *revel.Controller
39 40

  ApiHelper
zauberstuhl's avatar
zauberstuhl committed
41 42
}

zauberstuhl's avatar
zauberstuhl committed
43 44 45 46 47 48 49
/**
* @api {get} /comments/:id Display a specific comment
 * @apiName ApiComment.Show
 * @apiGroup Comments
 *
 * @apiParam {Number} id Comment database ID
 * @apiParam {String} access_token Oauth access token
50
 * @apiParam {String} [fields] Display only specific fields, e.g. fields=ID,Person(ID:Profile(ID:ImageUrl))
zauberstuhl's avatar
zauberstuhl committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
 *
 * @apiSuccess {String} CreatedAt Timestamp of creation
 * @apiSuccess {String} UpdatedAt Timestamp of last replacment
 * @apiSuccess {String} Guid Unique global ID
 * @apiSuccess {Number} ID Unique database ID
 * @apiSuccess {String} Text Status message
 * @apiSuccess {Number} ShareableID Post database ID
 * @apiSuccess {Number} PersonID Person database ID
 * @apiSuccess {String} ShareableType Shareable type e.g. Post, Comment
 * @apiSuccess {Hash} CommentSignature Signature
 * @apiSuccess {Hash} Person Person structure
 *
 * @apiSuccessExample {json} Success-Response
 *     HTTP/1.1 200 OK
 *     {
 *       "ID": 12,
 *       "CreatedAt": "2018-01-08T15:25:43Z",
 *       "UpdatedAt": "2018-01-08T15:25:43Z",
 *       "Text": "hi",
 *       "ShareableID": 20,
 *       "PersonID": 1,
 *       "Guid": "cc783a9749f09c7d817a1707a4c052bc",
 *       "LikesCount": 0,
 *       "ShareableType": "Post",
 *       [...]
 *     }
 *
 * @apiError (Errors) {String} error Contains the recent error message
 *
 * @apiErrorExample {json} Unauthorized
 *     HTTP/1.1 401 Unauthorized
 *     {
 *       "error": "[...]"
 *     }
 *
 * @apiErrorExample {json} NotFound
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "[...]"
 *     }
 *
 */
func (c ApiComment) Show() revel.Result {
  var (
95
    guid, fields string
zauberstuhl's avatar
zauberstuhl committed
96 97 98 99
    comment models.Comment
  )

  c.Params.Bind(&guid, "id")
100
  c.Params.Bind(&fields, "fields")
zauberstuhl's avatar
zauberstuhl committed
101 102 103 104 105 106 107 108 109 110 111

  id, err := strconv.ParseUint(guid, 10, 32);
  if err == nil {
    err = comment.FindByUserAndID(c.CurrentUser, uint(id))
  } else {
    err = comment.FindByUserAndGuid(c.CurrentUser, guid)
  }
  if err != nil {
    c.Log.Error(TAG, "db", err, "api", ERR_NOT_FOUND)
    return c.ApiError(http.StatusNotFound, ERR_NOT_FOUND)
  }
112
  return c.RenderJSON(helpers.SelectStructFields(comment, fields))
zauberstuhl's avatar
zauberstuhl committed
113 114
}

115 116
/**
 * @api {get} /posts/:id/comments Display comments related to post
zauberstuhl's avatar
zauberstuhl committed
117
 * @apiName ApiComment.IndexPost
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
 * @apiGroup Comments
 *
 * @apiParam {Number} id Post database ID
 * @apiParam {String} access_token Oauth access token
 *
 * @apiSuccess {String} CreatedAt Timestamp of creation
 * @apiSuccess {String} UpdatedAt Timestamp of last replacment
 * @apiSuccess {String} Guid Unique global ID
 * @apiSuccess {Number} ID Unique database ID
 * @apiSuccess {String} Text Status message
 * @apiSuccess {Number} ShareableID Post database ID
 * @apiSuccess {Number} PersonID Person database ID
 * @apiSuccess {String} ShareableType Shareable type e.g. Post, Comment
 * @apiSuccess {Hash} CommentSignature Signature
 * @apiSuccess {Hash} Person Person structure
 *
134
 * @apiSuccessExample {json} Success-Response
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
 *     HTTP/1.1 200 OK
 *     [
 *      {
 *        "ID": 12,
 *        "CreatedAt": "2018-01-08T15:25:43Z",
 *        "UpdatedAt": "2018-01-08T15:25:43Z",
 *        "Text": "hi",
 *        "ShareableID": 20,
 *        "PersonID": 1,
 *        "Guid": "cc783a9749f09c7d817a1707a4c052bc",
 *        "LikesCount": 0,
 *        "ShareableType": "Post",
 *        [...]
 *      }
 *     ]
 *
151
 * @apiError (Errors) {String} error Contains the recent error message
152
 *
153 154 155 156 157 158
 * @apiErrorExample {json} Unauthorized
 *     HTTP/1.1 401 Unauthorized
 *     {
 *       "error": "[...]"
 *     }
 *
159
 * @apiErrorExample {json} NotFound
160 161
 *     HTTP/1.1 404 Not Found
 *     {
162
 *       "error": "[...]"
163
 *     }
164
 *
165
 */
zauberstuhl's avatar
zauberstuhl committed
166
func (c ApiComment) IndexPost() revel.Result {
zauberstuhl's avatar
zauberstuhl committed
167 168 169 170 171 172 173 174
  var (
    postID uint
    comments models.Comments
  )

  c.Params.Bind(&postID, "id")
  err := comments.FindByPostID(postID)
  if err != nil {
175 176
    c.Log.Error(TAG, "db", err, "api", ERR_NOT_FOUND)
    return c.ApiError(http.StatusNotFound, ERR_NOT_FOUND)
zauberstuhl's avatar
zauberstuhl committed
177
  }
178
  return c.RenderJSON(comments)
zauberstuhl's avatar
zauberstuhl committed
179 180
}

181 182
/**
 * @api {post} /posts/:id/comments Create comment on post
zauberstuhl's avatar
zauberstuhl committed
183
 * @apiName ApiComment.CreatePost
184 185 186 187 188 189 190
 * @apiGroup Comments
 *
 * @apiParam {Number} id Post database ID
 * @apiParam {String} comment Status message
 * @apiParam {String} [fields] Display only specific fields, e.g. fields=ID,Person(ID:Profile(ID:ImageUrl))
 * @apiParam {String} access_token Oauth access token
 *
191
 * @apiError (Errors) {String} error Contains the recent error message
192
 *
193 194 195 196 197 198 199
 * @apiErrorExample {json} Unauthorized
 *     HTTP/1.1 401 Unauthorized
 *     {
 *       "error": "[...]"
 *     }
 *
 * @apiErrorExample {json} NotFound
200 201
 *     HTTP/1.1 404 Not Found
 *     {
202 203 204 205 206 207 208
 *       "error": "[...]"
 *     }
 *
 * @apiErrorExample {json} BadRequest
 *     HTTP/1.1 400 Bad Request
 *     {
 *       "error": "[...]"
209
 *     }
210
 *
211
 */
zauberstuhl's avatar
zauberstuhl committed
212
func (c ApiComment) CreatePost() revel.Result {
zauberstuhl's avatar
zauberstuhl committed
213
  var (
214
    text, fields string
zauberstuhl's avatar
zauberstuhl committed
215 216 217 218
    postID uint
    post models.Post
  )

219
  c.Params.Bind(&text, "comment")
zauberstuhl's avatar
zauberstuhl committed
220
  c.Params.Bind(&fields, "fields")
zauberstuhl's avatar
zauberstuhl committed
221 222
  c.Params.Bind(&postID, "id")

223
  if helpers.EmptyString(text) {
224 225 226 227
    c.Log.Error(TAG, "api", ERR_EMPTY_STRING)
    return c.ApiError(http.StatusBadRequest, ERR_EMPTY_STRING)
  }

228 229
  if !post.Exists(postID) {
    c.Log.Error(TAG, "api", ERR_NOT_FOUND)
230
    return c.ApiError(http.StatusNotFound, ERR_NOT_FOUND)
zauberstuhl's avatar
zauberstuhl committed
231 232
  }

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  guid, err := uuid.Uuid()
  if err != nil {
    c.Log.Error(TAG, "db", err, "api", ERR_SERVER)
    return c.ApiError(http.StatusInternalServerError, ERR_SERVER)
  }

  entityComment := federation.EntityComment{
    Author: c.CurrentUser.Person.Author,
    Guid: guid,
    ParentGuid: post.Guid,
    Text: text,
  }

  var comment models.Comment
  err = comment.Create(&entityComment)
  if err != nil {
    c.Log.Error(TAG, "db", err, "api", ERR_SERVER)
    return c.ApiError(http.StatusInternalServerError, ERR_SERVER)
  }
  entityComment.CreatedAt = federation.Time{
    Time: comment.CreatedAt,
  }

zauberstuhl's avatar
zauberstuhl committed
256
  dispatcher := jobs.Dispatcher{
257
    User: c.CurrentUser,
258 259
    Model: comment,
    Message: entityComment,
zauberstuhl's avatar
zauberstuhl committed
260
  }
261 262 263 264 265

    // XXX
    // notify local user about a new comment
    //go like.TriggerNotification(parentUser)
  // deliver to the network
zauberstuhl's avatar
zauberstuhl committed
266 267
  go dispatcher.Run()

268
  return c.RenderJSON(comment)
zauberstuhl's avatar
zauberstuhl committed
269 270
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
/**
 * @api {delete} /comments/:guid Delete a comment
 * @apiName ApiComment.Delete
 * @apiGroup Comments
 *
 * @apiParam {String} access_token Oauth access token
 * @apiParam {String} guid Unique global ID
 *
 * @apiSuccessExample {json} Success-Response
 *     HTTP/1.1 200 OK
 *     {}
 *
 * @apiError (Errors) {String} error Contains the recent error message
 *
 * @apiErrorExample {json} BadRequest
 *     HTTP/1.1 400 BadRequest
 *     {
 *       "error": "[...]"
 *     }
 *
 * @apiErrorExample {json} Unauthorized
 *     HTTP/1.1 401 Unauthorized
 *     {
 *       "error": "[...]"
 *     }
 *
 * @apiErrorExample {json} NotFound
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "[...]"
 *     }
 *
 */
func (c ApiComment) Delete(guid string) revel.Result {
  var comment models.Comment
  commentID, err := strconv.ParseUint(guid, 10, 32);
  if err == nil {
    err = comment.FindByUserAndID(c.CurrentUser, uint(commentID))
  } else {
    err = comment.FindByUserAndGuid(c.CurrentUser, guid)
  }
  if err != nil {
    c.Log.Error(TAG, "db", err, "api", ERR_POST_NOT_FOUND)
    return c.ApiError(http.StatusNotFound, ERR_POST_NOT_FOUND)
  }

  if comment.Person.ID == c.CurrentUser.Person.ID {
    entity := federation.EntityRetraction{
      Author: c.CurrentUser.Person.Author,
      TargetGuid: comment.Guid,
      TargetType: models.ShareableComment,
    }
    dispatcher := jobs.Dispatcher{Message: entity}
    // NOTE relay to other hosts if we own this entity
    // should be done before we start deleting db records
    // thats why this call is synchron XXX we should
    // change that !
    dispatcher.Run()

    err = comment.Delete()
    if err != nil {
      c.Log.Error(TAG, "db", err, "api", ERR_NOT_FOUND)
      return c.ApiError(http.StatusNotFound, ERR_NOT_FOUND)
    }
  } else {
    c.Log.Error(TAG, "api", ERR_BAD_REQUEST)
    return c.ApiError(http.StatusBadRequest, ERR_BAD_REQUEST)
  }
  return c.RenderJSON(struct{}{})
zauberstuhl's avatar
zauberstuhl committed
340
}