✨ Realms
This commit is contained in:
@ -14,6 +14,7 @@ import (
|
||||
|
||||
type PassportUserinfo struct {
|
||||
Sub string `json:"sub"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Picture string `json:"picture"`
|
||||
PreferredUsername string `json:"preferred_username"`
|
||||
@ -28,7 +29,8 @@ func LinkAccount(userinfo PassportUserinfo) (models.Account, error) {
|
||||
}).First(&account).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
account = models.Account{
|
||||
Name: userinfo.PreferredUsername,
|
||||
Name: userinfo.Name,
|
||||
Nick: userinfo.PreferredUsername,
|
||||
Avatar: userinfo.Picture,
|
||||
EmailAddress: userinfo.Email,
|
||||
PowerLevel: 0,
|
||||
@ -39,7 +41,14 @@ func LinkAccount(userinfo PassportUserinfo) (models.Account, error) {
|
||||
return account, err
|
||||
}
|
||||
|
||||
return account, nil
|
||||
account.Name = userinfo.Name
|
||||
account.Nick = userinfo.PreferredUsername
|
||||
account.Avatar = userinfo.Picture
|
||||
account.EmailAddress = userinfo.Email
|
||||
|
||||
err := database.C.Save(&account).Error
|
||||
|
||||
return account, err
|
||||
}
|
||||
|
||||
func GetToken(account models.Account) (string, string, error) {
|
||||
|
@ -12,11 +12,8 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func ListPost(tx *gorm.DB, take int, offset int) ([]*models.Post, error) {
|
||||
var posts []*models.Post
|
||||
if err := tx.
|
||||
Limit(take).
|
||||
Offset(offset).
|
||||
func PreloadRelatedPost(tx *gorm.DB) *gorm.DB {
|
||||
return tx.
|
||||
Preload("Author").
|
||||
Preload("Attachments").
|
||||
Preload("Categories").
|
||||
@ -30,7 +27,14 @@ func ListPost(tx *gorm.DB, take int, offset int) ([]*models.Post, error) {
|
||||
Preload("RepostTo.Categories").
|
||||
Preload("ReplyTo.Categories").
|
||||
Preload("RepostTo.Tags").
|
||||
Preload("ReplyTo.Tags").
|
||||
Preload("ReplyTo.Tags")
|
||||
}
|
||||
|
||||
func ListPost(tx *gorm.DB, take int, offset int) ([]*models.Post, error) {
|
||||
var posts []*models.Post
|
||||
if err := PreloadRelatedPost(tx).
|
||||
Limit(take).
|
||||
Offset(offset).
|
||||
Find(&posts).Error; err != nil {
|
||||
return posts, err
|
||||
}
|
||||
|
40
pkg/services/realms.go
Normal file
40
pkg/services/realms.go
Normal file
@ -0,0 +1,40 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"code.smartsheep.studio/hydrogen/interactive/pkg/database"
|
||||
"code.smartsheep.studio/hydrogen/interactive/pkg/models"
|
||||
)
|
||||
|
||||
func ListRealms(user models.Account) ([]models.Realm, error) {
|
||||
var realms []models.Realm
|
||||
if err := database.C.Where(&models.Realm{AccountID: user.ID}).Find(&realms).Error; err != nil {
|
||||
return realms, err
|
||||
}
|
||||
|
||||
return realms, nil
|
||||
}
|
||||
|
||||
func NewRealm(user models.Account, name, description string) (models.Realm, error) {
|
||||
realm := models.Realm{
|
||||
Name: name,
|
||||
Description: description,
|
||||
AccountID: user.ID,
|
||||
}
|
||||
|
||||
err := database.C.Save(&realm).Error
|
||||
|
||||
return realm, err
|
||||
}
|
||||
|
||||
func EditRealm(realm models.Realm, name, description string) (models.Realm, error) {
|
||||
realm.Name = name
|
||||
realm.Description = description
|
||||
|
||||
err := database.C.Save(&realm).Error
|
||||
|
||||
return realm, err
|
||||
}
|
||||
|
||||
func DeleteRealm(realm models.Realm) error {
|
||||
return database.C.Delete(&realm).Error
|
||||
}
|
Reference in New Issue
Block a user