Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
feat(topic): update parameters on PUT topic/param
Browse files Browse the repository at this point in the history
Usefull for https://github.com/ovh/tatwebui/issues/5

Signed-off-by: Yvonnick Esnault <yvonnick.esnault@ovh.net>
  • Loading branch information
yesnault committed Dec 6, 2015
1 parent fabb843 commit a38ed37
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,20 @@ curl -XPUT \
https://<tatHostname>:<tatPort>/topic/param
```

Parameters key is optional.

Example with key parameters :

```
curl -XPUT \
-H "Content-Type: application/json" \
-H "Tat_username: admin" \
-H "Tat_password: passwordAdmin" \
-d '{"topic":"/Internal/Alerts","recursive":false,"maxlength":300,"canForceDate":false,"canUpdateMsg":false,"canDeleteMsg":true,"canUpdateAllMsg":false,"canDeleteAllMsg":false,"isROPublic":false,"parameters":[{"key":"agileview","value":"qsdf#qsdf"},{"key":"tatwebui.view.default","value":"standardview-list"},{"key":"tatwebui.view.forced","value":""}]}' \
https://<tatHostname>:<tatPort>/topic/param
```


## Websockets
### Socket
```
Expand Down
24 changes: 13 additions & 11 deletions controllers/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,15 +516,16 @@ func (t *TopicsController) RemoveAdminGroup(ctx *gin.Context) {
}

type paramJSON struct {
Topic string `json:"topic"`
MaxLength int `json:"maxlength"`
CanForceDate bool `json:"canForceDate"`
CanUpdateMsg bool `json:"canUpdateMsg"`
CanDeleteMsg bool `json:"canDeleteMsg"`
CanUpdateAllMsg bool `json:"canUpdateAllMsg"`
CanDeleteAllMsg bool `json:"canDeleteAllMsg"`
IsROPublic bool `json:"isROPublic"`
Recursive bool `json:"recursive"`
Topic string `json:"topic"`
MaxLength int `json:"maxlength"`
CanForceDate bool `json:"canForceDate"`
CanUpdateMsg bool `json:"canUpdateMsg"`
CanDeleteMsg bool `json:"canDeleteMsg"`
CanUpdateAllMsg bool `json:"canUpdateAllMsg"`
CanDeleteAllMsg bool `json:"canDeleteAllMsg"`
IsROPublic bool `json:"isROPublic"`
Recursive bool `json:"recursive"`
Parameters []models.TopicParameter `json:"parameters"`
}

// SetParam update Topic Parameters : MaxLength, CanForeceDate, CanUpdateMsg, CanDeleteMsg, CanUpdateAllMsg, CanDeleteAllMsg, IsROPublic
Expand Down Expand Up @@ -557,11 +558,12 @@ func (t *TopicsController) SetParam(ctx *gin.Context) {
paramJSON.CanDeleteMsg,
paramJSON.CanUpdateAllMsg,
paramJSON.CanDeleteAllMsg,
paramJSON.IsROPublic)
paramJSON.IsROPublic,
paramJSON.Parameters)

if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, errors.New(err.Error()))
return
}
ctx.JSON(http.StatusCreated, "")
ctx.JSON(http.StatusCreated, gin.H{"info": fmt.Sprintf("Topic %s updated", topic.Topic)})
}
56 changes: 23 additions & 33 deletions models/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,10 @@ func (topic *Topic) FindByID(id string, isAdmin bool) error {
return err
}

