github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/unfurl/settings.go (about) 1 package unfurl 2 3 import ( 4 "context" 5 6 "github.com/keybase/client/go/chat/globals" 7 "github.com/keybase/client/go/chat/types" 8 "github.com/keybase/client/go/chat/utils" 9 "github.com/keybase/client/go/protocol/chat1" 10 "github.com/keybase/client/go/protocol/gregor1" 11 ) 12 13 const settingsModeName = "__unfurl_settings_mode" 14 const settingsWhitelistName = "__unfurl_settings_whitelist" 15 16 type modeRecord struct { 17 Mode chat1.UnfurlMode 18 } 19 20 type whitelistRecord struct { 21 Whitelist map[string]bool 22 } 23 24 type Settings struct { 25 utils.DebugLabeler 26 27 storage types.UserConversationBackedStorage 28 } 29 30 func NewSettings(g *globals.Context, storage types.UserConversationBackedStorage) *Settings { 31 return &Settings{ 32 DebugLabeler: utils.NewDebugLabeler(g.ExternalG(), "Settings", false), 33 storage: storage, 34 } 35 } 36 37 func (s *Settings) Get(ctx context.Context, uid gregor1.UID) (res chat1.UnfurlSettings, err error) { 38 defer s.Trace(ctx, &err, "Get")() 39 40 var mr modeRecord 41 found, err := s.storage.Get(ctx, uid, settingsModeName, &mr) 42 if err != nil { 43 return res, err 44 } 45 if !found { 46 s.Debug(ctx, "Get: no mode setting found, using whitelisted") 47 mr.Mode = chat1.UnfurlMode_WHITELISTED 48 } 49 50 var wr whitelistRecord 51 whitelist := make(map[string]bool) 52 if found, err = s.storage.Get(ctx, uid, settingsWhitelistName, &wr); err != nil { 53 return res, err 54 } 55 if found { 56 whitelist = wr.Whitelist 57 } 58 return chat1.UnfurlSettings{ 59 Mode: mr.Mode, 60 Whitelist: whitelist, 61 }, nil 62 } 63 64 func (s *Settings) WhitelistAdd(ctx context.Context, uid gregor1.UID, domain string) (err error) { 65 defer s.Trace(ctx, &err, "WhitelistAdd(%s)", domain)() 66 var wr whitelistRecord 67 found, err := s.storage.Get(ctx, uid, settingsWhitelistName, &wr) 68 if err != nil { 69 return err 70 } 71 if !found { 72 wr.Whitelist = make(map[string]bool) 73 } 74 if wr.Whitelist[domain] { 75 return nil 76 } 77 wr.Whitelist[domain] = true 78 return s.storage.Put(ctx, uid, settingsWhitelistName, wr) 79 } 80 81 func (s *Settings) WhitelistRemove(ctx context.Context, uid gregor1.UID, domain string) (err error) { 82 defer s.Trace(ctx, &err, "WhitelistRemove(%s)", domain)() 83 var wr whitelistRecord 84 found, err := s.storage.Get(ctx, uid, settingsWhitelistName, &wr) 85 if err != nil { 86 return err 87 } 88 if !found { 89 return nil 90 } 91 if !wr.Whitelist[domain] { 92 s.Debug(ctx, "WhitelistRemove: not found, doing nothing") 93 return nil 94 } 95 delete(wr.Whitelist, domain) 96 return s.storage.Put(ctx, uid, settingsWhitelistName, wr) 97 } 98 99 func (s *Settings) SetMode(ctx context.Context, uid gregor1.UID, mode chat1.UnfurlMode) (err error) { 100 defer s.Trace(ctx, &err, "SetMode(%v)", mode)() 101 return s.storage.Put(ctx, uid, settingsModeName, modeRecord{ 102 Mode: mode, 103 }) 104 } 105 106 func (s *Settings) Set(ctx context.Context, uid gregor1.UID, settings chat1.UnfurlSettings) (err error) { 107 defer s.Trace(ctx, &err, "Set")() 108 if err = s.storage.Put(ctx, uid, settingsModeName, modeRecord{ 109 Mode: settings.Mode, 110 }); err != nil { 111 return err 112 } 113 if err = s.storage.Put(ctx, uid, settingsWhitelistName, whitelistRecord{ 114 Whitelist: settings.Whitelist, 115 }); err != nil { 116 return err 117 } 118 return nil 119 }