code.gitea.io/gitea@v1.22.3/routers/api/v1/user/follower.go (about)

     1  // Copyright 2015 The Gogs Authors. All rights reserved.
     2  // Copyright 2020 The Gitea Authors.
     3  // SPDX-License-Identifier: MIT
     4  
     5  package user
     6  
     7  import (
     8  	"errors"
     9  	"net/http"
    10  
    11  	user_model "code.gitea.io/gitea/models/user"
    12  	api "code.gitea.io/gitea/modules/structs"
    13  	"code.gitea.io/gitea/routers/api/v1/utils"
    14  	"code.gitea.io/gitea/services/context"
    15  	"code.gitea.io/gitea/services/convert"
    16  )
    17  
    18  func responseAPIUsers(ctx *context.APIContext, users []*user_model.User) {
    19  	apiUsers := make([]*api.User, len(users))
    20  	for i := range users {
    21  		apiUsers[i] = convert.ToUser(ctx, users[i], ctx.Doer)
    22  	}
    23  	ctx.JSON(http.StatusOK, &apiUsers)
    24  }
    25  
    26  func listUserFollowers(ctx *context.APIContext, u *user_model.User) {
    27  	users, count, err := user_model.GetUserFollowers(ctx, u, ctx.Doer, utils.GetListOptions(ctx))
    28  	if err != nil {
    29  		ctx.Error(http.StatusInternalServerError, "GetUserFollowers", err)
    30  		return
    31  	}
    32  
    33  	ctx.SetTotalCountHeader(count)
    34  	responseAPIUsers(ctx, users)
    35  }
    36  
    37  // ListMyFollowers list the authenticated user's followers
    38  func ListMyFollowers(ctx *context.APIContext) {
    39  	// swagger:operation GET /user/followers user userCurrentListFollowers
    40  	// ---
    41  	// summary: List the authenticated user's followers
    42  	// parameters:
    43  	// - name: page
    44  	//   in: query
    45  	//   description: page number of results to return (1-based)
    46  	//   type: integer
    47  	// - name: limit
    48  	//   in: query
    49  	//   description: page size of results
    50  	//   type: integer
    51  	// produces:
    52  	// - application/json
    53  	// responses:
    54  	//   "200":
    55  	//     "$ref": "#/responses/UserList"
    56  
    57  	listUserFollowers(ctx, ctx.Doer)
    58  }
    59  
    60  // ListFollowers list the given user's followers
    61  func ListFollowers(ctx *context.APIContext) {
    62  	// swagger:operation GET /users/{username}/followers user userListFollowers
    63  	// ---
    64  	// summary: List the given user's followers
    65  	// produces:
    66  	// - application/json
    67  	// parameters:
    68  	// - name: username
    69  	//   in: path
    70  	//   description: username of user
    71  	//   type: string
    72  	//   required: true
    73  	// - name: page
    74  	//   in: query
    75  	//   description: page number of results to return (1-based)
    76  	//   type: integer
    77  	// - name: limit
    78  	//   in: query
    79  	//   description: page size of results
    80  	//   type: integer
    81  	// responses:
    82  	//   "200":
    83  	//     "$ref": "#/responses/UserList"
    84  	//   "404":
    85  	//     "$ref": "#/responses/notFound"
    86  
    87  	listUserFollowers(ctx, ctx.ContextUser)
    88  }
    89  
    90  func listUserFollowing(ctx *context.APIContext, u *user_model.User) {
    91  	users, count, err := user_model.GetUserFollowing(ctx, u, ctx.Doer, utils.GetListOptions(ctx))
    92  	if err != nil {
    93  		ctx.Error(http.StatusInternalServerError, "GetUserFollowing", err)
    94  		return
    95  	}
    96  
    97  	ctx.SetTotalCountHeader(count)
    98  	responseAPIUsers(ctx, users)
    99  }
   100  
   101  // ListMyFollowing list the users that the authenticated user is following
   102  func ListMyFollowing(ctx *context.APIContext) {
   103  	// swagger:operation GET /user/following user userCurrentListFollowing
   104  	// ---
   105  	// summary: List the users that the authenticated user is following
   106  	// parameters:
   107  	// - name: page
   108  	//   in: query
   109  	//   description: page number of results to return (1-based)
   110  	//   type: integer
   111  	// - name: limit
   112  	//   in: query
   113  	//   description: page size of results
   114  	//   type: integer
   115  	// produces:
   116  	// - application/json
   117  	// responses:
   118  	//   "200":
   119  	//     "$ref": "#/responses/UserList"
   120  
   121  	listUserFollowing(ctx, ctx.Doer)
   122  }
   123  
   124  // ListFollowing list the users that the given user is following
   125  func ListFollowing(ctx *context.APIContext) {
   126  	// swagger:operation GET /users/{username}/following user userListFollowing
   127  	// ---
   128  	// summary: List the users that the given user is following
   129  	// produces:
   130  	// - application/json
   131  	// parameters:
   132  	// - name: username
   133  	//   in: path
   134  	//   description: username of user
   135  	//   type: string
   136  	//   required: true
   137  	// - name: page
   138  	//   in: query
   139  	//   description: page number of results to return (1-based)
   140  	//   type: integer
   141  	// - name: limit
   142  	//   in: query
   143  	//   description: page size of results
   144  	//   type: integer
   145  	// responses:
   146  	//   "200":
   147  	//     "$ref": "#/responses/UserList"
   148  	//   "404":
   149  	//     "$ref": "#/responses/notFound"
   150  
   151  	listUserFollowing(ctx, ctx.ContextUser)
   152  }
   153  
   154  func checkUserFollowing(ctx *context.APIContext, u *user_model.User, followID int64) {
   155  	if user_model.IsFollowing(ctx, u.ID, followID) {
   156  		ctx.Status(http.StatusNoContent)
   157  	} else {
   158  		ctx.NotFound()
   159  	}
   160  }
   161  
   162  // CheckMyFollowing whether the given user is followed by the authenticated user
   163  func CheckMyFollowing(ctx *context.APIContext) {
   164  	// swagger:operation GET /user/following/{username} user userCurrentCheckFollowing
   165  	// ---
   166  	// summary: Check whether a user is followed by the authenticated user
   167  	// parameters:
   168  	// - name: username
   169  	//   in: path
   170  	//   description: username of followed user
   171  	//   type: string
   172  	//   required: true
   173  	// responses:
   174  	//   "204":
   175  	//     "$ref": "#/responses/empty"
   176  	//   "404":
   177  	//     "$ref": "#/responses/notFound"
   178  
   179  	checkUserFollowing(ctx, ctx.Doer, ctx.ContextUser.ID)
   180  }
   181  
   182  // CheckFollowing check if one user is following another user
   183  func CheckFollowing(ctx *context.APIContext) {
   184  	// swagger:operation GET /users/{username}/following/{target} user userCheckFollowing
   185  	// ---
   186  	// summary: Check if one user is following another user
   187  	// parameters:
   188  	// - name: username
   189  	//   in: path
   190  	//   description: username of following user
   191  	//   type: string
   192  	//   required: true
   193  	// - name: target
   194  	//   in: path
   195  	//   description: username of followed user
   196  	//   type: string
   197  	//   required: true
   198  	// responses:
   199  	//   "204":
   200  	//     "$ref": "#/responses/empty"
   201  	//   "404":
   202  	//     "$ref": "#/responses/notFound"
   203  
   204  	target := GetUserByParamsName(ctx, ":target")
   205  	if ctx.Written() {
   206  		return
   207  	}
   208  	checkUserFollowing(ctx, ctx.ContextUser, target.ID)
   209  }
   210  
   211  // Follow follow a user
   212  func Follow(ctx *context.APIContext) {
   213  	// swagger:operation PUT /user/following/{username} user userCurrentPutFollow
   214  	// ---
   215  	// summary: Follow a user
   216  	// parameters:
   217  	// - name: username
   218  	//   in: path
   219  	//   description: username of user to follow
   220  	//   type: string
   221  	//   required: true
   222  	// responses:
   223  	//   "204":
   224  	//     "$ref": "#/responses/empty"
   225  	//   "403":
   226  	//     "$ref": "#/responses/forbidden"
   227  	//   "404":
   228  	//     "$ref": "#/responses/notFound"
   229  
   230  	if err := user_model.FollowUser(ctx, ctx.Doer, ctx.ContextUser); err != nil {
   231  		if errors.Is(err, user_model.ErrBlockedUser) {
   232  			ctx.Error(http.StatusForbidden, "FollowUser", err)
   233  		} else {
   234  			ctx.Error(http.StatusInternalServerError, "FollowUser", err)
   235  		}
   236  		return
   237  	}
   238  	ctx.Status(http.StatusNoContent)
   239  }
   240  
   241  // Unfollow unfollow a user
   242  func Unfollow(ctx *context.APIContext) {
   243  	// swagger:operation DELETE /user/following/{username} user userCurrentDeleteFollow
   244  	// ---
   245  	// summary: Unfollow a user
   246  	// parameters:
   247  	// - name: username
   248  	//   in: path
   249  	//   description: username of user to unfollow
   250  	//   type: string
   251  	//   required: true
   252  	// responses:
   253  	//   "204":
   254  	//     "$ref": "#/responses/empty"
   255  	//   "404":
   256  	//     "$ref": "#/responses/notFound"
   257  
   258  	if err := user_model.UnfollowUser(ctx, ctx.Doer.ID, ctx.ContextUser.ID); err != nil {
   259  		ctx.Error(http.StatusInternalServerError, "UnfollowUser", err)
   260  		return
   261  	}
   262  	ctx.Status(http.StatusNoContent)
   263  }