✨ Third clients can send notifications
This commit is contained in:
31
pkg/services/clients.go
Normal file
31
pkg/services/clients.go
Normal 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
|
||||
}
|
25
pkg/services/notifications.go
Normal file
25
pkg/services/notifications.go
Normal 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(¬ification).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO Notify all the listeners
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user