woodpecker/server/handler/user.go

182 lines
4.2 KiB
Go
Raw Normal View History

2014-06-04 21:25:38 +00:00
package handler
import (
"encoding/json"
"net/http"
"github.com/drone/drone/plugin/remote"
2014-09-29 01:36:24 +00:00
"github.com/drone/drone/server/datastore"
"github.com/drone/drone/server/sync"
"github.com/drone/drone/shared/model"
2014-09-29 01:36:24 +00:00
"github.com/goji/context"
"github.com/zenazn/goji/web"
2014-06-04 21:25:38 +00:00
)
2014-09-29 01:36:24 +00:00
// GetUserCurrent accepts a request to retrieve the
// currently authenticated user from the datastore
// and return in JSON format.
//
// GET /api/user
//
func GetUserCurrent(c web.C, w http.ResponseWriter, r *http.Request) {
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
// return private data for the currently authenticated
// user, specifically, their auth token.
2014-06-04 21:25:38 +00:00
data := struct {
*model.User
2014-06-04 21:25:38 +00:00
Token string `json:"token"`
2014-09-29 01:36:24 +00:00
}{user, user.Token}
json.NewEncoder(w).Encode(&data)
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
// PutUser accepts a request to update the currently
// authenticated User profile.
//
// PUT /api/user
//
func PutUser(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
2014-06-04 21:25:38 +00:00
}
// unmarshal the repository from the payload
defer r.Body.Close()
in := model.User{}
2014-06-04 21:25:38 +00:00
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
2014-09-29 01:36:24 +00:00
w.WriteHeader(http.StatusBadRequest)
return
2014-06-04 21:25:38 +00:00
}
// update the user email
if len(in.Email) != 0 {
2014-09-29 01:36:24 +00:00
user.SetEmail(in.Email)
2014-06-04 21:25:38 +00:00
}
// update the user full name
if len(in.Name) != 0 {
2014-09-29 01:36:24 +00:00
user.Name = in.Name
2014-06-04 21:25:38 +00:00
}
// update the database
2014-09-29 01:36:24 +00:00
if err := datastore.PutUser(ctx, user); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
json.NewEncoder(w).Encode(user)
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
// GetRepos accepts a request to get the currently
// authenticated user's repository list from the datastore,
// encoded and returned in JSON format.
//
// GET /api/user/repos
//
func GetUserRepos(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
repos, err := datastore.GetRepoList(ctx, user)
2014-06-04 21:25:38 +00:00
if err != nil {
2014-09-29 01:36:24 +00:00
w.WriteHeader(http.StatusNotFound)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
json.NewEncoder(w).Encode(&repos)
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
// GetUserFeed accepts a request to get the user's latest
// build feed, across all repositories, from the datastore.
// The results are encoded and returned in JSON format.
//
// GET /api/user/feed
//
func GetUserFeed(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
repos, err := datastore.GetCommitListUser(ctx, user)
2014-06-04 21:25:38 +00:00
if err != nil {
2014-09-29 01:36:24 +00:00
w.WriteHeader(http.StatusNotFound)
return
2014-06-04 21:25:38 +00:00
}
2014-09-29 01:36:24 +00:00
json.NewEncoder(w).Encode(&repos)
2014-06-04 21:25:38 +00:00
}
2014-11-07 16:04:08 +00:00
// GetUserActivity accepts a request to get the user's latest
// build activity, across all repositories, from the datastore.
// The results are encoded and returned in JSON format.
//
// GET /api/user/activity
//
func GetUserActivity(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
repos, err := datastore.GetCommitListActivity(ctx, user)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(&repos)
}
// PostUserSync accepts a request to post user sync
//
// POST /api/user/sync
//
func PostUserSync(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
var remote = remote.Lookup(user.Remote)
if remote == nil {
w.WriteHeader(http.StatusNotFound)
return
}
if user.Syncing {
w.WriteHeader(http.StatusConflict)
return
}
// Request a new token and update
user_token, err := remote.GetToken(user)
if user_token != nil {
user.Access = user_token.AccessToken
user.Secret = user_token.RefreshToken
2015-01-27 22:42:20 +00:00
user.TokenExpiry = user_token.Expiry
} else if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
user.Syncing = true
if err := datastore.PutUser(ctx, user); err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
go sync.SyncUser(ctx, user, remote)
w.WriteHeader(http.StatusNoContent)
return
}