@@ -41,86 +41,85 @@ func (c *Reddit) MiraRequest(method string, target string, payload map[string]st
41
41
}
42
42
43
43
func (c * Reddit ) Me () * Reddit {
44
- c .Chain .Name = c .Creds .Username
45
- c .Chain .Type = "me"
44
+ c .addQueue (c .Creds .Username , "me" )
46
45
return c
47
46
}
48
47
49
48
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" )
52
50
return c
53
51
}
54
52
55
53
func (c * Reddit ) Submission (name string ) * Reddit {
56
- c .Chain .Name = name
57
- c .Chain .Type = "submission"
54
+ c .addQueue (name , "submission" )
58
55
return c
59
56
}
60
57
61
58
func (c * Reddit ) Comment (name string ) * Reddit {
62
- c .Chain .Name = name
63
- c .Chain .Type = "comment"
59
+ c .addQueue (name , "comment" )
64
60
return c
65
61
}
66
62
67
63
func (c * Reddit ) Redditor (name string ) * Reddit {
68
- c .Chain .Name = name
69
- c .Chain .Type = "redditor"
64
+ c .addQueue (name , "redditor" )
70
65
return c
71
66
}
72
67
73
68
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 {
75
71
case "subreddit" :
76
- return c .getSubredditPosts (c . Chain . Name , sort , tdur , limit )
72
+ return c .getSubredditPosts (name , sort , tdur , limit )
77
73
case "redditor" :
78
- return c .getRedditorPosts (c . Chain . Name , sort , tdur , limit )
74
+ return c .getRedditorPosts (name , sort , tdur , limit )
79
75
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 )
81
77
}
82
78
}
83
79
84
80
func (c * Reddit ) SubmissionsAfter (last string , limit int ) ([]models.PostListingChild , error ) {
85
- switch c .Chain .Type {
81
+ name , ttype := c .getQueue ()
82
+ switch ttype {
86
83
case "subreddit" :
87
- return c .getSubredditPostsAfter (c . Chain . Name , last , limit )
84
+ return c .getSubredditPostsAfter (name , last , limit )
88
85
case "redditor" :
89
- return c .getRedditorPostsAfter (c . Chain . Name , last , limit )
86
+ return c .getRedditorPostsAfter (name , last , limit )
90
87
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 )
92
89
}
93
90
}
94
91
95
92
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 {
97
95
case "subreddit" :
98
- return c .getSubredditComments (c . Chain . Name , sort , tdur , limit )
96
+ return c .getSubredditComments (name , sort , tdur , limit )
99
97
case "submission" :
100
- comments , _ , err := c .getSubmissionComments (c . Chain . Name , sort , tdur , limit )
98
+ comments , _ , err := c .getSubmissionComments (name , sort , tdur , limit )
101
99
if err != nil {
102
100
return nil , err
103
101
}
104
102
return comments , nil
105
103
case "redditor" :
106
- return c .getRedditorComments (c . Chain . Name , sort , tdur , limit )
104
+ return c .getRedditorComments (name , sort , tdur , limit )
107
105
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 )
109
107
}
110
108
}
111
109
112
110
func (c * Reddit ) Info () (MiraInterface , error ) {
113
- switch c .Chain .Type {
111
+ name , ttype := c .getQueue ()
112
+ switch ttype {
114
113
case "me" :
115
114
return c .getMe ()
116
115
case "submission" :
117
- return c .getSubmission (c . Chain . Name )
116
+ return c .getSubmission (name )
118
117
case "comment" :
119
- return c .getComment (c . Chain . Name )
118
+ return c .getComment (name )
120
119
case "subreddit" :
121
- return c .getSubreddit (c . Chain . Name )
120
+ return c .getSubreddit (name )
122
121
case "redditor" :
123
- return c .getUser (c . Chain . Name )
122
+ return c .getUser (name )
124
123
default :
125
124
return nil , fmt .Errorf ("returning type is not defined" )
126
125
}
@@ -183,11 +182,11 @@ func (c *Reddit) getComment(id string) (*models.Comment, error) {
183
182
// NOTE: If any error occurs, the method will return on error object.
184
183
// If it takes more than 12 calls, the function bails out.
185
184
func (c * Reddit ) Root () (string , error ) {
186
- err := c .checkType ("comment" )
185
+ name , _ , err := c .checkType ("comment" )
187
186
if err != nil {
188
187
return "" , err
189
188
}
190
- current := c . Chain . Name
189
+ current := name
191
190
// Not a comment passed
192
191
if string (current [1 ]) != "1" {
193
192
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
356
355
}
357
356
358
357
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 {
360
360
case "subreddit" :
361
- return c .getSubredditCommentsAfter (c . Chain . Name , sort , last , limit )
361
+ return c .getSubredditCommentsAfter (name , sort , last , limit )
362
362
case "redditor" :
363
- return c .getRedditorCommentsAfter (c . Chain . Name , sort , last , limit )
363
+ return c .getRedditorCommentsAfter (name , sort , last , limit )
364
364
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 )
366
366
}
367
367
}
368
368
@@ -379,14 +379,14 @@ func (c *Reddit) getSubredditCommentsAfter(sr string, sort string, last string,
379
379
}
380
380
381
381
func (c * Reddit ) Submit (title string , text string ) (* models.Submission , error ) {
382
- err := c .checkType ("subreddit" )
382
+ name , _ , err := c .checkType ("subreddit" )
383
383
if err != nil {
384
384
return nil , err
385
385
}
386
386
target := RedditOauth + "/api/submit"
387
387
ans , err := c .MiraRequest ("POST" , target , map [string ]string {
388
388
"title" : title ,
389
- "sr" : c . Chain . Name ,
389
+ "sr" : name ,
390
390
"text" : text ,
391
391
"kind" : "self" ,
392
392
"resubmit" : "true" ,
@@ -398,15 +398,15 @@ func (c *Reddit) Submit(title string, text string) (*models.Submission, error) {
398
398
}
399
399
400
400
func (c * Reddit ) Reply (text string ) (* models.CommentWrap , error ) {
401
- err := c .checkType ("comment" )
401
+ name , _ , err := c .checkType ("comment" )
402
402
if err != nil {
403
403
return nil , err
404
404
}
405
405
406
406
target := RedditOauth + "/api/comment"
407
407
ans , err := c .MiraRequest ("POST" , target , map [string ]string {
408
408
"text" : text ,
409
- "thing_id" : c . Chain . Name ,
409
+ "thing_id" : name ,
410
410
"api_type" : "json" ,
411
411
})
412
412
ret := & models.CommentWrap {}
@@ -415,14 +415,14 @@ func (c *Reddit) Reply(text string) (*models.CommentWrap, error) {
415
415
}
416
416
417
417
func (c * Reddit ) Save (text string ) (* models.CommentWrap , error ) {
418
- err := c .checkType ("submission" )
418
+ name , _ , err := c .checkType ("submission" )
419
419
if err != nil {
420
420
return nil , err
421
421
}
422
422
target := RedditOauth + "/api/comment"
423
423
ans , err := c .MiraRequest ("POST" , target , map [string ]string {
424
424
"text" : text ,
425
- "thing_id" : c . Chain . Name ,
425
+ "thing_id" : name ,
426
426
"api_type" : "json" ,
427
427
})
428
428
ret := & models.CommentWrap {}
@@ -431,39 +431,39 @@ func (c *Reddit) Save(text string) (*models.CommentWrap, error) {
431
431
}
432
432
433
433
func (c * Reddit ) Delete () error {
434
- err := c .checkType ("comment" , "submission" )
434
+ name , _ , err := c .checkType ("comment" , "submission" )
435
435
if err != nil {
436
436
return err
437
437
}
438
438
target := RedditOauth + "/api/del"
439
439
_ , err = c .MiraRequest ("POST" , target , map [string ]string {
440
- "id" : c . Chain . Name ,
440
+ "id" : name ,
441
441
"api_type" : "json" ,
442
442
})
443
443
return err
444
444
}
445
445
446
446
func (c * Reddit ) Approve () error {
447
- err := c .checkType ("comment" )
447
+ name , _ , err := c .checkType ("comment" )
448
448
if err != nil {
449
449
return err
450
450
}
451
451
target := RedditOauth + "/api/approve"
452
452
_ , err = c .MiraRequest ("POST" , target , map [string ]string {
453
- "id" : c . Chain . Name ,
453
+ "id" : name ,
454
454
"api_type" : "json" ,
455
455
})
456
456
return err
457
457
}
458
458
459
459
func (c * Reddit ) Distinguish (how string , sticky bool ) error {
460
- err := c .checkType ("comment" )
460
+ name , _ , err := c .checkType ("comment" )
461
461
if err != nil {
462
462
return err
463
463
}
464
464
target := RedditOauth + "/api/distinguish"
465
465
_ , err = c .MiraRequest ("POST" , target , map [string ]string {
466
- "id" : c . Chain . Name ,
466
+ "id" : name ,
467
467
"how" : how ,
468
468
"sticky" : strconv .FormatBool (sticky ),
469
469
"api_type" : "json" ,
@@ -472,14 +472,14 @@ func (c *Reddit) Distinguish(how string, sticky bool) error {
472
472
}
473
473
474
474
func (c * Reddit ) Edit (text string ) (* models.CommentWrap , error ) {
475
- err := c .checkType ("comment" , "submission" )
475
+ name , _ , err := c .checkType ("comment" , "submission" )
476
476
if err != nil {
477
477
return nil , err
478
478
}
479
479
target := RedditOauth + "/api/editusertext"
480
480
ans , err := c .MiraRequest ("POST" , target , map [string ]string {
481
481
"text" : text ,
482
- "thing_id" : c . Chain . Name ,
482
+ "thing_id" : name ,
483
483
"api_type" : "json" ,
484
484
})
485
485
ret := & models.CommentWrap {}
@@ -488,22 +488,22 @@ func (c *Reddit) Edit(text string) (*models.CommentWrap, error) {
488
488
}
489
489
490
490
func (c * Reddit ) Compose (subject , text string ) error {
491
- err := c .checkType ("redditor" )
491
+ name , _ , err := c .checkType ("redditor" )
492
492
if err != nil {
493
493
return err
494
494
}
495
495
target := RedditOauth + "/api/compose"
496
496
_ , err = c .MiraRequest ("POST" , target , map [string ]string {
497
497
"subject" : subject ,
498
498
"text" : text ,
499
- "to" : c . Chain . Name ,
499
+ "to" : name ,
500
500
"api_type" : "json" ,
501
501
})
502
502
return err
503
503
}
504
504
505
505
func (c * Reddit ) ReadMessage (message_id string ) error {
506
- err := c .checkType ("me" )
506
+ _ , _ , err := c .checkType ("me" )
507
507
if err != nil {
508
508
return err
509
509
}
@@ -515,7 +515,7 @@ func (c *Reddit) ReadMessage(message_id string) error {
515
515
}
516
516
517
517
func (c * Reddit ) ReadAllMessages () error {
518
- err := c .checkType ("me" )
518
+ _ , _ , err := c .checkType ("me" )
519
519
if err != nil {
520
520
return err
521
521
}
@@ -525,7 +525,7 @@ func (c *Reddit) ReadAllMessages() error {
525
525
}
526
526
527
527
func (c * Reddit ) ListUnreadMessages () ([]models.Comment , error ) {
528
- err := c .checkType ("me" )
528
+ _ , _ , err := c .checkType ("me" )
529
529
if err != nil {
530
530
return nil , err
531
531
}
@@ -539,16 +539,16 @@ func (c *Reddit) ListUnreadMessages() ([]models.Comment, error) {
539
539
}
540
540
541
541
func (c * Reddit ) SubredditUpdateSidebar (text string ) error {
542
- err := c .checkType ("subreddit" )
542
+ name , _ , err := c .checkType ("subreddit" )
543
543
if err != nil {
544
544
return err
545
545
}
546
546
target := RedditOauth + "/api/site_admin"
547
547
_ , err = c .MiraRequest ("POST" , target , map [string ]string {
548
- "sr" : c . Chain . Name ,
548
+ "sr" : name ,
549
549
"name" : "None" ,
550
550
"description" : text ,
551
- "title" : c . Chain . Name ,
551
+ "title" : name ,
552
552
"wikimode" : "anyone" ,
553
553
"link_type" : "any" ,
554
554
"type" : "public" ,
@@ -557,14 +557,31 @@ func (c *Reddit) SubredditUpdateSidebar(text string) error {
557
557
return err
558
558
}
559
559
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" )
563
564
}
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 , ", " ))
566
567
}
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
568
585
}
569
586
570
587
func findElem (elem string , arr []string ) bool {
0 commit comments