answer/internal/repo/activity_common/follow.go

130 lines
3.8 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package activity_common
import (
"context"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/activity_common"
"github.com/answerdev/answer/internal/service/unique"
"github.com/answerdev/answer/pkg/obj"
2022-09-27 17:59:05 +08:00
"github.com/segmentfault/pacman/errors"
)
// FollowRepo follow repository
type FollowRepo struct {
data *data.Data
uniqueIDRepo unique.UniqueIDRepo
activityRepo activity_common.ActivityRepo
}
// NewFollowRepo new repository
func NewFollowRepo(
data *data.Data,
uniqueIDRepo unique.UniqueIDRepo,
activityRepo activity_common.ActivityRepo,
) activity_common.FollowRepo {
return &FollowRepo{
data: data,
uniqueIDRepo: uniqueIDRepo,
activityRepo: activityRepo,
}
}
// GetFollowAmount get object id's follows
func (ar *FollowRepo) GetFollowAmount(ctx context.Context, objectId string) (follows int, err error) {
objectType, err := obj.GetObjectTypeStrByObjectID(objectId)
if err != nil {
return 0, err
}
switch objectType {
case "question":
model := &entity.Question{}
_, err = ar.data.DB.Where("id = ?", objectId).Cols("`follow_count`").Get(model)
if err == nil {
follows = int(model.FollowCount)
}
case "user":
model := &entity.User{}
_, err = ar.data.DB.Where("id = ?", objectId).Cols("`follow_count`").Get(model)
if err == nil {
follows = int(model.FollowCount)
}
case "tag":
model := &entity.Tag{}
_, err = ar.data.DB.Where("id = ?", objectId).Cols("`follow_count`").Get(model)
if err == nil {
follows = int(model.FollowCount)
}
default:
err = errors.InternalServer(reason.DisallowFollow).WithMsg("this object can't be followed")
}
if err != nil {
return 0, err
}
return follows, nil
}
// GetFollowUserIDs get follow userID by objectID
func (ar *FollowRepo) GetFollowUserIDs(ctx context.Context, objectID string) (userIDs []string, err error) {
objectTypeStr, err := obj.GetObjectTypeStrByObjectID(objectID)
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
activityType, err := ar.activityRepo.GetActivityTypeByObjKey(ctx, objectTypeStr, "follow")
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
userIDs = make([]string, 0)
session := ar.data.DB.Select("user_id")
session.Table(entity.Activity{}.TableName())
session.Where("object_id = ?", objectID)
session.Where("activity_type = ?", activityType)
session.Where("cancelled = 0")
err = session.Find(&userIDs)
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return userIDs, nil
}
// GetFollowIDs get all follow id list
func (ar *FollowRepo) GetFollowIDs(ctx context.Context, userID, objectKey string) (followIDs []string, err error) {
followIDs = make([]string, 0)
activityType, err := ar.activityRepo.GetActivityTypeByObjKey(ctx, objectKey, "follow")
session := ar.data.DB.Select("object_id")
session.Table(entity.Activity{}.TableName())
session.Where("user_id = ? AND activity_type = ?", userID, activityType)
session.Where("cancelled = 0")
err = session.Find(&followIDs)
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return followIDs, nil
}
// IsFollowed check user if follow object or not
func (ar *FollowRepo) IsFollowed(userId, objectId string) (bool, error) {
activityType, _, _, err := ar.activityRepo.GetActivityTypeByObjID(nil, objectId, "follow")
if err != nil {
return false, err
}
at := &entity.Activity{}
has, err := ar.data.DB.Where("user_id = ? AND object_id = ? AND activity_type = ?", userId, objectId, activityType).Get(at)
if err != nil {
return false, err
}
if !has {
return false, nil
}
if at.Cancelled == 1 {
return false, nil
} else {
return true, nil
}
}