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

     1  package protocol
     2  
     3  import (
     4  	"sync"
     5  
     6  	"go.uber.org/zap"
     7  
     8  	"github.com/status-im/status-go/protocol/encryption/multidevice"
     9  	"github.com/status-im/status-go/protocol/protobuf"
    10  )
    11  
    12  /*
    13  |--------------------------------------------------------------------------
    14  | chatMap
    15  |--------------------------------------------------------------------------
    16  |
    17  | A sync.Map wrapper for a specific mapping of map[string]*Chat
    18  |
    19  */
    20  
    21  type chatMap struct {
    22  	sm sync.Map
    23  }
    24  
    25  func (cm *chatMap) Load(chatID string) (*Chat, bool) {
    26  	chat, ok := cm.sm.Load(chatID)
    27  	if chat == nil {
    28  		return nil, ok
    29  	}
    30  	return chat.(*Chat), ok
    31  }
    32  
    33  func (cm *chatMap) Store(chatID string, chat *Chat) {
    34  	cm.sm.Store(chatID, chat)
    35  }
    36  
    37  func (cm *chatMap) Range(f func(chatID string, chat *Chat) (shouldContinue bool)) {
    38  	nf := func(key, value interface{}) (shouldContinue bool) {
    39  		return f(key.(string), value.(*Chat))
    40  	}
    41  	cm.sm.Range(nf)
    42  }
    43  
    44  func (cm *chatMap) Delete(chatID string) {
    45  	cm.sm.Delete(chatID)
    46  }
    47  
    48  /*
    49  |--------------------------------------------------------------------------
    50  | contactMap
    51  |--------------------------------------------------------------------------
    52  |
    53  | A sync.Map wrapper for a specific mapping of map[string]*Contact
    54  |
    55  */
    56  
    57  type contactMap struct {
    58  	sm     sync.Map
    59  	me     *Contact
    60  	logger *zap.Logger
    61  }
    62  
    63  func (cm *contactMap) Load(contactID string) (*Contact, bool) {
    64  	if contactID == cm.me.ID {
    65  		cm.logger.Warn("contacts map: loading own identity", zap.String("contactID", contactID))
    66  		return cm.me, true
    67  	}
    68  	contact, ok := cm.sm.Load(contactID)
    69  	if contact == nil {
    70  		return nil, ok
    71  	}
    72  	return contact.(*Contact), ok
    73  }
    74  
    75  func (cm *contactMap) Store(contactID string, contact *Contact) {
    76  	if contactID == cm.me.ID {
    77  		cm.logger.Warn("contacts map: storing own identity", zap.String("contactID", contactID))
    78  		return
    79  	}
    80  	cm.sm.Store(contactID, contact)
    81  }
    82  
    83  func (cm *contactMap) Range(f func(contactID string, contact *Contact) (shouldContinue bool)) {
    84  	nf := func(key, value interface{}) (shouldContinue bool) {
    85  		return f(key.(string), value.(*Contact))
    86  	}
    87  	cm.sm.Range(nf)
    88  }
    89  
    90  func (cm *contactMap) Delete(contactID string) {
    91  	if contactID == cm.me.ID {
    92  		cm.logger.Warn("contacts map: deleting own identity", zap.String("contactID", contactID))
    93  		return
    94  	}
    95  	cm.sm.Delete(contactID)
    96  }
    97  
    98  func (cm *contactMap) Len() int {
    99  	count := 0
   100  	cm.Range(func(contactID string, contact *Contact) (shouldContinue bool) {
   101  		count++
   102  		return true
   103  	})
   104  
   105  	return count
   106  }
   107  
   108  /*
   109  |--------------------------------------------------------------------------
   110  | systemMessageTranslationsMap
   111  |--------------------------------------------------------------------------
   112  |
   113  | A sync.Map wrapper for the specific mapping of map[protobuf.MembershipUpdateEvent_EventType]string
   114  |
   115  */
   116  
   117  type systemMessageTranslationsMap struct {
   118  	sm sync.Map
   119  }
   120  
   121  func (smtm *systemMessageTranslationsMap) Init(set map[protobuf.MembershipUpdateEvent_EventType]string) {
   122  	for eventType, message := range set {
   123  		smtm.Store(eventType, message)
   124  	}
   125  }
   126  
   127  func (smtm *systemMessageTranslationsMap) Load(eventType protobuf.MembershipUpdateEvent_EventType) (string, bool) {
   128  	message, ok := smtm.sm.Load(eventType)
   129  	if message == nil {
   130  		return "", ok
   131  	}
   132  	return message.(string), ok
   133  }
   134  
   135  func (smtm *systemMessageTranslationsMap) Store(eventType protobuf.MembershipUpdateEvent_EventType, message string) {
   136  	smtm.sm.Store(eventType, message)
   137  }
   138  
   139  func (smtm *systemMessageTranslationsMap) Range(f func(eventType protobuf.MembershipUpdateEvent_EventType, message string) (shouldContinue bool)) {
   140  	nf := func(key, value interface{}) (shouldContinue bool) {
   141  		return f(key.(protobuf.MembershipUpdateEvent_EventType), value.(string))
   142  	}
   143  	smtm.sm.Range(nf)
   144  }
   145  
   146  func (smtm *systemMessageTranslationsMap) Delete(eventType protobuf.MembershipUpdateEvent_EventType) {
   147  	smtm.sm.Delete(eventType)
   148  }
   149  
   150  /*
   151  |--------------------------------------------------------------------------
   152  | installationMap
   153  |--------------------------------------------------------------------------
   154  |
   155  | A sync.Map wrapper for the specific mapping of map[string]*multidevice.Installation
   156  |
   157  */
   158  
   159  type installationMap struct {
   160  	sm sync.Map
   161  }
   162  
   163  func (im *installationMap) Load(installationID string) (*multidevice.Installation, bool) {
   164  	installation, ok := im.sm.Load(installationID)
   165  	if installation == nil {
   166  		return nil, ok
   167  	}
   168  	return installation.(*multidevice.Installation), ok
   169  }
   170  
   171  func (im *installationMap) Store(installationID string, installation *multidevice.Installation) {
   172  	im.sm.Store(installationID, installation)
   173  }
   174  
   175  func (im *installationMap) Range(f func(installationID string, installation *multidevice.Installation) (shouldContinue bool)) {
   176  	nf := func(key, value interface{}) (shouldContinue bool) {
   177  		return f(key.(string), value.(*multidevice.Installation))
   178  	}
   179  	im.sm.Range(nf)
   180  }
   181  
   182  func (im *installationMap) Delete(installationID string) {
   183  	im.sm.Delete(installationID)
   184  }
   185  
   186  func (im *installationMap) Empty() bool {
   187  	count := 0
   188  	im.Range(func(installationID string, installation *multidevice.Installation) (shouldContinue bool) {
   189  		count++
   190  		return false
   191  	})
   192  
   193  	return count == 0
   194  }
   195  
   196  func (im *installationMap) Len() int {
   197  	count := 0
   198  	im.Range(func(installationID string, installation *multidevice.Installation) (shouldContinue bool) {
   199  		count++
   200  		return true
   201  	})
   202  
   203  	return count
   204  }
   205  
   206  /*
   207  |--------------------------------------------------------------------------
   208  | stringBoolMap
   209  |--------------------------------------------------------------------------
   210  |
   211  | A sync.Map wrapper for the specific mapping of map[string]bool
   212  |
   213  */
   214  
   215  type stringBoolMap struct {
   216  	sm sync.Map
   217  }
   218  
   219  func (sbm *stringBoolMap) Load(key string) (bool, bool) {
   220  	state, ok := sbm.sm.Load(key)
   221  	if state == nil {
   222  		return false, ok
   223  	}
   224  	return state.(bool), ok
   225  }
   226  
   227  func (sbm *stringBoolMap) Store(key string, value bool) {
   228  	sbm.sm.Store(key, value)
   229  }
   230  
   231  func (sbm *stringBoolMap) Range(f func(key string, value bool) (shouldContinue bool)) {
   232  	nf := func(key, value interface{}) (shouldContinue bool) {
   233  		return f(key.(string), value.(bool))
   234  	}
   235  	sbm.sm.Range(nf)
   236  }
   237  
   238  func (sbm *stringBoolMap) Delete(key string) {
   239  	sbm.sm.Delete(key)
   240  }
   241  
   242  func (sbm *stringBoolMap) Len() int {
   243  	count := 0
   244  	sbm.Range(func(key string, value bool) (shouldContinue bool) {
   245  		count++
   246  		return true
   247  	})
   248  
   249  	return count
   250  }