Third clients can send notifications

This commit is contained in:
2024-02-06 12:28:12 +08:00
parent 37ac26378f
commit 3b43205583
5 changed files with 109 additions and 9 deletions

31
pkg/services/clients.go Normal file
View File

@ -0,0 +1,31 @@
package services
import (
"code.smartsheep.studio/hydrogen/passport/pkg/database"
"code.smartsheep.studio/hydrogen/passport/pkg/models"
"fmt"
)
func GetThirdClient(id string) (models.ThirdClient, error) {
var client models.ThirdClient
if err := database.C.Where(&models.ThirdClient{
Alias: id,
}).First(&client).Error; err != nil {
return client, err
}
return client, nil
}
func GetThirdClientWithSecret(id, secret string) (models.ThirdClient, error) {
client, err := GetThirdClient(id)
if err != nil {
return client, err
}
if client.Secret != secret {
return client, fmt.Errorf("invalid client secret")
}
return client, nil
}

View File

@ -0,0 +1,25 @@
package services
import (
"code.smartsheep.studio/hydrogen/passport/pkg/database"
"code.smartsheep.studio/hydrogen/passport/pkg/models"
)
func NewNotification(user models.ThirdClient, target models.Account, subject, content string, important bool) error {
notification := models.Notification{
Subject: subject,
Content: content,
IsImportant: important,
ReadAt: nil,
SenderID: &user.ID,
RecipientID: target.ID,
}
if err := database.C.Save(&notification).Error; err != nil {
return err
}
// TODO Notify all the listeners
return nil
}