Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2

appview/oauth: create tangled profile on first login

Creates an empty sh.tangled.actor.profile on first login. This should
prevent profile picture uploads from breaking due to profile record
existing beforehand.

This should also allow for us to estimate total users better globally.

Signed-off-by: Anirudh Oppiliappan <anirudh@tangled.org>

authored by anirudh.fi and committed by tangled.org b217e460 2e71ee92

+52
+52
appview/oauth/handler.go
··· 10 10 "slices" 11 11 "time" 12 12 13 + comatproto "github.com/bluesky-social/indigo/api/atproto" 13 14 "github.com/bluesky-social/indigo/atproto/auth/oauth" 15 + lexutil "github.com/bluesky-social/indigo/lex/util" 14 16 "github.com/go-chi/chi/v5" 15 17 "github.com/posthog/posthog-go" 16 18 "tangled.org/core/api/tangled" 17 19 "tangled.org/core/appview/db" 20 + "tangled.org/core/appview/models" 18 21 "tangled.org/core/consts" 19 22 "tangled.org/core/orm" 20 23 "tangled.org/core/tid" ··· 85 82 } 86 83 87 84 o.Logger.Debug("session saved successfully") 85 + 88 86 go o.addToDefaultKnot(sessData.AccountDID.String()) 89 87 go o.addToDefaultSpindle(sessData.AccountDID.String()) 88 + go o.ensureTangledProfile(sessData) 90 89 91 90 if !o.Config.Core.Dev { 92 91 err = o.Posthog.Enqueue(posthog.Capture{ ··· 192 187 } 193 188 194 189 l.Debug("successfully addeds to default Knot") 190 + } 191 + 192 + func (o *OAuth) ensureTangledProfile(sessData *oauth.ClientSessionData) { 193 + ctx := context.Background() 194 + did := sessData.AccountDID.String() 195 + l := o.Logger.With("did", did) 196 + 197 + _, err := db.GetProfile(o.Db, did) 198 + if err == nil { 199 + l.Debug("profile already exists in DB") 200 + return 201 + } 202 + 203 + l.Debug("creating empty Tangled profile") 204 + 205 + sess, err := o.ClientApp.ResumeSession(ctx, sessData.AccountDID, sessData.SessionID) 206 + if err != nil { 207 + l.Error("failed to resume session for profile creation", "err", err) 208 + return 209 + } 210 + client := sess.APIClient() 211 + 212 + _, err = comatproto.RepoPutRecord(ctx, client, &comatproto.RepoPutRecord_Input{ 213 + Collection: tangled.ActorProfileNSID, 214 + Repo: did, 215 + Rkey: "self", 216 + Record: &lexutil.LexiconTypeDecoder{Val: &tangled.ActorProfile{}}, 217 + }) 218 + 219 + if err != nil { 220 + l.Error("failed to create empty profile on PDS", "err", err) 221 + return 222 + } 223 + 224 + tx, err := o.Db.BeginTx(ctx, nil) 225 + if err != nil { 226 + l.Error("failed to start transaction", "err", err) 227 + return 228 + } 229 + 230 + emptyProfile := &models.Profile{Did: did} 231 + if err := db.UpsertProfile(tx, emptyProfile); err != nil { 232 + l.Error("failed to create empty profile in DB", "err", err) 233 + return 234 + } 235 + 236 + l.Debug("successfully created empty Tangled profile on PDS and DB") 195 237 } 196 238 197 239 // create a session using apppasswords