github.com/status-im/status-go@v1.1.0/protocol/communities/community_event_message.go (about)

     1  package communities
     2  
     3  import (
     4  	"github.com/golang/protobuf/proto"
     5  
     6  	"github.com/status-im/status-go/protocol/protobuf"
     7  )
     8  
     9  type CommunityEventsMessage struct {
    10  	CommunityID                    []byte           `json:"communityId"`
    11  	EventsBaseCommunityDescription []byte           `json:"eventsBaseCommunityDescription"`
    12  	Events                         []CommunityEvent `json:"events,omitempty"`
    13  }
    14  
    15  func (m *CommunityEventsMessage) ToProtobuf() *protobuf.CommunityEventsMessage {
    16  	result := protobuf.CommunityEventsMessage{
    17  		CommunityId:                    m.CommunityID,
    18  		EventsBaseCommunityDescription: m.EventsBaseCommunityDescription,
    19  		SignedEvents:                   []*protobuf.SignedCommunityEvent{},
    20  	}
    21  
    22  	for _, event := range m.Events {
    23  		signedEvent := &protobuf.SignedCommunityEvent{
    24  			Signature: event.Signature,
    25  			Payload:   event.Payload,
    26  		}
    27  		result.SignedEvents = append(result.SignedEvents, signedEvent)
    28  	}
    29  
    30  	return &result
    31  }
    32  
    33  func CommunityEventsMessageFromProtobuf(msg *protobuf.CommunityEventsMessage) (*CommunityEventsMessage, error) {
    34  	result := &CommunityEventsMessage{
    35  		CommunityID:                    msg.CommunityId,
    36  		EventsBaseCommunityDescription: msg.EventsBaseCommunityDescription,
    37  		Events:                         []CommunityEvent{},
    38  	}
    39  
    40  	for _, signedEvent := range msg.SignedEvents {
    41  		event, err := communityEventFromProtobuf(signedEvent)
    42  		if err != nil {
    43  			return nil, err
    44  		}
    45  		result.Events = append(result.Events, *event)
    46  	}
    47  
    48  	return result, nil
    49  }
    50  
    51  func (m *CommunityEventsMessage) Marshal() ([]byte, error) {
    52  	pb := m.ToProtobuf()
    53  	return proto.Marshal(pb)
    54  }