-
Notifications
You must be signed in to change notification settings - Fork 4
/
profiles.go
109 lines (91 loc) · 2.9 KB
/
profiles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package middleware
import (
"context"
"time"
cache "github.com/Code-Hex/go-generics-cache"
"github.com/bcc-code/bcc-media-platform/backend/applications"
"github.com/bcc-code/bcc-media-platform/backend/common"
"github.com/bcc-code/bcc-media-platform/backend/remotecache"
"github.com/bcc-code/bcc-media-platform/backend/sqlc"
"github.com/bcc-code/bcc-media-platform/backend/user"
"github.com/bcc-code/bcc-media-platform/backend/utils"
"github.com/bcc-code/mediabank-bridge/log"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/samber/lo"
)
var profileCache = cache.New[string, []common.Profile]()
func getProfilesFromDatabase(ctx context.Context, queries *sqlc.ApplicationQueries, user *common.User) ([]common.Profile, error) {
profiles, err := queries.GetProfilesForUserIDs(ctx, []string{user.PersonID})
if err != nil {
return nil, err
}
if len(profiles) > 0 {
return profiles, nil
}
profile := common.Profile{
ID: uuid.New(),
Name: user.DisplayName,
UserID: user.PersonID,
}
err = queries.SaveProfile(ctx, profile)
if err != nil {
return nil, err
}
profiles = append(profiles, profile)
return profiles, nil
}
func getProfiles(ctx *gin.Context, queries *sqlc.ApplicationQueries, remoteCache *remotecache.Client, user *common.User) ([]common.Profile, error) {
key := "profiles:" + queries.GroupID.String() + ":" + user.PersonID
if p, ok := profileCache.Get(key); ok && len(p) > 0 {
return p, nil
}
lock := utils.Lock(key)
lock.Lock()
defer lock.Unlock()
if p, ok := profileCache.Get(user.PersonID); ok && len(p) > 0 {
return p, nil
}
profileFactory := func(o *remotecache.Options) ([]common.Profile, error) {
o.SetTTL(time.Minute * 5)
profiles, err := getProfilesFromDatabase(ctx, queries, user)
if err != nil {
return nil, err
}
return profiles, nil
}
profiles, err := remotecache.GetOrCreate(ctx, remoteCache, key, profileFactory)
if err != nil {
return nil, err
}
profileCache.Set(user.PersonID, profiles, cache.WithExpiration(time.Second*2))
return profiles, nil
}
// NewProfileMiddleware prefills context with a profileID
func NewProfileMiddleware(queries *sqlc.Queries, client *remotecache.Client) func(*gin.Context) {
return func(ctx *gin.Context) {
u := user.GetFromCtx(ctx)
if u.PersonID == "" {
return
}
app, err := applications.GetFromCtx(ctx)
if err != nil {
log.L.Error().Err(err).Msg("Failed to get application from context")
return
}
profiles, err := getProfiles(ctx, queries.ApplicationQueries(app.GroupID), client, u)
if err != nil {
log.L.Error().Err(err).Msg("Failed to retrieve profiles from loader")
return
}
profileID := ctx.GetHeader("x-profile")
profile, found := lo.Find(profiles, func(p common.Profile) bool {
return p.ID.String() == profileID || p.Name == profileID
})
if !found {
profile = profiles[0]
}
ctx.Set(user.CtxProfiles, profiles)
ctx.Set(user.CtxProfile, &profile)
}
}