This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
/
protocol_sync_update_user_img_ack.go
104 lines (83 loc) · 2.51 KB
/
protocol_sync_update_user_img_ack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2019 The go-pttai Authors
// This file is part of the go-pttai library.
//
// The go-pttai library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-pttai library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-pttai library. If not, see <http://www.gnu.org/licenses/>.
package account
import (
"encoding/json"
"reflect"
"github.com/ailabstw/go-pttai/common/types"
"github.com/ailabstw/go-pttai/log"
pkgservice "github.com/ailabstw/go-pttai/service"
)
type SyncUpdateUserImgAck struct {
Objs []*UserImg `json:"o"`
}
func (pm *ProtocolManager) HandleSyncUpdateUserImgAck(dataBytes []byte, peer *pkgservice.PttPeer) error {
data := &SyncUpdateUserImgAck{}
err := json.Unmarshal(dataBytes, data)
if err != nil {
return err
}
origObj := NewEmptyUserImg()
pm.SetUserImgDB(origObj)
for _, obj := range data.Objs {
pm.SetUserImgDB(obj)
pm.HandleSyncUpdateObjectAck(
obj,
peer,
origObj,
pm.userOplogMerkle,
pm.SetUserDB,
pm.updateSyncUserImg,
nil,
pm.broadcastUserOplogCore,
)
log.Debug("HandleSyncUpdateUserImgAck: after HandleSyncUpdateObjectAck", "obj.SyncInfo", obj.SyncInfo)
}
return nil
}
func (pm *ProtocolManager) updateSyncUserImg(theToSyncInfo pkgservice.SyncInfo, theFromObj pkgservice.Object, oplog *pkgservice.BaseOplog) error {
toSyncInfo, ok := theToSyncInfo.(*SyncUserImgInfo)
if !ok {
return pkgservice.ErrInvalidData
}
fromObj, ok := theFromObj.(*UserImg)
if !ok {
return pkgservice.ErrInvalidData
}
// op-data
opData := &UserOpUpdateUserImg{}
err := oplog.GetData(opData)
if err != nil {
return err
}
// logID
toLogID := toSyncInfo.GetLogID()
updateLogID := fromObj.GetUpdateLogID()
if !reflect.DeepEqual(toLogID, updateLogID) {
return pkgservice.ErrInvalidObject
}
// sync-info
str := fromObj.Str
hash := types.Hash([]byte(str))
if !reflect.DeepEqual(opData.Hash, hash) {
return pkgservice.ErrInvalidObject
}
toSyncInfo.ImgType = fromObj.ImgType
toSyncInfo.Width = fromObj.Width
toSyncInfo.Height = fromObj.Height
toSyncInfo.Str = str
return nil
}