Monorepo for Tangled tangled.org

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>

anirudh.fi cc28f84b 3b80c7e5

verified
+43
+43
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" ··· 82 84 } 83 85 84 86 o.Logger.Debug("session saved successfully") 87 + 85 88 go o.addToDefaultKnot(sessData.AccountDID.String()) 86 89 go o.addToDefaultSpindle(sessData.AccountDID.String()) 90 + go o.ensureTangledProfile(sessData) 87 91 88 92 if !o.Config.Core.Dev { 89 93 err = o.Posthog.Enqueue(posthog.Capture{ ··· 187 191 } 188 192 189 193 l.Debug("successfully addeds to default Knot") 194 + } 195 + 196 + func (o *OAuth) ensureTangledProfile(sessData *oauth.ClientSessionData) { 197 + ctx := context.Background() 198 + did := sessData.AccountDID.String() 199 + l := o.Logger.With("did", did) 200 + 201 + sess, err := o.ClientApp.ResumeSession(ctx, sessData.AccountDID, sessData.SessionID) 202 + if err != nil { 203 + l.Error("failed to resume session for profile creation", "err", err) 204 + return 205 + } 206 + client := sess.APIClient() 207 + 208 + getRecordResp, err := comatproto.RepoGetRecord(ctx, client, "", tangled.ActorProfileNSID, did, "self") 209 + if err == nil && getRecordResp != nil { 210 + l.Debug("profile already exists") 211 + return 212 + } 213 + 214 + l.Debug("creating empty Tangled profile") 215 + 216 + emptyProfile := &tangled.ActorProfile{ 217 + LexiconTypeID: tangled.ActorProfileNSID, 218 + } 219 + 220 + _, err = comatproto.RepoPutRecord(ctx, client, &comatproto.RepoPutRecord_Input{ 221 + Collection: tangled.ActorProfileNSID, 222 + Repo: did, 223 + Rkey: "self", 224 + Record: &lexutil.LexiconTypeDecoder{Val: emptyProfile}, 225 + }) 226 + 227 + if err != nil { 228 + l.Error("failed to create empty profile", "err", err) 229 + return 230 + } 231 + 232 + l.Debug("successfully created empty Tangled profile") 190 233 } 191 234 192 235 // create a session using apppasswords