// SetParam update param maxLength, canForceDate, canUpdateMsg, canDeleteMsg, canUpdateAllMsg, canDeleteAllMsg, isROPublic on topic
func (topic *Topic) SetParam(username string, recursive bool, maxLength int, canForceDate, canUpdateMsg, canDeleteMsg, canUpdateAllMsg, canDeleteAllMsg, isROPublic bool) error {
// SetParam update param maxLength, canForceDate, canUpdateMsg, canDeleteMsg, canUpdateAllMsg, canDeleteAllMsg, isROPublic, parameters on topic
func (topic *Topic) SetParam(username string, recursive bool, maxLength int,
canForceDate, canUpdateMsg, canDeleteMsg, canUpdateAllMsg, canDeleteAllMsg,
isROPublic bool, parameters []TopicParameter) error {

var selector bson.M

Expand All @@ -451,22 +453,23 @@ func (topic *Topic) SetParam(username string, recursive bool, maxLength int, can
maxLength = DefaultMessageMaxSize
}

_, err := Store().clTopics.UpdateAll(
selector,
bson.M{
"$set": bson.M{
"maxlength": maxLength,
"canForceDate": canForceDate,
"canUpdateMsg": canUpdateMsg,
"canDeleteMsg": canDeleteMsg,
"canUpdateAllMsg": canUpdateAllMsg,
"canDeleteAllMsg": canDeleteAllMsg,
"isROPublic": isROPublic,
},
},
)
update := bson.M{
"maxlength": maxLength,
"canForceDate": canForceDate,
"canUpdateMsg": canUpdateMsg,
"canDeleteMsg": canDeleteMsg,
"canUpdateAllMsg": canUpdateAllMsg,
"canDeleteAllMsg": canDeleteAllMsg,
"isROPublic": isROPublic,
}

if parameters != nil {
update["parameters"] = parameters
}
_, err := Store().clTopics.UpdateAll(selector, bson.M{"$set": update})

if err != nil {
log.Errorf("Error while updateAll parameters : %s", err.Error())
return err
}
h := fmt.Sprintf("update param to maxlength:%d, canForceDate:%t, canUpdateMsg:%t, canDeleteMsg:%t, canUpdateAllMsg:%t, canDeleteAllMsg:%t, isROPublic:%t", maxLength, canForceDate, canUpdateMsg, canDeleteMsg, canUpdateAllMsg, canDeleteAllMsg, isROPublic)
Expand Down Expand Up @@ -585,7 +588,6 @@ func (topic *Topic) RemoveRwGroup(admin string, username string, recursive bool)

// AddParameter add a parameter to the topic
func (topic *Topic) AddParameter(admin string, parameterKey string, parameterValue string, recursive bool) error {

return topic.actionOnSetParameter("$addToSet", "parameters", admin, TopicParameter{Key: parameterKey, Value: parameterValue}, recursive, "add to parameter")
}

Expand Down Expand Up @@ -621,11 +623,7 @@ func (topic *Topic) IsUserRW(user *User) bool {
groups = append(groups, g.Name)
}

if utils.ItemInBothArrays(topic.RWGroups, groups) {
return true
}

return false
return utils.ItemInBothArrays(topic.RWGroups, groups)
}

// IsUserReadAccess return true if user has read access to topic
Expand Down Expand Up @@ -661,13 +659,9 @@ func (topic *Topic) IsUserReadAccess(user User) bool {
groups = append(groups, g.Name)
}

if utils.ItemInBothArrays(currentTopic.RWGroups, groups) ||
return utils.ItemInBothArrays(currentTopic.RWGroups, groups) ||
utils.ItemInBothArrays(currentTopic.ROGroups, groups) ||
utils.ItemInBothArrays(currentTopic.AdminGroups, groups) {
return true
}

return false
utils.ItemInBothArrays(currentTopic.AdminGroups, groups)
}

// IsUserAdmin return true if user is Tat admin or is admin on this topic
Expand Down Expand Up @@ -698,11 +692,7 @@ func (topic *Topic) IsUserAdmin(user *User) bool {
}

// user is "Admin" on his /Private/usrname topics
if strings.HasPrefix(topic.Topic, "/Private/"+user.Username) {
return true
}

return false
return strings.HasPrefix(topic.Topic, "/Private/"+user.Username)
}

// CheckAndFixNameTopic Add a / to topic name is it is not present
Expand Down

0 comments on commit a38ed37

Please sign in to comment.