Add methods to sticky/pin posts

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-07-15 22:28:52 -04:00
parent b78a52eac8
commit 6b99e3957a
2 changed files with 189 additions and 8 deletions

90
post.go
View File

@ -220,3 +220,93 @@ func (s *PostService) Unspoiler(ctx context.Context, id string) (*Response, erro
return s.client.Do(ctx, req, nil) return s.client.Do(ctx, req, nil)
} }
// Sticky stickies a post in its subreddit.
// When bottom is true, the post will be set as the bottom sticky (the 2nd one).
// If no top sticky exists, the post will become the top sticky regardless.
// When attempting to sticky a post that's already stickied, it will return a 409 Conflict error.
func (s *PostService) Sticky(ctx context.Context, id string, bottom bool) (*Response, error) {
path := "api/set_subreddit_sticky"
form := url.Values{}
form.Set("api_type", "json")
form.Set("id", id)
form.Set("state", "true")
if !bottom {
form.Set("num", "1")
}
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Unsticky unstickies a post in its subreddit.
func (s *PostService) Unsticky(ctx context.Context, id string) (*Response, error) {
path := "api/set_subreddit_sticky"
form := url.Values{}
form.Set("api_type", "json")
form.Set("id", id)
form.Set("state", "false")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// PinToProfile pins one of your posts to your profile.
// TODO: very inconsistent behaviour, not sure I'm ready to include this parameter yet.
// The pos parameter should be a number between 1-4 (inclusive), indicating the position at which
// the post should appear on your profile.
// Note: The position will be bumped upward if there's space. E.g. if you only have 1 pinned post,
// and you try to pin another post to position 3, it will be pinned at 2.
// When attempting to pin a post that's already pinned, it will return a 409 Conflict error.
func (s *PostService) PinToProfile(ctx context.Context, id string) (*Response, error) {
path := "api/set_subreddit_sticky"
// if pos < 1 {
// pos = 1
// }
// if pos > 4 {
// pos = 4
// }
form := url.Values{}
form.Set("api_type", "json")
form.Set("id", id)
form.Set("state", "true")
form.Set("to_profile", "true")
// form.Set("num", fmt.Sprint(pos))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// UnpinFromProfile unpins one of your posts from your profile.
func (s *PostService) UnpinFromProfile(ctx context.Context, id string) (*Response, error) {
path := "api/set_subreddit_sticky"
form := url.Values{}
form.Set("api_type", "json")
form.Set("id", id)
form.Set("state", "false")
form.Set("to_profile", "true")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}

View File

@ -62,14 +62,14 @@ func TestPostService_MarkNSFW(t *testing.T) {
assert.Equal(t, http.MethodPost, r.Method) assert.Equal(t, http.MethodPost, r.Method)
form := url.Values{} form := url.Values{}
form.Set("id", "t1_test") form.Set("id", "t3_test")
err := r.ParseForm() err := r.ParseForm()
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, form, r.PostForm) assert.Equal(t, form, r.PostForm)
}) })
res, err := client.Post.MarkNSFW(ctx, "t1_test") res, err := client.Post.MarkNSFW(ctx, "t3_test")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode) assert.Equal(t, http.StatusOK, res.StatusCode)
} }
@ -82,14 +82,14 @@ func TestPostService_UnmarkNSFW(t *testing.T) {
assert.Equal(t, http.MethodPost, r.Method) assert.Equal(t, http.MethodPost, r.Method)
form := url.Values{} form := url.Values{}
form.Set("id", "t1_test") form.Set("id", "t3_test")
err := r.ParseForm() err := r.ParseForm()
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, form, r.PostForm) assert.Equal(t, form, r.PostForm)
}) })
res, err := client.Post.UnmarkNSFW(ctx, "t1_test") res, err := client.Post.UnmarkNSFW(ctx, "t3_test")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode) assert.Equal(t, http.StatusOK, res.StatusCode)
} }
@ -102,14 +102,14 @@ func TestPostService_Spoiler(t *testing.T) {
assert.Equal(t, http.MethodPost, r.Method) assert.Equal(t, http.MethodPost, r.Method)
form := url.Values{} form := url.Values{}
form.Set("id", "t1_test") form.Set("id", "t3_test")
err := r.ParseForm() err := r.ParseForm()
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, form, r.PostForm) assert.Equal(t, form, r.PostForm)
}) })
res, err := client.Post.Spoiler(ctx, "t1_test") res, err := client.Post.Spoiler(ctx, "t3_test")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode) assert.Equal(t, http.StatusOK, res.StatusCode)
} }
@ -122,14 +122,105 @@ func TestPostService_Unspoiler(t *testing.T) {
assert.Equal(t, http.MethodPost, r.Method) assert.Equal(t, http.MethodPost, r.Method)
form := url.Values{} form := url.Values{}
form.Set("id", "t1_test") form.Set("id", "t3_test")
err := r.ParseForm() err := r.ParseForm()
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, form, r.PostForm) assert.Equal(t, form, r.PostForm)
}) })
res, err := client.Post.Unspoiler(ctx, "t1_test") res, err := client.Post.Unspoiler(ctx, "t3_test")
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
}
func TestPostService_Sticky(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/api/set_subreddit_sticky", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
form := url.Values{}
form.Set("api_type", "json")
form.Set("id", "t3_test")
form.Set("state", "true")
form.Set("num", "1")
err := r.ParseForm()
assert.NoError(t, err)
assert.Equal(t, form, r.PostForm)
})
res, err := client.Post.Sticky(ctx, "t3_test", false)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
}
func TestPostService_Unsticky(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/api/set_subreddit_sticky", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
form := url.Values{}
form.Set("api_type", "json")
form.Set("id", "t3_test")
form.Set("state", "false")
err := r.ParseForm()
assert.NoError(t, err)
assert.Equal(t, form, r.PostForm)
})
res, err := client.Post.Unsticky(ctx, "t3_test")
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
}
func TestPostService_PinToProfile(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/api/set_subreddit_sticky", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
form := url.Values{}
form.Set("api_type", "json")
form.Set("id", "t3_test")
form.Set("state", "true")
form.Set("to_profile", "true")
err := r.ParseForm()
assert.NoError(t, err)
assert.Equal(t, form, r.PostForm)
})
res, err := client.Post.PinToProfile(ctx, "t3_test")
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
}
func TestPostService_UnpinFromProfile(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/api/set_subreddit_sticky", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
form := url.Values{}
form.Set("api_type", "json")
form.Set("id", "t3_test")
form.Set("state", "false")
form.Set("to_profile", "true")
err := r.ParseForm()
assert.NoError(t, err)
assert.Equal(t, form, r.PostForm)
})
res, err := client.Post.UnpinFromProfile(ctx, "t3_test")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode) assert.Equal(t, http.StatusOK, res.StatusCode)
} }