Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions routes/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ func AccountsCreate(w http.ResponseWriter, r *http.Request) {
Resource: models.MakeResource(account.ID, "Inbox"),
Builtin: true,
},
&models.Label{
Resource: models.MakeResource(account.ID, "Sent"),
Builtin: true,
},
&models.Label{
Resource: models.MakeResource(account.ID, "Trash"),
Builtin: true,
Expand Down
218 changes: 196 additions & 22 deletions routes/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,45 +40,182 @@ func LabelsList(c web.C, w http.ResponseWriter, r *http.Request) {
})
}

type LabelsCreateRequest struct {
Name string `json:"name"`
}

// LabelsCreateResponse contains the result of the LabelsCreate request.
type LabelsCreateResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Success bool `json:"success"`
Message string `json:"message"`
Label *models.Label `json:"label,omitempty"`
}

// LabelsCreate does *something* - TODO
func LabelsCreate(w http.ResponseWriter, r *http.Request) {
utils.JSONResponse(w, 501, &LabelsCreateResponse{
Success: false,
Message: "Sorry, not implemented yet",
func LabelsCreate(c web.C, w http.ResponseWriter, r *http.Request) {
// Decode the request
var input LabelsCreateRequest
err := utils.ParseRequest(r, &input)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err,
}).Warn("Unable to decode a request")

utils.JSONResponse(w, 400, &LabelsCreateResponse{
Success: false,
Message: "Invalid input format",
})
return
}

// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)

// Ensure that the input data isn't empty
if input.Name == "" {
utils.JSONResponse(w, 400, &LabelsCreateResponse{
Success: false,
Message: "Invalid request",
})
return
}

// Create a new label struct
label := &models.Label{
Resource: models.MakeResource(session.Owner, input.Name),
Builtin: false,
}

// Insert the label into the database
if err := env.Contacts.Insert(label); err != nil {
utils.JSONResponse(w, 500, &LabelsCreateResponse{
Success: false,
Message: "internal server error - LA/CR/01",
})

env.Log.WithFields(logrus.Fields{
"error": err,
}).Error("Could not insert a label into the database")
return
}

utils.JSONResponse(w, 201, &LabelsCreateResponse{
Success: true,
Label: label,
})
}

// LabelsGetResponse contains the result of the LabelsGet request.
type LabelsGetResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Label *models.Label `json:"label,omitempty"`
}

// LabelsGet does *something* - TODO
func LabelsGet(w http.ResponseWriter, r *http.Request) {
utils.JSONResponse(w, 501, &LabelsGetResponse{
Success: false,
Message: "Sorry, not implemented yet",
func LabelsGet(c web.C, w http.ResponseWriter, r *http.Request) {
// Get the label from the database
label, err := env.Labels.GetLabel(c.URLParams["id"])
if err != nil {
utils.JSONResponse(w, 404, &LabelsGetResponse{
Success: false,
Message: "Label not found",
})
return
}

// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)

// Check for ownership
if label.Owner != session.Owner {
utils.JSONResponse(w, 404, &LabelsGetResponse{
Success: false,
Message: "Label not found",
})
return
}

// Write the label to the response
utils.JSONResponse(w, 200, &LabelsGetResponse{
Success: true,
Label: label,
})
}

type LabelsUpdateRequest struct {
Name string `json:"name"`
}

// LabelsUpdateResponse contains the result of the LabelsUpdate request.
type LabelsUpdateResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Label *models.Label `json:"label,omitempty"`
}

// LabelsUpdate does *something* - TODO
func LabelsUpdate(w http.ResponseWriter, r *http.Request) {
utils.JSONResponse(w, 501, &LabelsUpdateResponse{
Success: false,
Message: "Sorry, not implemented yet",
func LabelsUpdate(c web.C, w http.ResponseWriter, r *http.Request) {
// Decode the request
var input LabelsUpdateRequest
err := utils.ParseRequest(r, &input)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err,
}).Warn("Unable to decode a request")

utils.JSONResponse(w, 400, &LabelsUpdateResponse{
Success: false,
Message: "Invalid input format",
})
return
}

// Get the label from the database
label, err := env.Labels.GetLabel(c.URLParams["id"])
if err != nil {
utils.JSONResponse(w, 404, &LabelsUpdateResponse{
Success: false,
Message: "Label not found",
})
return
}

// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)

// Check for ownership
if label.Owner != session.Owner {
utils.JSONResponse(w, 404, &LabelsUpdateResponse{
Success: false,
Message: "Label not found",
})
return
}

if input.Name != "" {
label.Name = input.Name
}

// Perform the update
err = env.Labels.UpdateID(c.URLParams["id"], input)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err,
"id": c.URLParams["id"],
}).Error("Unable to update a contact")

utils.JSONResponse(w, 500, &LabelsUpdateResponse{
Success: false,
Message: "Internal error (code LA/UP/01)",
})
return
}

// Write the contact to the response
utils.JSONResponse(w, 200, &LabelsUpdateResponse{
Success: true,
Label: label,
})
}

Expand All @@ -89,9 +226,46 @@ type LabelsDeleteResponse struct {
}

// LabelsDelete does *something* - TODO
func LabelsDelete(w http.ResponseWriter, r *http.Request) {
utils.JSONResponse(w, 501, &LabelsDeleteResponse{
Success: false,
Message: "Sorry, not implemented yet",
func LabelsDelete(c web.C, w http.ResponseWriter, r *http.Request) {
// Get the label from the database
label, err := env.Labels.GetLabel(c.URLParams["id"])
if err != nil {
utils.JSONResponse(w, 404, &LabelsDeleteResponse{
Success: false,
Message: "Label not found",
})
return
}

// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)

// Check for ownership
if label.Owner != session.Owner {
utils.JSONResponse(w, 404, &LabelsDeleteResponse{
Success: false,
Message: "Label not found",
})
return
}

// Perform the deletion
err = env.Labels.DeleteID(c.URLParams["id"])
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err,
"id": c.URLParams["id"],
}).Error("Unable to delete a label")

utils.JSONResponse(w, 500, &LabelsDeleteResponse{
Success: false,
Message: "Internal error (code LA/DE/01)",
})
return
}

utils.JSONResponse(w, 200, &LabelsDeleteResponse{
Success: true,
Message: "Label successfully removed",
})
}