Implement friend APIs and target profile support
- 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>
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"honoka-chan/internal/middleware"
|
||||
usermodel "honoka-chan/internal/model/user"
|
||||
"honoka-chan/internal/router"
|
||||
friendschema "honoka-chan/internal/schema/friend"
|
||||
"honoka-chan/internal/session"
|
||||
honokautils "honoka-chan/internal/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func request(ctx *gin.Context) {
|
||||
ss := session.Get(ctx)
|
||||
defer ss.Finalize()
|
||||
|
||||
req := friendschema.UserIDReq{}
|
||||
err := honokautils.ParseRequestData(ctx, &req)
|
||||
if ss.CheckErr(err) {
|
||||
return
|
||||
}
|
||||
|
||||
targetUserID, err := resolveActualFriendUserID(ss, req.UserID)
|
||||
if ss.CheckErr(err) {
|
||||
return
|
||||
}
|
||||
if targetUserID == ss.UserID {
|
||||
ss.CheckErr(errors.New("cannot add self as friend"))
|
||||
return
|
||||
}
|
||||
|
||||
isFriend, err := areUsersFriends(ss, ss.UserID, targetUserID)
|
||||
if ss.CheckErr(err) {
|
||||
return
|
||||
}
|
||||
if isFriend {
|
||||
ss.Respond(friendschema.RequestResp{
|
||||
ResponseData: friendschema.RequestData{
|
||||
IsFriend: true,
|
||||
},
|
||||
ReleaseInfo: []any{},
|
||||
StatusCode: 200,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
reversePending, err := ss.UserEng.Table(new(usermodel.UserFriend)).
|
||||
Where("user_id = ?", targetUserID).
|
||||
Where("friend_user_id = ?", ss.UserID).
|
||||
Where("status = ?", usermodel.FriendStatusPending).
|
||||
Exist()
|
||||
if ss.CheckErr(err) {
|
||||
return
|
||||
}
|
||||
|
||||
selfAwaitingApproval, err := ss.UserEng.Table(new(usermodel.UserFriend)).
|
||||
Where("user_id = ?", ss.UserID).
|
||||
Where("friend_user_id = ?", targetUserID).
|
||||
Where("status = ?", usermodel.FriendStatusAwaitingApproval).
|
||||
Exist()
|
||||
if ss.CheckErr(err) {
|
||||
return
|
||||
}
|
||||
|
||||
if reversePending && selfAwaitingApproval {
|
||||
err = usermodel.EnsureMutualFriendWithIsNew(ss.UserEng, ss.UserID, targetUserID, usermodel.FriendStatusApproved, true, true)
|
||||
if ss.CheckErr(err) {
|
||||
return
|
||||
}
|
||||
|
||||
ss.Respond(friendschema.RequestResp{
|
||||
ResponseData: friendschema.RequestData{
|
||||
IsFriend: true,
|
||||
},
|
||||
ReleaseInfo: []any{},
|
||||
StatusCode: 200,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
err = usermodel.EnsureFriendLink(ss.UserEng, ss.UserID, targetUserID, usermodel.FriendStatusPending, false)
|
||||
if ss.CheckErr(err) {
|
||||
return
|
||||
}
|
||||
err = usermodel.EnsureFriendLink(ss.UserEng, targetUserID, ss.UserID, usermodel.FriendStatusAwaitingApproval, true)
|
||||
if ss.CheckErr(err) {
|
||||
return
|
||||
}
|
||||
|
||||
ss.Respond(friendschema.RequestResp{
|
||||
ResponseData: friendschema.RequestData{
|
||||
IsFriend: false,
|
||||
},
|
||||
ReleaseInfo: []any{},
|
||||
StatusCode: 200,
|
||||
})
|
||||
}
|
||||
|
||||
func init() {
|
||||
router.AddHandler("main.php", "POST", "/friend/request", middleware.Common, request)
|
||||
}
|
||||
Reference in New Issue
Block a user