Skip to content
Browse files

Implemented sending links. Fixes #21.

  • Loading branch information...
1 parent 2063190 commit 6274ee2d6767ca2d8302a00b9c2e8a51bfb38df1 @paddyforan paddyforan committed
Showing with 79 additions and 5 deletions.
  1. +79 −5 links.go
View
84 links.go
@@ -1,12 +1,19 @@
package main
import (
+ "encoding/json"
"get.2cloud.org/twocloud"
+ "io/ioutil"
"net/http"
"secondbit.org/ruid"
"strconv"
+ "strings"
)
+type LinksReq struct {
+ Links []twocloud.Link `json:"links"`
+}
+
func getLinks(w http.ResponseWriter, r *twocloud.RequestBundle) {
username := r.Request.URL.Query().Get(":username")
user := r.AuthUser
@@ -18,6 +25,7 @@ func getLinks(w http.ResponseWriter, r *twocloud.RequestBundle) {
roleFlag = twocloud.RoleReceiver
}
var after, before ruid.RUID
+ var err error
afterstr := r.Request.URL.Query().Get("after")
if afterstr != "" {
after, err = ruid.RUIDFromString(afterstr)
@@ -28,7 +36,7 @@ func getLinks(w http.ResponseWriter, r *twocloud.RequestBundle) {
}
beforestr := r.Request.URL.Query().Get("before")
if beforestr != "" {
- before, err = ruid.RUIDFromSTring(beforestr)
+ before, err = ruid.RUIDFromString(beforestr)
if err != nil {
Respond(w, r, http.StatusBadRequest, "Invalid before ID.", []interface{}{})
return
@@ -83,25 +91,91 @@ func getLinks(w http.ResponseWriter, r *twocloud.RequestBundle) {
Respond(w, r, http.StatusBadRequest, "That device ID does not belong to that user.", []interface{}{})
return
}
- links, err = twocloud.GetLinksByDevice(device, roleFlag, before, after, count)
+ links, err = r.GetLinksByDevice(device, roleFlag, before, after, count)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
} else {
- links, err = twocloud.GetLinksByUser(user, roleFlag, before, after, count)
+ links, err = r.GetLinksByUser(user, roleFlag, before, after, count)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
}
- Respond(w, r, http.StatusOK, "Successfully retrieved a list of links", links)
+ Respond(w, r, http.StatusOK, "Successfully retrieved a list of links", []interface{}{links})
return
}
-func sendLink(w http.ResponseWriter, r *twocloud.RequestBundle) {
+func sendLinks(w http.ResponseWriter, r *twocloud.RequestBundle) {
+ username := r.Request.URL.Query().Get(":username")
+ user := r.AuthUser
+ if strings.ToLower(username) != strings.ToLower(r.AuthUser.Username) {
+ if !r.AuthUser.IsAdmin {
+ Respond(w, r, http.StatusUnauthorized, "You don't have access to that user's links.", []interface{}{})
+ return
+ }
+ id, err := r.GetUserID(username)
+ if err != nil {
+ r.Log.Error(err.Error())
+ Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
+ return
+ }
+ user, err = r.GetUser(id)
+ if err != nil {
+ r.Log.Error(err.Error())
+ Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
+ return
+ }
+ }
+ deviceID, err := ruid.RUIDFromString(r.Request.URL.Query().Get(":device"))
+ if err != nil {
+ r.Log.Error(err.Error())
+ Respond(w, r, http.StatusBadRequest, "Invalid device ID", []interface{}{})
+ return
+ }
+ device, err := r.GetDevice(deviceID)
+ if err != nil {
+ r.Log.Error(err.Error())
+ Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
+ return
+ }
+ if device.UserID != user.ID {
+ Respond(w, r, http.StatusBadRequest, "That device ID does not belong to that user.", []interface{}{})
+ return
+ }
+ var req LinksReq
+ body, err := ioutil.ReadAll(r.Request.Body)
+ if err != nil {
+ r.Log.Error(err.Error())
+ Respond(w, r, http.StatusInternalServerError, "Internal server error.", []interface{}{})
+ return
+ }
+ err = json.Unmarshal(body, &req)
+ if err != nil {
+ r.Log.Error(err.Error())
+ Respond(w, r, http.StatusBadRequest, "Error decoding request.", []interface{}{})
+ return
+ }
+ links := []twocloud.Link{}
+ for _, link := range req.Links {
+ if link.URL == nil || link.URL.Address == "" {
+ Respond(w, r, http.StatusBadRequest, "The address field must be specified.", []interface{}{})
+ return
+ }
+ link.Sender = r.Device
+ link.Receiver = device
+ links = append(links, link)
+ }
+ links, err = r.AddLinks(links)
+ if err != nil {
+ r.Log.Error(err.Error())
+ Respond(w, r, http.StatusInternalServerError, "Internal server error.", []interface{}{})
+ }
+ Respond(w, r, http.StatusCreated, "Successfully created links", []interface{}{links})
+ return
}
func getLink(w http.ResponseWriter, r *twocloud.RequestBundle) {

0 comments on commit 6274ee2

Please sign in to comment.
Something went wrong with that request. Please try again.