Skip to content

Commit

Permalink
feature: Retrieve the user assigned to the token (#100)
Browse files Browse the repository at this point in the history
* implement Users/Me

* fix warning

* add Request type.

* change exampleService_Me

* fix lint errors.
  • Loading branch information
ikasoba authored Sep 15, 2023
1 parent fd3bf55 commit 51b8fd6
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
98 changes: 98 additions & 0 deletions services/users/fixtures/i.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"id": "9dr5dkiiby",
"name": "test",
"username": "test",
"host": null,
"avatarUrl": "",
"avatarBlurhash": "edRC-.t7_4t7s;%Lj[NHfPoK%hj[RiayM{%NayRPayozt6ayWBj[ay",
"isBot": false,
"isCat": true,
"emojis": {},
"onlineStatus": "online",
"badgeRoles": [],
"url": null,
"uri": null,
"movedTo": null,
"alsoKnownAs": null,
"createdAt": "2023-04-19T11:01:05.178Z",
"updatedAt": "2023-09-14T06:20:41.417Z",
"lastFetchedAt": null,
"bannerUrl": null,
"bannerBlurhash": null,
"isLocked": false,
"isSilenced": false,
"isSuspended": false,
"description": "",
"location": "",
"birthday": "",
"lang": "ja",
"fields": [],
"followersCount": 0,
"followingCount": 0,
"notesCount": 0,
"pinnedNoteIds": [],
"pinnedNotes": [],
"pinnedPageId": null,
"pinnedPage": null,
"publicReactions": true,
"ffVisibility": "public",
"twoFactorEnabled": false,
"usePasswordLessLogin": false,
"securityKeys": false,
"roles": [],
"memo": null,
"avatarId": "9ivuypq810",
"bannerId": null,
"isModerator": false,
"isAdmin": false,
"injectFeaturedNote": true,
"receiveAnnouncementEmail": true,
"alwaysMarkNsfw": false,
"autoSensitive": false,
"carefulBot": false,
"autoAcceptFollowed": true,
"noCrawle": false,
"preventAiLearning": true,
"isExplorable": true,
"isDeleted": false,
"hideOnlineStatus": false,
"hasUnreadSpecifiedNotes": false,
"hasUnreadMentions": true,
"hasUnreadAnnouncement": false,
"hasUnreadAntenna": false,
"hasUnreadChannel": false,
"hasUnreadNotification": false,
"hasPendingReceivedFollowRequest": false,
"mutedWords": [],
"mutedInstances": [],
"mutingNotificationTypes": [],
"emailNotificationTypes": [],
"achievements": [],
"loggedInDays": 80,
"policies": {
"gtlAvailable": true,
"ltlAvailable": true,
"canPublicNote": true,
"canInvite": false,
"inviteLimit": 0,
"inviteLimitCycle": 10080,
"inviteExpirationTime": 0,
"canManageCustomEmojis": false,
"canSearchNotes": true,
"canHideAds": false,
"driveCapacityMb": 10000,
"alwaysMarkNsfw": false,
"pinLimit": 5,
"antennaLimit": 5,
"wordMuteLimit": 200,
"webhookLimit": 3,
"clipLimit": 10,
"noteEachClipsLimit": 200,
"userListLimit": 10,
"userEachUserListsLimit": 50,
"rateLimitFactor": 1
},
"email": "[email protected]",
"emailVerified": true,
"securityKeysList": []
}
28 changes: 28 additions & 0 deletions services/users/me.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package users

import (
"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/models"
)

// MeRequest represents a request to retrieve a user who has been issued a token.
type MeRequest struct{}

// Validate the request.
func (r MeRequest) Validate() error {
return nil
}

// Me gets the user issued the token.
func (s *Service) Me() (models.User, error) {
var user models.User
err := s.Call(
&core.JSONRequest{
Path: "/i",
Request: &MeRequest{},
},
&user,
)

return user, err
}
42 changes: 42 additions & 0 deletions services/users/me_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package users_test

import (
"log"
"net/http"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/yitsushi/go-misskey"
"github.com/yitsushi/go-misskey/services/users"
"github.com/yitsushi/go-misskey/test"
)

func TestService_Me(t *testing.T) {
client := test.MakeMockClient(test.SimpleMockOptions{
Endpoint: "/api/i",
ResponseFile: "i.json",
StatusCode: http.StatusOK,
RequestData: &users.MeRequest{},
})

user, err := client.Users().Me()
if !assert.NoError(t, err) {
return
}

assert.Equal(t, user.ID, "9dr5dkiiby")
}

func ExampleService_Me() {
client, _ := misskey.NewClientWithOptions(misskey.WithSimpleConfig("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN")))

user, err := client.Users().Me()
if err != nil {
log.Printf("[Users/Me] %s", err)

return
}

log.Printf("[Users/Me] %s %s", user.Username, user.Name)
}

0 comments on commit 51b8fd6

Please sign in to comment.