2023-03-12 16:00:57 +01:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-09-04 14:41:42 +02:00
|
|
|
|
|
|
|
package typeutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
2022-09-28 19:30:40 +02:00
|
|
|
"os"
|
2022-09-04 14:41:42 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultHeaderPath = "/assets/default_header.png"
|
|
|
|
|
|
|
|
// populateDefaultAvatars returns a slice of standard avatars found
|
|
|
|
// in the path [web-assets-base-dir]/default_avatars. The slice
|
|
|
|
// entries correspond to the relative url via which they can be
|
|
|
|
// retrieved from the server.
|
|
|
|
//
|
|
|
|
// So for example, an avatar called default.jpeg would be returned
|
|
|
|
// in the slice as "/assets/default_avatars/default.jpeg".
|
|
|
|
func populateDefaultAvatars() (defaultAvatars []string) {
|
|
|
|
webAssetsAbsFilePath, err := filepath.Abs(config.GetWebAssetBaseDir())
|
|
|
|
if err != nil {
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Panicf(nil, "error getting abs path for web assets: %s", err)
|
2022-09-04 14:41:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultAvatarsAbsFilePath := filepath.Join(webAssetsAbsFilePath, "default_avatars")
|
2022-09-28 19:30:40 +02:00
|
|
|
defaultAvatarFiles, err := os.ReadDir(defaultAvatarsAbsFilePath)
|
2022-09-04 14:41:42 +02:00
|
|
|
if err != nil {
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Warnf(nil, "error reading default avatars at %s: %s", defaultAvatarsAbsFilePath, err)
|
2022-09-04 14:41:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range defaultAvatarFiles {
|
|
|
|
// ignore directories
|
|
|
|
if f.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore files bigger than 50kb
|
2022-09-28 19:30:40 +02:00
|
|
|
if i, err := f.Info(); err != nil || i.Size() > 50000 {
|
2022-09-04 14:41:42 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the name of the file, eg avatar.jpeg
|
|
|
|
fileName := f.Name()
|
|
|
|
|
|
|
|
// get just the .jpeg, for example, from avatar.jpeg
|
|
|
|
extensionWithDot := filepath.Ext(fileName)
|
|
|
|
|
|
|
|
// remove the leading . to just get, eg, jpeg
|
|
|
|
extension := strings.TrimPrefix(extensionWithDot, ".")
|
|
|
|
|
|
|
|
// take only files with simple extensions
|
|
|
|
// that we know will work OK as avatars
|
|
|
|
switch strings.ToLower(extension) {
|
|
|
|
case "jpeg", "jpg", "gif", "png":
|
|
|
|
avatarURL := config.GetProtocol() + "://" + config.GetHost() + "/assets/default_avatars/" + fileName
|
|
|
|
defaultAvatars = append(defaultAvatars, avatarURL)
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensureAvatar ensures that the given account has a value set
|
|
|
|
// for the avatar URL.
|
|
|
|
//
|
|
|
|
// If no value is set, an avatar will be selected at random from
|
|
|
|
// the available default avatars. This selection is 'sticky', so
|
|
|
|
// the same account will get the same result on subsequent calls.
|
|
|
|
//
|
|
|
|
// If a value for the avatar URL is already set, this function is
|
|
|
|
// a no-op.
|
|
|
|
//
|
|
|
|
// If there are no default avatars available, this function is a
|
|
|
|
// no-op.
|
2023-09-23 18:44:11 +02:00
|
|
|
func (c *Converter) ensureAvatar(account *apimodel.Account) {
|
2022-09-04 14:41:42 +02:00
|
|
|
if (account.Avatar != "" && account.AvatarStatic != "") || len(c.defaultAvatars) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var avatar string
|
|
|
|
if avatarI, ok := c.randAvatars.Load(account.ID); ok {
|
|
|
|
// we already have a default avatar stored for this account
|
|
|
|
avatar, ok = avatarI.(string)
|
|
|
|
if !ok {
|
|
|
|
panic("avatarI was not a string")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// select + store a default avatar for this account at random
|
|
|
|
randomIndex := rand.Intn(len(c.defaultAvatars)) //nolint:gosec
|
|
|
|
avatar = c.defaultAvatars[randomIndex]
|
|
|
|
c.randAvatars.Store(account.ID, avatar)
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Avatar = avatar
|
|
|
|
account.AvatarStatic = avatar
|
|
|
|
}
|
|
|
|
|
2024-05-29 12:57:44 +02:00
|
|
|
// ensureHeader ensures that the given account has a value set
|
2022-09-04 14:41:42 +02:00
|
|
|
// for the header URL.
|
|
|
|
//
|
|
|
|
// If no value is set, the default header will be set.
|
|
|
|
//
|
|
|
|
// If a value for the header URL is already set, this function is
|
|
|
|
// a no-op.
|
2023-09-23 18:44:11 +02:00
|
|
|
func (c *Converter) ensureHeader(account *apimodel.Account) {
|
2022-09-04 14:41:42 +02:00
|
|
|
if account.Header != "" && account.HeaderStatic != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h := config.GetProtocol() + "://" + config.GetHost() + defaultHeaderPath
|
|
|
|
account.Header = h
|
|
|
|
account.HeaderStatic = h
|
|
|
|
}
|