/
chatroomoperator.go
100 lines (87 loc) · 2.29 KB
/
chatroomoperator.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
package actor
import (
"bytes"
"context"
"encoding/gob"
"github.com/howood/kangaroochat/domain/entity"
"github.com/howood/kangaroochat/domain/repository"
"github.com/howood/kangaroochat/infrastructure/uuid"
)
// chatRoom struct
type ChatRoomOperator struct {
repository.ChatRoomRepository
}
// NewChatRoomOperator creates a new ChatRoomRepository
func NewChatRoomOperator(ctx context.Context) *ChatRoomOperator {
return &ChatRoomOperator{&chatRoom{ctx: ctx}}
}
// chatRoom struct
type chatRoom struct {
roomData entity.ChatRoom
ctx context.Context
}
// Set sets roomname and password to roomdata
func (e *chatRoom) Set(roomname, password string) error {
hashedpassword, salt, err := PasswordOperator{}.GetHashedPassword(password)
if err != nil {
return err
}
e.roomData.Identifier = uuid.GetUUID(uuid.SegmentioKsuid)
e.roomData.RoomName = roomname
e.roomData.HashedPassword = hashedpassword
e.roomData.Salt = salt
return nil
}
// GetRoomName returns roomname of roomdata
func (e *chatRoom) GetRoomName() string {
return e.roomData.RoomName
}
// GetIdentifier returns Identifier of roomdata
func (e *chatRoom) GetIdentifier() string {
return e.roomData.Identifier
}
// ComparePassword compares input password to roomdata password
func (e *chatRoom) ComparePassword(password string) error {
return PasswordOperator{}.ComparePassword(e.roomData.HashedPassword, password, e.roomData.Salt)
}
// GobEncode serialized roomdata to bytes
func (e *chatRoom) GobEncode() ([]byte, error) {
w := new(bytes.Buffer)
encoder := gob.NewEncoder(w)
var err error
if err == nil {
err = encoder.Encode(e.roomData.Identifier)
}
if err == nil {
err = encoder.Encode(e.roomData.RoomName)
}
if err == nil {
err = encoder.Encode(e.roomData.HashedPassword)
}
if err == nil {
err = encoder.Encode(e.roomData.Salt)
}
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
// GobDecode decode bytes to roomdata
func (e *chatRoom) GobDecode(buf []byte) error {
r := bytes.NewBuffer(buf)
decoder := gob.NewDecoder(r)
var err error
if err == nil {
err = decoder.Decode(&e.roomData.Identifier)
}
if err == nil {
err = decoder.Decode(&e.roomData.RoomName)
}
if err == nil {
err = decoder.Decode(&e.roomData.HashedPassword)
}
if err == nil {
err = decoder.Decode(&e.roomData.Salt)
}
return err
}