예제 #1
0
파일: topic.go 프로젝트: ovh/tat
// MigrateToDedicatedTopic sets collection  attribute on topic
func MigrateToDedicatedTopic(topic *tat.Topic) error {

	if topic.Collection != "" {
		return fmt.Errorf("MigrateToDedicatedTopic> This topic is already dedicated on a collection")
	}

	topic.Collection = "messages" + topic.ID

	errUpdate := store.Tat().CTopics.Update(
		bson.M{"_id": topic.ID},
		bson.M{"$set": bson.M{"collection": topic.Collection}},
	)
	if errUpdate != nil {
		return fmt.Errorf("MigrateToDedicatedTopic> Error while update Topic collection:%s", errUpdate)
	}

	store.EnsureIndexesMessages(topic.Collection)

	return nil
}
예제 #2
0
파일: topic.go 프로젝트: ovh/tat
// Insert creates a new topic. User is read write on topic
func Insert(topic *tat.Topic, u *tat.User) error {
	if err := CheckAndFixName(topic); err != nil {
		return err
	}

	isParentRootTopic, parentTopic, err := getParentTopic(topic)
	if !isParentRootTopic {
		if err != nil {
			return tat.NewError(http.StatusNotFound, "Parent Topic not found %s", topic.Topic)
		}
		// If user create a Topic in /Private/username, no check or RW to create
		if !strings.HasPrefix(topic.Topic, "/Private/"+u.Username) {
			// check if user can create topic in /topic
			hasRW := IsUserAdmin(parentTopic, u)
			if !hasRW {
				return tat.NewError(http.StatusUnauthorized, "No RW access to parent topic %s", parentTopic.Topic)
			}
		}
	} else if !u.IsAdmin { // no parent topic, check admin
		return tat.NewError(http.StatusUnauthorized, "No write access to create parent topic %s", topic.Topic)
	}
	if _, err = FindByTopic(topic.Topic, true, false, false, nil); err == nil {
		return tat.NewError(http.StatusConflict, "Topic Already Exists : %s", topic.Topic)
	}

	topic.ID = bson.NewObjectId().Hex()
	topic.DateCreation = time.Now().Unix()
	topic.MaxLength = tat.DefaultMessageMaxSize // topic MaxLenth messages
	topic.CanForceDate = false
	topic.IsAutoComputeLabels = true
	topic.IsAutoComputeTags = true
	topic.Collection = "messages" + topic.ID

	if !isParentRootTopic {
		topic.ROGroups = parentTopic.ROGroups
		topic.RWGroups = parentTopic.RWGroups
		topic.ROUsers = parentTopic.ROUsers
		topic.RWUsers = parentTopic.RWUsers
		topic.AdminUsers = parentTopic.AdminUsers
		topic.AdminGroups = parentTopic.AdminGroups
		topic.MaxLength = parentTopic.MaxLength
		topic.CanForceDate = parentTopic.CanForceDate
		// topic.CanUpdateMsg can be set by user.createTopics for new users
		// with CanUpdateMsg=true
		if !topic.CanUpdateMsg {
			topic.CanUpdateMsg = parentTopic.CanUpdateMsg
		}
		// topic.CanDeleteMsg can be set by user.createTopics for new users
		// with CanDeleteMsg=true
		if !topic.CanDeleteMsg {
			topic.CanDeleteMsg = parentTopic.CanDeleteMsg
		}
		topic.CanUpdateAllMsg = parentTopic.CanUpdateAllMsg
		topic.CanDeleteAllMsg = parentTopic.CanDeleteAllMsg
		topic.AdminCanUpdateAllMsg = parentTopic.AdminCanUpdateAllMsg
		topic.AdminCanDeleteAllMsg = parentTopic.AdminCanDeleteAllMsg
		topic.IsAutoComputeTags = parentTopic.IsAutoComputeTags
		topic.IsAutoComputeLabels = parentTopic.IsAutoComputeLabels
		topic.Parameters = parentTopic.Parameters
	}

	if err = store.Tat().CTopics.Insert(topic); err != nil {
		log.Errorf("Error while inserting new topic %s", err)
	}

	if errC := store.Tat().Session.DB(store.DatabaseName).C(topic.Collection).Create(&mgo.CollectionInfo{ForceIdIndex: true}); errC != nil {
		log.Errorf("Error while create new collection %s", topic.Collection)
	}

	store.EnsureIndexesMessages(topic.Collection)

	h := fmt.Sprintf("create a new topic :%s", topic.Topic)
	err = addToHistory(topic, bson.M{"_id": topic.ID}, u.Username, h)
	if err != nil {
		log.Errorf("Error while inserting history for new topic %s", err)
	}

	log.Debugf("Insert: Clean topics cache for user %s", u.Username)
	cache.CleanAllTopicsLists()

	return AddRwUser(topic, u.Username, u.Username, false)
}