Return lists for bans, wikibans, messages, etc.
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
5349d53626
commit
b4feadda4f
4 changed files with 121 additions and 162 deletions
|
@ -35,22 +35,42 @@ type Message struct {
|
||||||
IsComment bool `json:"was_comment"`
|
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 {
|
type inboxListing struct {
|
||||||
Things inboxThings `json:"children"`
|
inboxThings
|
||||||
After string `json:"after"`
|
after string
|
||||||
Before string `json:"before"`
|
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.
|
// The returned JSON for comments is a bit different.
|
||||||
|
@ -93,22 +113,6 @@ func (t *inboxThings) UnmarshalJSON(b []byte) error {
|
||||||
return nil
|
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.
|
// SendMessageRequest represents a request to send a message.
|
||||||
type SendMessageRequest struct {
|
type SendMessageRequest struct {
|
||||||
// Username, or /r/name for that subreddit's moderators.
|
// 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.
|
// 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)
|
root, resp, err := s.inbox(ctx, "message/inbox", opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, resp, err
|
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.
|
// 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)
|
root, resp, err := s.inbox(ctx, "message/unread", opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, resp, err
|
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.
|
// 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)
|
root, resp, err := s.inbox(ctx, "message/sent", opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, resp, err
|
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)
|
path, err := addOptions(path, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
@ -299,7 +303,7 @@ func (s *MessageService) inbox(ctx context.Context, path string, opts *ListOptio
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
root := new(rootInboxListing)
|
root := new(inboxListing)
|
||||||
resp, err := s.client.Do(ctx, req, root)
|
resp, err := s.client.Do(ctx, req, root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|
|
@ -10,46 +10,38 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
var expectedCommentMessages = &Messages{
|
var expectedCommentMessages = []*Message{
|
||||||
Messages: []*Message{
|
{
|
||||||
{
|
ID: "g1xi2m9",
|
||||||
ID: "g1xi2m9",
|
FullID: "t1_g1xi2m9",
|
||||||
FullID: "t1_g1xi2m9",
|
Created: &Timestamp{time.Date(2020, 8, 18, 0, 24, 13, 0, time.UTC)},
|
||||||
Created: &Timestamp{time.Date(2020, 8, 18, 0, 24, 13, 0, time.UTC)},
|
|
||||||
|
|
||||||
Subject: "post reply",
|
Subject: "post reply",
|
||||||
Text: "u/testuser2 hello",
|
Text: "u/testuser2 hello",
|
||||||
ParentID: "t3_hs03f3",
|
ParentID: "t3_hs03f3",
|
||||||
|
|
||||||
Author: "testuser1",
|
Author: "testuser1",
|
||||||
To: "testuser2",
|
To: "testuser2",
|
||||||
|
|
||||||
IsComment: true,
|
IsComment: true,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
After: "",
|
|
||||||
Before: "",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var expectedMessages = &Messages{
|
var expectedMessages = []*Message{
|
||||||
Messages: []*Message{
|
{
|
||||||
{
|
ID: "qwki97",
|
||||||
ID: "qwki97",
|
FullID: "t4_qwki97",
|
||||||
FullID: "t4_qwki97",
|
Created: &Timestamp{time.Date(2020, 8, 18, 0, 16, 53, 0, time.UTC)},
|
||||||
Created: &Timestamp{time.Date(2020, 8, 18, 0, 16, 53, 0, time.UTC)},
|
|
||||||
|
|
||||||
Subject: "re: test",
|
Subject: "re: test",
|
||||||
Text: "test",
|
Text: "test",
|
||||||
ParentID: "t4_qwki4m",
|
ParentID: "t4_qwki4m",
|
||||||
|
|
||||||
Author: "testuser1",
|
Author: "testuser1",
|
||||||
To: "testuser2",
|
To: "testuser2",
|
||||||
|
|
||||||
IsComment: false,
|
IsComment: false,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
After: "",
|
|
||||||
Before: "",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMessageService_ReadAll(t *testing.T) {
|
func TestMessageService_ReadAll(t *testing.T) {
|
||||||
|
|
|
@ -34,13 +34,6 @@ type Relationship struct {
|
||||||
Created *Timestamp `json:"date,omitempty"`
|
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.
|
// Moderator is a user who moderates a subreddit.
|
||||||
type Moderator struct {
|
type Moderator struct {
|
||||||
*Relationship
|
*Relationship
|
||||||
|
@ -55,13 +48,6 @@ type Ban struct {
|
||||||
Note string `json:"note,omitempty"`
|
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?
|
// todo: interface{}, seriously?
|
||||||
func (s *SubredditService) getPosts(ctx context.Context, sort string, subreddit string, opts interface{}) ([]*Post, *Response, error) {
|
func (s *SubredditService) getPosts(ctx context.Context, sort string, subreddit string, opts interface{}) ([]*Post, *Response, error) {
|
||||||
path := sort
|
path := sort
|
||||||
|
@ -492,7 +478,7 @@ func (s *SubredditService) SubmissionText(ctx context.Context, name string) (str
|
||||||
}
|
}
|
||||||
|
|
||||||
// Banned gets banned users from the subreddit.
|
// 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 := fmt.Sprintf("r/%s/about/banned", subreddit)
|
||||||
|
|
||||||
path, err := addOptions(path, opts)
|
path, err := addOptions(path, opts)
|
||||||
|
@ -517,17 +503,14 @@ func (s *SubredditService) Banned(ctx context.Context, subreddit string, opts *L
|
||||||
return nil, resp, err
|
return nil, resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
bans := &Bans{
|
resp.After = root.Data.After
|
||||||
Bans: root.Data.Bans,
|
resp.Before = root.Data.Before
|
||||||
After: root.Data.After,
|
|
||||||
Before: root.Data.Before,
|
|
||||||
}
|
|
||||||
|
|
||||||
return bans, resp, nil
|
return root.Data.Bans, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Muted gets muted users from the subreddit.
|
// 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 := fmt.Sprintf("r/%s/about/muted", subreddit)
|
||||||
|
|
||||||
path, err := addOptions(path, opts)
|
path, err := addOptions(path, opts)
|
||||||
|
@ -552,17 +535,14 @@ func (s *SubredditService) Muted(ctx context.Context, subreddit string, opts *Li
|
||||||
return nil, resp, err
|
return nil, resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
relationships := &Relationships{
|
resp.After = root.Data.After
|
||||||
Relationships: root.Data.Relationships,
|
resp.Before = root.Data.Before
|
||||||
After: root.Data.After,
|
|
||||||
Before: root.Data.Before,
|
|
||||||
}
|
|
||||||
|
|
||||||
return relationships, resp, nil
|
return root.Data.Relationships, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WikiBanned gets banned users from the subreddit.
|
// 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 := fmt.Sprintf("r/%s/about/wikibanned", subreddit)
|
||||||
|
|
||||||
path, err := addOptions(path, opts)
|
path, err := addOptions(path, opts)
|
||||||
|
@ -575,29 +555,26 @@ func (s *SubredditService) WikiBanned(ctx context.Context, subreddit string, opt
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var root struct {
|
root := new(struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
Bans []*Ban `json:"children"`
|
Bans []*Ban `json:"children"`
|
||||||
After string `json:"after"`
|
After string `json:"after"`
|
||||||
Before string `json:"before"`
|
Before string `json:"before"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
})
|
||||||
resp, err := s.client.Do(ctx, req, &root)
|
resp, err := s.client.Do(ctx, req, root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, resp, err
|
return nil, resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
bans := &Bans{
|
resp.After = root.Data.After
|
||||||
Bans: root.Data.Bans,
|
resp.Before = root.Data.Before
|
||||||
After: root.Data.After,
|
|
||||||
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.
|
// 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 := fmt.Sprintf("r/%s/about/contributors", subreddit)
|
||||||
|
|
||||||
path, err := addOptions(path, opts)
|
path, err := addOptions(path, opts)
|
||||||
|
@ -622,17 +599,14 @@ func (s *SubredditService) Contributors(ctx context.Context, subreddit string, o
|
||||||
return nil, resp, err
|
return nil, resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
relationships := &Relationships{
|
resp.After = root.Data.After
|
||||||
Relationships: root.Data.Relationships,
|
resp.Before = root.Data.Before
|
||||||
After: root.Data.After,
|
|
||||||
Before: root.Data.Before,
|
|
||||||
}
|
|
||||||
|
|
||||||
return relationships, resp, nil
|
return root.Data.Relationships, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WikiContributors gets contributors of the wiki from the subreddit.
|
// 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 := fmt.Sprintf("r/%s/about/wikicontributors", subreddit)
|
||||||
|
|
||||||
path, err := addOptions(path, opts)
|
path, err := addOptions(path, opts)
|
||||||
|
@ -657,13 +631,10 @@ func (s *SubredditService) WikiContributors(ctx context.Context, subreddit strin
|
||||||
return nil, resp, err
|
return nil, resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
relationships := &Relationships{
|
resp.After = root.Data.After
|
||||||
Relationships: root.Data.Relationships,
|
resp.Before = root.Data.Before
|
||||||
After: root.Data.After,
|
|
||||||
Before: root.Data.Before,
|
|
||||||
}
|
|
||||||
|
|
||||||
return relationships, resp, nil
|
return root.Data.Relationships, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Moderators gets the moderators of the subreddit.
|
// Moderators gets the moderators of the subreddit.
|
||||||
|
|
|
@ -207,54 +207,46 @@ var expectedRandomSubreddit = &Subreddit{
|
||||||
Subscribers: 52357,
|
Subscribers: 52357,
|
||||||
}
|
}
|
||||||
|
|
||||||
var expectedRelationships3 = &Relationships{
|
var expectedRelationships3 = []*Relationship{
|
||||||
Relationships: []*Relationship{
|
{
|
||||||
{
|
ID: "rel_id1",
|
||||||
ID: "rel_id1",
|
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 2, 0, time.UTC)},
|
||||||
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 2, 0, time.UTC)},
|
User: "testuser1",
|
||||||
User: "testuser1",
|
UserID: "t2_user1",
|
||||||
UserID: "t2_user1",
|
},
|
||||||
},
|
{
|
||||||
{
|
ID: "rel_id2",
|
||||||
ID: "rel_id2",
|
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 0, 0, time.UTC)},
|
||||||
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 0, 0, time.UTC)},
|
User: "testuser2",
|
||||||
User: "testuser2",
|
UserID: "t2_user2",
|
||||||
UserID: "t2_user2",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
After: "",
|
|
||||||
Before: "",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var expectedBans = &Bans{
|
var expectedBans = []*Ban{
|
||||||
Bans: []*Ban{
|
{
|
||||||
{
|
Relationship: &Relationship{
|
||||||
Relationship: &Relationship{
|
ID: "rb_123",
|
||||||
ID: "rb_123",
|
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 2, 0, time.UTC)},
|
||||||
Created: &Timestamp{time.Date(2020, 8, 11, 2, 35, 2, 0, time.UTC)},
|
|
||||||
|
|
||||||
User: "testuser1",
|
User: "testuser1",
|
||||||
UserID: "t2_user1",
|
UserID: "t2_user1",
|
||||||
},
|
|
||||||
|
|
||||||
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",
|
DaysLeft: Int(43),
|
||||||
UserID: "t2_user2",
|
Note: "Spam",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
DaysLeft: nil,
|
Relationship: &Relationship{
|
||||||
Note: "Spam",
|
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{
|
var expectedModerators = []*Moderator{
|
||||||
|
|
Loading…
Reference in a new issue