Skip to content
Snippets Groups Projects
Commit 059b2eae authored by Benjamin Wenger's avatar Benjamin Wenger
Browse files

Merge branch 'ben/group' into 'release'

made bindigns group all pointerie

See merge request !50
parents 7e25bac1 2ef10120
No related branches found
No related tags found
2 merge requests!53Release,!50made bindigns group all pointerie
......@@ -23,66 +23,66 @@ type GroupChat struct {
// GroupRequestFunc contains a function callback that is called when a group
// request is received.
type GroupRequestFunc interface {
GroupRequestCallback(g Group)
GroupRequestCallback(g *Group)
}
// GroupReceiveFunc contains a function callback that is called when a group
// message is received.
type GroupReceiveFunc interface {
GroupReceiveCallback(msg GroupMessageReceive)
GroupReceiveCallback(msg *GroupMessageReceive)
}
// NewGroupManager creates a new group chat manager.
func NewGroupManager(client *Client, requestFunc GroupRequestFunc,
receiveFunc GroupReceiveFunc) (GroupChat, error) {
receiveFunc GroupReceiveFunc) (*GroupChat, error) {
requestCallback := func(g gs.Group) {
requestFunc.GroupRequestCallback(Group{g})
requestFunc.GroupRequestCallback(&Group{g})
}
receiveCallback := func(msg gc.MessageReceive) {
receiveFunc.GroupReceiveCallback(GroupMessageReceive{msg})
receiveFunc.GroupReceiveCallback(&GroupMessageReceive{msg})
}
// Create a new group chat manager
m, err := gc.NewManager(&client.api, requestCallback, receiveCallback)
if err != nil {
return GroupChat{}, err
return nil, err
}
// Start group request and message retrieval workers
err = client.api.AddService(m.StartProcesses)
if err != nil {
return GroupChat{}, err
return nil, err
}
return GroupChat{m}, nil
return &GroupChat{m}, nil
}
// MakeGroup creates a new group and sends a group request to all members in the
// group. The ID of the new group, the rounds the requests were sent on, and the
// status of the send are contained in NewGroupReport.
func (g GroupChat) MakeGroup(membership IdList, name, message []byte) (NewGroupReport, error) {
func (g *GroupChat) MakeGroup(membership *IdList, name, message []byte) (*NewGroupReport, error) {
grp, rounds, status, err := g.m.MakeGroup(membership.list, name, message)
return NewGroupReport{Group{grp}, rounds, status}, err
return &NewGroupReport{&Group{grp}, rounds, status}, err
}
// ResendRequest resends a group request to all members in the group. The rounds
// they were sent on and the status of the send are contained in NewGroupReport.
func (g GroupChat) ResendRequest(groupIdBytes []byte) (NewGroupReport, error) {
func (g *GroupChat) ResendRequest(groupIdBytes []byte) (*NewGroupReport, error) {
groupID, err := id.Unmarshal(groupIdBytes)
if err != nil {
return NewGroupReport{},
return nil,
errors.Errorf("Failed to unmarshal group ID: %+v", err)
}
rounds, status, err := g.m.ResendRequest(groupID)
return NewGroupReport{Group{}, rounds, status}, nil
return &NewGroupReport{&Group{}, rounds, status}, nil
}
// JoinGroup allows a user to join a group when they receive a request. The
// caller must pass in the serialized bytes of a Group.
func (g GroupChat) JoinGroup(serializedGroupData []byte) error {
func (g *GroupChat) JoinGroup(serializedGroupData []byte) error {
grp, err := gs.DeserializeGroup(serializedGroupData)
if err != nil {
return err
......@@ -91,7 +91,7 @@ func (g GroupChat) JoinGroup(serializedGroupData []byte) error {
}
// LeaveGroup deletes a group so a user no longer has access.
func (g GroupChat) LeaveGroup(groupIdBytes []byte) error {
func (g *GroupChat) LeaveGroup(groupIdBytes []byte) error {
groupID, err := id.Unmarshal(groupIdBytes)
if err != nil {
return errors.Errorf("Failed to unmarshal group ID: %+v", err)
......@@ -102,7 +102,7 @@ func (g GroupChat) LeaveGroup(groupIdBytes []byte) error {
// Send sends the message to the specified group. Returns the round the messages
// were sent on.
func (g GroupChat) Send(groupIdBytes, message []byte) (int64, error) {
func (g *GroupChat) Send(groupIdBytes, message []byte) (int64, error) {
groupID, err := id.Unmarshal(groupIdBytes)
if err != nil {
return 0, errors.Errorf("Failed to unmarshal group ID: %+v", err)
......@@ -114,28 +114,28 @@ func (g GroupChat) Send(groupIdBytes, message []byte) (int64, error) {
// GetGroups returns an IdList containing a list of group IDs that the user is a
// part of.
func (g GroupChat) GetGroups() IdList {
return IdList{g.m.GetGroups()}
func (g *GroupChat) GetGroups() *IdList {
return &IdList{g.m.GetGroups()}
}
// GetGroup returns the group with the group ID. If no group exists, then the
// error "failed to find group" is returned.
func (g GroupChat) GetGroup(groupIdBytes []byte) (Group, error) {
func (g *GroupChat) GetGroup(groupIdBytes []byte) (*Group, error) {
groupID, err := id.Unmarshal(groupIdBytes)
if err != nil {
return Group{}, errors.Errorf("Failed to unmarshal group ID: %+v", err)
return nil, errors.Errorf("Failed to unmarshal group ID: %+v", err)
}
grp, exists := g.m.GetGroup(groupID)
if !exists {
return Group{}, errors.New("failed to find group")
return nil, errors.New("failed to find group")
}
return Group{grp}, nil
return &Group{grp}, nil
}
// NumGroups returns the number of groups the user is a part of.
func (g GroupChat) NumGroups() int {
func (g *GroupChat) NumGroups() int {
return g.m.NumGroups()
}
......@@ -143,20 +143,20 @@ func (g GroupChat) NumGroups() int {
// the group, a list of rounds that the group requests were sent on, and the
// status of the send.
type NewGroupReport struct {
group Group
group *Group
rounds []id.Round
status gc.RequestStatus
}
// GetGroup returns the Group.
func (ngr NewGroupReport) GetGroup() Group {
func (ngr *NewGroupReport) GetGroup() *Group {
return ngr.group
}
// GetRoundList returns the RoundList containing a list of rounds requests were
// sent on.
func (ngr NewGroupReport) GetRoundList() RoundList {
return RoundList{ngr.rounds}
func (ngr *NewGroupReport) GetRoundList() *RoundList {
return &RoundList{ngr.rounds}
}
// GetStatus returns the status of the requests sent when creating a new group.
......@@ -164,7 +164,7 @@ func (ngr NewGroupReport) GetRoundList() RoundList {
// 1 all requests failed to send
// 2 some request failed and some succeeded
// 3, all requests sent successfully
func (ngr NewGroupReport) GetStatus() int {
func (ngr *NewGroupReport) GetStatus() int {
return int(ngr.status)
}
......@@ -179,24 +179,24 @@ type Group struct {
}
// GetName returns the name set by the user for the group.
func (g Group) GetName() []byte {
func (g *Group) GetName() []byte {
return g.g.Name
}
// GetID return the 33-byte unique group ID.
func (g Group) GetID() []byte {
func (g *Group) GetID() []byte {
return g.g.ID.Bytes()
}
// GetMembership returns a list of contacts, one for each member in the group.
// The list is in order; the first contact is the leader/creator of the group.
// All subsequent members are ordered by their ID.
func (g Group) GetMembership() GroupMembership {
return GroupMembership{g.g.Members}
func (g *Group) GetMembership() *GroupMembership {
return &GroupMembership{g.g.Members}
}
// Serialize serializes the Group.
func (g Group) Serialize() []byte {
func (g *Group) Serialize() []byte {
return g.g.Serialize()
}
......@@ -211,18 +211,18 @@ type GroupMembership struct {
}
// Len returns the number of members in the group membership.
func (gm GroupMembership) Len() int {
return gm.Len()
func (gm *GroupMembership) Len() int {
return len(gm.m)
}
// Get returns the member at the index. The member at index 0 is always the
// group leader. An error is returned if the index is out of range.
func (gm GroupMembership) Get(i int) (GroupMember, error) {
func (gm *GroupMembership) Get(i int) (*GroupMember, error) {
if i < 0 || i > gm.Len() {
return GroupMember{}, errors.Errorf("ID list index must be between %d "+
return nil, errors.Errorf("ID list index must be between %d "+
"and the last element %d.", 0, gm.Len())
}
return GroupMember{gm.m[i]}, nil
return &GroupMember{gm.m[i]}, nil
}
////
......@@ -240,7 +240,7 @@ func (gm GroupMember) GetID() []byte {
// GetDhKey returns the byte representation of the public Diffie–Hellman key of
// the member.
func (gm GroupMember) GetDhKey() []byte {
func (gm *GroupMember) GetDhKey() []byte {
return gm.DhKey.Bytes()
}
......@@ -255,47 +255,47 @@ type GroupMessageReceive struct {
}
// GetGroupID returns the 33-byte group ID.
func (gmr GroupMessageReceive) GetGroupID() []byte {
func (gmr *GroupMessageReceive) GetGroupID() []byte {
return gmr.GroupID.Bytes()
}
// GetMessageID returns the message ID.
func (gmr GroupMessageReceive) GetMessageID() []byte {
func (gmr *GroupMessageReceive) GetMessageID() []byte {
return gmr.ID.Bytes()
}
// GetPayload returns the message payload.
func (gmr GroupMessageReceive) GetPayload() []byte {
func (gmr *GroupMessageReceive) GetPayload() []byte {
return gmr.Payload
}
// GetSenderID returns the 33-byte user ID of the sender.
func (gmr GroupMessageReceive) GetSenderID() []byte {
func (gmr *GroupMessageReceive) GetSenderID() []byte {
return gmr.SenderID.Bytes()
}
// GetRecipientID returns the 33-byte user ID of the recipient.
func (gmr GroupMessageReceive) GetRecipientID() []byte {
func (gmr *GroupMessageReceive) GetRecipientID() []byte {
return gmr.RecipientID.Bytes()
}
// GetEphemeralID returns the ephemeral ID of the recipient.
func (gmr GroupMessageReceive) GetEphemeralID() int64 {
func (gmr *GroupMessageReceive) GetEphemeralID() int64 {
return gmr.EphemeralID.Int64()
}
// GetTimestampNano returns the message timestamp in nanoseconds.
func (gmr GroupMessageReceive) GetTimestampNano() int64 {
func (gmr *GroupMessageReceive) GetTimestampNano() int64 {
return gmr.Timestamp.UnixNano()
}
// GetRoundID returns the ID of the round the message was sent on.
func (gmr GroupMessageReceive) GetRoundID() int64 {
func (gmr *GroupMessageReceive) GetRoundID() int64 {
return int64(gmr.RoundID)
}
// GetRoundTimestampNano returns the timestamp, in nanoseconds, of the round the
// message was sent on.
func (gmr GroupMessageReceive) GetRoundTimestampNano() int64 {
func (gmr *GroupMessageReceive) GetRoundTimestampNano() int64 {
return gmr.RoundTimestamp.UnixNano()
}
......@@ -123,17 +123,17 @@ type IdList struct {
}
// MakeIdList creates a new empty IdList.
func MakeIdList() IdList {
return IdList{[]*id.ID{}}
func MakeIdList() *IdList {
return &IdList{[]*id.ID{}}
}
// Len returns the number of IDs in the list.
func (idl IdList) Len() int {
func (idl *IdList) Len() int {
return len(idl.list)
}
// Add appends the ID bytes to the end of the list.
func (idl IdList) Add(idBytes []byte) error {
func (idl *IdList) Add(idBytes []byte) error {
newID, err := id.Unmarshal(idBytes)
if err != nil {
return err
......@@ -145,7 +145,7 @@ func (idl IdList) Add(idBytes []byte) error {
// Get returns the ID at the index. An error is returned if the index is out of
// range.
func (idl IdList) Get(i int) ([]byte, error) {
func (idl *IdList) Get(i int) ([]byte, error) {
if i < 0 || i > len(idl.list) {
return nil, errors.Errorf("ID list index must be between %d and the "+
"last element %d.", 0, len(idl.list))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment