func (ps BoltPostStore) GetPosts(channelID string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		posts := make(map[string]*model.Post)
		channelPostsJson, err := ps.postsByChannel.Get([]byte(channelID))
		if err != nil {
			result.Err = model.NewAppError("BoltPostStore.", "Post is not valid", "")
		} else {
			channelPosts := model.BoolMapFromJson(strings.NewReader(string(channelPostsJson)))
			postItems, err := ps.postStore.Items()
			if err != nil {
				result.Err = model.NewAppError("BoltPostStore.", "Post is not valid", "")
				storeChannel <- result
				close(storeChannel)
				return
			}

			for _, postItem := range postItems {
				if channelPosts[string(postItem.Key)] {
					postString := string(postItem.Value)
					posts[string(postItem.Key)] = model.PostFromJson(strings.NewReader(postString))
				}
			}
			result.Data = posts
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
示例#2
0
func createPost(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	sessionContext := context.Get(r, "context").(Context)
	vars := mux.Vars(r)
	channelId := string(vars["id"])

	post := model.PostFromJson(r.Body)

	if post == nil {
		sessionContext.SetInvalidParam("Invalid post while create", "")
		w.WriteHeader(sessionContext.Err.StatusCode)
		return
	}

	post.ChannelId = channelId
	post.UserId = sessionContext.User.Id

	rPost, err := CreatePost(post)
	if err != nil {
		sessionContext.SetInvalidParam("Invalid post while create", "")
		w.WriteHeader(sessionContext.Err.StatusCode)
		return
	}
	message := model.NewMessage(rPost.UserId, rPost.ChannelId, model.ACTION_POSTED)
	PublishAndForget(message)

	w.WriteHeader(http.StatusOK)
	w.Write([]byte(rPost.ToJson()))
}
func (ps BoltPostStore) Get(postId string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		post, err := ps.postStore.Get([]byte(postId))
		if err != nil {
			result.Err = model.NewAppError("BoltPostStore.", "Post is not valid", "")
		} else {
			result.Data = model.PostFromJson(strings.NewReader(string(post)))
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
示例#4
0
func updatePost(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	sessionContext := context.Get(r, "context").(Context)
	// vars := mux.Vars(r)
	// channelId := string(vars["id"])

	post := model.PostFromJson(r.Body)

	if post == nil || post.Id == "" {
		sessionContext.SetInvalidParam("Invalid post while create", "")
		w.WriteHeader(sessionContext.Err.StatusCode)
		return
	}
	rPost, err := UpdatePost(post)
	if err != nil {
		sessionContext.SetInvalidParam("Invalid post while create", "")
		w.WriteHeader(sessionContext.Err.StatusCode)
		return
	}

	w.WriteHeader(http.StatusOK)
	w.Write([]byte(rPost.ToJson()))
}