Skip to content

Commit

Permalink
Merge pull request #640 from tdakkota/fix/file-id-decoding
Browse files Browse the repository at this point in the history
fix(fileid): use TDLibPeerID for dialog_id field
  • Loading branch information
ernado authored Dec 22, 2021
2 parents 083a7fe + 51c254d commit 4dd90ee
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
34 changes: 33 additions & 1 deletion fileid/from.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package fileid

import "github.com/gotd/td/tg"
import (
"github.com/gotd/td/constant"
"github.com/gotd/td/tg"
)

// FromDocument creates FileID from tg.Document.
func FromDocument(doc *tg.Document) FileID {
Expand Down Expand Up @@ -47,3 +50,32 @@ func FromPhoto(photo *tg.Photo, thumbType rune) FileID {
},
}
}

// ChatPhoto is interface for user profile photo and chat photo structures.
type ChatPhoto interface {
GetDCID() int
GetPhotoID() int64
}

var _ = []ChatPhoto{
(*tg.ChatPhoto)(nil),
(*tg.UserProfilePhoto)(nil),
}

// FromChatPhoto creates new FileID from ChatPhoto.
func FromChatPhoto(id constant.TDLibPeerID, accessHash int64, photo ChatPhoto, big bool) FileID {
typ := PhotoSizeSourceDialogPhotoSmall
if big {
typ = PhotoSizeSourceDialogPhotoBig
}
return FileID{
Type: ProfilePhoto,
DC: photo.GetDCID(),
ID: photo.GetPhotoID(),
PhotoSizeSource: PhotoSizeSource{
Type: typ,
DialogID: id,
DialogAccessHash: accessHash,
},
}
}
2 changes: 1 addition & 1 deletion fileid/into_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestFileID_AsInputFileLocation(t *testing.T) {
&tg.InputPeerPhotoFileLocation{
Big: true,
Peer: &tg.InputPeerChannel{
ChannelID: -1001228418968,
ChannelID: 1228418968,
AccessHash: -3299551084991488399,
},
PhotoID: 5291818339590582253,
Expand Down
20 changes: 9 additions & 11 deletions fileid/photo_size_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type PhotoSizeSource struct {
FileType Type
ThumbnailType rune

DialogID int64
DialogID constant.TDLibPeerID
DialogAccessHash int64

StickerSetID int64
Expand All @@ -36,20 +36,18 @@ func (p *PhotoSizeSource) stickerSet() tg.InputStickerSetClass {

func (p *PhotoSizeSource) dialogPeer() tg.InputPeerClass {
switch id := p.DialogID; {
case id > 0 && id <= constant.MaxTDLibUserID:
case id.IsUser():
return &tg.InputPeerUser{
UserID: id,
UserID: id.ToPlain(),
AccessHash: p.DialogAccessHash,
}
case id < 0 && -constant.MaxTDLibChatID <= id:
case id.IsChat():
return &tg.InputPeerChat{
ChatID: id,
ChatID: id.ToPlain(),
}
case id < 0 &&
constant.ZeroTDLibChannelID-constant.MaxTDLibChannelID <= id &&
id != constant.ZeroTDLibChannelID:
case id.IsChannel():
return &tg.InputPeerChannel{
ChannelID: id,
ChannelID: id.ToPlain(),
AccessHash: p.DialogAccessHash,
}
}
Expand Down Expand Up @@ -80,7 +78,7 @@ func (p *PhotoSizeSource) readDialog(b *bin.Buffer) error {
if err != nil {
return errors.Wrap(err, "read dialog_id")
}
p.DialogID = v
p.DialogID = constant.TDLibPeerID(v)
}
{
v, err := b.Long()
Expand Down Expand Up @@ -252,7 +250,7 @@ func (p *PhotoSizeSource) writeLocalIDVolumeID(b *bin.Buffer) {
}

func (p *PhotoSizeSource) writeDialog(b *bin.Buffer) {
b.PutLong(p.DialogID)
b.PutLong(int64(p.DialogID))
b.PutLong(p.DialogAccessHash)
}

Expand Down
2 changes: 1 addition & 1 deletion fileid/photo_size_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestPhotoSizeSourceEncodeDecode(t *testing.T) {
Type: PhotoSizeSourceDialogPhotoBigLegacy,
VolumeID: 13,
LocalID: 14,
DialogID: 1099511627774,
DialogID: -1001228418968,
DialogAccessHash: 15,
},
{
Expand Down

0 comments on commit 4dd90ee

Please sign in to comment.