Semaphore/api/events.go

51 lines
1.1 KiB
Go
Raw Normal View History

2016-05-24 11:55:48 +02:00
package api
import (
"github.com/semaphoreui/semaphore/api/helpers"
"github.com/semaphoreui/semaphore/db"
"net/http"
2017-02-23 00:21:49 +01:00
"github.com/gorilla/context"
)
2023-09-17 16:15:44 +02:00
// nolint: gocyclo
2020-12-16 21:57:54 +01:00
func getEvents(w http.ResponseWriter, r *http.Request, limit int) {
user := context.Get(r, "user").(*db.User)
projectObj, exists := context.GetOk(r, "project")
2020-12-17 12:52:00 +01:00
var err error
var events []db.Event
2020-12-17 12:52:00 +01:00
if exists {
project := projectObj.(db.Project)
2023-09-17 16:15:44 +02:00
if !user.Admin { // check permissions to view events
_, err = helpers.Store(r).GetProjectUser(project.ID, user.ID)
}
2020-12-17 12:52:00 +01:00
if err != nil {
helpers.WriteError(w, err)
return
}
events, err = helpers.Store(r).GetEvents(project.ID, db.RetrieveQueryParams{Count: limit})
} else {
events, err = helpers.Store(r).GetUserEvents(user.ID, db.RetrieveQueryParams{Count: limit})
}
2020-12-16 21:57:54 +01:00
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, events)
}
func getLastEvents(w http.ResponseWriter, r *http.Request) {
getEvents(w, r, 200)
}
func getAllEvents(w http.ResponseWriter, r *http.Request) {
getEvents(w, r, 0)
}