Add stream search, offset & limit, improved bans

This commit is contained in:
Marco Realacci 2022-11-22 15:17:08 +01:00
parent 72f11a2b15
commit 963e392cea
14 changed files with 298 additions and 65 deletions

View file

@ -11,6 +11,8 @@ func (rt *_router) Handler() http.Handler {
rt.router.PUT("/users/:user_id/username", rt.wrap(rt.UpdateUsername))
rt.router.GET("/users", rt.wrap(rt.GetSearchUsers))
rt.router.GET("/users/:user_id/followers", rt.wrap(rt.GetFollowersFollowing))
rt.router.PUT("/users/:user_id/followers/:follower_uid", rt.wrap(rt.PutFollow))
rt.router.DELETE("/users/:user_id/followers/:follower_uid", rt.wrap(rt.DeleteFollow))
@ -33,6 +35,8 @@ func (rt *_router) Handler() http.Handler {
rt.router.GET("/users/:user_id", rt.wrap(rt.GetUserProfile))
rt.router.GET("/stream", rt.wrap(rt.GetUserStream)) //todo: why not "/users/:user_id/stream"?
rt.router.GET("/", rt.getHelloWorld)
rt.router.GET("/context", rt.wrap(rt.getContextReply))

View file

@ -34,7 +34,7 @@ func (rt *_router) GetComments(w http.ResponseWriter, r *http.Request, ps httpro
}
// get the user's comments
success, comments, err := rt.db.GetComments(uid, photo_id)
success, comments, err := rt.db.GetComments(uid, photo_id, ctx.Auth.GetUserID())
if err != nil {
helpers.SendInternalError(err, "Database error: GetComments", w, rt.baseLogger)

View file

@ -28,10 +28,10 @@ func (rt *_router) GetFollowersFollowing(w http.ResponseWriter, r *http.Request,
// Check if client is asking for followers or following
if strings.HasSuffix(r.URL.Path, "/followers") {
// Get the followers from the database
status, users, err = rt.db.GetUserFollowers(uid)
status, users, err = rt.db.GetUserFollowers(uid, ctx.Auth.GetUserID())
} else {
// Get the following users from the database
status, users, err = rt.db.GetUserFollowing(uid)
status, users, err = rt.db.GetUserFollowing(uid, ctx.Auth.GetUserID())
}
// Send a 500 response if there was an error

48
service/api/get-stream.go Normal file
View file

@ -0,0 +1,48 @@
package api
import (
"encoding/json"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/notherealmarco/WASAPhoto/service/api/authorization"
"github.com/notherealmarco/WASAPhoto/service/api/helpers"
"github.com/notherealmarco/WASAPhoto/service/api/reqcontext"
)
func (rt *_router) GetUserStream(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) {
// We must know who is requesting the stream
if !authorization.SendErrorIfNotLoggedIn(ctx.Auth.Authorized, rt.db, w, rt.baseLogger) {
return
}
// Get user id, probably this should be changed as it's not really REST
uid := ctx.Auth.GetUserID()
// Get start index and limit, or their default values
start_index, limit, err := helpers.GetLimits(r.URL.Query())
if err != nil {
helpers.SendBadRequest(w, "Invalid start_index or limit value", rt.baseLogger)
return
}
// Get the stream
stream, err := rt.db.GetUserStream(uid, start_index, limit)
if err != nil {
helpers.SendInternalError(err, "Database error: GetUserProfile", w, rt.baseLogger)
return
}
// Return the stream in json format
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(stream)
if err != nil {
helpers.SendInternalError(err, "Error encoding json", w, rt.baseLogger)
return
}
}

View file

@ -0,0 +1,33 @@
package helpers
import (
"net/url"
"strconv"
)
const (
DEFAULT_LIMIT = 10 // todo: move to config
DEFAULT_OFFSET = 0
)
func GetLimits(query url.Values) (int, int, error) {
limit := DEFAULT_LIMIT
start_index := DEFAULT_OFFSET
var err error
if query.Get("limit") != "" {
limit, err = strconv.Atoi(query.Get("limit"))
}
if query.Get("start_index") != "" {
start_index, err = strconv.Atoi(query.Get("start_index"))
}
if err != nil {
return 0, 0, err
}
return start_index, limit, nil
}

View file

@ -29,7 +29,7 @@ func (rt *_router) GetLikes(w http.ResponseWriter, r *http.Request, ps httproute
}
// get the user's likes
success, likes, err := rt.db.GetPhotoLikes(uid, photo_id)
success, likes, err := rt.db.GetPhotoLikes(uid, photo_id, ctx.Auth.GetUserID())
if err != nil {
helpers.SendInternalError(err, "Database error: GetLikes", w, rt.baseLogger)

53
service/api/search.go Normal file
View file

@ -0,0 +1,53 @@
package api
import (
"encoding/json"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/notherealmarco/WASAPhoto/service/api/authorization"
"github.com/notherealmarco/WASAPhoto/service/api/helpers"
"github.com/notherealmarco/WASAPhoto/service/api/reqcontext"
)
func (rt *_router) GetSearchUsers(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) {
// We require user to be authenticated
if !authorization.SendErrorIfNotLoggedIn(ctx.Auth.Authorized, rt.db, w, rt.baseLogger) {
return
}
// Get search query
query := r.URL.Query().Get("query")
if query == "" {
helpers.SendBadRequest(w, "Missing query parameter", rt.baseLogger)
return
}
// Get start index and limit, or their default values
start_index, limit, err := helpers.GetLimits(r.URL.Query())
if err != nil {
helpers.SendBadRequest(w, "Invalid start_index or limit value", rt.baseLogger)
return
}
// Get search results
results, err := rt.db.SearchByName(query, ctx.Auth.GetUserID(), start_index, limit)
if err != nil {
helpers.SendInternalError(err, "Database error: SearchByName", w, rt.baseLogger)
return
}
// Return the results in json format
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(results)
if err != nil {
helpers.SendInternalError(err, "Error encoding json", w, rt.baseLogger)
return
}
}