2016-05-24 11:55:48 +02:00
|
|
|
package api
|
2016-04-16 21:42:57 +02:00
|
|
|
|
|
|
|
import (
|
2024-10-26 14:56:17 +02:00
|
|
|
"github.com/semaphoreui/semaphore/api/helpers"
|
|
|
|
"github.com/semaphoreui/semaphore/db"
|
2020-12-01 20:06:49 +01:00
|
|
|
"net/http"
|
2019-07-09 19:45:27 +02:00
|
|
|
|
2017-02-23 00:21:49 +01:00
|
|
|
"github.com/gorilla/context"
|
2016-04-16 21:42:57 +02:00
|
|
|
)
|
|
|
|
|
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) {
|
2020-12-04 23:41:26 +01:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
2017-02-22 23:21:52 +01:00
|
|
|
projectObj, exists := context.GetOk(r, "project")
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2020-12-17 12:52:00 +01:00
|
|
|
var err error
|
|
|
|
var events []db.Event
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2020-12-17 12:52:00 +01:00
|
|
|
if exists {
|
|
|
|
project := projectObj.(db.Project)
|
2016-04-16 21:42:57 +02:00
|
|
|
|
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)
|
|
|
|
}
|
2016-04-16 21:42:57 +02:00
|
|
|
|
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})
|
|
|
|
}
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2020-12-16 21:57:54 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2016-04-16 21:42:57 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, events)
|
2016-04-16 21:42:57 +02:00
|
|
|
}
|
2017-07-25 06:21:36 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
func getLastEvents(w http.ResponseWriter, r *http.Request) {
|
|
|
|
getEvents(w, r, 200)
|
2017-07-25 06:21:36 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
func getAllEvents(w http.ResponseWriter, r *http.Request) {
|
|
|
|
getEvents(w, r, 0)
|
2017-07-25 06:21:36 +02:00
|
|
|
}
|