+109
appview/db/profile.go
+109
appview/db/profile.go
···
348
return tx.Commit()
349
}
350
351
+
func GetProfiles(e Execer, filters ...filter) ([]Profile, error) {
352
+
var conditions []string
353
+
var args []any
354
+
for _, filter := range filters {
355
+
conditions = append(conditions, filter.Condition())
356
+
args = append(args, filter.Arg()...)
357
+
}
358
+
359
+
whereClause := ""
360
+
if conditions != nil {
361
+
whereClause = " where " + strings.Join(conditions, " and ")
362
+
}
363
+
364
+
profilesQuery := fmt.Sprintf(
365
+
`select
366
+
id,
367
+
did,
368
+
description,
369
+
include_bluesky,
370
+
location
371
+
from
372
+
profile
373
+
%s`,
374
+
whereClause,
375
+
)
376
+
rows, err := e.Query(profilesQuery, args...)
377
+
if err != nil {
378
+
return nil, err
379
+
}
380
+
381
+
profileMap := make(map[string]*Profile)
382
+
for rows.Next() {
383
+
var profile Profile
384
+
var includeBluesky int
385
+
386
+
err = rows.Scan(&profile.ID, &profile.Did, &profile.Description, &includeBluesky, &profile.Location)
387
+
if err != nil {
388
+
return nil, err
389
+
}
390
+
391
+
if includeBluesky != 0 {
392
+
profile.IncludeBluesky = true
393
+
}
394
+
395
+
profileMap[profile.Did] = &profile
396
+
}
397
+
if err = rows.Err(); err != nil {
398
+
return nil, err
399
+
}
400
+
401
+
// populate profile links
402
+
inClause := strings.TrimSuffix(strings.Repeat("?, ", len(profileMap)), ", ")
403
+
args = make([]any, len(profileMap))
404
+
i := 0
405
+
for did := range profileMap {
406
+
args[i] = did
407
+
i++
408
+
}
409
+
410
+
linksQuery := fmt.Sprintf("select link, did from profile_links where did in (%s)", inClause)
411
+
rows, err = e.Query(linksQuery, args...)
412
+
if err != nil {
413
+
return nil, err
414
+
}
415
+
idxs := make(map[string]int)
416
+
for did := range profileMap {
417
+
idxs[did] = 0
418
+
}
419
+
for rows.Next() {
420
+
var link, did string
421
+
if err = rows.Scan(&link, &did); err != nil {
422
+
return nil, err
423
+
}
424
+
425
+
idx := idxs[did]
426
+
log.Println("idx", "idx", idx, "link", link)
427
+
profileMap[did].Links[idx] = link
428
+
idxs[did] = idx + 1
429
+
}
430
+
431
+
pinsQuery := fmt.Sprintf("select at_uri, did from profile_pinned_repositories where did in (%s)", inClause)
432
+
rows, err = e.Query(pinsQuery, args...)
433
+
if err != nil {
434
+
return nil, err
435
+
}
436
+
idxs = make(map[string]int)
437
+
for did := range profileMap {
438
+
idxs[did] = 0
439
+
}
440
+
for rows.Next() {
441
+
var link syntax.ATURI
442
+
var did string
443
+
if err = rows.Scan(&link, &did); err != nil {
444
+
return nil, err
445
+
}
446
+
447
+
idx := idxs[did]
448
+
profileMap[did].PinnedRepos[idx] = link
449
+
idxs[did] = idx + 1
450
+
}
451
+
452
+
var profiles []Profile
453
+
for _, p := range profileMap {
454
+
profiles = append(profiles, *p)
455
+
}
456
+
457
+
return profiles, nil
458
+
}
459
+
460
func GetProfile(e Execer, did string) (*Profile, error) {
461
var profile Profile
462
profile.Did = did