Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.3 KiB

package Friends
import (
"encoding/json"
"net/http"
"net/url"
"strconv"
"time"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
)
// FriendRequestList gets friend request list
func FriendRequestList(w http.ResponseWriter, r *http.Request) {
var (
userSession Database.Session
friends Database.FriendRequestList
query url.Values
acceptedAtString []string
acceptedAt time.Time
page int
ok bool
returnJSON []byte
err error
)
query = r.URL.Query()
page, err = strconv.Atoi(query.Get("page"))
if err != nil {
page = 0
}
acceptedAtString, ok = query["updated_at"]
if ok {
acceptedAt, err = time.Parse(time.RFC3339, acceptedAtString[0])
if err != nil {
http.Error(w, "Invalid Data", http.StatusBadGateway)
return
}
}
userSession, _ = Auth.CheckCookie(r)
friends, err = Database.GetFriendRequestsByUserID(userSession.UserID.String(), page, acceptedAt)
if err != nil {
http.Error(w, "Error", http.StatusInternalServerError)
return
}
returnJSON, err = json.MarshalIndent(friends, "", " ")
if err != nil {
http.Error(w, "Error", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(returnJSON)
}