diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/post.go | 66 | ||||
-rw-r--r-- | api/post_test.go | 77 | ||||
-rw-r--r-- | api/user.go | 19 | ||||
-rw-r--r-- | api/user_test.go | 8 |
4 files changed, 167 insertions, 3 deletions
diff --git a/api/post.go b/api/post.go index c98bf2d71..31a7ab3b5 100644 --- a/api/post.go +++ b/api/post.go @@ -31,6 +31,8 @@ func InitPost(r *mux.Router) { sr.Handle("/posts/{time:[0-9]+}", ApiUserRequiredActivity(getPostsSince, false)).Methods("GET") sr.Handle("/post/{post_id:[A-Za-z0-9]+}", ApiUserRequired(getPost)).Methods("GET") sr.Handle("/post/{post_id:[A-Za-z0-9]+}/delete", ApiUserRequired(deletePost)).Methods("POST") + sr.Handle("/post/{post_id:[A-Za-z0-9]+}/before/{offset:[0-9]+}/{num_posts:[0-9]+}", ApiUserRequired(getPostsBefore)).Methods("GET") + sr.Handle("/post/{post_id:[A-Za-z0-9]+}/after/{offset:[0-9]+}/{num_posts:[0-9]+}", ApiUserRequired(getPostsAfter)).Methods("GET") } func createPost(c *Context, w http.ResponseWriter, r *http.Request) { @@ -812,6 +814,70 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { } } +func getPostsBefore(c *Context, w http.ResponseWriter, r *http.Request) { + getPostsBeforeOrAfter(c, w, r, true) +} +func getPostsAfter(c *Context, w http.ResponseWriter, r *http.Request) { + getPostsBeforeOrAfter(c, w, r, false) +} +func getPostsBeforeOrAfter(c *Context, w http.ResponseWriter, r *http.Request, before bool) { + params := mux.Vars(r) + + id := params["id"] + if len(id) != 26 { + c.SetInvalidParam("getPostsBeforeOrAfter", "channelId") + return + } + + postId := params["post_id"] + if len(postId) != 26 { + c.SetInvalidParam("getPostsBeforeOrAfter", "postId") + return + } + + numPosts, err := strconv.Atoi(params["num_posts"]) + if err != nil || numPosts <= 0 { + c.SetInvalidParam("getPostsBeforeOrAfter", "numPosts") + return + } + + offset, err := strconv.Atoi(params["offset"]) + if err != nil || offset < 0 { + c.SetInvalidParam("getPostsBeforeOrAfter", "offset") + return + } + + cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, id, c.Session.UserId) + // We can do better than this etag in this situation + etagChan := Srv.Store.Post().GetEtag(id) + + if !c.HasPermissionsToChannel(cchan, "getPostsBeforeOrAfter") { + return + } + + etag := (<-etagChan).Data.(string) + if HandleEtag(etag, w, r) { + return + } + + var pchan store.StoreChannel + if before { + pchan = Srv.Store.Post().GetPostsBefore(id, postId, numPosts, offset) + } else { + pchan = Srv.Store.Post().GetPostsAfter(id, postId, numPosts, offset) + } + + if result := <-pchan; result.Err != nil { + c.Err = result.Err + return + } else { + list := result.Data.(*model.PostList) + + w.Header().Set(model.HEADER_ETAG_SERVER, etag) + w.Write([]byte(list.ToJson())) + } +} + func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) { terms := r.FormValue("terms") diff --git a/api/post_test.go b/api/post_test.go index e54e9ef0c..3452c9788 100644 --- a/api/post_test.go +++ b/api/post_test.go @@ -329,6 +329,83 @@ func TestGetPostsSince(t *testing.T) { } } +func TestGetPostsBeforeAfter(t *testing.T) { + Setup() + + team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN} + team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team) + + user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"} + user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User) + store.Must(Srv.Store.User().VerifyEmail(user1.Id)) + + Client.LoginByEmail(team.Name, user1.Email, "pwd") + + channel1 := &model.Channel{DisplayName: "TestGetPosts", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id} + channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel) + + time.Sleep(10 * time.Millisecond) + post0 := &model.Post{ChannelId: channel1.Id, Message: "a" + model.NewId() + "a"} + post0 = Client.Must(Client.CreatePost(post0)).Data.(*model.Post) + + time.Sleep(10 * time.Millisecond) + post1 := &model.Post{ChannelId: channel1.Id, Message: "a" + model.NewId() + "a"} + post1 = Client.Must(Client.CreatePost(post1)).Data.(*model.Post) + + time.Sleep(10 * time.Millisecond) + post1a1 := &model.Post{ChannelId: channel1.Id, Message: "a" + model.NewId() + "a", RootId: post1.Id} + post1a1 = Client.Must(Client.CreatePost(post1a1)).Data.(*model.Post) + + time.Sleep(10 * time.Millisecond) + post2 := &model.Post{ChannelId: channel1.Id, Message: "a" + model.NewId() + "a"} + post2 = Client.Must(Client.CreatePost(post2)).Data.(*model.Post) + + time.Sleep(10 * time.Millisecond) + post3 := &model.Post{ChannelId: channel1.Id, Message: "a" + model.NewId() + "a"} + post3 = Client.Must(Client.CreatePost(post3)).Data.(*model.Post) + + time.Sleep(10 * time.Millisecond) + post3a1 := &model.Post{ChannelId: channel1.Id, Message: "a" + model.NewId() + "a", RootId: post3.Id} + post3a1 = Client.Must(Client.CreatePost(post3a1)).Data.(*model.Post) + + r1 := Client.Must(Client.GetPostsBefore(channel1.Id, post1a1.Id, 0, 10, "")).Data.(*model.PostList) + + if r1.Order[0] != post1.Id { + t.Fatal("wrong order") + } + + if r1.Order[1] != post0.Id { + t.Fatal("wrong order") + } + + if len(r1.Posts) != 2 { + t.Fatal("wrong size") + } + + r2 := Client.Must(Client.GetPostsAfter(channel1.Id, post3a1.Id, 0, 3, "")).Data.(*model.PostList) + + if len(r2.Posts) != 0 { + t.Fatal("should have been empty") + } + + post2.Message = "new message" + Client.Must(Client.UpdatePost(post2)) + + r3 := Client.Must(Client.GetPostsAfter(channel1.Id, post1a1.Id, 0, 2, "")).Data.(*model.PostList) + + if r3.Order[0] != post3.Id { + t.Fatal("wrong order") + } + + if r3.Order[1] != post2.Id { + t.Fatal("wrong order") + } + + if len(r3.Order) != 2 { + t.Fatal("missing post update") + } +} + func TestSearchPosts(t *testing.T) { Setup() diff --git a/api/user.go b/api/user.go index 732c6b9a8..42d3a43e7 100644 --- a/api/user.go +++ b/api/user.go @@ -49,7 +49,7 @@ func InitUser(r *mux.Router) { sr.Handle("/newimage", ApiUserRequired(uploadProfileImage)).Methods("POST") sr.Handle("/me", ApiAppHandler(getMe)).Methods("GET") - sr.Handle("/status", ApiUserRequiredActivity(getStatuses, false)).Methods("GET") + sr.Handle("/status", ApiUserRequiredActivity(getStatuses, false)).Methods("POST") sr.Handle("/profiles", ApiUserRequired(getProfiles)).Methods("GET") sr.Handle("/profiles/{id:[A-Za-z0-9]+}", ApiUserRequired(getProfiles)).Methods("GET") sr.Handle("/{id:[A-Za-z0-9]+}", ApiUserRequired(getUser)).Methods("GET") @@ -1483,16 +1483,31 @@ func updateUserNotify(c *Context, w http.ResponseWriter, r *http.Request) { } func getStatuses(c *Context, w http.ResponseWriter, r *http.Request) { + userIds := model.ArrayFromJson(r.Body) + if len(userIds) == 0 { + c.SetInvalidParam("getStatuses", "userIds") + return + } if result := <-Srv.Store.User().GetProfiles(c.Session.TeamId); result.Err != nil { c.Err = result.Err return } else { - profiles := result.Data.(map[string]*model.User) statuses := map[string]string{} for _, profile := range profiles { + found := false + for _, uid := range userIds { + if uid == profile.Id { + found = true + } + } + + if !found { + continue + } + if profile.IsOffline() { statuses[profile.Id] = model.USER_OFFLINE } else if profile.IsAway() { diff --git a/api/user_test.go b/api/user_test.go index 0ad3541bc..f067182cb 100644 --- a/api/user_test.go +++ b/api/user_test.go @@ -1020,9 +1020,15 @@ func TestStatuses(t *testing.T) { ruser := Client.Must(Client.CreateUser(&user, "")).Data.(*model.User) store.Must(Srv.Store.User().VerifyEmail(ruser.Id)) + user2 := model.User{TeamId: rteam.Data.(*model.Team).Id, Email: strings.ToLower(model.NewId()) + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"} + ruser2 := Client.Must(Client.CreateUser(&user2, "")).Data.(*model.User) + store.Must(Srv.Store.User().VerifyEmail(ruser2.Id)) + Client.LoginByEmail(team.Name, user.Email, user.Password) - r1, err := Client.GetStatuses() + userIds := []string{ruser2.Id} + + r1, err := Client.GetStatuses(userIds) if err != nil { t.Fatal(err) } |