Example #1
0
func (s *aliasStore) AddAlias(alias ct.Alias, room ct.RoomId) types.Error {
	inserted, err := s.idMap.Insert(ct.Id(alias), ct.Id(room))
	if err != nil {
		return types.InternalError(err)
	}
	if !inserted {
		return types.RoomInUseError("room alias '" + alias.String() + "' already exists")
	}
	return nil
}
Example #2
0
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
}