@@ -16,27 +16,73 @@ type SpaceSubscription struct {
1616 BaseSubscription
1717}
1818
19- func (ss SpaceSubscription ) Add (s * Subscriptions ) {
19+ func (ss SpaceSubscription ) Add (s * Subscriptions ) error {
20+ s .EnsureDefaults ()
21+
22+ // Update OldAlias to current Alias as we don't need the old alias when creating a new subscription
23+ ss .OldAlias = ss .Alias
24+
2025 if _ , valid := s .ByChannelID [ss .ChannelID ]; ! valid {
2126 s .ByChannelID [ss .ChannelID ] = make (StringSubscription )
2227 }
28+ if s .ByChannelID [ss .ChannelID ] == nil {
29+ return errors .New ("ByChannelID entry is nil" )
30+ }
2331 s.ByChannelID [ss.ChannelID ][ss.Alias ] = ss
2432 key := store .GetURLSpaceKeyCombinationKey (ss .BaseURL , ss .SpaceKey )
33+
2534 if _ , ok := s .ByURLSpaceKey [key ]; ! ok {
2635 s .ByURLSpaceKey [key ] = make (map [string ][]string )
2736 }
37+ if s .ByURLSpaceKey [key ] == nil {
38+ return errors .New ("ByURLSpaceKey entry is nil" )
39+ }
2840 s.ByURLSpaceKey [key ][ss.ChannelID ] = ss .Events
41+ return nil
2942}
3043
31- func (ss SpaceSubscription ) Remove (s * Subscriptions ) {
32- delete (s .ByChannelID [ss .ChannelID ], ss .Alias )
44+ func (ss SpaceSubscription ) Remove (s * Subscriptions ) error {
45+ if s .ByChannelID == nil {
46+ return errors .New ("ByChannelID map is nil" )
47+ }
48+ if channelMap , ok := s .ByChannelID [ss .ChannelID ]; ok {
49+ aliasToRemove := ss .OldAlias
50+ if aliasToRemove == "" {
51+ aliasToRemove = ss .Alias
52+ }
53+
54+ if _ , aliasOk := channelMap [aliasToRemove ]; aliasOk {
55+ delete (channelMap , aliasToRemove )
56+ } else {
57+ return errors .New ("alias not found in ByChannelID" )
58+ }
59+ } else {
60+ return errors .New ("channelID not found in ByChannelID" )
61+ }
3362 key := store .GetURLSpaceKeyCombinationKey (ss .BaseURL , ss .SpaceKey )
34- delete (s .ByURLSpaceKey [key ], ss .ChannelID )
63+ if s .ByURLSpaceKey == nil {
64+ return errors .New ("ByURLSpaceKey map is nil" )
65+ }
66+ if urlSpaceMap , ok := s .ByURLSpaceKey [key ]; ok {
67+ if _ , channelOk := urlSpaceMap [ss .ChannelID ]; channelOk {
68+ delete (urlSpaceMap , ss .ChannelID )
69+ } else {
70+ return errors .New ("channelID not found in ByURLSpaceKey entry" )
71+ }
72+ } else {
73+ return errors .New ("key not found in ByURLSpaceKey" )
74+ }
75+ return nil
3576}
3677
37- func (ss SpaceSubscription ) Edit (s * Subscriptions ) {
38- ss .Remove (s )
39- ss .Add (s )
78+ func (ss SpaceSubscription ) Edit (s * Subscriptions ) error {
79+ if err := ss .Remove (s ); err != nil {
80+ return err
81+ }
82+ if err := ss .Add (s ); err != nil {
83+ return err
84+ }
85+ return nil
4086}
4187
4288func (ss SpaceSubscription ) Name () string {
0 commit comments