users.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "context"
  7. "fmt"
  8. "os"
  9. "strings"
  10. "time"
  11. "github.com/go-macaron/binding"
  12. api "github.com/gogs/go-gogs-client"
  13. "github.com/pkg/errors"
  14. "gorm.io/gorm"
  15. log "unknwon.dev/clog/v2"
  16. "gogs.io/gogs/internal/auth"
  17. "gogs.io/gogs/internal/conf"
  18. "gogs.io/gogs/internal/cryptoutil"
  19. "gogs.io/gogs/internal/errutil"
  20. "gogs.io/gogs/internal/osutil"
  21. "gogs.io/gogs/internal/tool"
  22. "gogs.io/gogs/internal/userutil"
  23. )
  24. // UsersStore is the persistent interface for users.
  25. //
  26. // NOTE: All methods are sorted in alphabetical order.
  27. type UsersStore interface {
  28. // Authenticate validates username and password via given login source ID. It
  29. // returns ErrUserNotExist when the user was not found.
  30. //
  31. // When the "loginSourceID" is negative, it aborts the process and returns
  32. // ErrUserNotExist if the user was not found in the database.
  33. //
  34. // When the "loginSourceID" is non-negative, it returns ErrLoginSourceMismatch
  35. // if the user has different login source ID than the "loginSourceID".
  36. //
  37. // When the "loginSourceID" is positive, it tries to authenticate via given
  38. // login source and creates a new user when not yet exists in the database.
  39. Authenticate(ctx context.Context, username, password string, loginSourceID int64) (*User, error)
  40. // Create creates a new user and persists to database. It returns
  41. // ErrUserAlreadyExist when a user with same name already exists, or
  42. // ErrEmailAlreadyUsed if the email has been used by another user.
  43. Create(ctx context.Context, username, email string, opts CreateUserOptions) (*User, error)
  44. // DeleteCustomAvatar deletes the current user custom avatar and falls back to
  45. // use look up avatar by email.
  46. DeleteCustomAvatar(ctx context.Context, userID int64) error
  47. // GetByEmail returns the user (not organization) with given email. It ignores
  48. // records with unverified emails and returns ErrUserNotExist when not found.
  49. GetByEmail(ctx context.Context, email string) (*User, error)
  50. // GetByID returns the user with given ID. It returns ErrUserNotExist when not
  51. // found.
  52. GetByID(ctx context.Context, id int64) (*User, error)
  53. // GetByUsername returns the user with given username. It returns
  54. // ErrUserNotExist when not found.
  55. GetByUsername(ctx context.Context, username string) (*User, error)
  56. // HasForkedRepository returns true if the user has forked given repository.
  57. HasForkedRepository(ctx context.Context, userID, repoID int64) bool
  58. // ListFollowers returns a list of users that are following the given user.
  59. // Results are paginated by given page and page size, and sorted by the time of
  60. // follow in descending order.
  61. ListFollowers(ctx context.Context, userID int64, page, pageSize int) ([]*User, error)
  62. // ListFollowings returns a list of users that are followed by the given user.
  63. // Results are paginated by given page and page size, and sorted by the time of
  64. // follow in descending order.
  65. ListFollowings(ctx context.Context, userID int64, page, pageSize int) ([]*User, error)
  66. // UseCustomAvatar uses the given avatar as the user custom avatar.
  67. UseCustomAvatar(ctx context.Context, userID int64, avatar []byte) error
  68. }
  69. var Users UsersStore
  70. var _ UsersStore = (*users)(nil)
  71. type users struct {
  72. *gorm.DB
  73. }
  74. // NewUsersStore returns a persistent interface for users with given database
  75. // connection.
  76. func NewUsersStore(db *gorm.DB) UsersStore {
  77. return &users{DB: db}
  78. }
  79. type ErrLoginSourceMismatch struct {
  80. args errutil.Args
  81. }
  82. func (err ErrLoginSourceMismatch) Error() string {
  83. return fmt.Sprintf("login source mismatch: %v", err.args)
  84. }
  85. func (db *users) Authenticate(ctx context.Context, login, password string, loginSourceID int64) (*User, error) {
  86. login = strings.ToLower(login)
  87. query := db.WithContext(ctx)
  88. if strings.Contains(login, "@") {
  89. query = query.Where("email = ?", login)
  90. } else {
  91. query = query.Where("lower_name = ?", login)
  92. }
  93. user := new(User)
  94. err := query.First(user).Error
  95. if err != nil && err != gorm.ErrRecordNotFound {
  96. return nil, errors.Wrap(err, "get user")
  97. }
  98. var authSourceID int64 // The login source ID will be used to authenticate the user
  99. createNewUser := false // Whether to create a new user after successful authentication
  100. // User found in the database
  101. if err == nil {
  102. // Note: This check is unnecessary but to reduce user confusion at login page
  103. // and make it more consistent from user's perspective.
  104. if loginSourceID >= 0 && user.LoginSource != loginSourceID {
  105. return nil, ErrLoginSourceMismatch{args: errutil.Args{"expect": loginSourceID, "actual": user.LoginSource}}
  106. }
  107. // Validate password hash fetched from database for local accounts.
  108. if user.IsLocal() {
  109. if userutil.ValidatePassword(user.Password, user.Salt, password) {
  110. return user, nil
  111. }
  112. return nil, auth.ErrBadCredentials{Args: map[string]interface{}{"login": login, "userID": user.ID}}
  113. }
  114. authSourceID = user.LoginSource
  115. } else {
  116. // Non-local login source is always greater than 0.
  117. if loginSourceID <= 0 {
  118. return nil, auth.ErrBadCredentials{Args: map[string]interface{}{"login": login}}
  119. }
  120. authSourceID = loginSourceID
  121. createNewUser = true
  122. }
  123. source, err := LoginSources.GetByID(ctx, authSourceID)
  124. if err != nil {
  125. return nil, errors.Wrap(err, "get login source")
  126. }
  127. if !source.IsActived {
  128. return nil, errors.Errorf("login source %d is not activated", source.ID)
  129. }
  130. extAccount, err := source.Provider.Authenticate(login, password)
  131. if err != nil {
  132. return nil, err
  133. }
  134. if !createNewUser {
  135. return user, nil
  136. }
  137. // Validate username make sure it satisfies requirement.
  138. if binding.AlphaDashDotPattern.MatchString(extAccount.Name) {
  139. return nil, fmt.Errorf("invalid pattern for attribute 'username' [%s]: must be valid alpha or numeric or dash(-_) or dot characters", extAccount.Name)
  140. }
  141. return db.Create(ctx, extAccount.Name, extAccount.Email,
  142. CreateUserOptions{
  143. FullName: extAccount.FullName,
  144. LoginSource: authSourceID,
  145. LoginName: extAccount.Login,
  146. Location: extAccount.Location,
  147. Website: extAccount.Website,
  148. Activated: true,
  149. Admin: extAccount.Admin,
  150. },
  151. )
  152. }
  153. type CreateUserOptions struct {
  154. FullName string
  155. Password string
  156. LoginSource int64
  157. LoginName string
  158. Location string
  159. Website string
  160. Activated bool
  161. Admin bool
  162. }
  163. type ErrUserAlreadyExist struct {
  164. args errutil.Args
  165. }
  166. func IsErrUserAlreadyExist(err error) bool {
  167. _, ok := err.(ErrUserAlreadyExist)
  168. return ok
  169. }
  170. func (err ErrUserAlreadyExist) Error() string {
  171. return fmt.Sprintf("user already exists: %v", err.args)
  172. }
  173. type ErrEmailAlreadyUsed struct {
  174. args errutil.Args
  175. }
  176. func IsErrEmailAlreadyUsed(err error) bool {
  177. _, ok := err.(ErrEmailAlreadyUsed)
  178. return ok
  179. }
  180. func (err ErrEmailAlreadyUsed) Email() string {
  181. email, ok := err.args["email"].(string)
  182. if ok {
  183. return email
  184. }
  185. return "<email not found>"
  186. }
  187. func (err ErrEmailAlreadyUsed) Error() string {
  188. return fmt.Sprintf("email has been used: %v", err.args)
  189. }
  190. func (db *users) Create(ctx context.Context, username, email string, opts CreateUserOptions) (*User, error) {
  191. err := isUsernameAllowed(username)
  192. if err != nil {
  193. return nil, err
  194. }
  195. _, err = db.GetByUsername(ctx, username)
  196. if err == nil {
  197. return nil, ErrUserAlreadyExist{args: errutil.Args{"name": username}}
  198. } else if !IsErrUserNotExist(err) {
  199. return nil, err
  200. }
  201. _, err = db.GetByEmail(ctx, email)
  202. if err == nil {
  203. return nil, ErrEmailAlreadyUsed{args: errutil.Args{"email": email}}
  204. } else if !IsErrUserNotExist(err) {
  205. return nil, err
  206. }
  207. user := &User{
  208. LowerName: strings.ToLower(username),
  209. Name: username,
  210. FullName: opts.FullName,
  211. Email: email,
  212. Password: opts.Password,
  213. LoginSource: opts.LoginSource,
  214. LoginName: opts.LoginName,
  215. Location: opts.Location,
  216. Website: opts.Website,
  217. MaxRepoCreation: -1,
  218. IsActive: opts.Activated,
  219. IsAdmin: opts.Admin,
  220. Avatar: cryptoutil.MD5(email),
  221. AvatarEmail: email,
  222. }
  223. user.Rands, err = GetUserSalt()
  224. if err != nil {
  225. return nil, err
  226. }
  227. user.Salt, err = GetUserSalt()
  228. if err != nil {
  229. return nil, err
  230. }
  231. user.Password = userutil.EncodePassword(user.Password, user.Salt)
  232. return user, db.WithContext(ctx).Create(user).Error
  233. }
  234. func (db *users) DeleteCustomAvatar(ctx context.Context, userID int64) error {
  235. _ = os.Remove(userutil.CustomAvatarPath(userID))
  236. return db.WithContext(ctx).
  237. Model(&User{}).
  238. Where("id = ?", userID).
  239. Updates(map[string]interface{}{
  240. "use_custom_avatar": false,
  241. "updated_unix": db.NowFunc().Unix(),
  242. }).
  243. Error
  244. }
  245. var _ errutil.NotFound = (*ErrUserNotExist)(nil)
  246. type ErrUserNotExist struct {
  247. args errutil.Args
  248. }
  249. func IsErrUserNotExist(err error) bool {
  250. _, ok := err.(ErrUserNotExist)
  251. return ok
  252. }
  253. func (err ErrUserNotExist) Error() string {
  254. return fmt.Sprintf("user does not exist: %v", err.args)
  255. }
  256. func (ErrUserNotExist) NotFound() bool {
  257. return true
  258. }
  259. func (db *users) GetByEmail(ctx context.Context, email string) (*User, error) {
  260. email = strings.ToLower(email)
  261. if email == "" {
  262. return nil, ErrUserNotExist{args: errutil.Args{"email": email}}
  263. }
  264. // First try to find the user by primary email
  265. user := new(User)
  266. err := db.WithContext(ctx).
  267. Where("email = ? AND type = ? AND is_active = ?", email, UserTypeIndividual, true).
  268. First(user).
  269. Error
  270. if err == nil {
  271. return user, nil
  272. } else if err != gorm.ErrRecordNotFound {
  273. return nil, err
  274. }
  275. // Otherwise, check activated email addresses
  276. emailAddress := new(EmailAddress)
  277. err = db.WithContext(ctx).
  278. Where("email = ? AND is_activated = ?", email, true).
  279. First(emailAddress).
  280. Error
  281. if err != nil {
  282. if err == gorm.ErrRecordNotFound {
  283. return nil, ErrUserNotExist{args: errutil.Args{"email": email}}
  284. }
  285. return nil, err
  286. }
  287. return db.GetByID(ctx, emailAddress.UID)
  288. }
  289. func (db *users) GetByID(ctx context.Context, id int64) (*User, error) {
  290. user := new(User)
  291. err := db.WithContext(ctx).Where("id = ?", id).First(user).Error
  292. if err != nil {
  293. if err == gorm.ErrRecordNotFound {
  294. return nil, ErrUserNotExist{args: errutil.Args{"userID": id}}
  295. }
  296. return nil, err
  297. }
  298. return user, nil
  299. }
  300. func (db *users) GetByUsername(ctx context.Context, username string) (*User, error) {
  301. user := new(User)
  302. err := db.WithContext(ctx).Where("lower_name = ?", strings.ToLower(username)).First(user).Error
  303. if err != nil {
  304. if err == gorm.ErrRecordNotFound {
  305. return nil, ErrUserNotExist{args: errutil.Args{"name": username}}
  306. }
  307. return nil, err
  308. }
  309. return user, nil
  310. }
  311. func (db *users) HasForkedRepository(ctx context.Context, userID, repoID int64) bool {
  312. var count int64
  313. db.WithContext(ctx).Model(new(Repository)).Where("owner_id = ? AND fork_id = ?", userID, repoID).Count(&count)
  314. return count > 0
  315. }
  316. func (db *users) ListFollowers(ctx context.Context, userID int64, page, pageSize int) ([]*User, error) {
  317. /*
  318. Equivalent SQL for PostgreSQL:
  319. SELECT * FROM "user"
  320. LEFT JOIN follow ON follow.user_id = "user".id
  321. WHERE follow.follow_id = @userID
  322. ORDER BY follow.id DESC
  323. LIMIT @limit OFFSET @offset
  324. */
  325. users := make([]*User, 0, pageSize)
  326. tx := db.WithContext(ctx).
  327. Where("follow.follow_id = ?", userID).
  328. Limit(pageSize).Offset((page - 1) * pageSize).
  329. Order("follow.id DESC")
  330. if conf.UsePostgreSQL {
  331. tx.Joins(`LEFT JOIN follow ON follow.user_id = "user".id`)
  332. } else {
  333. tx.Joins(`LEFT JOIN follow ON follow.user_id = user.id`)
  334. }
  335. return users, tx.Find(&users).Error
  336. }
  337. func (db *users) ListFollowings(ctx context.Context, userID int64, page, pageSize int) ([]*User, error) {
  338. /*
  339. Equivalent SQL for PostgreSQL:
  340. SELECT * FROM "user"
  341. LEFT JOIN follow ON follow.user_id = "user".id
  342. WHERE follow.user_id = @userID
  343. ORDER BY follow.id DESC
  344. LIMIT @limit OFFSET @offset
  345. */
  346. users := make([]*User, 0, pageSize)
  347. tx := db.WithContext(ctx).
  348. Where("follow.user_id = ?", userID).
  349. Limit(pageSize).Offset((page - 1) * pageSize).
  350. Order("follow.id DESC")
  351. if conf.UsePostgreSQL {
  352. tx.Joins(`LEFT JOIN follow ON follow.follow_id = "user".id`)
  353. } else {
  354. tx.Joins(`LEFT JOIN follow ON follow.follow_id = user.id`)
  355. }
  356. return users, tx.Find(&users).Error
  357. }
  358. func (db *users) UseCustomAvatar(ctx context.Context, userID int64, avatar []byte) error {
  359. err := userutil.SaveAvatar(userID, avatar)
  360. if err != nil {
  361. return errors.Wrap(err, "save avatar")
  362. }
  363. return db.WithContext(ctx).
  364. Model(&User{}).
  365. Where("id = ?", userID).
  366. Updates(map[string]interface{}{
  367. "use_custom_avatar": true,
  368. "updated_unix": db.NowFunc().Unix(),
  369. }).
  370. Error
  371. }
  372. // UserType indicates the type of the user account.
  373. type UserType int
  374. const (
  375. UserTypeIndividual UserType = iota // NOTE: Historic reason to make it starts at 0.
  376. UserTypeOrganization
  377. )
  378. // User represents the object of an individual or an organization.
  379. type User struct {
  380. ID int64 `gorm:"primaryKey"`
  381. LowerName string `xorm:"UNIQUE NOT NULL" gorm:"unique;not null"`
  382. Name string `xorm:"UNIQUE NOT NULL" gorm:"not null"`
  383. FullName string
  384. // Email is the primary email address (to be used for communication)
  385. Email string `xorm:"NOT NULL" gorm:"not null"`
  386. Password string `xorm:"passwd NOT NULL" gorm:"column:passwd;not null"`
  387. LoginSource int64 `xorm:"NOT NULL DEFAULT 0" gorm:"not null;default:0"`
  388. LoginName string
  389. Type UserType
  390. OwnedOrgs []*User `xorm:"-" gorm:"-" json:"-"`
  391. Orgs []*User `xorm:"-" gorm:"-" json:"-"`
  392. Repos []*Repository `xorm:"-" gorm:"-" json:"-"`
  393. Location string
  394. Website string
  395. Rands string `xorm:"VARCHAR(10)" gorm:"type:VARCHAR(10)"`
  396. Salt string `xorm:"VARCHAR(10)" gorm:"type:VARCHAR(10)"`
  397. Created time.Time `xorm:"-" gorm:"-" json:"-"`
  398. CreatedUnix int64
  399. Updated time.Time `xorm:"-" gorm:"-" json:"-"`
  400. UpdatedUnix int64
  401. // Remember visibility choice for convenience, true for private
  402. LastRepoVisibility bool
  403. // Maximum repository creation limit, -1 means use global default
  404. MaxRepoCreation int `xorm:"NOT NULL DEFAULT -1" gorm:"not null;default:-1"`
  405. // Permissions
  406. IsActive bool // Activate primary email
  407. IsAdmin bool
  408. AllowGitHook bool
  409. AllowImportLocal bool // Allow migrate repository by local path
  410. ProhibitLogin bool
  411. // Avatar
  412. Avatar string `xorm:"VARCHAR(2048) NOT NULL" gorm:"type:VARCHAR(2048);not null"`
  413. AvatarEmail string `xorm:"NOT NULL" gorm:"not null"`
  414. UseCustomAvatar bool
  415. // Counters
  416. NumFollowers int
  417. NumFollowing int `xorm:"NOT NULL DEFAULT 0" gorm:"not null;default:0"`
  418. NumStars int
  419. NumRepos int
  420. // For organization
  421. Description string
  422. NumTeams int
  423. NumMembers int
  424. Teams []*Team `xorm:"-" gorm:"-" json:"-"`
  425. Members []*User `xorm:"-" gorm:"-" json:"-"`
  426. }
  427. // BeforeCreate implements the GORM create hook.
  428. func (u *User) BeforeCreate(tx *gorm.DB) error {
  429. if u.CreatedUnix == 0 {
  430. u.CreatedUnix = tx.NowFunc().Unix()
  431. u.UpdatedUnix = u.CreatedUnix
  432. }
  433. return nil
  434. }
  435. // AfterFind implements the GORM query hook.
  436. func (u *User) AfterFind(_ *gorm.DB) error {
  437. u.Created = time.Unix(u.CreatedUnix, 0).Local()
  438. u.Updated = time.Unix(u.UpdatedUnix, 0).Local()
  439. return nil
  440. }
  441. // IsLocal returns true if user is created as local account.
  442. func (u *User) IsLocal() bool {
  443. return u.LoginSource <= 0
  444. }
  445. // APIFormat returns the API format of a user.
  446. func (u *User) APIFormat() *api.User {
  447. return &api.User{
  448. ID: u.ID,
  449. UserName: u.Name,
  450. Login: u.Name,
  451. FullName: u.FullName,
  452. Email: u.Email,
  453. AvatarUrl: u.AvatarURL(),
  454. }
  455. }
  456. // maxNumRepos returns the maximum number of repositories that the user can have
  457. // direct ownership.
  458. func (u *User) maxNumRepos() int {
  459. if u.MaxRepoCreation <= -1 {
  460. return conf.Repository.MaxCreationLimit
  461. }
  462. return u.MaxRepoCreation
  463. }
  464. // canCreateRepo returns true if the user can create a repository.
  465. func (u *User) canCreateRepo() bool {
  466. return u.maxNumRepos() <= -1 || u.NumRepos < u.maxNumRepos()
  467. }
  468. // CanCreateOrganization returns true if user can create organizations.
  469. func (u *User) CanCreateOrganization() bool {
  470. return !conf.Admin.DisableRegularOrgCreation || u.IsAdmin
  471. }
  472. // CanEditGitHook returns true if user can edit Git hooks.
  473. func (u *User) CanEditGitHook() bool {
  474. return u.IsAdmin || u.AllowGitHook
  475. }
  476. // CanImportLocal returns true if user can migrate repositories by local path.
  477. func (u *User) CanImportLocal() bool {
  478. return conf.Repository.EnableLocalPathMigration && (u.IsAdmin || u.AllowImportLocal)
  479. }
  480. // HomeURLPath returns the URL path to the user or organization home page.
  481. //
  482. // TODO(unknwon): This is also used in templates, which should be fixed by
  483. // having a dedicated type `template.User` and move this to the "userutil"
  484. // package.
  485. func (u *User) HomeURLPath() string {
  486. return conf.Server.Subpath + "/" + u.Name
  487. }
  488. // HTMLURL returns the full URL to the user or organization home page.
  489. //
  490. // TODO(unknwon): This is also used in templates, which should be fixed by
  491. // having a dedicated type `template.User` and move this to the "userutil"
  492. // package.
  493. func (u *User) HTMLURL() string {
  494. return conf.Server.ExternalURL + u.Name
  495. }
  496. // AvatarURLPath returns the URL path to the user or organization avatar. If the
  497. // user enables Gravatar-like service, then an external URL will be returned.
  498. //
  499. // TODO(unknwon): This is also used in templates, which should be fixed by
  500. // having a dedicated type `template.User` and move this to the "userutil"
  501. // package.
  502. func (u *User) AvatarURLPath() string {
  503. defaultURLPath := conf.UserDefaultAvatarURLPath()
  504. if u.ID <= 0 {
  505. return defaultURLPath
  506. }
  507. hasCustomAvatar := osutil.IsFile(userutil.CustomAvatarPath(u.ID))
  508. switch {
  509. case u.UseCustomAvatar:
  510. if !hasCustomAvatar {
  511. return defaultURLPath
  512. }
  513. return fmt.Sprintf("%s/%s/%d", conf.Server.Subpath, conf.UsersAvatarPathPrefix, u.ID)
  514. case conf.Picture.DisableGravatar:
  515. if !hasCustomAvatar {
  516. if err := userutil.GenerateRandomAvatar(u.ID, u.Name, u.Email); err != nil {
  517. log.Error("Failed to generate random avatar [user_id: %d]: %v", u.ID, err)
  518. }
  519. }
  520. return fmt.Sprintf("%s/%s/%d", conf.Server.Subpath, conf.UsersAvatarPathPrefix, u.ID)
  521. }
  522. return tool.AvatarLink(u.AvatarEmail)
  523. }
  524. // AvatarURL returns the full URL to the user or organization avatar. If the
  525. // user enables Gravatar-like service, then an external URL will be returned.
  526. //
  527. // TODO(unknwon): This is also used in templates, which should be fixed by
  528. // having a dedicated type `template.User` and move this to the "userutil"
  529. // package.
  530. func (u *User) AvatarURL() string {
  531. link := u.AvatarURLPath()
  532. if link[0] == '/' && link[1] != '/' {
  533. return conf.Server.ExternalURL + strings.TrimPrefix(link, conf.Server.Subpath)[1:]
  534. }
  535. return link
  536. }
  537. // IsFollowing returns true if the user is following the given user.
  538. //
  539. // TODO(unknwon): This is also used in templates, which should be fixed by
  540. // having a dedicated type `template.User`.
  541. func (u *User) IsFollowing(followID int64) bool {
  542. return Follows.IsFollowing(context.TODO(), u.ID, followID)
  543. }