- Add friend search/list/request/requestCancel/response/expel handlers and schemas - Store friend relationships in user_friend and backfill default friend links at startup - Auto-add the default user as a friend for newly created accounts - Support target user_id in /api profile requests and return target profile data - Make profile accessory_info optional to avoid zero-value accessory payloads - Update login topInfo friend counters from friend relationship state Signed-off-by: Sean Du <do4suki@gmail.com>
88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
package friend
|
|
|
|
import (
|
|
"errors"
|
|
usermodel "honoka-chan/internal/model/user"
|
|
"honoka-chan/internal/session"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func resolveActualFriendUserID(ss *session.Session, requestedUserID int) (int, error) {
|
|
if requestedUserID <= 0 {
|
|
return 0, errors.New("invalid user_id")
|
|
}
|
|
|
|
pref := usermodel.UserPref{}
|
|
has, err := ss.UserEng.Table(new(usermodel.UserPref)).
|
|
Where("invite_code = ?", strconv.Itoa(requestedUserID)).
|
|
Cols("user_id").
|
|
Get(&pref)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if has && pref.UserID > 0 {
|
|
return pref.UserID, nil
|
|
}
|
|
|
|
user := usermodel.Users{}
|
|
has, err = ss.UserEng.Table(new(usermodel.Users)).
|
|
Where("user_id = ?", requestedUserID).
|
|
Cols("user_id").
|
|
Get(&user)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if !has || user.UserID <= 0 {
|
|
return 0, errors.New("user not found")
|
|
}
|
|
|
|
return user.UserID, nil
|
|
}
|
|
|
|
func areUsersFriends(ss *session.Session, userID, friendUserID int) (bool, error) {
|
|
forward, err := ss.UserEng.Table(new(usermodel.UserFriend)).
|
|
Where("user_id = ?", userID).
|
|
Where("friend_user_id = ?", friendUserID).
|
|
Where("status = ?", usermodel.FriendStatusApproved).
|
|
Exist()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if !forward {
|
|
return false, nil
|
|
}
|
|
|
|
return ss.UserEng.Table(new(usermodel.UserFriend)).
|
|
Where("user_id = ?", friendUserID).
|
|
Where("friend_user_id = ?", userID).
|
|
Where("status = ?", usermodel.FriendStatusApproved).
|
|
Exist()
|
|
}
|
|
|
|
func resolveUserIDByInviteCode(ss *session.Session, inviteCode string) (int, error) {
|
|
digits := strings.Map(func(r rune) rune {
|
|
if r >= '0' && r <= '9' {
|
|
return r
|
|
}
|
|
return -1
|
|
}, inviteCode)
|
|
if digits == "" {
|
|
return 0, errors.New("invalid invite_code")
|
|
}
|
|
|
|
pref := usermodel.UserPref{}
|
|
has, err := ss.UserEng.Table(new(usermodel.UserPref)).
|
|
Where("invite_code = ?", digits).
|
|
Cols("user_id").
|
|
Get(&pref)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if !has || pref.UserID <= 0 {
|
|
return 0, errors.New("user not found")
|
|
}
|
|
|
|
return pref.UserID, nil
|
|
}
|