diff --git a/grpcapi/events.go b/grpcapi/events.go index 8a5da0d..6f4c083 100644 --- a/grpcapi/events.go +++ b/grpcapi/events.go @@ -25,10 +25,14 @@ func (e Event) ToStorageType() storage.Event { Endtime: e.Endtime, Allday: e.Allday, MaxSubscribers: e.MaxSubscribers, - Subscribers: e.Subscribers, + Subscriptions: []storage.Subscription{}, Data: map[string]any{}, } + for _, v := range e.Subscriptions { + event.Subscriptions = append(event.Subscriptions, v.ToStorageType()) + } + for k, d := range e.Data.GetFields() { jsondata, err := protojson.Marshal(d) if err != nil { @@ -43,6 +47,15 @@ func (e Event) ToStorageType() storage.Event { return event } +func (s Subscription) ToStorageType() storage.Subscription { + subscription := storage.Subscription{ + Subscriber: s.Subscriber, + Tags: s.Tags, + } + + return subscription +} + func EventFromStorageType(event *storage.Event) (*Event, error) { d, err := sanitizeData(event.Data) @@ -56,6 +69,13 @@ func EventFromStorageType(event *storage.Event) (*Event, error) { return nil, err } + subscriptions := []*Subscription{} + + for _, v := range event.Subscriptions { + sub, _ := SubscriptionFromStorageType(v) + subscriptions = append(subscriptions, sub) + } + return &Event{ Id: event.ID, Namespace: event.Namespace, @@ -70,11 +90,33 @@ func EventFromStorageType(event *storage.Event) (*Event, error) { Endtime: event.Endtime, Allday: event.Allday, MaxSubscribers: event.MaxSubscribers, - Subscribers: event.Subscribers, + Subscriptions: subscriptions, Data: data, }, nil } +func SubscriptionFromStorageType(s storage.Subscription) (*Subscription, error) { + + d, err := sanitizeData(s.Data) + if err != nil { + return nil, err + } + + data, err := structpb.NewStruct(d) + + if err != nil { + fmt.Println(err) + return nil, err + } + subscription := Subscription{ + Subscriber: s.Subscriber, + Tags: s.Tags, + Data: data, + } + + return &subscription, nil +} + func sanitizeData(data map[string]any) (d map[string]any, err error) { j, err := json.Marshal(data) if err != nil { diff --git a/grpcapi/events.pb.go b/grpcapi/events.pb.go index 8b126f7..d294b08 100644 --- a/grpcapi/events.pb.go +++ b/grpcapi/events.pb.go @@ -40,7 +40,7 @@ type Event struct { Starttime string `protobuf:"bytes,11,opt,name=starttime,proto3" json:"starttime,omitempty"` Endtime string `protobuf:"bytes,12,opt,name=endtime,proto3" json:"endtime,omitempty"` MaxSubscribers int64 `protobuf:"varint,13,opt,name=max_subscribers,json=maxSubscribers,proto3" json:"max_subscribers,omitempty"` - Subscribers []string `protobuf:"bytes,14,rep,name=subscribers,proto3" json:"subscribers,omitempty"` + Subscriptions []*Subscription `protobuf:"bytes,14,rep,name=subscriptions,proto3" json:"subscriptions,omitempty"` Data *structpb.Struct `protobuf:"bytes,15,opt,name=data,proto3" json:"data,omitempty"` } @@ -167,9 +167,9 @@ func (x *Event) GetMaxSubscribers() int64 { return 0 } -func (x *Event) GetSubscribers() []string { +func (x *Event) GetSubscriptions() []*Subscription { if x != nil { - return x.Subscribers + return x.Subscriptions } return nil } @@ -181,6 +181,69 @@ func (x *Event) GetData() *structpb.Struct { return nil } +type Subscription struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Subscriber string `protobuf:"bytes,20,opt,name=subscriber,proto3" json:"subscriber,omitempty"` + Tags []string `protobuf:"bytes,21,rep,name=tags,proto3" json:"tags,omitempty"` + Data *structpb.Struct `protobuf:"bytes,22,opt,name=data,proto3" json:"data,omitempty"` +} + +func (x *Subscription) Reset() { + *x = Subscription{} + if protoimpl.UnsafeEnabled { + mi := &file_events_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Subscription) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Subscription) ProtoMessage() {} + +func (x *Subscription) ProtoReflect() protoreflect.Message { + mi := &file_events_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Subscription.ProtoReflect.Descriptor instead. +func (*Subscription) Descriptor() ([]byte, []int) { + return file_events_proto_rawDescGZIP(), []int{1} +} + +func (x *Subscription) GetSubscriber() string { + if x != nil { + return x.Subscriber + } + return "" +} + +func (x *Subscription) GetTags() []string { + if x != nil { + return x.Tags + } + return nil +} + +func (x *Subscription) GetData() *structpb.Struct { + if x != nil { + return x.Data + } + return nil +} + var File_events_proto protoreflect.FileDescriptor var file_events_proto_rawDesc = []byte{ @@ -188,7 +251,7 @@ var file_events_proto_rawDesc = []byte{ 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x74, 0x69, - 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xf4, 0x03, + 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x87, 0x04, 0x0a, 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6e, 0x61, 0x6d, 0x65, @@ -215,15 +278,23 @@ var file_events_proto_rawDesc = []byte{ 0x01, 0x28, 0x09, 0x52, 0x07, 0x65, 0x6e, 0x64, 0x74, 0x69, 0x6d, 0x65, 0x12, 0x27, 0x0a, 0x0f, 0x6d, 0x61, 0x78, 0x5f, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x72, 0x73, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0e, 0x6d, 0x61, 0x78, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, - 0x69, 0x62, 0x65, 0x72, 0x73, 0x12, 0x20, 0x0a, 0x0b, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, - 0x62, 0x65, 0x72, 0x73, 0x18, 0x0e, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0b, 0x73, 0x75, 0x62, 0x73, - 0x63, 0x72, 0x69, 0x62, 0x65, 0x72, 0x73, 0x12, 0x2b, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, - 0x0f, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, 0x63, 0x74, 0x52, 0x04, - 0x64, 0x61, 0x74, 0x61, 0x42, 0x2e, 0x5a, 0x2c, 0x67, 0x69, 0x74, 0x2e, 0x63, 0x6f, 0x6f, 0x70, - 0x67, 0x6f, 0x2e, 0x69, 0x6f, 0x2f, 0x63, 0x6f, 0x6f, 0x70, 0x67, 0x6f, 0x2d, 0x70, 0x6c, 0x61, - 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x2f, 0x61, 0x67, 0x65, 0x6e, 0x64, 0x61, 0x2f, 0x67, 0x72, 0x70, - 0x63, 0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x69, 0x62, 0x65, 0x72, 0x73, 0x12, 0x33, 0x0a, 0x0d, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x0e, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x53, + 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0d, 0x73, 0x75, 0x62, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x2b, 0x0a, 0x04, 0x64, 0x61, + 0x74, 0x61, 0x18, 0x0f, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, + 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, 0x63, + 0x74, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x6f, 0x0a, 0x0c, 0x53, 0x75, 0x62, 0x73, 0x63, + 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1e, 0x0a, 0x0a, 0x73, 0x75, 0x62, 0x73, 0x63, + 0x72, 0x69, 0x62, 0x65, 0x72, 0x18, 0x14, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x73, 0x75, 0x62, + 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x61, 0x67, 0x73, 0x18, + 0x15, 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x74, 0x61, 0x67, 0x73, 0x12, 0x2b, 0x0a, 0x04, 0x64, + 0x61, 0x74, 0x61, 0x18, 0x16, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, + 0x63, 0x74, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x42, 0x2e, 0x5a, 0x2c, 0x67, 0x69, 0x74, 0x2e, + 0x63, 0x6f, 0x6f, 0x70, 0x67, 0x6f, 0x2e, 0x69, 0x6f, 0x2f, 0x63, 0x6f, 0x6f, 0x70, 0x67, 0x6f, + 0x2d, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x2f, 0x61, 0x67, 0x65, 0x6e, 0x64, 0x61, + 0x2f, 0x67, 0x72, 0x70, 0x63, 0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -238,21 +309,24 @@ func file_events_proto_rawDescGZIP() []byte { return file_events_proto_rawDescData } -var file_events_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_events_proto_msgTypes = make([]protoimpl.MessageInfo, 2) var file_events_proto_goTypes = []interface{}{ (*Event)(nil), // 0: Event - (*timestamppb.Timestamp)(nil), // 1: google.protobuf.Timestamp - (*structpb.Struct)(nil), // 2: google.protobuf.Struct + (*Subscription)(nil), // 1: Subscription + (*timestamppb.Timestamp)(nil), // 2: google.protobuf.Timestamp + (*structpb.Struct)(nil), // 3: google.protobuf.Struct } var file_events_proto_depIdxs = []int32{ - 1, // 0: Event.startdate:type_name -> google.protobuf.Timestamp - 1, // 1: Event.enddate:type_name -> google.protobuf.Timestamp - 2, // 2: Event.data:type_name -> google.protobuf.Struct - 3, // [3:3] is the sub-list for method output_type - 3, // [3:3] is the sub-list for method input_type - 3, // [3:3] is the sub-list for extension type_name - 3, // [3:3] is the sub-list for extension extendee - 0, // [0:3] is the sub-list for field type_name + 2, // 0: Event.startdate:type_name -> google.protobuf.Timestamp + 2, // 1: Event.enddate:type_name -> google.protobuf.Timestamp + 1, // 2: Event.subscriptions:type_name -> Subscription + 3, // 3: Event.data:type_name -> google.protobuf.Struct + 3, // 4: Subscription.data:type_name -> google.protobuf.Struct + 5, // [5:5] is the sub-list for method output_type + 5, // [5:5] is the sub-list for method input_type + 5, // [5:5] is the sub-list for extension type_name + 5, // [5:5] is the sub-list for extension extendee + 0, // [0:5] is the sub-list for field type_name } func init() { file_events_proto_init() } @@ -273,6 +347,18 @@ func file_events_proto_init() { return nil } } + file_events_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Subscription); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ @@ -280,7 +366,7 @@ func file_events_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_events_proto_rawDesc, NumEnums: 0, - NumMessages: 1, + NumMessages: 2, NumExtensions: 0, NumServices: 0, }, diff --git a/grpcapi/events.proto b/grpcapi/events.proto index bba7f99..b81df43 100644 --- a/grpcapi/events.proto +++ b/grpcapi/events.proto @@ -19,6 +19,12 @@ message Event { string starttime = 11; string endtime = 12; int64 max_subscribers = 13; - repeated string subscribers = 14; + repeated Subscription subscriptions = 14; google.protobuf.Struct data = 15; +} + +message Subscription { + string subscriber = 20; + repeated string tags = 21; + google.protobuf.Struct data = 22; } \ No newline at end of file diff --git a/handlers/events.go b/handlers/events.go index fe8c854..61d9569 100644 --- a/handlers/events.go +++ b/handlers/events.go @@ -2,6 +2,7 @@ package handlers import ( "fmt" + "time" "git.coopgo.io/coopgo-platform/agenda/storage" "github.com/google/uuid" @@ -12,8 +13,8 @@ func (h AgendaHandler) CreateEvent(event storage.Event) (*storage.Event, error) event.ID = uuid.NewString() } - if event.Subscribers == nil { - event.Subscribers = []string{} + if event.Subscriptions == nil { + event.Subscriptions = []storage.Subscription{} } if err := h.storage.CreateEvent(event); err != nil { @@ -35,6 +36,13 @@ func (h AgendaHandler) GetEvents(namespaces []string) (events []storage.Event, e } func (h AgendaHandler) SubscribeEvent(eventid string, subscriber string) (err error) { - err = h.storage.AddSubscriber(eventid, subscriber) + subscription := storage.Subscription{ + Subscriber: subscriber, + Tags: []string{}, + Data: map[string]any{ + "created_at": time.Now(), + }, + } + err = h.storage.AddSubscription(eventid, subscription) return } diff --git a/storage/events.go b/storage/events.go index fbd2f4b..6f9ffee 100644 --- a/storage/events.go +++ b/storage/events.go @@ -3,26 +3,33 @@ package storage import "time" type Event struct { - ID string `json:"id" bson:"_id"` - Namespace string `json:"namespace"` - Owners []string `json:"owners"` - RestrictedTo []string `json:"restricted_to"` - Type string `json:"type"` - Name string `json:"name"` - Description string `json:"description"` - Startdate time.Time `json:"startdate"` - Enddate time.Time `json:"enddate"` - Starttime string `json:"starttime"` - Endtime string `json:"endtime"` - Allday bool `json:"allday"` - MaxSubscribers int64 `json:"max_subscribers"` - Subscribers []string `json:"subscribers" bson:"subscribers,omitempty` - Data map[string]any `json:"data"` + ID string `json:"id" bson:"_id"` + Namespace string `json:"namespace"` + Owners []string `json:"owners"` + RestrictedTo []string `json:"restricted_to"` + Type string `json:"type"` + Name string `json:"name"` + Description string `json:"description"` + Startdate time.Time `json:"startdate"` + Enddate time.Time `json:"enddate"` + Starttime string `json:"starttime"` + Endtime string `json:"endtime"` + Allday bool `json:"allday"` + MaxSubscribers int64 `json:"max_subscribers"` + //Subscribers []string `json:"subscribers" bson:"subscribers,omitempty` + Subscriptions []Subscription `json:"subscriptions" bson:"subscriptions,omitempty"` + Data map[string]any `json:"data"` +} + +type Subscription struct { + Subscriber string `json:"subscriber"` + Tags []string `json:"tags"` + Data map[string]any `json:"data"` } func (e Event) RemainingSubscriptions() int { if e.MaxSubscribers == 0 { return 999 } - return int(e.MaxSubscribers) - len(e.Subscribers) + return int(e.MaxSubscribers) - len(e.Subscriptions) } diff --git a/storage/mongodb.go b/storage/mongodb.go index 852ea39..49e4bc3 100644 --- a/storage/mongodb.go +++ b/storage/mongodb.go @@ -111,11 +111,11 @@ func (s MongoDBStorage) GetEvents(namespaces []string) (events []Event, err erro return } -func (s MongoDBStorage) AddSubscriber(eventid string, subscriber string) error { +func (s MongoDBStorage) AddSubscription(eventid string, subscription Subscription) error { collection := s.Client.Database(s.DbName).Collection(s.Collections["events"]) filter := bson.M{"_id": eventid} - push := bson.M{"$push": bson.M{"subscribers": subscriber}} + push := bson.M{"$push": bson.M{"subscriptions": subscription}} _, err := collection.UpdateOne(context.TODO(), filter, push) return err diff --git a/storage/storage.go b/storage/storage.go index 6d4209d..966aa1a 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -10,7 +10,7 @@ type Storage interface { CreateEvent(Event) error GetEvent(string) (*Event, error) GetEvents(namespaces []string) ([]Event, error) - AddSubscriber(eventid string, subscriber string) error + AddSubscription(eventid string, subscription Subscription) error } type StorageImpl struct { }