⬆️ Upgrade to support the latest version Hydrogen Project standard

This commit is contained in:
2024-06-22 18:05:41 +08:00
parent fa50baf927
commit 9366f6e56e
55 changed files with 603 additions and 385 deletions

View File

@ -0,0 +1,18 @@
package models
// Account profiles basically fetched from Hydrogen.Identity
// But cache at here for better usage
// At the same time this model can make relations between local models
type Account struct {
BaseModel
Name string `json:"name"`
Nick string `json:"nick"`
Avatar string `json:"avatar"`
Banner string `json:"banner"`
Description string `json:"description"`
EmailAddress string `json:"email_address"`
PowerLevel int `json:"power_level"`
Channels []Channel `json:"channels"`
ExternalID uint `json:"external_id"`
}

View File

@ -0,0 +1,17 @@
package models
import (
"time"
"gorm.io/datatypes"
"gorm.io/gorm"
)
type JSONMap = datatypes.JSONType[map[string]any]
type BaseModel struct {
ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}

View File

@ -0,0 +1,15 @@
package models
import "time"
type Call struct {
BaseModel
EndedAt *time.Time `json:"ended_at"`
ExternalID string `json:"external_id"`
FounderID uint `json:"founder_id"`
ChannelID uint `json:"channel_id"`
Founder ChannelMember `json:"founder"`
Channel Channel `json:"channel"`
}

View File

@ -0,0 +1,49 @@
package models
type ChannelType = uint8
const (
ChannelTypeCommon = ChannelType(iota)
ChannelTypeDirect
)
type Channel struct {
BaseModel
Alias string `json:"alias"`
Name string `json:"name"`
Description string `json:"description"`
Members []ChannelMember `json:"members"`
Messages []Message `json:"messages"`
Calls []Call `json:"calls"`
Type ChannelType `json:"type"`
Account Account `json:"account"`
AccountID uint `json:"account_id"`
IsEncrypted bool `json:"is_encrypted"`
Realm Realm `json:"realm"`
RealmID *uint `json:"realm_id"`
}
type NotifyLevel = int8
const (
NotifyLevelAll = NotifyLevel(iota)
NotifyLevelMentioned
NotifyLevelNone
)
type ChannelMember struct {
BaseModel
ChannelID uint `json:"channel_id"`
AccountID uint `json:"account_id"`
Nick *string `json:"nick"`
Channel Channel `json:"channel"`
Account Account `json:"account"`
Notify NotifyLevel `json:"notify"`
PowerLevel int `json:"power_level"`
Calls []Call `json:"calls" gorm:"foreignKey:FounderID"`
Messages []Message `json:"messages" gorm:"foreignKey:SenderID"`
}

View File

@ -0,0 +1,22 @@
package models
import "gorm.io/datatypes"
const (
MessageTextType = "m.text"
)
type Message struct {
BaseModel
Uuid string `json:"uuid"`
Content datatypes.JSONMap `json:"content"`
Type string `json:"type"`
Attachments datatypes.JSONSlice[uint] `json:"attachments"`
Channel Channel `json:"channel"`
Sender ChannelMember `json:"sender"`
ReplyID *uint `json:"reply_id"`
ReplyTo *Message `json:"reply_to" gorm:"foreignKey:ReplyID"`
ChannelID uint `json:"channel_id"`
SenderID uint `json:"sender_id"`
}

View File

@ -0,0 +1,15 @@
package models
// Realm profiles basically fetched from Hydrogen.Passport
// But cache at here for better usage and database relations
type Realm struct {
BaseModel
Alias string `json:"alias"`
Name string `json:"name"`
Description string `json:"description"`
Channels []Channel `json:"channels"`
IsPublic bool `json:"is_public"`
IsCommunity bool `json:"is_community"`
ExternalID uint `json:"external_id"`
}

View File

@ -0,0 +1,21 @@
package models
import jsoniter "github.com/json-iterator/go"
type UnifiedCommand struct {
Action string `json:"w"`
Message string `json:"m"`
Payload any `json:"p"`
}
func UnifiedCommandFromError(err error) UnifiedCommand {
return UnifiedCommand{
Action: "error",
Message: err.Error(),
}
}
func (v UnifiedCommand) Marshal() []byte {
data, _ := jsoniter.Marshal(v)
return data
}

View File

@ -0,0 +1,8 @@
package models
import jsoniter "github.com/json-iterator/go"
func FitStruct(src any, out any) {
raw, _ := jsoniter.Marshal(src)
_ = jsoniter.Unmarshal(raw, out)
}