github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/devstorage.go (about) 1 package chat 2 3 import ( 4 "context" 5 "encoding/json" 6 7 "github.com/keybase/client/go/chat/globals" 8 "github.com/keybase/client/go/chat/types" 9 "github.com/keybase/client/go/chat/utils" 10 "github.com/keybase/client/go/protocol/chat1" 11 "github.com/keybase/client/go/protocol/gregor1" 12 "github.com/keybase/client/go/protocol/keybase1" 13 ) 14 15 type DevConversationBackedStorage struct { 16 globals.Contextified 17 utils.DebugLabeler 18 19 ri func() chat1.RemoteInterface 20 } 21 22 var _ types.UserConversationBackedStorage = &DevConversationBackedStorage{} 23 24 func NewDevConversationBackedStorage(g *globals.Context, ri func() chat1.RemoteInterface) *DevConversationBackedStorage { 25 return &DevConversationBackedStorage{ 26 Contextified: globals.NewContextified(g), 27 DebugLabeler: utils.NewDebugLabeler(g.ExternalG(), "DevConversationBackedStorage", false), 28 ri: ri, 29 } 30 } 31 32 func (s *DevConversationBackedStorage) Put(ctx context.Context, uid gregor1.UID, name string, src interface{}) (err error) { 33 defer s.Trace(ctx, &err, "Put(%s)", name)() 34 un, err := s.G().GetUPAKLoader().LookupUsername(ctx, keybase1.UID(uid.String())) 35 if err != nil { 36 return err 37 } 38 username := un.String() 39 dat, err := json.Marshal(src) 40 if err != nil { 41 return err 42 } 43 conv, _, err := NewConversation(ctx, s.G(), uid, username, &name, chat1.TopicType_DEV, 44 chat1.ConversationMembersType_IMPTEAMNATIVE, keybase1.TLFVisibility_PRIVATE, nil, s.ri, 45 NewConvFindExistingNormal) 46 if err != nil { 47 return err 48 } 49 if _, _, err = NewBlockingSender(s.G(), NewBoxer(s.G()), s.ri).Send(ctx, conv.GetConvID(), 50 chat1.MessagePlaintext{ 51 ClientHeader: chat1.MessageClientHeader{ 52 Conv: conv.Info.Triple, 53 TlfName: username, 54 MessageType: chat1.MessageType_TEXT, 55 }, 56 MessageBody: chat1.NewMessageBodyWithText(chat1.MessageText{ 57 Body: string(dat), 58 }), 59 }, 0, nil, nil, nil); err != nil { 60 return err 61 } 62 return nil 63 } 64 65 func (s *DevConversationBackedStorage) Get(ctx context.Context, uid gregor1.UID, name string, 66 dest interface{}) (found bool, err error) { 67 defer s.Trace(ctx, &err, "Get(%s)", name)() 68 un, err := s.G().GetUPAKLoader().LookupUsername(ctx, keybase1.UID(uid.String())) 69 if err != nil { 70 return false, err 71 } 72 username := un.String() 73 convs, err := FindConversations(ctx, s.G(), s.DebugLabeler, types.InboxSourceDataSourceAll, s.ri, uid, 74 username, chat1.TopicType_DEV, chat1.ConversationMembersType_IMPTEAMNATIVE, 75 keybase1.TLFVisibility_PRIVATE, name, nil) 76 if err != nil { 77 return false, err 78 } 79 if len(convs) == 0 { 80 return false, nil 81 } 82 conv := convs[0] 83 tv, err := s.G().ConvSource.Pull(ctx, conv.GetConvID(), uid, chat1.GetThreadReason_GENERAL, nil, 84 &chat1.GetThreadQuery{ 85 MessageTypes: []chat1.MessageType{chat1.MessageType_TEXT}, 86 }, &chat1.Pagination{Num: 1}) 87 if err != nil { 88 return false, err 89 } 90 if len(tv.Messages) == 0 { 91 return false, nil 92 } 93 msg := tv.Messages[0] 94 if !msg.IsValid() { 95 return false, nil 96 } 97 body := msg.Valid().MessageBody 98 if !body.IsType(chat1.MessageType_TEXT) { 99 return false, nil 100 } 101 if err = json.Unmarshal([]byte(body.Text().Body), dest); err != nil { 102 return false, err 103 } 104 return true, nil 105 }