This commit is contained in:
1 parent
899223270a
commit
da15c3a7b7
4 files changed
+258
-242
No files matched your search
@@ -45,15 +45,15 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
sharedauth "repos.gfranz.ch/Gabriel20xx/shared/backend/auth"
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/carriernodeclient"
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/mediaauth"
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/syncsocket"
|
||||
)
|
||||
|
||||
// Config wires a Core to the rest of one service's dependencies.
|
||||
type Config struct {
|
||||
DB *pgxpool.Pool
|
||||
Auth *mediaauth.Service
|
||||
Auth *sharedauth.Service
|
||||
Sync *syncsocket.Hub
|
||||
CarrierNode *carriernodeclient.Client
|
||||
// ServiceName is recorded on mutations that track which app produced
|
||||
@@ -65,7 +65,7 @@ type Config struct {
|
||||
// what they need to run.
|
||||
type Core struct {
|
||||
db *pgxpool.Pool
|
||||
auth *mediaauth.Service
|
||||
auth *sharedauth.Service
|
||||
sync *syncsocket.Hub
|
||||
carrierNode *carriernodeclient.Client
|
||||
serviceName string
|
||||
@@ -214,7 +214,7 @@ func (c *Core) mediaState(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "missing mediaKey")
|
||||
return
|
||||
}
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var likes, saves, views int64
|
||||
if err := c.db.QueryRow(r.Context(), `SELECT COUNT(*) FROM media_stats WHERE media_key = $1 AND stat_type = 'like'`, key).Scan(&likes); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "media state unavailable")
|
||||
@@ -240,7 +240,7 @@ func (c *Core) mediaSave(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) mediaStatMutation(w http.ResponseWriter, r *http.Request, statType string) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
if user == nil {
|
||||
writeError(w, http.StatusUnauthorized, "not authenticated")
|
||||
return
|
||||
@@ -283,7 +283,7 @@ func (c *Core) mediaStatMutation(w http.ResponseWriter, r *http.Request, statTyp
|
||||
}
|
||||
|
||||
func (c *Core) mediaView(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var input struct {
|
||||
MediaKey string `json:"mediaKey"`
|
||||
}
|
||||
@@ -307,7 +307,7 @@ func (c *Core) mediaView(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) mediaViewSession(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var input struct {
|
||||
MediaKey string `json:"mediaKey"`
|
||||
DurationMs int64 `json:"durationMs"`
|
||||
@@ -338,7 +338,7 @@ func (c *Core) mediaViewSession(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) mediaLiked(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
rows, err := c.db.Query(r.Context(), `SELECT media_key, created_at FROM media_stats WHERE user_id = $1 AND stat_type = 'like' ORDER BY created_at DESC`, user.ID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "liked media unavailable")
|
||||
@@ -379,7 +379,7 @@ func (c *Core) mediaTags(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) addMediaTag(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var input struct {
|
||||
Tag string `json:"tag"`
|
||||
}
|
||||
@@ -402,7 +402,7 @@ func (c *Core) addMediaTag(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) voteMediaTag(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var input struct {
|
||||
Vote int `json:"vote"`
|
||||
}
|
||||
@@ -468,7 +468,7 @@ func (c *Core) tagMedia(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) playlists(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
rows, err := c.db.Query(r.Context(), `SELECT id, name, created_at FROM playlists WHERE user_id=$1 ORDER BY created_at DESC`, user.ID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "playlists unavailable")
|
||||
@@ -488,7 +488,7 @@ func (c *Core) playlists(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) playlistSummary(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
if user == nil {
|
||||
writeJSON(w, http.StatusOK, success(map[string]any{"playlists": []any{}}, "No playlists"))
|
||||
return
|
||||
@@ -512,7 +512,7 @@ func (c *Core) playlistSummary(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) playlist(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil || user == nil {
|
||||
writeError(w, http.StatusNotFound, "playlist not found")
|
||||
@@ -528,7 +528,7 @@ func (c *Core) playlist(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) createPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var input struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
@@ -552,7 +552,7 @@ func (c *Core) createPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) deletePlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
||||
@@ -567,7 +567,7 @@ func (c *Core) deletePlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) playlistItems(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil || user == nil {
|
||||
writeError(w, http.StatusNotFound, "playlist not found")
|
||||
@@ -598,7 +598,7 @@ func (c *Core) playlistItems(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) addPlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
||||
@@ -631,7 +631,7 @@ func (c *Core) addPlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) deletePlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
||||
@@ -655,7 +655,7 @@ func (c *Core) deletePlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) randomPlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
if user == nil {
|
||||
writeError(w, http.StatusNotFound, "playlist not found")
|
||||
return
|
||||
@@ -674,7 +674,7 @@ func (c *Core) randomPlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) reorderPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
||||
@@ -699,7 +699,7 @@ func (c *Core) reorderPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) profile(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
if user == nil {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"user": nil})
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user