accounts.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package mastodon
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. )
  10. type AccountPleroma struct {
  11. Relationship Relationship `json:"relationship"`
  12. }
  13. // Account hold information for mastodon account.
  14. type Account struct {
  15. ID string `json:"id"`
  16. Username string `json:"username"`
  17. Acct string `json:"acct"`
  18. DisplayName string `json:"display_name"`
  19. Locked bool `json:"locked"`
  20. CreatedAt time.Time `json:"created_at"`
  21. FollowersCount int64 `json:"followers_count"`
  22. FollowingCount int64 `json:"following_count"`
  23. StatusesCount int64 `json:"statuses_count"`
  24. Note string `json:"note"`
  25. URL string `json:"url"`
  26. Avatar string `json:"avatar"`
  27. AvatarStatic string `json:"avatar_static"`
  28. Header string `json:"header"`
  29. HeaderStatic string `json:"header_static"`
  30. Emojis []Emoji `json:"emojis"`
  31. Moved *Account `json:"moved"`
  32. Fields []Field `json:"fields"`
  33. Bot bool `json:"bot"`
  34. Pleroma *AccountPleroma `json:"pleroma"`
  35. }
  36. // Field is a Mastodon account profile field.
  37. type Field struct {
  38. Name string `json:"name"`
  39. Value string `json:"value"`
  40. VerifiedAt time.Time `json:"verified_at"`
  41. }
  42. // AccountSource is a Mastodon account profile field.
  43. type AccountSource struct {
  44. Privacy *string `json:"privacy"`
  45. Sensitive *bool `json:"sensitive"`
  46. Language *string `json:"language"`
  47. Note *string `json:"note"`
  48. Fields *[]Field `json:"fields"`
  49. }
  50. // GetAccount return Account.
  51. func (c *Client) GetAccount(ctx context.Context, id string) (*Account, error) {
  52. var account Account
  53. err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s", url.PathEscape(string(id))), nil, &account, nil)
  54. if err != nil {
  55. return nil, err
  56. }
  57. if account.Pleroma == nil {
  58. rs, err := c.GetAccountRelationships(ctx, []string{id})
  59. if err != nil {
  60. return nil, err
  61. }
  62. if len(rs) > 0 {
  63. account.Pleroma = &AccountPleroma{*rs[0]}
  64. }
  65. }
  66. return &account, nil
  67. }
  68. // GetAccountCurrentUser return Account of current user.
  69. func (c *Client) GetAccountCurrentUser(ctx context.Context) (*Account, error) {
  70. var account Account
  71. err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/verify_credentials", nil, &account, nil)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return &account, nil
  76. }
  77. // Profile is a struct for updating profiles.
  78. type Profile struct {
  79. // If it is nil it will not be updated.
  80. // If it is empty, update it with empty.
  81. DisplayName *string
  82. Note *string
  83. Locked *bool
  84. Fields *[]Field
  85. Source *AccountSource
  86. // Set the base64 encoded character string of the image.
  87. Avatar string
  88. Header string
  89. }
  90. // AccountUpdate updates the information of the current user.
  91. func (c *Client) AccountUpdate(ctx context.Context, profile *Profile) (*Account, error) {
  92. params := url.Values{}
  93. if profile.DisplayName != nil {
  94. params.Set("display_name", *profile.DisplayName)
  95. }
  96. if profile.Note != nil {
  97. params.Set("note", *profile.Note)
  98. }
  99. if profile.Locked != nil {
  100. params.Set("locked", strconv.FormatBool(*profile.Locked))
  101. }
  102. if profile.Fields != nil {
  103. for idx, field := range *profile.Fields {
  104. params.Set(fmt.Sprintf("fields_attributes[%d][name]", idx), field.Name)
  105. params.Set(fmt.Sprintf("fields_attributes[%d][value]", idx), field.Value)
  106. }
  107. }
  108. if profile.Source != nil {
  109. if profile.Source.Privacy != nil {
  110. params.Set("source[privacy]", *profile.Source.Privacy)
  111. }
  112. if profile.Source.Sensitive != nil {
  113. params.Set("source[sensitive]", strconv.FormatBool(*profile.Source.Sensitive))
  114. }
  115. if profile.Source.Language != nil {
  116. params.Set("source[language]", *profile.Source.Language)
  117. }
  118. }
  119. if profile.Avatar != "" {
  120. params.Set("avatar", profile.Avatar)
  121. }
  122. if profile.Header != "" {
  123. params.Set("header", profile.Header)
  124. }
  125. var account Account
  126. err := c.doAPI(ctx, http.MethodPatch, "/api/v1/accounts/update_credentials", params, &account, nil)
  127. if err != nil {
  128. return nil, err
  129. }
  130. return &account, nil
  131. }
  132. // GetAccountStatuses return statuses by specified accuont.
  133. func (c *Client) GetAccountStatuses(ctx context.Context, id string, onlyMedia bool, pg *Pagination) ([]*Status, error) {
  134. var statuses []*Status
  135. params := url.Values{}
  136. params.Set("only_media", strconv.FormatBool(onlyMedia))
  137. err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/statuses", url.PathEscape(string(id))), params, &statuses, pg)
  138. if err != nil {
  139. return nil, err
  140. }
  141. return statuses, nil
  142. }
  143. // GetAccountFollowers return followers list.
  144. func (c *Client) GetAccountFollowers(ctx context.Context, id string, pg *Pagination) ([]*Account, error) {
  145. var accounts []*Account
  146. err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/followers", url.PathEscape(string(id))), nil, &accounts, pg)
  147. if err != nil {
  148. return nil, err
  149. }
  150. return accounts, nil
  151. }
  152. // GetAccountFollowing return following list.
  153. func (c *Client) GetAccountFollowing(ctx context.Context, id string, pg *Pagination) ([]*Account, error) {
  154. var accounts []*Account
  155. err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/following", url.PathEscape(string(id))), nil, &accounts, pg)
  156. if err != nil {
  157. return nil, err
  158. }
  159. return accounts, nil
  160. }
  161. // GetBlocks return block list.
  162. func (c *Client) GetBlocks(ctx context.Context, pg *Pagination) ([]*Account, error) {
  163. var accounts []*Account
  164. err := c.doAPI(ctx, http.MethodGet, "/api/v1/blocks", nil, &accounts, pg)
  165. if err != nil {
  166. return nil, err
  167. }
  168. return accounts, nil
  169. }
  170. // Relationship hold information for relation-ship to the account.
  171. type Relationship struct {
  172. ID string `json:"id"`
  173. Following bool `json:"following"`
  174. FollowedBy bool `json:"followed_by"`
  175. Blocking bool `json:"blocking"`
  176. Muting bool `json:"muting"`
  177. MutingNotifications bool `json:"muting_notifications"`
  178. Subscribing bool `json:"subscribing"`
  179. Requested bool `json:"requested"`
  180. DomainBlocking bool `json:"domain_blocking"`
  181. ShowingReblogs bool `json:"showing_reblogs"`
  182. Endorsed bool `json:"endorsed"`
  183. }
  184. // AccountFollow follow the account.
  185. func (c *Client) AccountFollow(ctx context.Context, id string, reblogs *bool) (*Relationship, error) {
  186. var relationship Relationship
  187. params := url.Values{}
  188. if reblogs != nil {
  189. params.Set("reblogs", strconv.FormatBool(*reblogs))
  190. }
  191. err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/follow", url.PathEscape(id)), params, &relationship, nil)
  192. if err != nil {
  193. return nil, err
  194. }
  195. return &relationship, nil
  196. }
  197. // AccountUnfollow unfollow the account.
  198. func (c *Client) AccountUnfollow(ctx context.Context, id string) (*Relationship, error) {
  199. var relationship Relationship
  200. err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unfollow", url.PathEscape(string(id))), nil, &relationship, nil)
  201. if err != nil {
  202. return nil, err
  203. }
  204. return &relationship, nil
  205. }
  206. // AccountBlock block the account.
  207. func (c *Client) AccountBlock(ctx context.Context, id string) (*Relationship, error) {
  208. var relationship Relationship
  209. err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/block", url.PathEscape(string(id))), nil, &relationship, nil)
  210. if err != nil {
  211. return nil, err
  212. }
  213. return &relationship, nil
  214. }
  215. // AccountUnblock unblock the account.
  216. func (c *Client) AccountUnblock(ctx context.Context, id string) (*Relationship, error) {
  217. var relationship Relationship
  218. err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unblock", url.PathEscape(string(id))), nil, &relationship, nil)
  219. if err != nil {
  220. return nil, err
  221. }
  222. return &relationship, nil
  223. }
  224. // AccountMute mute the account.
  225. func (c *Client) AccountMute(ctx context.Context, id string) (*Relationship, error) {
  226. var relationship Relationship
  227. err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/mute", url.PathEscape(string(id))), nil, &relationship, nil)
  228. if err != nil {
  229. return nil, err
  230. }
  231. return &relationship, nil
  232. }
  233. // AccountUnmute unmute the account.
  234. func (c *Client) AccountUnmute(ctx context.Context, id string) (*Relationship, error) {
  235. var relationship Relationship
  236. err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unmute", url.PathEscape(string(id))), nil, &relationship, nil)
  237. if err != nil {
  238. return nil, err
  239. }
  240. return &relationship, nil
  241. }
  242. // GetAccountRelationships return relationship for the account.
  243. func (c *Client) GetAccountRelationships(ctx context.Context, ids []string) ([]*Relationship, error) {
  244. params := url.Values{}
  245. for _, id := range ids {
  246. params.Add("id[]", id)
  247. }
  248. var relationships []*Relationship
  249. err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/relationships", params, &relationships, nil)
  250. if err != nil {
  251. return nil, err
  252. }
  253. return relationships, nil
  254. }
  255. // AccountsSearch search accounts by query.
  256. func (c *Client) AccountsSearch(ctx context.Context, q string, limit int64) ([]*Account, error) {
  257. params := url.Values{}
  258. params.Set("q", q)
  259. params.Set("limit", fmt.Sprint(limit))
  260. var accounts []*Account
  261. err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/search", params, &accounts, nil)
  262. if err != nil {
  263. return nil, err
  264. }
  265. return accounts, nil
  266. }
  267. // FollowRemoteUser send follow-request.
  268. func (c *Client) FollowRemoteUser(ctx context.Context, uri string) (*Account, error) {
  269. params := url.Values{}
  270. params.Set("uri", uri)
  271. var account Account
  272. err := c.doAPI(ctx, http.MethodPost, "/api/v1/follows", params, &account, nil)
  273. if err != nil {
  274. return nil, err
  275. }
  276. return &account, nil
  277. }
  278. // GetFollowRequests return follow-requests.
  279. func (c *Client) GetFollowRequests(ctx context.Context, pg *Pagination) ([]*Account, error) {
  280. var accounts []*Account
  281. err := c.doAPI(ctx, http.MethodGet, "/api/v1/follow_requests", nil, &accounts, pg)
  282. if err != nil {
  283. return nil, err
  284. }
  285. return accounts, nil
  286. }
  287. // FollowRequestAuthorize is authorize the follow request of user with id.
  288. func (c *Client) FollowRequestAuthorize(ctx context.Context, id string) error {
  289. return c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%s/authorize", url.PathEscape(string(id))), nil, nil, nil)
  290. }
  291. // FollowRequestReject is rejects the follow request of user with id.
  292. func (c *Client) FollowRequestReject(ctx context.Context, id string) error {
  293. return c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%s/reject", url.PathEscape(string(id))), nil, nil, nil)
  294. }
  295. // GetMutes returns the list of users muted by the current user.
  296. func (c *Client) GetMutes(ctx context.Context, pg *Pagination) ([]*Account, error) {
  297. var accounts []*Account
  298. err := c.doAPI(ctx, http.MethodGet, "/api/v1/mutes", nil, &accounts, pg)
  299. if err != nil {
  300. return nil, err
  301. }
  302. return accounts, nil
  303. }
  304. // Subscribe to receive notifications for all statuses posted by a user
  305. func (c *Client) Subscribe(ctx context.Context, id string) (*Relationship, error) {
  306. var relationship *Relationship
  307. err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/pleroma/accounts/%s/subscribe", url.PathEscape(id)), nil, &relationship, nil)
  308. if err != nil {
  309. return nil, err
  310. }
  311. return relationship, nil
  312. }
  313. // UnSubscribe to stop receiving notifications from user statuses
  314. func (c *Client) UnSubscribe(ctx context.Context, id string) (*Relationship, error) {
  315. var relationship *Relationship
  316. err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/pleroma/accounts/%s/unsubscribe", url.PathEscape(id)), nil, &relationship, nil)
  317. if err != nil {
  318. return nil, err
  319. }
  320. return relationship, nil
  321. }
  322. // GetBookmarks returns the list of bookmarked statuses
  323. func (c *Client) GetBookmarks(ctx context.Context, pg *Pagination) ([]*Status, error) {
  324. var statuses []*Status
  325. err := c.doAPI(ctx, http.MethodGet, "/api/v1/bookmarks", nil, &statuses, pg)
  326. if err != nil {
  327. return nil, err
  328. }
  329. return statuses, nil
  330. }