2018-05-17 09:35:00 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2018-05-17 09:35:00 +05:30
|
|
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-09-22 11:08:34 +05:30
|
|
|
"io"
|
2022-01-21 23:29:26 +05:30
|
|
|
"math/big"
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
2020-09-25 09:39:23 +05:30
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-02-24 23:43:49 +05:30
|
|
|
"slices"
|
2018-05-17 09:35:00 +05:30
|
|
|
"strings"
|
|
|
|
|
2023-12-28 15:08:59 +05:30
|
|
|
"code.gitea.io/gitea/models/avatars"
|
2021-09-24 17:02:56 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-11 12:33:30 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2018-05-17 09:35:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-02-04 18:59:09 +05:30
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2018-05-17 09:35:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-06-26 19:49:22 +05:30
|
|
|
"code.gitea.io/gitea/modules/translation"
|
2021-06-05 18:02:19 +05:30
|
|
|
"code.gitea.io/gitea/modules/typesniffer"
|
2020-12-04 11:50:30 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-03-07 13:42:43 +05:30
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2024-02-27 12:42:22 +05:30
|
|
|
"code.gitea.io/gitea/services/context"
|
2021-04-07 01:14:05 +05:30
|
|
|
"code.gitea.io/gitea/services/forms"
|
2021-11-22 20:51:55 +05:30
|
|
|
user_service "code.gitea.io/gitea/services/user"
|
2018-05-17 09:35:00 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplSettingsProfile base.TplName = "user/settings/profile"
|
2021-10-27 21:10:08 +05:30
|
|
|
tplSettingsAppearance base.TplName = "user/settings/appearance"
|
2018-05-17 09:35:00 +05:30
|
|
|
tplSettingsOrganization base.TplName = "user/settings/organization"
|
|
|
|
tplSettingsRepositories base.TplName = "user/settings/repos"
|
|
|
|
)
|
|
|
|
|
2024-02-27 01:13:04 +05:30
|
|
|
// must be kept in sync with `web_src/js/features/user-settings.js`
|
2024-02-24 23:54:20 +05:30
|
|
|
var recognisedPronouns = []string{"", "he/him", "she/her", "they/them", "it/its", "any pronouns"}
|
2024-02-24 23:43:49 +05:30
|
|
|
|
2018-05-17 09:35:00 +05:30
|
|
|
// Profile render user's profile page
|
|
|
|
func Profile(ctx *context.Context) {
|
2023-02-02 04:26:10 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("settings.profile")
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Data["PageIsSettingsProfile"] = true
|
2021-06-28 00:17:35 +05:30
|
|
|
ctx.Data["AllowedUserVisibilityModes"] = setting.Service.AllowedUserVisibilityModesSlice.ToVisibleTypeSlice()
|
2023-10-05 06:38:19 +05:30
|
|
|
ctx.Data["DisableGravatar"] = setting.Config().Picture.DisableGravatar.Value(ctx)
|
2024-02-24 23:43:49 +05:30
|
|
|
ctx.Data["PronounsAreCustom"] = !slices.Contains(recognisedPronouns, ctx.Doer.Pronouns)
|
2018-06-18 23:54:45 +05:30
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSettingsProfile)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ProfilePost response for change user's profile
|
2021-01-26 21:06:53 +05:30
|
|
|
func ProfilePost(ctx *context.Context) {
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsProfile"] = true
|
2023-08-08 13:59:14 +05:30
|
|
|
ctx.Data["AllowedUserVisibilityModes"] = setting.Service.AllowedUserVisibilityModesSlice.ToVisibleTypeSlice()
|
2023-10-05 06:38:19 +05:30
|
|
|
ctx.Data["DisableGravatar"] = setting.Config().Picture.DisableGravatar.Value(ctx)
|
2024-02-24 23:43:49 +05:30
|
|
|
ctx.Data["PronounsAreCustom"] = !slices.Contains(recognisedPronouns, ctx.Doer.Pronouns)
|
2018-05-17 09:35:00 +05:30
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSettingsProfile)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-04 18:59:09 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.UpdateProfileForm)
|
|
|
|
|
|
|
|
if form.Name != "" {
|
|
|
|
if err := user_service.RenameUser(ctx, ctx.Doer, form.Name); err != nil {
|
|
|
|
switch {
|
|
|
|
case user_model.IsErrUserIsNotLocal(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("form.username_change_not_local_user"))
|
|
|
|
case user_model.IsErrUserAlreadyExist(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("form.username_been_taken"))
|
|
|
|
case db.IsErrNameReserved(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("user.form.name_reserved", form.Name))
|
|
|
|
case db.IsErrNamePatternNotAllowed(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("user.form.name_pattern_not_allowed", form.Name))
|
|
|
|
case db.IsErrNameCharsNotAllowed(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("user.form.name_chars_not_allowed", form.Name))
|
|
|
|
default:
|
|
|
|
ctx.ServerError("RenameUser", err)
|
|
|
|
return
|
|
|
|
}
|
2021-01-10 17:44:02 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
|
|
|
return
|
|
|
|
}
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
|
2024-02-04 18:59:09 +05:30
|
|
|
opts := &user_service.UpdateOptions{
|
|
|
|
FullName: optional.Some(form.FullName),
|
|
|
|
KeepEmailPrivate: optional.Some(form.KeepEmailPrivate),
|
|
|
|
Description: optional.Some(form.Description),
|
2023-09-26 08:09:12 +05:30
|
|
|
Pronouns: optional.Some(form.Pronouns),
|
2024-02-04 18:59:09 +05:30
|
|
|
Website: optional.Some(form.Website),
|
|
|
|
Location: optional.Some(form.Location),
|
|
|
|
Visibility: optional.Some(form.Visibility),
|
|
|
|
KeepActivityPrivate: optional.Some(form.KeepActivityPrivate),
|
|
|
|
}
|
|
|
|
if err := user_service.UpdateUser(ctx, ctx.Doer, opts); err != nil {
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.ServerError("UpdateUser", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
log.Trace("User settings updated: %s", ctx.Doer.Name)
|
2023-02-05 21:36:26 +05:30
|
|
|
ctx.Flash.Success(ctx.Tr("settings.update_profile_success"))
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAvatarSetting update user's avatar
|
|
|
|
// FIXME: limit size.
|
2021-11-24 15:19:20 +05:30
|
|
|
func UpdateAvatarSetting(ctx *context.Context, form *forms.AvatarForm, ctxUser *user_model.User) error {
|
2021-04-07 01:14:05 +05:30
|
|
|
ctxUser.UseCustomAvatar = form.Source == forms.AvatarLocal
|
2018-05-17 09:35:00 +05:30
|
|
|
if len(form.Gravatar) > 0 {
|
2020-10-23 23:25:10 +05:30
|
|
|
if form.Avatar != nil {
|
2023-12-28 15:08:59 +05:30
|
|
|
ctxUser.Avatar = avatars.HashEmail(form.Gravatar)
|
2020-10-23 23:25:10 +05:30
|
|
|
} else {
|
|
|
|
ctxUser.Avatar = ""
|
|
|
|
}
|
2018-05-17 09:35:00 +05:30
|
|
|
ctxUser.AvatarEmail = form.Gravatar
|
|
|
|
}
|
|
|
|
|
2018-08-01 15:08:56 +05:30
|
|
|
if form.Avatar != nil && form.Avatar.Filename != "" {
|
2018-05-17 09:35:00 +05:30
|
|
|
fr, err := form.Avatar.Open()
|
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("Avatar.Open: %w", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
defer fr.Close()
|
|
|
|
|
2020-10-14 18:37:51 +05:30
|
|
|
if form.Avatar.Size > setting.Avatar.MaxFileSize {
|
2024-02-15 03:18:45 +05:30
|
|
|
return errors.New(ctx.Locale.TrString("settings.uploaded_avatar_is_too_big", form.Avatar.Size/1024, setting.Avatar.MaxFileSize/1024))
|
2019-05-30 07:52:26 +05:30
|
|
|
}
|
|
|
|
|
2021-09-22 11:08:34 +05:30
|
|
|
data, err := io.ReadAll(fr)
|
2018-05-17 09:35:00 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("io.ReadAll: %w", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
2021-06-05 18:02:19 +05:30
|
|
|
|
|
|
|
st := typesniffer.DetectContentType(data)
|
|
|
|
if !(st.IsImage() && !st.IsSvgImage()) {
|
2024-02-15 03:18:45 +05:30
|
|
|
return errors.New(ctx.Locale.TrString("settings.uploaded_avatar_not_a_image"))
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
2023-10-11 09:54:07 +05:30
|
|
|
if err = user_service.UploadAvatar(ctx, ctxUser, data); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("UploadAvatar: %w", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
2020-10-14 18:37:51 +05:30
|
|
|
} else if ctxUser.UseCustomAvatar && ctxUser.Avatar == "" {
|
2018-05-17 09:35:00 +05:30
|
|
|
// No avatar is uploaded but setting has been changed to enable,
|
|
|
|
// generate a random one when needed.
|
2022-05-20 19:38:52 +05:30
|
|
|
if err := user_model.GenerateRandomAvatar(ctx, ctxUser); err != nil {
|
2019-06-13 01:11:28 +05:30
|
|
|
log.Error("GenerateRandomAvatar[%d]: %v", ctxUser.ID, err)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 20:52:54 +05:30
|
|
|
if err := user_model.UpdateUserCols(ctx, ctxUser, "avatar", "avatar_email", "use_custom_avatar"); err != nil {
|
2024-02-04 18:59:09 +05:30
|
|
|
return fmt.Errorf("UpdateUserCols: %w", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AvatarPost response for change user's avatar request
|
2021-01-26 21:06:53 +05:30
|
|
|
func AvatarPost(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.AvatarForm)
|
2022-03-22 12:33:22 +05:30
|
|
|
if err := UpdateAvatarSetting(ctx, form, ctx.Doer); err != nil {
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Flash.Error(err.Error())
|
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.update_avatar_success"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAvatar render delete avatar page
|
|
|
|
func DeleteAvatar(ctx *context.Context) {
|
2023-10-11 09:54:07 +05:30
|
|
|
if err := user_service.DeleteAvatar(ctx, ctx.Doer); err != nil {
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Flash.Error(err.Error())
|
|
|
|
}
|
|
|
|
|
2023-08-23 15:06:57 +05:30
|
|
|
ctx.JSONRedirect(setting.AppSubURL + "/user/settings")
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Organization render all the organization of the user
|
|
|
|
func Organization(ctx *context.Context) {
|
2023-02-02 04:26:10 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("settings.organization")
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Data["PageIsSettingsOrganization"] = true
|
2021-11-22 19:21:45 +05:30
|
|
|
|
2022-03-29 11:59:02 +05:30
|
|
|
opts := organization.FindOrgOptions{
|
2021-11-22 19:21:45 +05:30
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
PageSize: setting.UI.Admin.UserPagingNum,
|
|
|
|
Page: ctx.FormInt("page"),
|
|
|
|
},
|
2022-03-22 12:33:22 +05:30
|
|
|
UserID: ctx.Doer.ID,
|
2021-11-22 19:21:45 +05:30
|
|
|
IncludePrivate: ctx.IsSigned,
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Page <= 0 {
|
|
|
|
opts.Page = 1
|
|
|
|
}
|
|
|
|
|
2023-11-24 09:19:41 +05:30
|
|
|
orgs, total, err := db.FindAndCount[organization.Organization](ctx, opts)
|
2021-11-22 19:21:45 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("FindOrgs", err)
|
|
|
|
return
|
|
|
|
}
|
2023-11-24 09:19:41 +05:30
|
|
|
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Data["Orgs"] = orgs
|
2021-11-22 19:21:45 +05:30
|
|
|
pager := context.NewPagination(int(total), opts.PageSize, opts.Page, 5)
|
|
|
|
pager.SetDefaultParams(ctx)
|
|
|
|
ctx.Data["Page"] = pager
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSettingsOrganization)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Repos display a list of all repositories of the user
|
|
|
|
func Repos(ctx *context.Context) {
|
2023-02-02 04:26:10 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("settings.repos")
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Data["PageIsSettingsRepos"] = true
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.Data["allowAdopt"] = ctx.IsUserSiteAdmin() || setting.Repository.AllowAdoptionOfUnadoptedRepositories
|
|
|
|
ctx.Data["allowDelete"] = ctx.IsUserSiteAdmin() || setting.Repository.AllowDeleteOfUnadoptedRepositories
|
2018-05-17 09:35:00 +05:30
|
|
|
|
2021-09-24 17:02:56 +05:30
|
|
|
opts := db.ListOptions{
|
2020-09-25 09:39:23 +05:30
|
|
|
PageSize: setting.UI.Admin.UserPagingNum,
|
2021-07-29 07:12:15 +05:30
|
|
|
Page: ctx.FormInt("page"),
|
2020-09-25 09:39:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Page <= 0 {
|
|
|
|
opts.Page = 1
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
2020-09-25 09:39:23 +05:30
|
|
|
start := (opts.Page - 1) * opts.PageSize
|
|
|
|
end := start + opts.PageSize
|
2018-05-17 09:35:00 +05:30
|
|
|
|
2020-09-25 09:39:23 +05:30
|
|
|
adoptOrDelete := ctx.IsUserSiteAdmin() || (setting.Repository.AllowAdoptionOfUnadoptedRepositories && setting.Repository.AllowDeleteOfUnadoptedRepositories)
|
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
ctxUser := ctx.Doer
|
2020-09-25 09:39:23 +05:30
|
|
|
count := 0
|
|
|
|
|
|
|
|
if adoptOrDelete {
|
|
|
|
repoNames := make([]string, 0, setting.UI.Admin.UserPagingNum)
|
2021-12-10 06:57:50 +05:30
|
|
|
repos := map[string]*repo_model.Repository{}
|
2020-09-25 09:39:23 +05:30
|
|
|
// We're going to iterate by pagesize.
|
2021-11-24 15:19:20 +05:30
|
|
|
root := user_model.UserPath(ctxUser.Name)
|
2023-01-16 21:51:44 +05:30
|
|
|
if err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
|
2018-05-17 09:35:00 +05:30
|
|
|
if err != nil {
|
2020-12-31 13:15:54 +05:30
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-25 09:39:23 +05:30
|
|
|
return err
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
2023-01-16 21:51:44 +05:30
|
|
|
if !d.IsDir() || path == root {
|
2020-09-25 09:39:23 +05:30
|
|
|
return nil
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
2023-01-16 21:51:44 +05:30
|
|
|
name := d.Name()
|
2020-09-25 09:39:23 +05:30
|
|
|
if !strings.HasSuffix(name, ".git") {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
name = name[:len(name)-4]
|
2021-12-12 21:18:20 +05:30
|
|
|
if repo_model.IsUsableRepoName(name) != nil || strings.ToLower(name) != name {
|
2020-09-25 09:39:23 +05:30
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
if count >= start && count < end {
|
|
|
|
repoNames = append(repoNames, name)
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
return filepath.SkipDir
|
|
|
|
}); err != nil {
|
2023-01-16 21:51:44 +05:30
|
|
|
ctx.ServerError("filepath.WalkDir", err)
|
2020-09-25 09:39:23 +05:30
|
|
|
return
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
|
2023-10-15 21:16:06 +05:30
|
|
|
userRepos, _, err := repo_model.GetUserRepositories(ctx, &repo_model.SearchRepoOptions{
|
2021-11-22 20:51:55 +05:30
|
|
|
Actor: ctxUser,
|
|
|
|
Private: true,
|
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
Page: 1,
|
|
|
|
PageSize: setting.UI.Admin.UserPagingNum,
|
|
|
|
},
|
|
|
|
LowerNames: repoNames,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetUserRepositories", err)
|
2020-09-25 09:39:23 +05:30
|
|
|
return
|
|
|
|
}
|
2021-11-22 20:51:55 +05:30
|
|
|
for _, repo := range userRepos {
|
2020-09-25 09:39:23 +05:30
|
|
|
if repo.IsFork {
|
2022-12-03 08:18:26 +05:30
|
|
|
if err := repo.GetBaseRepo(ctx); err != nil {
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.ServerError("GetBaseRepo", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
repos[repo.LowerName] = repo
|
|
|
|
}
|
|
|
|
ctx.Data["Dirs"] = repoNames
|
|
|
|
ctx.Data["ReposMap"] = repos
|
|
|
|
} else {
|
2023-10-15 21:16:06 +05:30
|
|
|
repos, count64, err := repo_model.GetUserRepositories(ctx, &repo_model.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: opts})
|
2020-09-25 09:39:23 +05:30
|
|
|
if err != nil {
|
2021-11-22 20:51:55 +05:30
|
|
|
ctx.ServerError("GetUserRepositories", err)
|
2020-09-25 09:39:23 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
count = int(count64)
|
|
|
|
|
|
|
|
for i := range repos {
|
|
|
|
if repos[i].IsFork {
|
2022-12-03 08:18:26 +05:30
|
|
|
if err := repos[i].GetBaseRepo(ctx); err != nil {
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.ServerError("GetBaseRepo", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["Repos"] = repos
|
|
|
|
}
|
2023-04-20 23:03:30 +05:30
|
|
|
ctx.Data["ContextUser"] = ctxUser
|
2022-06-20 15:32:49 +05:30
|
|
|
pager := context.NewPagination(count, opts.PageSize, opts.Page, 5)
|
2020-09-25 09:39:23 +05:30
|
|
|
pager.SetDefaultParams(ctx)
|
|
|
|
ctx.Data["Page"] = pager
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSettingsRepositories)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
2021-10-27 21:10:08 +05:30
|
|
|
|
|
|
|
// Appearance render user's appearance settings
|
|
|
|
func Appearance(ctx *context.Context) {
|
2023-02-02 04:26:10 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("settings.appearance")
|
2021-10-27 21:10:08 +05:30
|
|
|
ctx.Data["PageIsSettingsAppearance"] = true
|
|
|
|
|
2022-01-21 23:29:26 +05:30
|
|
|
var hiddenCommentTypes *big.Int
|
2023-09-15 11:43:19 +05:30
|
|
|
val, err := user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyHiddenCommentTypes)
|
2022-01-21 23:29:26 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetUserSetting", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hiddenCommentTypes, _ = new(big.Int).SetString(val, 10) // we can safely ignore the failed conversion here
|
|
|
|
|
|
|
|
ctx.Data["IsCommentTypeGroupChecked"] = func(commentTypeGroup string) bool {
|
|
|
|
return forms.IsUserHiddenCommentTypeGroupChecked(commentTypeGroup, hiddenCommentTypes)
|
|
|
|
}
|
|
|
|
|
2021-10-27 21:10:08 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSettingsAppearance)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUIThemePost is used to update users' specific theme
|
|
|
|
func UpdateUIThemePost(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*forms.UpdateThemeForm)
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsAppearance"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !form.IsThemeExists() {
|
|
|
|
ctx.Flash.Error(ctx.Tr("settings.theme_update_error"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-04 18:59:09 +05:30
|
|
|
opts := &user_service.UpdateOptions{
|
|
|
|
Theme: optional.Some(form.Theme),
|
|
|
|
}
|
|
|
|
if err := user_service.UpdateUser(ctx, ctx.Doer, opts); err != nil {
|
2021-10-27 21:10:08 +05:30
|
|
|
ctx.Flash.Error(ctx.Tr("settings.theme_update_error"))
|
2024-02-04 18:59:09 +05:30
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.theme_update_success"))
|
2021-10-27 21:10:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUserLang update a user's language
|
|
|
|
func UpdateUserLang(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*forms.UpdateLanguageForm)
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsAppearance"] = true
|
|
|
|
|
2024-02-04 18:59:09 +05:30
|
|
|
if form.Language != "" {
|
Improve utils of slices (#22379)
- Move the file `compare.go` and `slice.go` to `slice.go`.
- Fix `ExistsInSlice`, it's buggy
- It uses `sort.Search`, so it assumes that the input slice is sorted.
- It passes `func(i int) bool { return slice[i] == target })` to
`sort.Search`, that's incorrect, check the doc of `sort.Search`.
- Conbine `IsInt64InSlice(int64, []int64)` and `ExistsInSlice(string,
[]string)` to `SliceContains[T]([]T, T)`.
- Conbine `IsSliceInt64Eq([]int64, []int64)` and `IsEqualSlice([]string,
[]string)` to `SliceSortedEqual[T]([]T, T)`.
- Add `SliceEqual[T]([]T, T)` as a distinction from
`SliceSortedEqual[T]([]T, T)`.
- Redesign `RemoveIDFromList([]int64, int64) ([]int64, bool)` to
`SliceRemoveAll[T]([]T, T) []T`.
- Add `SliceContainsFunc[T]([]T, func(T) bool)` and
`SliceRemoveAllFunc[T]([]T, func(T) bool)` for general use.
- Add comments to explain why not `golang.org/x/exp/slices`.
- Add unit tests.
2023-01-11 11:01:16 +05:30
|
|
|
if !util.SliceContainsString(setting.Langs, form.Language) {
|
2021-10-27 21:10:08 +05:30
|
|
|
ctx.Flash.Error(ctx.Tr("settings.update_language_not_found", form.Language))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-04 18:59:09 +05:30
|
|
|
opts := &user_service.UpdateOptions{
|
|
|
|
Language: optional.Some(form.Language),
|
|
|
|
}
|
|
|
|
if err := user_service.UpdateUser(ctx, ctx.Doer, opts); err != nil {
|
|
|
|
ctx.ServerError("UpdateUser", err)
|
2021-10-27 21:10:08 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the language to the one we just set
|
2022-03-22 12:33:22 +05:30
|
|
|
middleware.SetLocaleCookie(ctx.Resp, ctx.Doer.Language, 0)
|
2021-10-27 21:10:08 +05:30
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
log.Trace("User settings updated: %s", ctx.Doer.Name)
|
2024-02-15 03:18:45 +05:30
|
|
|
ctx.Flash.Success(translation.NewLocale(ctx.Doer.Language).TrString("settings.update_language_success"))
|
2021-10-27 21:10:08 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
|
|
|
|
}
|
2022-01-21 23:29:26 +05:30
|
|
|
|
2024-03-01 17:52:40 +05:30
|
|
|
// UpdateUserHints updates a user's hints settings
|
|
|
|
func UpdateUserHints(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*forms.UpdateHintsForm)
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsAppearance"] = true
|
|
|
|
|
|
|
|
opts := &user_service.UpdateOptions{
|
|
|
|
EnableRepoUnitHints: optional.Some(form.EnableRepoUnitHints),
|
|
|
|
}
|
|
|
|
if err := user_service.UpdateUser(ctx, ctx.Doer, opts); err != nil {
|
|
|
|
ctx.ServerError("UpdateUser", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("User settings updated: %s", ctx.Doer.Name)
|
|
|
|
ctx.Flash.Success(translation.NewLocale(ctx.Doer.Language).TrString("settings.update_hints_success"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
|
|
|
|
}
|
|
|
|
|
2022-01-21 23:29:26 +05:30
|
|
|
// UpdateUserHiddenComments update a user's shown comment types
|
|
|
|
func UpdateUserHiddenComments(ctx *context.Context) {
|
2023-09-15 11:43:19 +05:30
|
|
|
err := user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyHiddenCommentTypes, forms.UserHiddenCommentTypesFromRequest(ctx).String())
|
2022-01-21 23:29:26 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SetUserSetting", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
log.Trace("User settings updated: %s", ctx.Doer.Name)
|
2022-01-21 23:29:26 +05:30
|
|
|
ctx.Flash.Success(ctx.Tr("settings.saved_successfully"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
|
|
|
|
}
|