Monorepo for Tangled tangled.org

appview/oauth: create tangled profile on first login #1010

merged opened by anirudh.fi targeting master from icy/tolqpt

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

Labels

None yet.

assignee

None yet.

Participants 2
AT URI
at://did:plc:hwevmowznbiukdf6uk5dwrrq/sh.tangled.repo.pull/3mcyvm2bhkj22
+24 -15
Interdiff #0 #1
+24 -15
appview/oauth/handler.go
··· 17 "github.com/posthog/posthog-go" 18 "tangled.org/core/api/tangled" 19 "tangled.org/core/appview/db" 20 "tangled.org/core/consts" 21 "tangled.org/core/orm" 22 "tangled.org/core/tid" ··· 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") 233 } 234 235 // create a session using apppasswords
··· 17 "github.com/posthog/posthog-go" 18 "tangled.org/core/api/tangled" 19 "tangled.org/core/appview/db" 20 + "tangled.org/core/appview/models" 21 "tangled.org/core/consts" 22 "tangled.org/core/orm" 23 "tangled.org/core/tid" ··· 199 did := sessData.AccountDID.String() 200 l := o.Logger.With("did", did) 201 202 + _, err := db.GetProfile(o.Db, did) 203 + if err == nil { 204 + l.Debug("profile already exists in DB") 205 return 206 } 207 208 l.Debug("creating empty Tangled profile") 209 210 + sess, err := o.ClientApp.ResumeSession(ctx, sessData.AccountDID, sessData.SessionID) 211 + if err != nil { 212 + l.Error("failed to resume session for profile creation", "err", err) 213 + return 214 } 215 + client := sess.APIClient() 216 217 _, err = comatproto.RepoPutRecord(ctx, client, &comatproto.RepoPutRecord_Input{ 218 Collection: tangled.ActorProfileNSID, 219 Repo: did, 220 Rkey: "self", 221 + Record: &lexutil.LexiconTypeDecoder{Val: &tangled.ActorProfile{}}, 222 }) 223 224 if err != nil { 225 + l.Error("failed to create empty profile on PDS", "err", err) 226 + return 227 + } 228 + 229 + tx, err := o.Db.BeginTx(ctx, nil) 230 + if err != nil { 231 + l.Error("failed to start transaction", "err", err) 232 + return 233 + } 234 + 235 + emptyProfile := &models.Profile{Did: did} 236 + if err := db.UpsertProfile(tx, emptyProfile); err != nil { 237 + l.Error("failed to create empty profile in DB", "err", err) 238 return 239 } 240 241 + l.Debug("successfully created empty Tangled profile on PDS and DB") 242 } 243 244 // create a session using apppasswords

History

3 rounds 3 comments
sign up or login to add to the discussion
1 commit
expand
appview/oauth: create tangled profile on first login
3/3 success
expand
expand 1 comment

this change lgtm! will test out the whole thing.

pull request successfully merged
1 commit
expand
appview/oauth: create tangled profile on first login
3/3 success
expand
expand 1 comment

Makes sense, done.

1 commit
expand
appview/oauth: create tangled profile on first login
expand 1 comment

do we need this? would have thought just new(tangled.ActorProfile) or &tangled.ActorProfile{} would suffice.

i would also suggest that we do this:

  • change the existence check to look in the DB instead of the user's PDS
  • if not present, putRecord in the PDS as well as insert an empty record in the DB

this way the existence check, upon login, is much faster.