calendar_event.go 4.95 KB
Newer Older
zauberstuhl's avatar
wip  
zauberstuhl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
package controllers
//
// GangGo API Library
// 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 (
  "net/http"
  "github.com/revel/revel"
  "gopkg.in/ganggo/ganggo.v0/app/helpers"
  "gopkg.in/ganggo/ganggo.v0/app/models"
  federation "gopkg.in/ganggo/federation.v0"
  "time"
  "strconv"
)

type ApiCalendarEvent struct {
  *revel.Controller

  ApiHelper
}

/**
 * @api {post} /search Discover a new person
 * @apiName ApiSearch.Create
 * @apiGroup Search
 *
 * @apiParam {String} handle Person network handle
 * @apiParam {String} access_token Oauth access token
 *
 * @apiSuccess {String} CreatedAt Timestamp of creation
 * @apiSuccess {String} UpdatedAt Timestamp of last replacment
 * @apiSuccess {Number} ID Unique database ID
 * @apiSuccess {String} Guid Person global ID
 * @apiSuccess {String} Author Handle with TLD
 * @apiSuccess {String} SerializedPublicKey Public key
 * @apiSuccess {Number} UserID User database ID (null if not a local user)
 *
 * @apiSuccessExample {json} Success-Response
 *     HTTP/1.1 200 OK
 *     {
 *       "ID": 12,
 *       "CreatedAt": "2018-01-08T15:25:43Z",
 *       "UpdatedAt": "2018-01-08T15:25:43Z",
 *       "Guid": "cc783a9749f09c7d817a1707a4c052bc",
 *       "Author": "test@localhost",
 *       "SerializedPublicKey": "[...]",
 *       "UserID": 1,
 *     }
 *
 * @apiError (Errors) {String} error Contains the recent error message
 *
 * @apiErrorExample {json} NotFound
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "[...]"
 *     }
 *
 */
func (p ApiCalendarEvent) Create() revel.Result {
  var allDay bool
  var description, summary string
  var location, formStart, formEnd string
  var calendarID, aspectID uint

  p.Params.Bind(&allDay, "all_day")
  p.Params.Bind(&description, "description")
  p.Params.Bind(&summary, "summary")
  p.Params.Bind(&formStart, "start")
  p.Params.Bind(&formEnd, "end")
  p.Params.Bind(&location, "location")
  p.Params.Bind(&calendarID, "calendarID")
  p.Params.Bind(&aspectID, "aspectID")

  start, err := time.Parse(federation.TIME_FORMAT, formStart)
  if err != nil {
    p.Log.Error(TAG, "err", err, "api", ERR_INVALID_DATETIME)
    return p.ApiError(http.StatusBadRequest, ERR_INVALID_DATETIME)
  }

  var end time.Time
  if !allDay {
    end, err = time.Parse(federation.TIME_FORMAT, formEnd)
    if err != nil {
      p.Log.Error(TAG, "err", err, "api", ERR_INVALID_DATETIME)
      return p.ApiError(http.StatusBadRequest, ERR_INVALID_DATETIME)
    }
  }

  guid, err := helpers.Uuid()
  if err != nil {
    p.Log.Error(TAG, "db", err, "api", ERR_SERVER)
    return p.ApiError(http.StatusInternalServerError, ERR_SERVER)
  }

  event := models.CalendarEvent{
    PersonID: p.CurrentUser.Person.ID,
    Guid: guid,
    Start: start,
    End: end,
    AllDay: allDay,
    Location: location,
    Description: description,
    Summary: summary,
  }

  if calendarID > 0 {
    var calendar models.Calendar
    err = calendar.FindByUserAndID(p.CurrentUser, calendarID)
    if err != nil {
      p.Log.Error(TAG, "err", err, "api", ERR_NOT_FOUND)
      return p.ApiError(http.StatusNotFound, ERR_NOT_FOUND)
    }
    event.CalendarID = calendar.ID
  } else {
    event.Public = true
  }
  err = event.Create()
  if err != nil {
    p.Log.Error(TAG, "err", err, "api", ERR_SERVER)
    return p.ApiError(http.StatusInternalServerError, ERR_SERVER)
  }

  if aspectID > 0 {
    shareable := models.Shareable{
      ShareableID: event.ID,
      UserID: p.CurrentUser.ID,
      ShareableType: models.ShareableEvent,
    }
    if err := shareable.Create(); err != nil {
      p.Log.Error(TAG, "db", err, "api", ERR_SERVER)
      return p.ApiError(http.StatusInternalServerError, ERR_SERVER)
    }

    // create an aspect visibility entry
    // the host has to remember the recipients
    visibility := models.AspectVisibility{
      ShareableID: event.ID,
      AspectID: aspectID,
      ShareableType: models.ShareableEvent,
    }
    if err := visibility.Create(); err != nil {
      p.Log.Error(TAG, "db", err, "api", ERR_SERVER)
      return p.ApiError(http.StatusInternalServerError, ERR_SERVER)
    }
  }

  apiPost := ApiPost{p.Controller, p.ApiHelper}
  apiPost.Params.Set("post", p.Message("calendars.preambel"))
  apiPost.Params.Set("eventID",
    strconv.FormatUint(uint64(event.ID), 10))
  apiPost.Params.Set("aspectID",
    strconv.FormatUint(uint64(aspectID), 10))
  apiPost.Create()

  return p.RenderJSON(event)
}