Return lists for bans, wikibans, messages, etc.

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-08-29 14:45:45 -04:00
parent 5349d53626
commit b4feadda4f
4 changed files with 121 additions and 162 deletions

View File

@ -35,22 +35,42 @@ type Message struct {
IsComment bool `json:"was_comment"`
}
// Messages is a list of messages.
type Messages struct {
Messages []*Message `json:"messages"`
After string `json:"after"`
Before string `json:"before"`
}
type rootInboxListing struct {
Kind string `json:"kind"`
Data inboxListing `json:"data"`
}
type inboxListing struct {
Things inboxThings `json:"children"`
After string `json:"after"`
Before string `json:"before"`
inboxThings
after string
before string
}
var _ anchor = &inboxListing{}
func (l *inboxListing) After() string {
return l.after
}
func (l *inboxListing) Before() string {
return l.before
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (l *inboxListing) UnmarshalJSON(b []byte) error {
root := new(struct {
Data struct {
Things inboxThings `json:"children"`
After string `json:"after"`
Before string `json:"before"`
} `json:"data"`
})
err := json.Unmarshal(b, root)
if err != nil {
return err
}
l.inboxThings = root.Data.Things
l.after = root.Data.After
l.before = root.Data.Before
return nil
}
// The returned JSON for comments is a bit different.
@ -93,22 +113,6 @@ func (t *inboxThings) UnmarshalJSON(b []byte) error {
return nil
}
func (l *rootInboxListing) getComments() *Messages {
return &Messages{
Messages: l.Data.Things.Comments,
After: l.Data.After,
Before: l.Data.Before,
}
}
func (l *rootInboxListing) getMessages() *Messages {
return &Messages{
Messages: l.Data.Things.Messages,
After: l.Data.After,
Before: l.Data.Before,
}
}
// SendMessageRequest represents a request to send a message.
type SendMessageRequest struct {
// Username, or /r/name for that subreddit's moderators.
@ -262,33 +266,33 @@ func (s *MessageService) Send(ctx context.Context, sendRequest *SendMessageReque
}
// Inbox returns comments and messages that appear in your inbox, respectively.
func (s *MessageService) Inbox(ctx context.Context, opts *ListOptions) (*Messages, *Messages, *Response, error) {
func (s *MessageService) Inbox(ctx context.Context, opts *ListOptions) ([]*Message, []*Message, *Response, error) {
root, resp, err := s.inbox(ctx, "message/inbox", opts)
if err != nil {
return nil, nil, resp, err
}
return root.getComments(), root.getMessages(), resp, nil
return root.Comments, root.Messages, resp, nil
}
// InboxUnread returns unread comments and messages that appear in your inbox, respectively.
func (s *MessageService) InboxUnread(ctx context.Context, opts *ListOptions) (*Messages, *Messages, *Response, error) {
func (s *MessageService) InboxUnread(ctx context.Context, opts *ListOptions) ([]*Message, []*Message, *Response, error) {
root, resp, err := s.inbox(ctx, "message/unread", opts)
if err != nil {
return nil, nil, resp, err
}
return root.getComments(), root.getMessages(), resp, nil
return root.Comments, root.Messages, resp, nil
}
// Sent returns messages that you've sent.
func (s *MessageService) Sent(ctx context.Context, opts *ListOptions) (*Messages, *Response, error) {
func (s *MessageService) Sent(ctx context.Context, opts *ListOptions) ([]*Message, *Response, error) {
root, resp, err := s.inbox(ctx, "message/sent", opts)
if err != nil {
return nil, resp, err
}
return root.getMessages(), resp, nil
return root.Messages, resp, nil
}
func (s *MessageService) inbox(ctx context.Context, path string, opts *ListOptions) (*rootInboxListing, *Response, error) {
func (s *MessageService) inbox(ctx context.Context, path string, opts *ListOptions) (*inboxListing, *Response, error) {
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
@ -299,7 +303,7 @@ func (s *MessageService) inbox(ctx context.Context, path string, opts *ListOptio
return nil, nil, err
}
root := new(rootInboxListing)
root := new(inboxListing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, nil, err

View File

@ -10,46 +10,38 @@ import (
"github.com/stretchr/testify/require"
)
var expectedCommentMessages = &Messages{
Messages: []*Message{
{
ID: "g1xi2m9",
FullID: "t1_g1xi2m9",
Created: &Timestamp{time.Date(2020, 8, 18, 0, 24, 13, 0, time.UTC)},
var expectedCommentMessages = []*Message{
{
ID: "g1xi2m9",
FullID: "t1_g1xi2m9",
Created: &Timestamp{time.Date(2020, 8, 18, 0, 24, 13, 0, time.UTC)},
Subject: "post reply",
Text: "u/testuser2 hello",
ParentID: "t3_hs03f3",
Subject: "post reply",
Text: "u/testuser2 hello",
ParentID: "t3_hs03f3",
Author: "testuser1",
To: "testuser2",
Author: "testuser1",
To: "testuser2",
IsComment: true,
},
IsComment: true,
},
After: "",
Before: "",
}
var expectedMessages = &Messages{
Messages: []*Message{
{
ID: "qwki97",
FullID: "t4_qwki97",
Created: &Timestamp{time.Date(2020, 8, 18, 0, 16, 53, 0, time.UTC)},
var expectedMessages = []*Message{
{
ID: "qwki97",
FullID: "t4_qwki97",
Created: &Timestamp{time.Date(2020, 8, 18, 0, 16, 53, 0, time.UTC)},
Subject: "re: test",
Text: "test",
ParentID: "t4_qwki4m",
Subject: "re: test",
Text: "test",
ParentID: "t4_qwki4m",
Author: "testuser1",
To: "testuser2",
Author: "testuser1",
To: "testuser2",
IsComment: false,
},
IsComment: false,
},
After: "",
Before: "",
}
func TestMessageService_ReadAll(t *testing.T) {

View File

@ -34,13 +34,6 @@ type Relationship struct {
Created *Timestamp `json:"date,omitempty"`
}
// Relationships is a listing of relationships.
type Relationships struct {
Relationships []*Relationship `json:"relationships"`
After string `json:"after"`
Before string `json:"before"`
}
// Moderator is a user who moderates a subreddit.
type Moderator struct {
*Relationship
@ -55,13 +48,6 @@ type Ban struct {
Note string `json:"note,omitempty"`
}
// Bans is a listing of bans.
type Bans struct {
Bans []*Ban `json:"bans"`
After string `json:"after"`
Before string `json:"before"`
}
// todo: interface{}, seriously?
func (s *SubredditService) getPosts(ctx context.Context, sort string, subreddit string, opts interface{}) ([]*Post, *Response, error) {
path := sort
@ -492,7 +478,7 @@ func (s *SubredditService) SubmissionText(ctx context.Context, name string) (str
}
// Banned gets banned users from the subreddit.
func (s *SubredditService) Banned(ctx context.Context, subreddit string, opts *ListOptions) (*Bans, *Response, error) {
func (s *SubredditService) Banned(ctx context.Context, subreddit string, opts *ListOptions) ([]*Ban, *Response, error) {
path := fmt.Sprintf("r/%s/about/banned", subreddit)
path, err := addOptions(path, opts)
@ -517,17 +503,14 @@ func (s *SubredditService) Banned(ctx context.Context, subreddit string, opts *L
return nil, resp, err
}
bans := &Bans{
Bans: root.Data.Bans,
After: root.Data.After,
Before: root.Data.Before,
}
resp.After = root.Data.After
resp.Before = root.Data.Before
return bans, resp, nil
return root.Data.Bans, resp, nil
}
// Muted gets muted users from the subreddit.
func (s *SubredditService) Muted(ctx context.Context, subreddit string, opts *ListOptions) (*Relationships, *Response, error) {
func (s *SubredditService) Muted(ctx context.Context, subreddit string, opts *ListOptions) ([]*Relationship, *Response, error) {
path := fmt.Sprintf("r/%s/about/muted", subreddit)
path, err := addOptions(path, opts)
@ -552,17 +535,14 @@ func (s *SubredditService) Muted(ctx context.Context, subreddit string, opts *Li
return nil, resp, err
}
relationships := &Relationships{
Relationships: root.Data.Relationships,
After: root.Data.After,
Before: root.Data.Before,
}
resp.After = root.Data.After
resp.Before = root.Data.Before
return relationships, resp, nil
return root.Data.Relationships, resp, nil
}
// WikiBanned gets banned users from the subreddit.
func (s *SubredditService) WikiBanned(ctx context.Context, subreddit string, opts *ListOptions) (*Bans, *Response, error) {
func (s *SubredditService) WikiBanned(ctx context.Context, subreddit string, opts *ListOptions) ([]*Ban, *Response, error) {
path := fmt.Sprintf("r/%s/about/wikibanned", subreddit)
path, err := addOptions(path, opts)
@ -575,29 +555,26 @@ func (s *SubredditService) WikiBanned(ctx context.Context, subreddit string, opt
return nil, nil, err
}
var root struct {
root := new(struct {
Data struct {
Bans []*Ban `json:"children"`
After string `json:"after"`
Before string `json:"before"`
} `json:"data"`
}
resp, err := s.client.Do(ctx, req, &root)
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
bans := &Bans{
Bans: root.Data.Bans,
After: root.Data.After,
Before: root.Data.Before,
}
resp.After = root.Data.After
resp.Before = root.Data.Before
return bans, resp, nil
return root.Data.Bans, resp, nil
}
// Contributors gets contributors (also known as approved users) from the subreddit.
func (s *SubredditService) Contributors(ctx context.Context, subreddit string, opts *ListOptions) (*Relationships, *Response, error) {
func (s *SubredditService) Contributors(ctx context.Context, subreddit string, opts *ListOptions) ([]*Relationship, *Response, error) {
path := fmt.Sprintf("r/%s/about/contributors", subreddit)
path, err := addOptions(path, opts)
@ -622,17 +599,14 @@ func (s *SubredditService) Contributors(ctx context.Context, subreddit string, o
return nil, resp, err
}
relationships := &Relationships{
Relationships: root.Data.Relationships,
After: root.Data.After,
Before: root.Data.Before,
}
resp.After = root.Data.After
resp.Before = root.Data.Before
return relationships, resp, nil
return root.Data.Relationships, resp, nil
}
// WikiContributors gets contributors of the wiki from the subreddit.
func (s *SubredditService) WikiContributors(ctx context.Context, subreddit string, opts *ListOptions) (*Relationships, *Response, error) {
func (s *SubredditService) WikiContributors(ctx context.Context, subreddit string, opts *ListOptions) ([]*Relationship, *Response, error) {
path := fmt.Sprintf("r/%s/about/wikicontributors", subreddit)
path, err := addOptions(path, opts)
@ -657,13 +631,10 @@ func (s *SubredditService) WikiContributors(ctx context.Context, subreddit strin
return nil, resp, err
}
relationships := &Relationships{
Relationships: root.Data.Relationships,
After: root.Data.After,
Before: root.Data.Before,
}
resp.After = root.Data.After
resp.Before = root.Data.Before
return relationships, resp, nil
return root.Data.Relationships, resp, nil
}
// Moderators gets the moderators of the subreddit.

View File

@ -207,54 +207,46 @@ var expectedRandomSubreddit = &Subreddit{
Subscribers: 52357,
}
var expectedRelationships3 = &Relationships{
Relationships: []*Relationship{
{
ID: "rel_id1",
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 2, 0, time.UTC)},
User: "testuser1",
UserID: "t2_user1",
},
{
ID: "rel_id2",
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 0, 0, time.UTC)},
User: "testuser2",
UserID: "t2_user2",
},
var expectedRelationships3 = []*Relationship{
{
ID: "rel_id1",
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 2, 0, time.UTC)},
User: "testuser1",
UserID: "t2_user1",
},
{
ID: "rel_id2",
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 0, 0, time.UTC)},
User: "testuser2",
UserID: "t2_user2",
},
After: "",
Before: "",
}
var expectedBans = &Bans{
Bans: []*Ban{
{
Relationship: &Relationship{
ID: "rb_123",
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 2, 0, time.UTC)},
var expectedBans = []*Ban{
{
Relationship: &Relationship{
ID: "rb_123",
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 2, 0, time.UTC)},
User: "testuser1",
UserID: "t2_user1",
},
DaysLeft: Int(43),
Note: "Spam",
User: "testuser1",
UserID: "t2_user1",
},
{
Relationship: &Relationship{
ID: "rb_456",
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 0, 0, time.UTC)},
User: "testuser2",
UserID: "t2_user2",
},
DaysLeft: nil,
Note: "Spam",
},
DaysLeft: Int(43),
Note: "Spam",
},
{
Relationship: &Relationship{
ID: "rb_456",
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 0, 0, time.UTC)},
User: "testuser2",
UserID: "t2_user2",
},
DaysLeft: nil,
Note: "Spam",
},
After: "",
Before: "",
}
var expectedModerators = []*Moderator{