// List messages on one topic, with given criterias func (m *MessagesController) List(ctx *gin.Context) { var criteria = m.buildCriteria(ctx) presenceArg := ctx.Query("presence") topicIn, err := GetParam(ctx, "topic") if err != nil { return } criteria.Topic = topicIn // add / if search on topic // as topic is in path, it can't start with a / if criteria.Topic != "" && string(criteria.Topic[0]) != "/" { criteria.Topic = "/" + criteria.Topic } var topic = models.Topic{} err = topic.FindByTopic(criteria.Topic, true) if err != nil { topicCriteria := "" _, topicCriteria, err = m.checkDMTopic(ctx, criteria.Topic) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": "topic " + criteria.Topic + " does not exist"}) return } // hack to get new created DM Topic err := topic.FindByTopic(criteria.Topic, true) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": "topic " + criteria.Topic + " does not exist (2)"}) return } criteria.Topic = topicCriteria } out := &messagesJSON{} var user models.User var e error if utils.GetCtxUsername(ctx) != "" { user, e = PreCheckUser(ctx) if e != nil { return } isReadAccess := topic.IsUserReadAccess(user) if !isReadAccess { ctx.JSON(http.StatusForbidden, gin.H{"error": "No Read Access to this topic"}) return } out.IsTopicRw = topic.IsUserRW(&user) } else if !topic.IsROPublic { ctx.JSON(http.StatusForbidden, gin.H{"error": "No Public Read Access Public to this topic"}) return } else if topic.IsROPublic && strings.HasPrefix(topic.Topic, "/Private") { ctx.JSON(http.StatusForbidden, gin.H{"error": "No Public Read Access to this topic"}) return } // send presence if presenceArg != "" && !user.IsSystem { go func() { var presence = models.Presence{} err := presence.Upsert(user, topic, presenceArg) if err != nil { log.Errorf("Error while InsertPresence %s", err) } go models.WSPresence(&models.WSPresenceJSON{Action: "create", Presence: presence}) }() } messages, err := models.ListMessages(criteria) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } out.Messages = messages ctx.JSON(http.StatusOK, out) }
// Delete a message, works only on /Private/username/... topics func (m *MessagesController) Delete(ctx *gin.Context) { idMessageIn, err := GetParam(ctx, "idMessage") if err != nil { return } message := models.Message{} err = message.FindByID(idMessageIn) if err != nil { ctx.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Message %s does not exist", idMessageIn)}) return } user, e := PreCheckUser(ctx) if e != nil { return } topic := models.Topic{} err = topic.FindByTopic(message.Topics[0], true) if err != nil { ctx.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Topic %s does not exist", message.Topics[0])}) return } isRw := topic.IsUserRW(&user) if !isRw { ctx.JSON(http.StatusForbidden, gin.H{"error": fmt.Sprintf("No RW Access to topic %s", message.Topics[0])}) return } if !strings.HasPrefix(message.Topics[0], "/Private/"+user.Username) && !topic.CanDeleteMsg && !topic.CanDeleteAllMsg { if !topic.CanDeleteMsg && !topic.CanDeleteAllMsg { ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("You can't delete a message on this topic %s", topic.Topic)}) return } ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Could not delete a message in a non private topic %s", message.Topics[0])}) return } if !topic.CanDeleteAllMsg && message.Author.Username != user.Username { ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Could not delete a message from another user %s than you %s", message.Author.Username, user.Username)}) return } for _, topicName := range message.Topics { // if msg is only in tasks topic, ok to delete it if strings.HasPrefix(topicName, "/Private/") && strings.HasSuffix(topicName, "/Tasks") && len(message.Topics) > 1 { ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Could not delete a message in a tasks topic")}) return } } err = message.Delete() if err != nil { log.Errorf("Error while delete a message %s", err) ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } go models.WSMessage(&models.WSMessageJSON{Action: "delete", Username: user.Username, Message: message}) ctx.JSON(http.StatusOK, gin.H{"info": fmt.Sprintf("Message deleted from %s", topic.Topic)}) }