示例#1
0
文件: topics.go 项目: ovh/tat
// Create creates a new topic
func (*TopicsController) Create(ctx *gin.Context) {
	var topicIn tat.TopicCreateJSON
	ctx.Bind(&topicIn)

	var user = tat.User{}
	found, err := userDB.FindByUsername(&user, getCtxUsername(ctx))
	if !found {
		ctx.JSON(http.StatusUnauthorized, gin.H{"error": "User unknown"})
		return
	} else if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching user."})
		return
	}

	var topic tat.Topic
	topic.Topic = topicIn.Topic
	topic.Description = topicIn.Description

	err = topicDB.Insert(&topic, &user)
	if err != nil {
		log.Errorf("Error while InsertTopic %s", err)
		ctx.JSON(tat.Error(err))
		return
	}
	ctx.JSON(http.StatusCreated, topic)
}
示例#2
0
文件: messages.go 项目: ovh/tat
func insertTopicDM(userFrom, userTo tat.User) (tat.Topic, error) {
	var topic = tat.Topic{}
	topicName := "/Private/" + userFrom.Username + "/DM/" + userTo.Username
	topic.Topic = topicName
	topic.Description = userTo.Fullname
	if err := topicDB.Insert(&topic, &userFrom); err != nil {
		log.Errorf("Error while InsertTopic %s", err)
		return topic, err
	}
	return topic, nil
}
示例#3
0
文件: topic.go 项目: ovh/tat
// CheckAndFixName Add a / to topic name is it is not present
// return an error if length of name is < 4 or > 100
func CheckAndFixName(topic *tat.Topic) error {
	name, err := tat.CheckAndFixNameTopic(topic.Topic)
	if err != nil {
		return err
	}
	topic.Topic = name
	return nil
}
示例#4
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
}
示例#5
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)
}