alias.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package handlers
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/mk6i/open-oscar-server/state"
  6. "github.com/mk6i/open-oscar-server/wire"
  7. )
  8. // FeedbagAliases collects the aliases the feedbag owner has assigned to their
  9. // buddies, keyed by normalized screen name. Buddies without an alias are absent.
  10. func FeedbagAliases(items []wire.FeedbagItem) map[string]string {
  11. aliases := make(map[string]string)
  12. for _, item := range items {
  13. if item.ClassID != wire.FeedbagClassIdBuddy || item.Name == "" {
  14. continue
  15. }
  16. alias, ok := item.String(wire.FeedbagAttributesAlias)
  17. if !ok || alias == "" {
  18. continue
  19. }
  20. aliases[state.NewIdentScreenName(item.Name).String()] = alias
  21. }
  22. return aliases
  23. }
  24. // LookupBuddyAliases returns the aliases the session owner has assigned to their
  25. // buddies, keyed by normalized screen name.
  26. //
  27. // Aliases are private to the viewer and live only in their feedbag, so they cannot
  28. // be derived from a locate reply the way display names are.
  29. func LookupBuddyAliases(ctx context.Context, feedbagService FeedbagService, instance *state.SessionInstance) (map[string]string, error) {
  30. frame := wire.SNACFrame{FoodGroup: wire.Feedbag, SubGroup: wire.FeedbagQuery}
  31. snac, err := feedbagService.Query(ctx, instance, frame)
  32. if err != nil {
  33. return nil, err
  34. }
  35. reply, ok := snac.Body.(wire.SNAC_0x13_0x06_FeedbagReply)
  36. if !ok {
  37. return nil, fmt.Errorf("unexpected feedbag reply type")
  38. }
  39. return FeedbagAliases(reply.Items), nil
  40. }