2017-07-24 19:10:49 +01:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
|
|
|
// This software (Documize Community Edition) is licensed under
|
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
|
|
|
// by contacting <sales@documize.com>.
|
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
|
|
|
package pin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
"github.com/documize/community/core/env"
|
2017-07-24 19:10:49 +01:00
|
|
|
"github.com/documize/community/core/request"
|
|
|
|
"github.com/documize/community/core/response"
|
|
|
|
"github.com/documize/community/core/uniqueid"
|
|
|
|
"github.com/documize/community/domain"
|
2017-07-26 10:50:26 +01:00
|
|
|
"github.com/documize/community/model/audit"
|
|
|
|
"github.com/documize/community/model/pin"
|
2017-07-24 19:10:49 +01:00
|
|
|
)
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
// Handler contains the runtime information such as logging and database.
|
|
|
|
type Handler struct {
|
|
|
|
Runtime *env.Runtime
|
|
|
|
Store *domain.Store
|
|
|
|
}
|
|
|
|
|
2017-07-24 19:10:49 +01:00
|
|
|
// Add saves pinned item.
|
|
|
|
func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "pin.Add"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 19:10:49 +01:00
|
|
|
|
|
|
|
userID := request.Param(r, "userID")
|
|
|
|
if len(userID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "userID")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !h.Runtime.Product.License.IsValid() {
|
|
|
|
response.WriteBadLicense(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
if !ctx.Authenticated {
|
2017-07-24 19:10:49 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, "body")
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
var pin pin.Pin
|
2017-07-24 19:10:49 +01:00
|
|
|
err = json.Unmarshal(body, &pin)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, "pin")
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pin.RefID = uniqueid.Generate()
|
2017-07-26 10:50:26 +01:00
|
|
|
pin.OrgID = ctx.OrgID
|
|
|
|
pin.UserID = ctx.UserID
|
2017-07-24 19:10:49 +01:00
|
|
|
pin.Pin = strings.TrimSpace(pin.Pin)
|
|
|
|
if len(pin.Pin) > 20 {
|
|
|
|
pin.Pin = pin.Pin[0:20]
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
2017-07-24 19:10:49 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
err = h.Store.Pin.Add(ctx, pin)
|
2017-07-24 19:10:49 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 19:10:49 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Commit()
|
2017-07-24 19:10:49 +01:00
|
|
|
|
2018-02-04 15:43:57 +00:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypePinAdd)
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
newPin, err := h.Store.Pin.GetPin(ctx, pin.RefID)
|
2017-07-24 19:10:49 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteJSON(w, newPin)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserPins returns users' pins.
|
|
|
|
func (h *Handler) GetUserPins(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "pin.GetUserPins"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 19:10:49 +01:00
|
|
|
|
|
|
|
userID := request.Param(r, "userID")
|
|
|
|
if len(userID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "userID")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
if !ctx.Authenticated {
|
2017-07-24 19:10:49 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
pins, err := h.Store.Pin.GetUserPins(ctx, userID)
|
2017-07-24 19:10:49 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
2017-07-26 10:50:26 +01:00
|
|
|
pins = []pin.Pin{}
|
2017-07-24 19:10:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteJSON(w, pins)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUserPin removes saved user pin.
|
|
|
|
func (h *Handler) DeleteUserPin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "pin.DeleteUserPin"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 19:10:49 +01:00
|
|
|
|
|
|
|
userID := request.Param(r, "userID")
|
|
|
|
if len(userID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "userID")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pinID := request.Param(r, "pinID")
|
|
|
|
if len(pinID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "pinID")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !h.Runtime.Product.License.IsValid() {
|
|
|
|
response.WriteBadLicense(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
if ctx.UserID != userID {
|
2017-07-24 19:10:49 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
2017-07-24 19:10:49 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
_, err = h.Store.Pin.DeletePin(ctx, pinID)
|
2017-07-24 19:10:49 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 19:10:49 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Commit()
|
2017-07-24 19:10:49 +01:00
|
|
|
|
2018-02-04 15:43:57 +00:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypePinDelete)
|
|
|
|
|
2017-07-24 19:10:49 +01:00
|
|
|
response.WriteEmpty(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePinSequence records order of pinned items.
|
|
|
|
func (h *Handler) UpdatePinSequence(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "pin.DeleteUserPin"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 19:10:49 +01:00
|
|
|
|
|
|
|
userID := request.Param(r, "userID")
|
|
|
|
if len(userID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "userID")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !h.Runtime.Product.License.IsValid() {
|
|
|
|
response.WriteBadLicense(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
if !ctx.Authenticated {
|
2017-07-24 19:10:49 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var pins []string
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &pins)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
2017-07-24 19:10:49 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range pins {
|
2017-07-26 10:50:26 +01:00
|
|
|
err = h.Store.Pin.UpdatePinSequence(ctx, v, k+1)
|
2017-07-24 19:10:49 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 19:10:49 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Commit()
|
2017-07-24 19:10:49 +01:00
|
|
|
|
2018-02-04 15:43:57 +00:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypePinResequence)
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
newPins, err := h.Store.Pin.GetUserPins(ctx, userID)
|
2017-07-24 19:10:49 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 19:10:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteJSON(w, newPins)
|
|
|
|
}
|