func (db *roomDb) SetRoomState(roomId types.RoomId, userId types.UserId, content types.TypedContent, stateKey string) (*matrixTypes.State, matrixTypes.Error) { db.roomsLock.RLock() defer db.roomsLock.RUnlock() room := db.rooms[roomId] if room == nil { return nil, matrixTypes.NotFoundError("room '" + roomId.String() + "' doesn't exist") } var eventId = types.DeriveEventId(utils.RandomString(16), types.Id(userId)) stateId := stateId{content.GetEventType(), stateKey} state := new(matrixTypes.State) state.EventId = eventId state.RoomId = roomId state.UserId = userId state.EventType = content.GetEventType() state.StateKey = stateKey state.Timestamp = types.Timestamp{time.Now()} state.Content = content state.OldState = (*matrixTypes.OldState)(room.states[stateId]) room.stateLock.Lock() defer room.stateLock.Unlock() room.states[stateId] = state return state, nil }
func (s roomService) sendMessage( room ct.RoomId, user ct.UserId, content types.TypedContent, ) (*types.Message, types.Error) { log.Printf("Sending message: %#v, %#v, %#v, %#v", room, user, content) message := new(types.Message) message.EventId = ct.DeriveEventId(utils.RandomString(16), ct.Id(user)) message.RoomId = room message.UserId = user message.EventType = content.GetEventType() message.Timestamp = ct.Timestamp{time.Now()} message.Content = content _, err := s.eventSink.Send(message) if err != nil { return nil, err } return message, nil }
func (e authEndpoint) registerWithPassword(hostname string, body *authRequest) interface{} { if body.Username == "" { body.Username = utils.RandomString(24) } if body.Password == "" { return types.BadJsonError("Missing or invalid password") } userId := ct.NewUserId(body.Username, hostname) err := e.userService.CreateUser(userId) if err != nil { return err } if err := e.userService.SetPassword(userId, userId, body.Password); err != nil { return err } accessToken, err := e.tokenService.NewAccessToken(userId) if err != nil { return err } return authResponse{ UserId: userId, AccessToken: accessToken.String(), } }
func (t tokenInfo) String() string { encodedUserId := base64.RawURLEncoding.EncodeToString([]byte(t.userId.String())) return fmt.Sprintf("%s..%s", encodedUserId, utils.RandomString(16)) }
func (s roomService) CreateRoom( domain string, creator ct.UserId, desc *types.RoomDescription, ) (ct.RoomId, *ct.Alias, types.Error) { var alias *ct.Alias id := ct.NewRoomId(utils.RandomString(16), domain) if desc.Alias != nil { a := ct.NewAlias(*desc.Alias, domain) err := s.aliases.AddAlias(a, id) if err != nil { return ct.RoomId{}, nil, err } alias = &a } exists, err := s.rooms.CreateRoom(id) if exists { return ct.RoomId{}, nil, types.RoomInUseError("room '" + id.String() + "' already exists") } if err != nil { return ct.RoomId{}, nil, err } s.members.AddMember(id, creator) _, err = s.sendMessage(id, creator, &types.CreateEventContent{creator}) if err != nil { return ct.RoomId{}, nil, err } _, err = s.setState(id, creator, &types.CreateEventContent{creator}, "") if err != nil { return ct.RoomId{}, nil, err } profile, err := s.profileProvider.Profile(creator) if err != nil { return ct.RoomId{}, nil, err } membership := &types.MembershipEventContent{&profile, types.MembershipMember} _, err = s.setState(id, creator, membership, creator.String()) if err != nil { return ct.RoomId{}, nil, err } _, err = s.setState(id, creator, types.DefaultPowerLevels(creator), "") if err != nil { return ct.RoomId{}, nil, err } joinRuleContent := types.JoinRulesEventContent{desc.Visibility.ToJoinRule()} _, err = s.setState(id, creator, &joinRuleContent, "") if err != nil { return ct.RoomId{}, nil, err } if alias != nil { _, err = s.setState(id, creator, &types.AliasesEventContent{[]ct.Alias{*alias}}, "") if err != nil { return ct.RoomId{}, nil, err } } if desc.Name != nil { _, err = s.setState(id, creator, &types.NameEventContent{*desc.Name}, "") if err != nil { return ct.RoomId{}, nil, err } } if desc.Topic != nil { _, err = s.setState(id, creator, &types.TopicEventContent{*desc.Topic}, "") if err != nil { return ct.RoomId{}, nil, err } } for _, invited := range desc.Invited { membership := types.MembershipEventContent{nil, types.MembershipInvited} _, err = s.setState(id, creator, &membership, invited.String()) if err != nil { return ct.RoomId{}, nil, err } } return id, alias, nil }