2024-02-03 07:20:32 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-02-21 14:58:51 +00:00
|
|
|
"code.smartsheep.studio/hydrogen/identity/pkg/grpc/proto"
|
2024-02-03 07:20:32 +00:00
|
|
|
"code.smartsheep.studio/hydrogen/interactive/pkg/database"
|
2024-02-21 14:58:51 +00:00
|
|
|
"code.smartsheep.studio/hydrogen/interactive/pkg/grpc"
|
2024-02-03 07:20:32 +00:00
|
|
|
"code.smartsheep.studio/hydrogen/interactive/pkg/models"
|
2024-02-21 14:58:51 +00:00
|
|
|
"context"
|
2024-02-08 10:47:29 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-02-21 14:58:51 +00:00
|
|
|
"time"
|
2024-02-03 07:20:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func FollowAccount(followerId, followingId uint) error {
|
|
|
|
relationship := models.AccountMembership{
|
|
|
|
FollowerID: followerId,
|
|
|
|
FollowingID: followingId,
|
|
|
|
}
|
|
|
|
return database.C.Create(&relationship).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnfollowAccount(followerId, followingId uint) error {
|
|
|
|
return database.C.Where(models.AccountMembership{
|
|
|
|
FollowerID: followerId,
|
|
|
|
FollowingID: followingId,
|
|
|
|
}).Delete(&models.AccountMembership{}).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAccountFollowed(user models.Account, target models.Account) (models.AccountMembership, bool) {
|
|
|
|
var relationship models.AccountMembership
|
|
|
|
err := database.C.Model(&models.AccountMembership{}).
|
|
|
|
Where(&models.AccountMembership{FollowerID: user.ID, FollowingID: target.ID}).
|
2024-02-03 12:44:16 +00:00
|
|
|
First(&relationship).
|
2024-02-03 07:20:32 +00:00
|
|
|
Error
|
2024-02-03 12:44:16 +00:00
|
|
|
return relationship, err == nil
|
2024-02-03 07:20:32 +00:00
|
|
|
}
|
2024-02-08 10:47:29 +00:00
|
|
|
|
2024-02-21 14:58:51 +00:00
|
|
|
func NotifyAccount(user models.Account, subject, content string, links ...*proto.NotifyLink) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
|
|
|
defer cancel()
|
2024-02-08 10:47:29 +00:00
|
|
|
|
2024-02-21 14:58:51 +00:00
|
|
|
_, err := grpc.Notify.NotifyUser(ctx, &proto.NotifyRequest{
|
|
|
|
ClientId: viper.GetString("identity.client_id"),
|
|
|
|
ClientSecret: viper.GetString("identity.client_secret"),
|
|
|
|
Subject: subject,
|
|
|
|
Content: content,
|
|
|
|
Links: links,
|
|
|
|
RecipientId: uint64(user.ID),
|
|
|
|
IsImportant: false,
|
|
|
|
})
|
2024-02-08 10:47:29 +00:00
|
|
|
|
2024-02-21 14:58:51 +00:00
|
|
|
return err
|
2024-02-08 10:47:29 +00:00
|
|
|
}
|