Skip to content

Commit acab042

Browse files
committed
Fixed the bug with mixed values in the chains.
From now on, all values will be queued. Signed-off-by: Sandy <thecsw@ku.edu>
1 parent afe9633 commit acab042

File tree

3 files changed

+99
-86
lines changed

3 files changed

+99
-86
lines changed

reddit.go

+79-62
Original file line numberDiff line numberDiff line change
@@ -41,86 +41,85 @@ func (c *Reddit) MiraRequest(method string, target string, payload map[string]st
4141
}
4242

4343
func (c *Reddit) Me() *Reddit {
44-
c.Chain.Name = c.Creds.Username
45-
c.Chain.Type = "me"
44+
c.addQueue(c.Creds.Username, "me")
4645
return c
4746
}
4847

4948
func (c *Reddit) Subreddit(name ...string) *Reddit {
50-
c.Chain.Name = strings.Join(name, "+")
51-
c.Chain.Type = "subreddit"
49+
c.addQueue(strings.Join(name, "+"), "subreddit")
5250
return c
5351
}
5452

5553
func (c *Reddit) Submission(name string) *Reddit {
56-
c.Chain.Name = name
57-
c.Chain.Type = "submission"
54+
c.addQueue(name, "submission")
5855
return c
5956
}
6057

6158
func (c *Reddit) Comment(name string) *Reddit {
62-
c.Chain.Name = name
63-
c.Chain.Type = "comment"
59+
c.addQueue(name, "comment")
6460
return c
6561
}
6662

6763
func (c *Reddit) Redditor(name string) *Reddit {
68-
c.Chain.Name = name
69-
c.Chain.Type = "redditor"
64+
c.addQueue(name, "redditor")
7065
return c
7166
}
7267

