-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.go
More file actions
742 lines (638 loc) · 29 KB
/
users.go
File metadata and controls
742 lines (638 loc) · 29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
package forge
import (
"context"
"iter"
"net/http"
"net/url"
"strconv"
core "dappco.re/go/core"
"dappco.re/go/core/forge/types"
)
// UserService handles user operations.
//
// Usage:
//
// f := forge.NewForge("https://forge.lthn.ai", "token")
// _, err := f.Users.GetCurrent(ctx)
type UserService struct {
Resource[types.User, struct{}, struct{}]
}
// UserSearchOptions controls filtering for user searches.
//
// Usage:
//
// opts := forge.UserSearchOptions{UID: 1001}
type UserSearchOptions struct {
UID int64
}
// String returns a safe summary of the user search filters.
func (o UserSearchOptions) String() string {
return optionString("forge.UserSearchOptions", "uid", o.UID)
}
// GoString returns a safe Go-syntax summary of the user search filters.
func (o UserSearchOptions) GoString() string { return o.String() }
func (o UserSearchOptions) queryParams() map[string]string {
if o.UID == 0 {
return nil
}
return map[string]string{
"uid": strconv.FormatInt(o.UID, 10),
}
}
// UserKeyListOptions controls filtering for authenticated user public key listings.
//
// Usage:
//
// opts := forge.UserKeyListOptions{Fingerprint: "AB:CD"}
type UserKeyListOptions struct {
Fingerprint string
}
// String returns a safe summary of the user key filters.
func (o UserKeyListOptions) String() string {
return optionString("forge.UserKeyListOptions", "fingerprint", o.Fingerprint)
}
// GoString returns a safe Go-syntax summary of the user key filters.
func (o UserKeyListOptions) GoString() string { return o.String() }
func (o UserKeyListOptions) queryParams() map[string]string {
if o.Fingerprint == "" {
return nil
}
return map[string]string{
"fingerprint": o.Fingerprint,
}
}
type userSearchResults struct {
Data []*types.User `json:"data,omitempty"`
OK bool `json:"ok,omitempty"`
}
func newUserService(c *Client) *UserService {
return &UserService{
Resource: *NewResource[types.User, struct{}, struct{}](
c, "/api/v1/users/{username}",
),
}
}
// GetCurrent returns the authenticated user.
func (s *UserService) GetCurrent(ctx context.Context) (*types.User, error) {
var out types.User
if err := s.client.Get(ctx, "/api/v1/user", &out); err != nil {
return nil, err
}
return &out, nil
}
// GetSettings returns the authenticated user's settings.
func (s *UserService) GetSettings(ctx context.Context) (*types.UserSettings, error) {
var out types.UserSettings
if err := s.client.Get(ctx, "/api/v1/user/settings", &out); err != nil {
return nil, err
}
return &out, nil
}
// UpdateSettings updates the authenticated user's settings.
func (s *UserService) UpdateSettings(ctx context.Context, opts *types.UserSettingsOptions) (*types.UserSettings, error) {
var out types.UserSettings
if err := s.client.Patch(ctx, "/api/v1/user/settings", opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// GetQuota returns the authenticated user's quota information.
func (s *UserService) GetQuota(ctx context.Context) (*types.QuotaInfo, error) {
var out types.QuotaInfo
if err := s.client.Get(ctx, "/api/v1/user/quota", &out); err != nil {
return nil, err
}
return &out, nil
}
// SearchUsersPage returns a single page of users matching the search filters.
func (s *UserService) SearchUsersPage(ctx context.Context, query string, pageOpts ListOptions, filters ...UserSearchOptions) (*PagedResult[types.User], error) {
if pageOpts.Page < 1 {
pageOpts.Page = 1
}
if pageOpts.Limit < 1 {
pageOpts.Limit = 50
}
u, err := url.Parse("/api/v1/users/search")
if err != nil {
return nil, core.E("UserService.SearchUsersPage", "forge: parse path", err)
}
q := u.Query()
q.Set("q", query)
for _, filter := range filters {
for key, value := range filter.queryParams() {
q.Set(key, value)
}
}
q.Set("page", strconv.Itoa(pageOpts.Page))
q.Set("limit", strconv.Itoa(pageOpts.Limit))
u.RawQuery = q.Encode()
var out userSearchResults
resp, err := s.client.doJSON(ctx, http.MethodGet, u.String(), nil, &out)
if err != nil {
return nil, err
}
totalCount, _ := strconv.Atoi(resp.Header.Get("X-Total-Count"))
items := make([]types.User, 0, len(out.Data))
for _, user := range out.Data {
if user != nil {
items = append(items, *user)
}
}
return &PagedResult[types.User]{
Items: items,
TotalCount: totalCount,
Page: pageOpts.Page,
HasMore: (totalCount > 0 && (pageOpts.Page-1)*pageOpts.Limit+len(items) < totalCount) ||
(totalCount == 0 && len(items) >= pageOpts.Limit),
}, nil
}
// SearchUsers returns all users matching the search filters.
func (s *UserService) SearchUsers(ctx context.Context, query string, filters ...UserSearchOptions) ([]types.User, error) {
var all []types.User
page := 1
for {
result, err := s.SearchUsersPage(ctx, query, ListOptions{Page: page, Limit: 50}, filters...)
if err != nil {
return nil, err
}
all = append(all, result.Items...)
if !result.HasMore {
break
}
page++
}
return all, nil
}
// IterSearchUsers returns an iterator over users matching the search filters.
func (s *UserService) IterSearchUsers(ctx context.Context, query string, filters ...UserSearchOptions) iter.Seq2[types.User, error] {
return func(yield func(types.User, error) bool) {
page := 1
for {
result, err := s.SearchUsersPage(ctx, query, ListOptions{Page: page, Limit: 50}, filters...)
if err != nil {
yield(*new(types.User), err)
return
}
for _, item := range result.Items {
if !yield(item, nil) {
return
}
}
if !result.HasMore {
break
}
page++
}
}
}
// ListQuotaArtifacts returns all artifacts affecting the authenticated user's quota.
func (s *UserService) ListQuotaArtifacts(ctx context.Context) ([]types.QuotaUsedArtifact, error) {
return ListAll[types.QuotaUsedArtifact](ctx, s.client, "/api/v1/user/quota/artifacts", nil)
}
// IterQuotaArtifacts returns an iterator over all artifacts affecting the authenticated user's quota.
func (s *UserService) IterQuotaArtifacts(ctx context.Context) iter.Seq2[types.QuotaUsedArtifact, error] {
return ListIter[types.QuotaUsedArtifact](ctx, s.client, "/api/v1/user/quota/artifacts", nil)
}
// ListQuotaAttachments returns all attachments affecting the authenticated user's quota.
func (s *UserService) ListQuotaAttachments(ctx context.Context) ([]types.QuotaUsedAttachment, error) {
return ListAll[types.QuotaUsedAttachment](ctx, s.client, "/api/v1/user/quota/attachments", nil)
}
// IterQuotaAttachments returns an iterator over all attachments affecting the authenticated user's quota.
func (s *UserService) IterQuotaAttachments(ctx context.Context) iter.Seq2[types.QuotaUsedAttachment, error] {
return ListIter[types.QuotaUsedAttachment](ctx, s.client, "/api/v1/user/quota/attachments", nil)
}
// ListQuotaPackages returns all packages affecting the authenticated user's quota.
func (s *UserService) ListQuotaPackages(ctx context.Context) ([]types.QuotaUsedPackage, error) {
return ListAll[types.QuotaUsedPackage](ctx, s.client, "/api/v1/user/quota/packages", nil)
}
// IterQuotaPackages returns an iterator over all packages affecting the authenticated user's quota.
func (s *UserService) IterQuotaPackages(ctx context.Context) iter.Seq2[types.QuotaUsedPackage, error] {
return ListIter[types.QuotaUsedPackage](ctx, s.client, "/api/v1/user/quota/packages", nil)
}
// ListEmails returns all email addresses for the authenticated user.
func (s *UserService) ListEmails(ctx context.Context) ([]types.Email, error) {
return ListAll[types.Email](ctx, s.client, "/api/v1/user/emails", nil)
}
// IterEmails returns an iterator over all email addresses for the authenticated user.
func (s *UserService) IterEmails(ctx context.Context) iter.Seq2[types.Email, error] {
return ListIter[types.Email](ctx, s.client, "/api/v1/user/emails", nil)
}
// AddEmails adds email addresses for the authenticated user.
func (s *UserService) AddEmails(ctx context.Context, emails ...string) ([]types.Email, error) {
var out []types.Email
if err := s.client.Post(ctx, "/api/v1/user/emails", types.CreateEmailOption{Emails: emails}, &out); err != nil {
return nil, err
}
return out, nil
}
// DeleteEmails deletes email addresses for the authenticated user.
func (s *UserService) DeleteEmails(ctx context.Context, emails ...string) error {
return s.client.DeleteWithBody(ctx, "/api/v1/user/emails", types.DeleteEmailOption{Emails: emails})
}
// UpdateAvatar updates the authenticated user's avatar.
func (s *UserService) UpdateAvatar(ctx context.Context, opts *types.UpdateUserAvatarOption) error {
return s.client.Post(ctx, "/api/v1/user/avatar", opts, nil)
}
// DeleteAvatar deletes the authenticated user's avatar.
func (s *UserService) DeleteAvatar(ctx context.Context) error {
return s.client.Delete(ctx, "/api/v1/user/avatar")
}
// ListKeys returns all public keys owned by the authenticated user.
func (s *UserService) ListKeys(ctx context.Context, filters ...UserKeyListOptions) ([]types.PublicKey, error) {
query := make(map[string]string, len(filters))
for _, filter := range filters {
for key, value := range filter.queryParams() {
query[key] = value
}
}
if len(query) == 0 {
query = nil
}
return ListAll[types.PublicKey](ctx, s.client, "/api/v1/user/keys", query)
}
// IterKeys returns an iterator over all public keys owned by the authenticated user.
func (s *UserService) IterKeys(ctx context.Context, filters ...UserKeyListOptions) iter.Seq2[types.PublicKey, error] {
query := make(map[string]string, len(filters))
for _, filter := range filters {
for key, value := range filter.queryParams() {
query[key] = value
}
}
if len(query) == 0 {
query = nil
}
return ListIter[types.PublicKey](ctx, s.client, "/api/v1/user/keys", query)
}
// CreateKey creates a public key for the authenticated user.
func (s *UserService) CreateKey(ctx context.Context, opts *types.CreateKeyOption) (*types.PublicKey, error) {
var out types.PublicKey
if err := s.client.Post(ctx, "/api/v1/user/keys", opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// GetKey returns a single public key owned by the authenticated user.
func (s *UserService) GetKey(ctx context.Context, id int64) (*types.PublicKey, error) {
path := ResolvePath("/api/v1/user/keys/{id}", pathParams("id", int64String(id)))
var out types.PublicKey
if err := s.client.Get(ctx, path, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteKey removes a public key owned by the authenticated user.
func (s *UserService) DeleteKey(ctx context.Context, id int64) error {
path := ResolvePath("/api/v1/user/keys/{id}", pathParams("id", int64String(id)))
return s.client.Delete(ctx, path)
}
// ListUserKeys returns all public keys for a user.
func (s *UserService) ListUserKeys(ctx context.Context, username string, filters ...UserKeyListOptions) ([]types.PublicKey, error) {
path := ResolvePath("/api/v1/users/{username}/keys", pathParams("username", username))
query := make(map[string]string, len(filters))
for _, filter := range filters {
for key, value := range filter.queryParams() {
query[key] = value
}
}
if len(query) == 0 {
query = nil
}
return ListAll[types.PublicKey](ctx, s.client, path, query)
}
// IterUserKeys returns an iterator over all public keys for a user.
func (s *UserService) IterUserKeys(ctx context.Context, username string, filters ...UserKeyListOptions) iter.Seq2[types.PublicKey, error] {
path := ResolvePath("/api/v1/users/{username}/keys", pathParams("username", username))
query := make(map[string]string, len(filters))
for _, filter := range filters {
for key, value := range filter.queryParams() {
query[key] = value
}
}
if len(query) == 0 {
query = nil
}
return ListIter[types.PublicKey](ctx, s.client, path, query)
}
// ListGPGKeys returns all GPG keys owned by the authenticated user.
func (s *UserService) ListGPGKeys(ctx context.Context) ([]types.GPGKey, error) {
return ListAll[types.GPGKey](ctx, s.client, "/api/v1/user/gpg_keys", nil)
}
// IterGPGKeys returns an iterator over all GPG keys owned by the authenticated user.
func (s *UserService) IterGPGKeys(ctx context.Context) iter.Seq2[types.GPGKey, error] {
return ListIter[types.GPGKey](ctx, s.client, "/api/v1/user/gpg_keys", nil)
}
// CreateGPGKey adds a GPG key for the authenticated user.
func (s *UserService) CreateGPGKey(ctx context.Context, opts *types.CreateGPGKeyOption) (*types.GPGKey, error) {
var out types.GPGKey
if err := s.client.Post(ctx, "/api/v1/user/gpg_keys", opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// GetGPGKey returns a single GPG key owned by the authenticated user.
func (s *UserService) GetGPGKey(ctx context.Context, id int64) (*types.GPGKey, error) {
path := ResolvePath("/api/v1/user/gpg_keys/{id}", pathParams("id", int64String(id)))
var out types.GPGKey
if err := s.client.Get(ctx, path, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteGPGKey removes a GPG key owned by the authenticated user.
func (s *UserService) DeleteGPGKey(ctx context.Context, id int64) error {
path := ResolvePath("/api/v1/user/gpg_keys/{id}", pathParams("id", int64String(id)))
return s.client.Delete(ctx, path)
}
// ListUserGPGKeys returns all GPG keys for a user.
func (s *UserService) ListUserGPGKeys(ctx context.Context, username string) ([]types.GPGKey, error) {
path := ResolvePath("/api/v1/users/{username}/gpg_keys", pathParams("username", username))
return ListAll[types.GPGKey](ctx, s.client, path, nil)
}
// IterUserGPGKeys returns an iterator over all GPG keys for a user.
func (s *UserService) IterUserGPGKeys(ctx context.Context, username string) iter.Seq2[types.GPGKey, error] {
path := ResolvePath("/api/v1/users/{username}/gpg_keys", pathParams("username", username))
return ListIter[types.GPGKey](ctx, s.client, path, nil)
}
// GetGPGKeyVerificationToken returns the token used to verify a GPG key.
func (s *UserService) GetGPGKeyVerificationToken(ctx context.Context) (string, error) {
data, err := s.client.GetRaw(ctx, "/api/v1/user/gpg_key_token")
if err != nil {
return "", err
}
return string(data), nil
}
// VerifyGPGKey verifies a GPG key for the authenticated user.
func (s *UserService) VerifyGPGKey(ctx context.Context) (*types.GPGKey, error) {
var out types.GPGKey
if err := s.client.Post(ctx, "/api/v1/user/gpg_key_verify", nil, &out); err != nil {
return nil, err
}
return &out, nil
}
// ListTokens returns all access tokens for a user.
func (s *UserService) ListTokens(ctx context.Context, username string) ([]types.AccessToken, error) {
path := ResolvePath("/api/v1/users/{username}/tokens", pathParams("username", username))
return ListAll[types.AccessToken](ctx, s.client, path, nil)
}
// IterTokens returns an iterator over all access tokens for a user.
func (s *UserService) IterTokens(ctx context.Context, username string) iter.Seq2[types.AccessToken, error] {
path := ResolvePath("/api/v1/users/{username}/tokens", pathParams("username", username))
return ListIter[types.AccessToken](ctx, s.client, path, nil)
}
// CreateToken creates an access token for a user.
func (s *UserService) CreateToken(ctx context.Context, username string, opts *types.CreateAccessTokenOption) (*types.AccessToken, error) {
path := ResolvePath("/api/v1/users/{username}/tokens", pathParams("username", username))
var out types.AccessToken
if err := s.client.Post(ctx, path, opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteToken deletes an access token for a user.
func (s *UserService) DeleteToken(ctx context.Context, username, token string) error {
path := ResolvePath("/api/v1/users/{username}/tokens/{token}", pathParams("username", username, "token", token))
return s.client.Delete(ctx, path)
}
// ListOAuth2Applications returns all OAuth2 applications owned by the authenticated user.
func (s *UserService) ListOAuth2Applications(ctx context.Context) ([]types.OAuth2Application, error) {
return ListAll[types.OAuth2Application](ctx, s.client, "/api/v1/user/applications/oauth2", nil)
}
// IterOAuth2Applications returns an iterator over all OAuth2 applications owned by the authenticated user.
func (s *UserService) IterOAuth2Applications(ctx context.Context) iter.Seq2[types.OAuth2Application, error] {
return ListIter[types.OAuth2Application](ctx, s.client, "/api/v1/user/applications/oauth2", nil)
}
// CreateOAuth2Application creates a new OAuth2 application for the authenticated user.
func (s *UserService) CreateOAuth2Application(ctx context.Context, opts *types.CreateOAuth2ApplicationOptions) (*types.OAuth2Application, error) {
var out types.OAuth2Application
if err := s.client.Post(ctx, "/api/v1/user/applications/oauth2", opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// GetOAuth2Application returns a single OAuth2 application owned by the authenticated user.
func (s *UserService) GetOAuth2Application(ctx context.Context, id int64) (*types.OAuth2Application, error) {
path := ResolvePath("/api/v1/user/applications/oauth2/{id}", pathParams("id", int64String(id)))
var out types.OAuth2Application
if err := s.client.Get(ctx, path, &out); err != nil {
return nil, err
}
return &out, nil
}
// UpdateOAuth2Application updates an OAuth2 application owned by the authenticated user.
func (s *UserService) UpdateOAuth2Application(ctx context.Context, id int64, opts *types.CreateOAuth2ApplicationOptions) (*types.OAuth2Application, error) {
path := ResolvePath("/api/v1/user/applications/oauth2/{id}", pathParams("id", int64String(id)))
var out types.OAuth2Application
if err := s.client.Patch(ctx, path, opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteOAuth2Application deletes an OAuth2 application owned by the authenticated user.
func (s *UserService) DeleteOAuth2Application(ctx context.Context, id int64) error {
path := ResolvePath("/api/v1/user/applications/oauth2/{id}", pathParams("id", int64String(id)))
return s.client.Delete(ctx, path)
}
// ListStopwatches returns all existing stopwatches for the authenticated user.
func (s *UserService) ListStopwatches(ctx context.Context) ([]types.StopWatch, error) {
return ListAll[types.StopWatch](ctx, s.client, "/api/v1/user/stopwatches", nil)
}
// IterStopwatches returns an iterator over all existing stopwatches for the authenticated user.
func (s *UserService) IterStopwatches(ctx context.Context) iter.Seq2[types.StopWatch, error] {
return ListIter[types.StopWatch](ctx, s.client, "/api/v1/user/stopwatches", nil)
}
// ListBlockedUsers returns all users blocked by the authenticated user.
func (s *UserService) ListBlockedUsers(ctx context.Context) ([]types.BlockedUser, error) {
return ListAll[types.BlockedUser](ctx, s.client, "/api/v1/user/list_blocked", nil)
}
// IterBlockedUsers returns an iterator over all users blocked by the authenticated user.
func (s *UserService) IterBlockedUsers(ctx context.Context) iter.Seq2[types.BlockedUser, error] {
return ListIter[types.BlockedUser](ctx, s.client, "/api/v1/user/list_blocked", nil)
}
// Block blocks a user as the authenticated user.
func (s *UserService) Block(ctx context.Context, username string) error {
path := ResolvePath("/api/v1/user/block/{username}", pathParams("username", username))
return s.client.Put(ctx, path, nil, nil)
}
// Unblock unblocks a user as the authenticated user.
func (s *UserService) Unblock(ctx context.Context, username string) error {
path := ResolvePath("/api/v1/user/unblock/{username}", pathParams("username", username))
return s.client.Put(ctx, path, nil, nil)
}
// ListMySubscriptions returns all repositories watched by the authenticated user.
func (s *UserService) ListMySubscriptions(ctx context.Context) ([]types.Repository, error) {
return ListAll[types.Repository](ctx, s.client, "/api/v1/user/subscriptions", nil)
}
// IterMySubscriptions returns an iterator over all repositories watched by the authenticated user.
func (s *UserService) IterMySubscriptions(ctx context.Context) iter.Seq2[types.Repository, error] {
return ListIter[types.Repository](ctx, s.client, "/api/v1/user/subscriptions", nil)
}
// ListMyStarred returns all repositories starred by the authenticated user.
func (s *UserService) ListMyStarred(ctx context.Context) ([]types.Repository, error) {
return ListAll[types.Repository](ctx, s.client, "/api/v1/user/starred", nil)
}
// IterMyStarred returns an iterator over all repositories starred by the authenticated user.
func (s *UserService) IterMyStarred(ctx context.Context) iter.Seq2[types.Repository, error] {
return ListIter[types.Repository](ctx, s.client, "/api/v1/user/starred", nil)
}
// ListMyFollowers returns all followers of the authenticated user.
func (s *UserService) ListMyFollowers(ctx context.Context) ([]types.User, error) {
return ListAll[types.User](ctx, s.client, "/api/v1/user/followers", nil)
}
// IterMyFollowers returns an iterator over all followers of the authenticated user.
func (s *UserService) IterMyFollowers(ctx context.Context) iter.Seq2[types.User, error] {
return ListIter[types.User](ctx, s.client, "/api/v1/user/followers", nil)
}
// ListMyFollowing returns all users followed by the authenticated user.
func (s *UserService) ListMyFollowing(ctx context.Context) ([]types.User, error) {
return ListAll[types.User](ctx, s.client, "/api/v1/user/following", nil)
}
// IterMyFollowing returns an iterator over all users followed by the authenticated user.
func (s *UserService) IterMyFollowing(ctx context.Context) iter.Seq2[types.User, error] {
return ListIter[types.User](ctx, s.client, "/api/v1/user/following", nil)
}
// ListMyTeams returns all teams the authenticated user belongs to.
func (s *UserService) ListMyTeams(ctx context.Context) ([]types.Team, error) {
return ListAll[types.Team](ctx, s.client, "/api/v1/user/teams", nil)
}
// IterMyTeams returns an iterator over all teams the authenticated user belongs to.
func (s *UserService) IterMyTeams(ctx context.Context) iter.Seq2[types.Team, error] {
return ListIter[types.Team](ctx, s.client, "/api/v1/user/teams", nil)
}
// ListMyTrackedTimes returns all tracked times logged by the authenticated user.
func (s *UserService) ListMyTrackedTimes(ctx context.Context) ([]types.TrackedTime, error) {
return ListAll[types.TrackedTime](ctx, s.client, "/api/v1/user/times", nil)
}
// IterMyTrackedTimes returns an iterator over all tracked times logged by the authenticated user.
func (s *UserService) IterMyTrackedTimes(ctx context.Context) iter.Seq2[types.TrackedTime, error] {
return ListIter[types.TrackedTime](ctx, s.client, "/api/v1/user/times", nil)
}
// CheckQuota reports whether the authenticated user is over quota.
func (s *UserService) CheckQuota(ctx context.Context) (bool, error) {
var out bool
if err := s.client.Get(ctx, "/api/v1/user/quota/check", &out); err != nil {
return false, err
}
return out, nil
}
// GetRunnerRegistrationToken returns the authenticated user's actions runner registration token.
func (s *UserService) GetRunnerRegistrationToken(ctx context.Context) (string, error) {
path := "/api/v1/user/actions/runners/registration-token"
resp, err := s.client.doJSON(ctx, http.MethodGet, path, nil, nil)
if err != nil {
return "", err
}
return resp.Header.Get("token"), nil
}
// ListFollowers returns all followers of a user.
func (s *UserService) ListFollowers(ctx context.Context, username string) ([]types.User, error) {
path := ResolvePath("/api/v1/users/{username}/followers", pathParams("username", username))
return ListAll[types.User](ctx, s.client, path, nil)
}
// IterFollowers returns an iterator over all followers of a user.
func (s *UserService) IterFollowers(ctx context.Context, username string) iter.Seq2[types.User, error] {
path := ResolvePath("/api/v1/users/{username}/followers", pathParams("username", username))
return ListIter[types.User](ctx, s.client, path, nil)
}
// ListSubscriptions returns all repositories watched by a user.
func (s *UserService) ListSubscriptions(ctx context.Context, username string) ([]types.Repository, error) {
path := ResolvePath("/api/v1/users/{username}/subscriptions", pathParams("username", username))
return ListAll[types.Repository](ctx, s.client, path, nil)
}
// IterSubscriptions returns an iterator over all repositories watched by a user.
func (s *UserService) IterSubscriptions(ctx context.Context, username string) iter.Seq2[types.Repository, error] {
path := ResolvePath("/api/v1/users/{username}/subscriptions", pathParams("username", username))
return ListIter[types.Repository](ctx, s.client, path, nil)
}
// ListFollowing returns all users that a user is following.
func (s *UserService) ListFollowing(ctx context.Context, username string) ([]types.User, error) {
path := ResolvePath("/api/v1/users/{username}/following", pathParams("username", username))
return ListAll[types.User](ctx, s.client, path, nil)
}
// IterFollowing returns an iterator over all users that a user is following.
func (s *UserService) IterFollowing(ctx context.Context, username string) iter.Seq2[types.User, error] {
path := ResolvePath("/api/v1/users/{username}/following", pathParams("username", username))
return ListIter[types.User](ctx, s.client, path, nil)
}
// ListActivityFeeds returns a user's activity feed entries.
func (s *UserService) ListActivityFeeds(ctx context.Context, username string) ([]types.Activity, error) {
path := ResolvePath("/api/v1/users/{username}/activities/feeds", pathParams("username", username))
return ListAll[types.Activity](ctx, s.client, path, nil)
}
// IterActivityFeeds returns an iterator over a user's activity feed entries.
func (s *UserService) IterActivityFeeds(ctx context.Context, username string) iter.Seq2[types.Activity, error] {
path := ResolvePath("/api/v1/users/{username}/activities/feeds", pathParams("username", username))
return ListIter[types.Activity](ctx, s.client, path, nil)
}
// ListRepos returns all repositories owned by a user.
func (s *UserService) ListRepos(ctx context.Context, username string) ([]types.Repository, error) {
path := ResolvePath("/api/v1/users/{username}/repos", pathParams("username", username))
return ListAll[types.Repository](ctx, s.client, path, nil)
}
// IterRepos returns an iterator over all repositories owned by a user.
func (s *UserService) IterRepos(ctx context.Context, username string) iter.Seq2[types.Repository, error] {
path := ResolvePath("/api/v1/users/{username}/repos", pathParams("username", username))
return ListIter[types.Repository](ctx, s.client, path, nil)
}
// Follow follows a user as the authenticated user.
func (s *UserService) Follow(ctx context.Context, username string) error {
path := ResolvePath("/api/v1/user/following/{username}", pathParams("username", username))
return s.client.Put(ctx, path, nil, nil)
}
// CheckFollowing reports whether one user is following another user.
func (s *UserService) CheckFollowing(ctx context.Context, username, target string) (bool, error) {
path := ResolvePath("/api/v1/users/{username}/following/{target}", pathParams("username", username, "target", target))
resp, err := s.client.doJSON(ctx, http.MethodGet, path, nil, nil)
if err != nil {
if IsNotFound(err) {
return false, nil
}
return false, err
}
return resp.StatusCode == http.StatusNoContent, nil
}
// Unfollow unfollows a user as the authenticated user.
func (s *UserService) Unfollow(ctx context.Context, username string) error {
path := ResolvePath("/api/v1/user/following/{username}", pathParams("username", username))
return s.client.Delete(ctx, path)
}
// ListStarred returns all repositories starred by a user.
func (s *UserService) ListStarred(ctx context.Context, username string) ([]types.Repository, error) {
path := ResolvePath("/api/v1/users/{username}/starred", pathParams("username", username))
return ListAll[types.Repository](ctx, s.client, path, nil)
}
// IterStarred returns an iterator over all repositories starred by a user.
func (s *UserService) IterStarred(ctx context.Context, username string) iter.Seq2[types.Repository, error] {
path := ResolvePath("/api/v1/users/{username}/starred", pathParams("username", username))
return ListIter[types.Repository](ctx, s.client, path, nil)
}
// GetHeatmap returns a user's contribution heatmap data.
func (s *UserService) GetHeatmap(ctx context.Context, username string) ([]types.UserHeatmapData, error) {
path := ResolvePath("/api/v1/users/{username}/heatmap", pathParams("username", username))
var out []types.UserHeatmapData
if err := s.client.Get(ctx, path, &out); err != nil {
return nil, err
}
return out, nil
}
// Star stars a repository as the authenticated user.
func (s *UserService) Star(ctx context.Context, owner, repo string) error {
path := ResolvePath("/api/v1/user/starred/{owner}/{repo}", pathParams("owner", owner, "repo", repo))
return s.client.Put(ctx, path, nil, nil)
}
// Unstar unstars a repository as the authenticated user.
func (s *UserService) Unstar(ctx context.Context, owner, repo string) error {
path := ResolvePath("/api/v1/user/starred/{owner}/{repo}", pathParams("owner", owner, "repo", repo))
return s.client.Delete(ctx, path)
}
// CheckStarring reports whether the authenticated user is starring a repository.
func (s *UserService) CheckStarring(ctx context.Context, owner, repo string) (bool, error) {
path := ResolvePath("/api/v1/user/starred/{owner}/{repo}", pathParams("owner", owner, "repo", repo))
resp, err := s.client.doJSON(ctx, http.MethodGet, path, nil, nil)
if err != nil {
if IsNotFound(err) {
return false, nil
}
return false, err
}
return resp.StatusCode == http.StatusNoContent, nil
}