func (s *messageStream) Send(event types.Event) (uint64, types.Error) { s.lock.Lock() defer s.lock.Unlock() index := atomic.AddUint64(&s.max, 1) - 1 indexed := indexedEvent{event, index} if currentItem, ok := s.byId[event.GetEventKey()]; ok { s.byIndex[currentItem.index] = nil } s.byIndex = append(s.byIndex, &indexed) s.byId[event.GetEventKey()] = indexed users, err := s.members.Users(*event.GetRoomId()) if err != nil { return 0, nil } if extraUser := extraUserForEvent(event); extraUser != nil { l := len(users) allUsers := make([]ct.UserId, l+1) copy(allUsers, users) allUsers[l] = *extraUser users = allUsers } s.asyncEventSink.Send(users, &indexed) return index, nil }
func extraUserForEvent(event types.Event) *ct.UserId { if event.GetEventType() == types.EventTypeMembership { membership := event.GetContent().(*types.MembershipEventContent).Membership isInvited := membership == types.MembershipInvited isKnocking := membership == types.MembershipKnocking isBanned := membership == types.MembershipBanned if isInvited || isKnocking || isBanned { state, ok := event.(*types.State) if !ok { log.Println("membership event was not a state event:", event) return nil } user, err := ct.ParseUserId(state.StateKey) if err != nil { log.Println("failed to parse user id state key:", state.StateKey) return nil } return &user } } return nil }