7368
func (c *Reddit) Submissions(sort string, tdur string, limit int) ([]models.PostListingChild, error) {
74-
switch c.Chain.Type {
69+
name, ttype := c.getQueue()
70+
switch ttype {
7571
case "subreddit":
76-
return c.getSubredditPosts(c.Chain.Name, sort, tdur, limit)
72+
return c.getSubredditPosts(name, sort, tdur, limit)
7773
case "redditor":
78-
return c.getRedditorPosts(c.Chain.Name, sort, tdur, limit)
74+
return c.getRedditorPosts(name, sort, tdur, limit)
7975
default:
80-
return nil, fmt.Errorf("'%s' type does not have an option for submissions", c.Chain.Type)
76+
return nil, fmt.Errorf("'%s' type does not have an option for submissions", ttype)
8177
}
8278
}
8379

8480
func (c *Reddit) SubmissionsAfter(last string, limit int) ([]models.PostListingChild, error) {
85-
switch c.Chain.Type {
81+
name, ttype := c.getQueue()
82+
switch ttype {
8683
case "subreddit":
87-
return c.getSubredditPostsAfter(c.Chain.Name, last, limit)
84+
return c.getSubredditPostsAfter(name, last, limit)
8885
case "redditor":
89-
return c.getRedditorPostsAfter(c.Chain.Name, last, limit)
86+
return c.getRedditorPostsAfter(name, last, limit)
9087
default:
91-
return nil, fmt.Errorf("'%s' type does not have an option for submissions", c.Chain.Type)
88+
return nil, fmt.Errorf("'%s' type does not have an option for submissions", ttype)
9289
}
9390
}
9491

9592
func (c *Reddit) Comments(sort string, tdur string, limit int) ([]models.Comment, error) {
96-
switch c.Chain.Type {
93+
name, ttype := c.getQueue()
94+
switch ttype {
9795
case "subreddit":
98-
return c.getSubredditComments(c.Chain.Name, sort, tdur, limit)
96+
return c.getSubredditComments(name, sort, tdur, limit)
9997
case "submission":
100-
comments, _, err := c.getSubmissionComments(c.Chain.Name, sort, tdur, limit)
98+
comments, _, err := c.getSubmissionComments(name, sort, tdur, limit)
10199
if err != nil {
102100
return nil, err
103101
}
104102
return comments, nil
105103
case "redditor":
106-
return c.getRedditorComments(c.Chain.Name, sort, tdur, limit)
104+
return c.getRedditorComments(name, sort, tdur, limit)
107105
default:
108-
return nil, fmt.Errorf("'%s' type does not have an option for comments", c.Chain.Type)
106+
return nil, fmt.Errorf("'%s' type does not have an option for comments", ttype)
109107
}
110108
}
111109

112110
func (c *Reddit) Info() (MiraInterface, error) {
113-
switch c.Chain.Type {
111+
name, ttype := c.getQueue()
112+
switch ttype {
114113
case "me":
115114
return c.getMe()
116115
case "submission":
117-
return c.getSubmission(c.Chain.Name)
116+
return c.getSubmission(name)
118117
case "comment":
119-
return c.getComment(c.Chain.Name)
118+
return c.getComment(name)
120119
case "subreddit":
121-
return c.getSubreddit(c.Chain.Name)
120+
return c.getSubreddit(name)
122121
case "redditor":
123-
return c.getUser(c.Chain.Name)
122+
return c.getUser(name)
124123
default:
125124
return nil, fmt.Errorf("returning type is not defined")
126125
}
@@ -183,11 +182,11 @@ func (c *Reddit) getComment(id string) (*models.Comment, error) {
183182
// NOTE: If any error occurs, the method will return on error object.
184183
// If it takes more than 12 calls, the function bails out.
185184
func (c *Reddit) Root() (string, error) {
186-
err := c.checkType("comment")
185+
name, _, err := c.checkType("comment")
187186
if err != nil {
188187
return "", err
189188
}
190-
current := c.Chain.Name
189+
current := name
191190
// Not a comment passed
192191
if string(current[1]) != "1" {
193192
return "", errors.New("the passed ID is not a comment")
@@ -356,13 +355,14 @@ func (c *Reddit) getSubredditPostsAfter(sr string, last string, limit int) ([]mo
356355
}
357356

358357
func (c *Reddit) CommentsAfter(sort string, last string, limit int) ([]models.Comment, error) {
359-
switch c.Chain.Type {
358+
name, ttype := c.getQueue()
359+
switch ttype {
360360
case "subreddit":
361-
return c.getSubredditCommentsAfter(c.Chain.Name, sort, last, limit)
361+
return c.getSubredditCommentsAfter(name, sort, last, limit)
362362
case "redditor":
363-
return c.getRedditorCommentsAfter(c.Chain.Name, sort, last, limit)
363+
return c.getRedditorCommentsAfter(name, sort, last, limit)
364364
default:
365-
return nil, fmt.Errorf("'%s' type does not have an option for comments", c.Chain.Type)
365+
return nil, fmt.Errorf("'%s' type does not have an option for comments", ttype)
366366
}
367367
}
368368

@@ -379,14 +379,14 @@ func (c *Reddit) getSubredditCommentsAfter(sr string, sort string, last string,
379379
}
380380

381381
func (c *Reddit) Submit(title string, text string) (*models.Submission, error) {
382-
err := c.checkType("subreddit")
382+
name, _, err := c.checkType("subreddit")
383383
if err != nil {
384384
return nil, err
385385
}
386386
target := RedditOauth + "/api/submit"
387387
ans, err := c.MiraRequest("POST", target, map[string]string{
388388
"title": title,
389-
"sr": c.Chain.Name,
389+
"sr": name,
390390
"text": text,
391391
"kind": "self",
392392
"resubmit": "true",
@@ -398,15 +398,15 @@ func (c *Reddit) Submit(title string, text string) (*models.Submission, error) {
398398
}
399399

400400
func (c *Reddit) Reply(text string) (*models.CommentWrap, error) {
401-
err := c.checkType("comment")
401+
name, _, err := c.checkType("comment")
402402
if err != nil {
403403
return nil, err
404404
}
405405

406406
target := RedditOauth + "/api/comment"
407407
ans, err := c.MiraRequest("POST", target, map[string]string{
408408
"text": text,
409-
"thing_id": c.Chain.Name,
409+
"thing_id": name,
410410
"api_type": "json",
411411
})
412412
ret := &models.CommentWrap{}
@@ -415,14 +415,14 @@ func (c *Reddit) Reply(text string) (*models.CommentWrap, error) {
415415
}
416416

417417
func (c *Reddit) Save(text string) (*models.CommentWrap, error) {
418-
err := c.checkType("submission")
418+
name, _, err := c.checkType("submission")
419419
if err != nil {
420420
return nil, err
421421
}
422422
target := RedditOauth + "/api/comment"
423423
ans, err := c.MiraRequest("POST", target, map[string]string{
424424
"text": text,
425-
"thing_id": c.Chain.Name,
425+
"thing_id": name,
426426
"api_type": "json",
427427
})
428428
ret := &models.CommentWrap{}
@@ -431,39 +431,39 @@ func (c *Reddit) Save(text string) (*models.CommentWrap, error) {
431431
}
432432

433433
func (c *Reddit) Delete() error {
434-
err := c.checkType("comment", "submission")
434+
name, _, err := c.checkType("comment", "submission")
435435
if err != nil {
436436
return err
437437
}
438438
target := RedditOauth + "/api/del"
439439
_, err = c.MiraRequest("POST", target, map[string]string{
440-
"id": c.Chain.Name,
440+
"id": name,
441441
"api_type": "json",
442442
})
443443
return err
444444
}
445445

446446
func (c *Reddit) Approve() error {
447-
err := c.checkType("comment")
447+
name, _, err := c.checkType("comment")
448448
if err != nil {
449449
return err
450450
}
451451
target := RedditOauth + "/api/approve"
452452
_, err = c.MiraRequest("POST", target, map[string]string{
453-
"id": c.Chain.Name,
453+
"id": name,
454454
"api_type": "json",
455455
})
456456
return err
457457
}
458458

459459
func (c *Reddit) Distinguish(how string, sticky bool) error {
460-
err := c.checkType("comment")
460+
name, _, err := c.checkType("comment")
461461
if err != nil {
462462
return err
463463
}
464464
target := RedditOauth + "/api/distinguish"
465465
_, err = c.MiraRequest("POST", target, map[string]string{
466-
"id": c.Chain.Name,
466+
"id": name,
467467
"how": how,
468468
"sticky": strconv.FormatBool(sticky),
469469
"api_type": "json",
@@ -472,14 +472,14 @@ func (c *Reddit) Distinguish(how string, sticky bool) error {
472472
}
473473

474474
func (c *Reddit) Edit(text string) (*models.CommentWrap, error) {
475-
err := c.checkType("comment", "submission")
475+
name, _, err := c.checkType("comment", "submission")
476476
if err != nil {
477477
return nil, err
478478
}
479479
target := RedditOauth + "/api/editusertext"
480480
ans, err := c.MiraRequest("POST", target, map[string]string{
481481
"text": text,
482-
"thing_id": c.Chain.Name,
482+
"thing_id": name,
483483
"api_type": "json",
484484
})
485485
ret := &models.CommentWrap{}
@@ -488,22 +488,22 @@ func (c *Reddit) Edit(text string) (*models.CommentWrap, error) {
488488
}
489489

490490
func (c *Reddit) Compose(subject, text string) error {
491-
err := c.checkType("redditor")
491+
name, _, err := c.checkType("redditor")
492492
if err != nil {
493493
return err
494494
}
495495
target := RedditOauth + "/api/compose"
496496
_, err = c.MiraRequest("POST", target, map[string]string{
497497
"subject": subject,
498498
"text": text,
499-
"to": c.Chain.Name,
499+
"to": name,
500500
"api_type": "json",
501501
})
502502
return err
503503
}
504504

505505
func (c *Reddit) ReadMessage(message_id string) error {
506-
err := c.checkType("me")
506+
_, _, err := c.checkType("me")
507507
if err != nil {
508508
return err
509509
}
@@ -515,7 +515,7 @@ func (c *Reddit) ReadMessage(message_id string) error {
515515
}
516516

517517
func (c *Reddit) ReadAllMessages() error {
518-
err := c.checkType("me")
518+
_, _, err := c.checkType("me")
519519
if err != nil {
520520
return err
521521
}
@@ -525,7 +525,7 @@ func (c *Reddit) ReadAllMessages() error {
525525
}
526526

527527
func (c *Reddit) ListUnreadMessages() ([]models.Comment, error) {
528-
err := c.checkType("me")
528+
_, _, err := c.checkType("me")
529529
if err != nil {
530530
return nil, err
531531
}
@@ -539,16 +539,16 @@ func (c *Reddit) ListUnreadMessages() ([]models.Comment, error) {
539539
}
540540

541541
func (c *Reddit) SubredditUpdateSidebar(text string) error {
542-
err := c.checkType("subreddit")
542+
name, _, err := c.checkType("subreddit")
543543
if err != nil {
544544
return err
545545
}
546546
target := RedditOauth + "/api/site_admin"
547547
_, err = c.MiraRequest("POST", target, map[string]string{
548-
"sr": c.Chain.Name,
548+
"sr": name,
549549
"name": "None",
550550
"description": text,
551-
"title": c.Chain.Name,
551+
"title": name,
552552
"wikimode": "anyone",
553553
"link_type": "any",
554554
"type": "public",
@@ -557,14 +557,31 @@ func (c *Reddit) SubredditUpdateSidebar(text string) error {
557557
return err
558558
}
559559

560-
func (c *Reddit) checkType(rtype ...string) error {
561-
if c.Chain.Name == "" {
562-
return fmt.Errorf("identifier is empty")
560+
func (c *Reddit) checkType(rtype ...string) (string, string, error) {
561+
name, ttype := c.getQueue()
562+
if name == "" {
563+
return "", "", fmt.Errorf("identifier is empty")
563564
}
564-
if !findElem(c.Chain.Type, rtype) {
565-
return fmt.Errorf("the passed type is not a valid type for this call | expected: %s", strings.Join(rtype, ", "))
565+
if !findElem(ttype, rtype) {
566+
return "", "", fmt.Errorf("the passed type is not a valid type for this call | expected: %s", strings.Join(rtype, ", "))
566567
}
567-
return nil
568+
return name, ttype, nil
569+
}
570+
571+
func (c *Reddit) addQueue(name string, ttype string) {
572+
c.Chain.Name = append(c.Chain.Name, name)
573+
c.Chain.Type = append(c.Chain.Type, ttype)
574+
}
575+
576+
func (c *Reddit) getQueue() (string, string) {
577+
if len(c.Chain.Name) < 1 || len(c.Chain.Type) < 1 {
578+
return "", ""
579+
}
580+
name := c.Chain.Name[0]
581+
c.Chain.Name = c.Chain.Name[1:]
582+
type_t := c.Chain.Type[0]
583+
c.Chain.Type = c.Chain.Type[1:]
584+
return name, type_t
568585
}
569586

570587
func findElem(elem string, arr []string) bool {

reddit_struct.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ type RedditVals struct {
2626
}
2727

2828
type ChainVals struct {
29-
Name string
30-
Type string
29+
Name []string
30+
Type []string
3131
}

0 commit comments

Comments
 (0)