241 lines
6.9 KiB
Go
241 lines
6.9 KiB
Go
package graph
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"tailly_back_v2/proto"
|
|
)
|
|
|
|
// FollowUser is the resolver for the followUser field.
|
|
func (r *mutationResolver) FollowUser(ctx context.Context, followingID int) (*FollowResult, error) {
|
|
followerID, err := getUserIDFromContext(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("authentication required: %w", err)
|
|
}
|
|
|
|
res, err := r.SubscribeClient.FollowUser(ctx, &proto.FollowRequest{
|
|
FollowerId: int32(followerID),
|
|
FollowingId: int32(followingID),
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to follow user: %w", err)
|
|
}
|
|
return &FollowResult{Success: res.Success, Message: res.Message}, nil
|
|
}
|
|
|
|
// UnfollowUser is the resolver for the unfollowUser field.
|
|
func (r *mutationResolver) UnfollowUser(ctx context.Context, followingID int) (*UnfollowResult, error) {
|
|
followerID, err := getUserIDFromContext(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("authentication required: %w", err)
|
|
}
|
|
|
|
res, err := r.SubscribeClient.UnfollowUser(ctx, &proto.UnfollowRequest{
|
|
FollowerId: int32(followerID),
|
|
FollowingId: int32(followingID),
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to unfollow user: %w", err)
|
|
}
|
|
|
|
return &UnfollowResult{Success: res.Success, Message: res.Message}, nil
|
|
}
|
|
|
|
// MarkNotificationAsRead is the resolver for the markNotificationAsRead field.
|
|
func (r *mutationResolver) MarkNotificationAsRead(ctx context.Context, notificationID int) (*MarkNotificationReadResult, error) {
|
|
userID, err := getUserIDFromContext(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("authentication required: %w", err)
|
|
}
|
|
|
|
res, err := r.SubscribeClient.MarkNotificationAsRead(ctx, &proto.MarkNotificationReadRequest{
|
|
UserId: int32(userID),
|
|
NotificationId: int32(notificationID),
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to unfollow user: %w", err)
|
|
}
|
|
|
|
return &MarkNotificationReadResult{Success: res.Success, Message: res.Message}, nil
|
|
}
|
|
|
|
// GetFollowersCount is the resolver for the getFollowersCount field.
|
|
func (r *queryResolver) GetFollowersCount(ctx context.Context, userID int) (int, error) {
|
|
|
|
res, err := r.SubscribeClient.GetFollowersCount(ctx, &proto.GetCountRequest{
|
|
UserId: int32(userID),
|
|
})
|
|
if err != nil {
|
|
return 0, fmt.Errorf("failed to get followers count: %w", err)
|
|
}
|
|
|
|
return int(res.Count), nil
|
|
}
|
|
|
|
// GetFollowingCount is the resolver for the getFollowingCount field.
|
|
func (r *queryResolver) GetFollowingCount(ctx context.Context, userID int) (int, error) {
|
|
res, err := r.SubscribeClient.GetFollowingCount(ctx, &proto.GetCountRequest{
|
|
UserId: int32(userID),
|
|
})
|
|
if err != nil {
|
|
return 0, fmt.Errorf("failed to get following count: %w", err)
|
|
}
|
|
|
|
return int(res.Count), nil
|
|
}
|
|
|
|
// IsFollowing is the resolver for the isFollowing field.
|
|
func (r *queryResolver) IsFollowing(ctx context.Context, followingID int) (bool, error) {
|
|
followerID, err := getUserIDFromContext(ctx)
|
|
if err != nil {
|
|
return false, fmt.Errorf("authentication required: %w", err)
|
|
}
|
|
|
|
res, err := r.SubscribeClient.IsFollowing(ctx, &proto.IsFollowingRequest{
|
|
FollowerId: int32(followerID),
|
|
FollowingId: int32(followingID),
|
|
})
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to check subscription status: %w", err)
|
|
}
|
|
|
|
return res.IsFollowing, nil
|
|
}
|
|
|
|
// GetFollowers is the resolver for the getFollowers field.
|
|
func (r *queryResolver) GetFollowers(ctx context.Context, userID int, limit *int, offset *int) (*FollowersResponse, error) {
|
|
|
|
limitVal := 20
|
|
if limit != nil {
|
|
limitVal = *limit
|
|
}
|
|
|
|
offsetVal := 0
|
|
if offset != nil {
|
|
offsetVal = *offset
|
|
}
|
|
|
|
res, err := r.SubscribeClient.GetFollowers(ctx, &proto.GetFollowersRequest{
|
|
UserId: int32(userID),
|
|
Limit: int32(limitVal),
|
|
Offset: int32(offsetVal),
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get followers: %w", err)
|
|
}
|
|
|
|
return &FollowersResponse{
|
|
Followers: convertProtoFollowersToGraphQL(res.Followers),
|
|
TotalCount: int(res.TotalCount),
|
|
}, nil
|
|
}
|
|
|
|
// Вспомогательная функция для преобразования
|
|
func convertProtoFollowersToGraphQL(protoFollowers []*proto.GetFollowersResponse_Follower) []*Follower {
|
|
var followers []*Follower
|
|
for _, pf := range protoFollowers {
|
|
followers = append(followers, &Follower{
|
|
UserID: int(pf.UserId),
|
|
Username: pf.Username,
|
|
Avatar: pf.Avatar,
|
|
})
|
|
}
|
|
return followers
|
|
}
|
|
|
|
// GetFollowing is the resolver for the getFollowing field.
|
|
func (r *queryResolver) GetFollowing(ctx context.Context, userID int, limit *int, offset *int) (*FollowingResponse, error) {
|
|
|
|
limitVal := 20
|
|
if limit != nil {
|
|
limitVal = *limit
|
|
}
|
|
|
|
offsetVal := 0
|
|
if offset != nil {
|
|
offsetVal = *offset
|
|
}
|
|
|
|
res, err := r.SubscribeClient.GetFollowing(ctx, &proto.GetFollowingRequest{
|
|
UserId: int32(userID),
|
|
Limit: int32(limitVal),
|
|
Offset: int32(offsetVal),
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get following: %w", err)
|
|
}
|
|
|
|
return &FollowingResponse{
|
|
Following: convertProtoFollowingToGraphQL(res.Following),
|
|
TotalCount: int(res.TotalCount),
|
|
}, nil
|
|
}
|
|
|
|
// Вспомогательная функция для преобразования
|
|
func convertProtoFollowingToGraphQL(protoFollowing []*proto.GetFollowingResponse_Following) []*Following {
|
|
var following []*Following
|
|
for _, pf := range protoFollowing {
|
|
following = append(following, &Following{
|
|
UserID: int(pf.UserId),
|
|
Username: pf.Username,
|
|
Avatar: pf.Avatar,
|
|
})
|
|
}
|
|
return following
|
|
}
|
|
|
|
// GetSubscriptionNotifications is the resolver for the getSubscriptionNotifications field.
|
|
func (r *queryResolver) GetSubscriptionNotifications(ctx context.Context, unreadOnly *bool, limit *int, offset *int) (*NotificationsResponse, error) {
|
|
|
|
userID, err := getUserIDFromContext(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("authentication required: %w", err)
|
|
}
|
|
|
|
unreadOnlyVal := false
|
|
if unreadOnly != nil {
|
|
unreadOnlyVal = *unreadOnly
|
|
}
|
|
|
|
limitVal := 20
|
|
if limit != nil {
|
|
limitVal = *limit
|
|
}
|
|
|
|
offsetVal := 0
|
|
if offset != nil {
|
|
offsetVal = *offset
|
|
}
|
|
|
|
res, err := r.SubscribeClient.GetSubscriptionNotifications(ctx, &proto.GetNotificationsRequest{
|
|
UserId: int32(userID),
|
|
UnreadOnly: unreadOnlyVal,
|
|
Limit: int32(limitVal),
|
|
Offset: int32(offsetVal),
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get notifications: %w", err)
|
|
}
|
|
|
|
return &NotificationsResponse{
|
|
Notifications: convertProtoNotificationsToGraphQL(res.Notifications),
|
|
TotalCount: int(res.TotalCount),
|
|
UnreadCount: int(res.UnreadCount),
|
|
}, nil
|
|
}
|
|
func convertProtoNotificationsToGraphQL(protoNotifs []*proto.GetNotificationsResponse_Notification) []*SubscriptionNotification {
|
|
var notifications []*SubscriptionNotification
|
|
for _, pn := range protoNotifs {
|
|
notifications = append(notifications, &SubscriptionNotification{
|
|
ID: int(pn.Id),
|
|
FollowerID: int(pn.FollowerId),
|
|
FollowerUsername: pn.FollowerUsername,
|
|
FollowerAvatar: pn.FollowerAvatar,
|
|
IsRead: pn.IsRead,
|
|
CreatedAt: pn.CreatedAt,
|
|
ExpiresAt: pn.ExpiresAt,
|
|
})
|
|
}
|
|
return notifications
|
|
}
|