♻️ Refactored remain modules and make it up and running

This commit is contained in:
LittleSheep 2024-11-02 13:40:37 +08:00
parent 06031620b7
commit 2d05be679d
23 changed files with 82 additions and 271 deletions

2
go.mod
View File

@ -3,7 +3,6 @@ module git.solsynth.dev/hypernet/messaging
go 1.23.2 go 1.23.2
require ( require (
git.solsynth.dev/hydrogen/dealer v0.0.0-20240919131945-00c52eba6827
git.solsynth.dev/hypernet/nexus v0.0.0-20241031133156-6bb8eab3fcd8 git.solsynth.dev/hypernet/nexus v0.0.0-20241031133156-6bb8eab3fcd8
git.solsynth.dev/hypernet/passport v0.0.0-20241102044832-40a040352174 git.solsynth.dev/hypernet/passport v0.0.0-20241102044832-40a040352174
git.solsynth.dev/hypernet/pusher v0.0.0-20241026153052-cd2c326efa4e git.solsynth.dev/hypernet/pusher v0.0.0-20241026153052-cd2c326efa4e
@ -14,7 +13,6 @@ require (
github.com/go-playground/validator/v10 v10.22.1 github.com/go-playground/validator/v10 v10.22.1
github.com/gofiber/fiber/v2 v2.52.5 github.com/gofiber/fiber/v2 v2.52.5
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
github.com/json-iterator/go v1.1.12 github.com/json-iterator/go v1.1.12
github.com/livekit/protocol v1.14.0 github.com/livekit/protocol v1.14.0
github.com/livekit/server-sdk-go v1.1.8 github.com/livekit/server-sdk-go v1.1.8

6
go.sum
View File

@ -1,11 +1,7 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
git.solsynth.dev/hydrogen/dealer v0.0.0-20240919131945-00c52eba6827 h1:1ACMPm2ArRpVNYrND/y/R6oPiuMfKe49fP+lG3mcNug=
git.solsynth.dev/hydrogen/dealer v0.0.0-20240919131945-00c52eba6827/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI=
git.solsynth.dev/hypernet/nexus v0.0.0-20241031133156-6bb8eab3fcd8 h1:fo9WuAXcmxdGfYXZKTiAbqGLHAkeL7vf0zpwbjoUNc0= git.solsynth.dev/hypernet/nexus v0.0.0-20241031133156-6bb8eab3fcd8 h1:fo9WuAXcmxdGfYXZKTiAbqGLHAkeL7vf0zpwbjoUNc0=
git.solsynth.dev/hypernet/nexus v0.0.0-20241031133156-6bb8eab3fcd8/go.mod h1:fXQsHXGio+7/0U95IitKF07wS4yTdCMp5ms8wpFBwVI= git.solsynth.dev/hypernet/nexus v0.0.0-20241031133156-6bb8eab3fcd8/go.mod h1:fXQsHXGio+7/0U95IitKF07wS4yTdCMp5ms8wpFBwVI=
git.solsynth.dev/hypernet/passport v0.0.0-20241031144010-d1f1183beb36 h1:rzDuXRTCbWJOgfT7bG0cWFcYqbWKnI1aEfiovpXfsdM=
git.solsynth.dev/hypernet/passport v0.0.0-20241031144010-d1f1183beb36/go.mod h1:EjUDX5HdTo3J1GfAN5mfDf0JxRspzASj8uysa4V/ow0=
git.solsynth.dev/hypernet/passport v0.0.0-20241102044832-40a040352174 h1:XplLpEeQOFlZR2/A4YIeh0B/g9//pXg/TXtWrvHDops= git.solsynth.dev/hypernet/passport v0.0.0-20241102044832-40a040352174 h1:XplLpEeQOFlZR2/A4YIeh0B/g9//pXg/TXtWrvHDops=
git.solsynth.dev/hypernet/passport v0.0.0-20241102044832-40a040352174/go.mod h1:EjUDX5HdTo3J1GfAN5mfDf0JxRspzASj8uysa4V/ow0= git.solsynth.dev/hypernet/passport v0.0.0-20241102044832-40a040352174/go.mod h1:EjUDX5HdTo3J1GfAN5mfDf0JxRspzASj8uysa4V/ow0=
git.solsynth.dev/hypernet/pusher v0.0.0-20241026153052-cd2c326efa4e h1:DtHhMjgxS/spUt/KEdbRFtaVnepI6Vx8pbHdJaNH1hs= git.solsynth.dev/hypernet/pusher v0.0.0-20241026153052-cd2c326efa4e h1:DtHhMjgxS/spUt/KEdbRFtaVnepI6Vx8pbHdJaNH1hs=
@ -210,8 +206,6 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=

View File

@ -2,11 +2,12 @@ package database
import ( import (
"git.solsynth.dev/hypernet/messaging/pkg/internal/models" "git.solsynth.dev/hypernet/messaging/pkg/internal/models"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"gorm.io/gorm" "gorm.io/gorm"
) )
var AutoMaintainRange = []any{ var AutoMaintainRange = []any{
&models.Realm{}, &authm.Realm{},
&models.Channel{}, &models.Channel{},
&models.ChannelMember{}, &models.ChannelMember{},
&models.Call{}, &models.Call{},

View File

@ -2,10 +2,11 @@ package api
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/passport/pkg/authkit"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hypernet/messaging/pkg/internal/http/exts" "git.solsynth.dev/hypernet/messaging/pkg/internal/http/exts"
"git.solsynth.dev/hypernet/messaging/pkg/internal/database" "git.solsynth.dev/hypernet/messaging/pkg/internal/database"
@ -19,7 +20,7 @@ func listChannelMembers(c *fiber.Ctx) error {
var err error var err error
var channel models.Channel var channel models.Channel
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channel, err = services.GetChannelWithAlias(alias, val.ID) channel, err = services.GetChannelWithAlias(alias, val.ID)
} else { } else {
channel, err = services.GetChannelWithAlias(alias) channel, err = services.GetChannelWithAlias(alias)
@ -44,7 +45,7 @@ func getMyChannelMembership(c *fiber.Ctx) error {
var err error var err error
var channel models.Channel var channel models.Channel
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channel, err = services.GetChannelWithAlias(alias, val.ID) channel, err = services.GetChannelWithAlias(alias, val.ID)
} else { } else {
channel, err = services.GetChannelWithAlias(alias) channel, err = services.GetChannelWithAlias(alias)
@ -91,9 +92,7 @@ func addChannelMember(c *fiber.Ctx) error {
} }
var account authm.Account var account authm.Account
if err := database.C.Where(&hyper.BaseUser{ if err := database.C.Where("name = ?", data.Target).First(&account).Error; err != nil {
Name: data.Target,
}).First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} }
@ -136,9 +135,7 @@ func removeChannelMember(c *fiber.Ctx) error {
} }
var account authm.Account var account authm.Account
if err := database.C.Where(&hyper.BaseUser{ if err := database.C.Where("name = ?", data.Target).First(&account).Error; err != nil {
Name: data.Target,
}).First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} }
@ -167,7 +164,7 @@ func editMyChannelMembership(c *fiber.Ctx) error {
var err error var err error
var channel models.Channel var channel models.Channel
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channel, err = services.GetChannelWithAlias(alias, val.ID) channel, err = services.GetChannelWithAlias(alias, val.ID)
} else { } else {
channel, err = services.GetChannelWithAlias(alias) channel, err = services.GetChannelWithAlias(alias)
@ -218,9 +215,7 @@ func joinChannel(c *fiber.Ctx) error {
} }
if channel.RealmID != nil { if channel.RealmID != nil {
if realm, err := services.GetRealmWithExtID(channel.Realm.ID); err != nil { if _, err := authkit.GetRealmMember(gap.Nx, *channel.RealmID, user.ID); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("invalid channel, related realm was not found: %v", err))
} else if _, err := services.GetRealmMember(realm.ID, user.ID); err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a part of the realm: %v", err)) return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a part of the realm: %v", err))
} }
} }

View File

@ -2,10 +2,11 @@ package api
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/passport/pkg/authkit"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hypernet/messaging/pkg/internal/http/exts" "git.solsynth.dev/hypernet/messaging/pkg/internal/http/exts"
"git.solsynth.dev/hypernet/messaging/pkg/internal/database" "git.solsynth.dev/hypernet/messaging/pkg/internal/database"
@ -20,7 +21,7 @@ func getChannel(c *fiber.Ctx) error {
var err error var err error
var channel models.Channel var channel models.Channel
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channel, err = services.GetChannelWithAlias(alias, val.ID) channel, err = services.GetChannelWithAlias(alias, val.ID)
} else { } else {
channel, err = services.GetChannelWithAlias(alias) channel, err = services.GetChannelWithAlias(alias)
@ -42,7 +43,7 @@ func getChannelIdentity(c *fiber.Ctx) error {
var err error var err error
var member models.ChannelMember var member models.ChannelMember
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
_, member, err = services.GetChannelIdentity(alias, user.ID, val) _, member, err = services.GetChannelIdentity(alias, user.ID, val)
} else { } else {
_, member, err = services.GetChannelIdentity(alias, user.ID) _, member, err = services.GetChannelIdentity(alias, user.ID)
@ -63,7 +64,7 @@ func listChannel(c *fiber.Ctx) error {
var err error var err error
var channels []models.Channel var channels []models.Channel
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channels, err = services.ListChannel(user, val.ID) channels, err = services.ListChannel(user, val.ID)
} else { } else {
channels, err = services.ListChannel(user) channels, err = services.ListChannel(user)
@ -83,7 +84,7 @@ func listOwnedChannel(c *fiber.Ctx) error {
var err error var err error
var channels []models.Channel var channels []models.Channel
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channels, err = services.ListChannelWithUser(user, val.ID) channels, err = services.ListChannelWithUser(user, val.ID)
} else { } else {
channels, err = services.ListChannelWithUser(user) channels, err = services.ListChannelWithUser(user)
@ -111,7 +112,7 @@ func listAvailableChannel(c *fiber.Ctx) error {
var err error var err error
var channels []models.Channel var channels []models.Channel
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channels, err = services.ListAvailableChannel(tx, user, val.ID) channels, err = services.ListAvailableChannel(tx, user, val.ID)
} else { } else {
channels, err = services.ListAvailableChannel(tx, user) channels, err = services.ListAvailableChannel(tx, user)
@ -143,11 +144,11 @@ func createChannel(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} }
var realm *models.Realm var realm *authm.Realm
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
if info, err := services.GetRealmMember(val.ID, user.ID); err != nil { if info, err := authkit.GetRealmMember(gap.Nx, val.ID, user.ID); err != nil {
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it")
} else if info.GetPowerLevel() < 50 { } else if info.PowerLevel < 50 {
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it")
} else { } else {
realm = &val realm = &val
@ -198,12 +199,12 @@ func editChannel(c *fiber.Ctx) error {
return err return err
} }
tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}}) tx := database.C.Where("id = ?", id)
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
if info, err := services.GetRealmMember(val.ID, user.ID); err != nil { if info, err := authkit.GetRealmMember(gap.Nx, val.ID, user.ID); err != nil {
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can edit channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can edit channel related to it")
} else if info.GetPowerLevel() < 50 { } else if info.PowerLevel < 50 {
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can edit channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can edit channel related to it")
} else { } else {
tx = tx.Where("realm_id = ?", val.ID) tx = tx.Where("realm_id = ?", val.ID)
@ -240,12 +241,12 @@ func deleteChannel(c *fiber.Ctx) error {
user := c.Locals("user").(authm.Account) user := c.Locals("user").(authm.Account)
id, _ := c.ParamsInt("channelId", 0) id, _ := c.ParamsInt("channelId", 0)
tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}}) tx := database.C.Where("id = ?", id)
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
if info, err := services.GetRealmMember(val.ID, user.ID); err != nil { if info, err := authkit.GetRealmMember(gap.Nx, val.ID, user.ID); err != nil {
return fmt.Errorf("you must be a part of that realm then can delete channel related to it") return fmt.Errorf("you must be a part of that realm then can delete channel related to it")
} else if info.GetPowerLevel() < 50 { } else if info.PowerLevel < 50 {
return fmt.Errorf("you must be a moderator of that realm then can delete channel related to it") return fmt.Errorf("you must be a moderator of that realm then can delete channel related to it")
} else { } else {
tx = tx.Where("realm_id = ?", val.ID) tx = tx.Where("realm_id = ?", val.ID)

View File

@ -2,7 +2,9 @@ package api
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/passport/pkg/authkit"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/messaging/pkg/internal/http/exts" "git.solsynth.dev/hypernet/messaging/pkg/internal/http/exts"
@ -33,11 +35,11 @@ func createDirectChannel(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} }
var realm *models.Realm var realm *authm.Realm
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
if info, err := services.GetRealmMember(val.ID, user.ID); err != nil { if info, err := authkit.GetRealmMember(gap.Nx, val.ID, user.ID); err != nil {
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it")
} else if info.GetPowerLevel() < 50 { } else if info.PowerLevel < 50 {
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it")
} else { } else {
realm = &val realm = &val

View File

@ -21,7 +21,7 @@ func getEvent(c *fiber.Ctx) error {
var err error var err error
var channel models.Channel var channel models.Channel
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channel, err = services.GetChannelWithAlias(alias, val.ID) channel, err = services.GetChannelWithAlias(alias, val.ID)
} else { } else {
channel, err = services.GetChannelWithAlias(alias) channel, err = services.GetChannelWithAlias(alias)
@ -51,7 +51,7 @@ func listEvent(c *fiber.Ctx) error {
var err error var err error
var channel models.Channel var channel models.Channel
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channel, err = services.GetChannelWithAlias(alias, val.ID) channel, err = services.GetChannelWithAlias(alias, val.ID)
} else { } else {
channel, err = services.GetChannelWithAlias(alias) channel, err = services.GetChannelWithAlias(alias)
@ -97,7 +97,7 @@ func newRawEvent(c *fiber.Ctx) error {
var channel models.Channel var channel models.Channel
var member models.ChannelMember var member models.ChannelMember
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channel, member, err = services.GetChannelIdentity(alias, user.ID, val) channel, member, err = services.GetChannelIdentity(alias, user.ID, val)
} else { } else {
channel, member, err = services.GetChannelIdentity(alias, user.ID) channel, member, err = services.GetChannelIdentity(alias, user.ID)

View File

@ -40,7 +40,7 @@ func newMessageEvent(c *fiber.Ctx) error {
var channel models.Channel var channel models.Channel
var member models.ChannelMember var member models.ChannelMember
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channel, member, err = services.GetChannelIdentity(alias, user.ID, val) channel, member, err = services.GetChannelIdentity(alias, user.ID, val)
} else { } else {
channel, member, err = services.GetChannelIdentity(alias, user.ID) channel, member, err = services.GetChannelIdentity(alias, user.ID)
@ -99,7 +99,7 @@ func editMessageEvent(c *fiber.Ctx) error {
var channel models.Channel var channel models.Channel
var member models.ChannelMember var member models.ChannelMember
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channel, member, err = services.GetChannelIdentity(alias, user.ID, val) channel, member, err = services.GetChannelIdentity(alias, user.ID, val)
} else { } else {
channel, member, err = services.GetChannelIdentity(alias, user.ID) channel, member, err = services.GetChannelIdentity(alias, user.ID)
@ -134,7 +134,7 @@ func deleteMessageEvent(c *fiber.Ctx) error {
var channel models.Channel var channel models.Channel
var member models.ChannelMember var member models.ChannelMember
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(authm.Realm); ok {
channel, member, err = services.GetChannelIdentity(alias, user.ID, val) channel, member, err = services.GetChannelIdentity(alias, user.ID, val)
} else { } else {
channel, member, err = services.GetChannelIdentity(alias, user.ID) channel, member, err = services.GetChannelIdentity(alias, user.ID)

View File

@ -2,15 +2,16 @@ package api
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
"git.solsynth.dev/hypernet/passport/pkg/authkit"
"git.solsynth.dev/hypernet/messaging/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func realmMiddleware(c *fiber.Ctx) error { func realmMiddleware(c *fiber.Ctx) error {
realmAlias := c.Params("realm") realmAlias := c.Params("realm")
if len(realmAlias) > 0 && realmAlias != "global" { if len(realmAlias) > 0 && realmAlias != "global" {
realm, err := services.GetRealmWithAlias(realmAlias) realm, err := authkit.GetRealmByAlias(gap.Nx, realmAlias)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("requested channel with realm, but realm was not found: %v", err)) return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("requested channel with realm, but realm was not found: %v", err))
} else { } else {

View File

@ -55,7 +55,6 @@ func getWhatsNew(c *fiber.Ctx) error {
Limit(take). Limit(take).
Order("created_at DESC"). Order("created_at DESC").
Preload("Sender"). Preload("Sender").
Preload("Sender.Account").
Preload("Channel"). Preload("Channel").
Preload("Channel.Realm"). Preload("Channel.Realm").
Find(&items).Error; err != nil { Find(&items).Error; err != nil {

View File

@ -1,14 +1,13 @@
package models package models
import ( import (
"time" "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"time"
) )
type Call struct { type Call struct {
hyper.BaseModel cruda.BaseModel
EndedAt *time.Time `json:"ended_at"` EndedAt *time.Time `json:"ended_at"`

View File

@ -2,8 +2,8 @@ package models
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
) )
type ChannelType = uint8 type ChannelType = uint8
@ -14,7 +14,7 @@ const (
) )
type Channel struct { type Channel struct {
hyper.BaseModel cruda.BaseModel
Alias string `json:"alias"` Alias string `json:"alias"`
Name string `json:"name"` Name string `json:"name"`
@ -27,8 +27,8 @@ type Channel struct {
IsPublic bool `json:"is_public"` IsPublic bool `json:"is_public"`
IsCommunity bool `json:"is_community"` IsCommunity bool `json:"is_community"`
Realm Realm `json:"realm"` Realm *authm.Realm `json:"realm" gorm:"-"`
RealmID *uint `json:"realm_id"` RealmID *uint `json:"realm_id"`
} }
func (v Channel) DisplayText() string { func (v Channel) DisplayText() string {
@ -50,7 +50,7 @@ const (
) )
type ChannelMember struct { type ChannelMember struct {
hyper.BaseModel cruda.BaseModel
Name string `json:"name"` Name string `json:"name"`
Nick string `json:"nick"` Nick string `json:"nick"`

View File

@ -1,7 +1,7 @@
package models package models
import ( import (
"git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"gorm.io/datatypes" "gorm.io/datatypes"
) )
@ -13,7 +13,7 @@ const (
) )
type Event struct { type Event struct {
hyper.BaseModel cruda.BaseModel
Uuid string `json:"uuid"` Uuid string `json:"uuid"`
Body datatypes.JSONMap `json:"body"` Body datatypes.JSONMap `json:"body"`

View File

@ -1,11 +0,0 @@
package models
import "git.solsynth.dev/hydrogen/dealer/pkg/hyper"
// Realm profiles basically fetched from Hypernet.Passport
// But cache at here for better usage and database relations
type Realm struct {
hyper.BaseRealm
Channels []Channel `json:"channels"`
}

View File

@ -1,31 +0,0 @@
package services
import (
"context"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"time"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
"github.com/samber/lo"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
)
func NotifyAccountMessagerBatch(users []authm.Account, notification *proto.NotifyRequest) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
pc, err := gap.Nx.GetClientGrpcConn(nex.ServiceTypeAuth)
if err != nil {
return err
}
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
UserId: lo.Map(users, func(item authm.Account, idx int) uint64 {
return uint64(item.ID)
}),
Notify: notification,
})
return err
}

View File

@ -6,12 +6,12 @@ import (
"fmt" "fmt"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
"git.solsynth.dev/hypernet/nexus/pkg/nex" "git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"git.solsynth.dev/hypernet/passport/pkg/authkit" "git.solsynth.dev/hypernet/passport/pkg/authkit"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit" "git.solsynth.dev/hypernet/pusher/pkg/pushkit"
"time" "time"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hypernet/messaging/pkg/internal/database" "git.solsynth.dev/hypernet/messaging/pkg/internal/database"
"git.solsynth.dev/hypernet/messaging/pkg/internal/models" "git.solsynth.dev/hypernet/messaging/pkg/internal/models"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
@ -30,7 +30,6 @@ func ListCall(channel models.Channel, take, offset int) ([]models.Call, error) {
Limit(take). Limit(take).
Offset(offset). Offset(offset).
Preload("Founder"). Preload("Founder").
Preload("Founder.Account").
Preload("Channel"). Preload("Channel").
Order("created_at DESC"). Order("created_at DESC").
Find(&calls).Error; err != nil { Find(&calls).Error; err != nil {
@ -44,11 +43,10 @@ func GetCall(channel models.Channel, id uint) (models.Call, error) {
var call models.Call var call models.Call
if err := database.C. if err := database.C.
Where(models.Call{ Where(models.Call{
BaseModel: hyper.BaseModel{ID: id}, BaseModel: cruda.BaseModel{ID: id},
ChannelID: channel.ID, ChannelID: channel.ID,
}). }).
Preload("Founder"). Preload("Founder").
Preload("Founder.Account").
Preload("Channel"). Preload("Channel").
Order("created_at DESC"). Order("created_at DESC").
First(&call).Error; err != nil { First(&call).Error; err != nil {
@ -125,6 +123,12 @@ func NewCall(channel models.Channel, founder models.ChannelMember) (models.Call,
} }
channel, _ = GetChannel(channel.ID) channel, _ = GetChannel(channel.ID)
if channel.RealmID == nil {
realm, err := authkit.GetRealm(gap.Nx, *channel.RealmID)
if err == nil {
channel.Realm = &realm
}
}
err = authkit.NotifyUserBatch( err = authkit.NotifyUserBatch(
gap.Nx, gap.Nx,

View File

@ -10,7 +10,6 @@ import (
"github.com/eko/gocache/lib/v4/store" "github.com/eko/gocache/lib/v4/store"
"regexp" "regexp"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hypernet/messaging/pkg/internal/database" "git.solsynth.dev/hypernet/messaging/pkg/internal/database"
"git.solsynth.dev/hypernet/messaging/pkg/internal/models" "git.solsynth.dev/hypernet/messaging/pkg/internal/models"
"github.com/samber/lo" "github.com/samber/lo"
@ -46,7 +45,7 @@ func CacheChannelIdentityCache(channel models.Channel, member models.ChannelMemb
) )
} }
func GetChannelIdentity(alias string, user uint, realm ...models.Realm) (models.Channel, models.ChannelMember, error) { func GetChannelIdentity(alias string, user uint, realm ...authm.Realm) (models.Channel, models.ChannelMember, error) {
cacheManager := cache.New[any](localCache.S) cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager) marshal := marshaler.New(cacheManager)
contx := context.Background() contx := context.Background()
@ -93,9 +92,7 @@ func GetChannelAliasAvailability(alias string) error {
func GetChannel(id uint) (models.Channel, error) { func GetChannel(id uint) (models.Channel, error) {
var channel models.Channel var channel models.Channel
tx := database.C.Where(models.Channel{ tx := database.C.Where("id = ?", id).Preload("Account").Preload("Realm")
BaseModel: hyper.BaseModel{ID: id},
}).Preload("Account").Preload("Realm")
tx = PreloadDirectChannelMembers(tx) tx = PreloadDirectChannelMembers(tx)
if err := tx.First(&channel).Error; err != nil { if err := tx.First(&channel).Error; err != nil {
return channel, err return channel, err
@ -165,7 +162,7 @@ func PreloadDirectChannelMembers(tx *gorm.DB) *gorm.DB {
), ),
models.ChannelTypeDirect, models.ChannelTypeDirect,
) )
}).Preload("Members.Account") })
} }
func ListChannel(user *authm.Account, realmId ...uint) ([]models.Channel, error) { func ListChannel(user *authm.Account, realmId ...uint) ([]models.Channel, error) {

View File

@ -1,8 +0,0 @@
package services
import jsoniter "github.com/json-iterator/go"
func EncodeJSONBody(in any) []byte {
out, _ := jsoniter.Marshal(in)
return out
}

View File

@ -4,11 +4,11 @@ import (
"fmt" "fmt"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
"git.solsynth.dev/hypernet/nexus/pkg/nex" "git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"git.solsynth.dev/hypernet/passport/pkg/authkit" "git.solsynth.dev/hypernet/passport/pkg/authkit"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit" "git.solsynth.dev/hypernet/pusher/pkg/pushkit"
"strings" "strings"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hypernet/messaging/pkg/internal/database" "git.solsynth.dev/hypernet/messaging/pkg/internal/database"
"git.solsynth.dev/hypernet/messaging/pkg/internal/models" "git.solsynth.dev/hypernet/messaging/pkg/internal/models"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
@ -39,7 +39,6 @@ func ListEvent(channel models.Channel, take int, offset int) ([]models.Event, er
}).Limit(take).Offset(offset). }).Limit(take).Offset(offset).
Order("created_at DESC"). Order("created_at DESC").
Preload("Sender"). Preload("Sender").
Preload("Sender.Account").
Find(&events).Error; err != nil { Find(&events).Error; err != nil {
return events, err return events, err
} else { } else {
@ -50,12 +49,8 @@ func ListEvent(channel models.Channel, take int, offset int) ([]models.Event, er
func GetEvent(channel models.Channel, id uint) (models.Event, error) { func GetEvent(channel models.Channel, id uint) (models.Event, error) {
var event models.Event var event models.Event
if err := database.C. if err := database.C.
Where(models.Event{ Where("id = ? AND channel_id = ?", id, channel.ID).
BaseModel: hyper.BaseModel{ID: id},
ChannelID: channel.ID,
}).
Preload("Sender"). Preload("Sender").
Preload("Sender.Account").
First(&event).Error; err != nil { First(&event).Error; err != nil {
return event, err return event, err
} else { } else {
@ -66,7 +61,7 @@ func GetEvent(channel models.Channel, id uint) (models.Event, error) {
func GetEventWithSender(channel models.Channel, member models.ChannelMember, id uint) (models.Event, error) { func GetEventWithSender(channel models.Channel, member models.ChannelMember, id uint) (models.Event, error) {
var event models.Event var event models.Event
if err := database.C.Where(models.Event{ if err := database.C.Where(models.Event{
BaseModel: hyper.BaseModel{ID: id}, BaseModel: cruda.BaseModel{ID: id},
ChannelID: channel.ID, ChannelID: channel.ID,
SenderID: member.ID, SenderID: member.ID,
}).First(&event).Error; err != nil { }).First(&event).Error; err != nil {
@ -98,6 +93,12 @@ func NewEvent(event models.Event) (models.Event, error) {
if strings.HasPrefix(event.Type, "messages") { if strings.HasPrefix(event.Type, "messages") {
event.Channel, _ = GetChannel(event.ChannelID) event.Channel, _ = GetChannel(event.ChannelID)
if event.Channel.RealmID == nil {
realm, err := authkit.GetRealm(gap.Nx, *event.Channel.RealmID)
if err == nil {
event.Channel.Realm = &realm
}
}
NotifyMessageEvent(members, event) NotifyMessageEvent(members, event)
} }

View File

@ -1,51 +0,0 @@
package services
import (
"crypto/tls"
"fmt"
"net/smtp"
"net/textproto"
"github.com/jordan-wright/email"
"github.com/spf13/viper"
)
func SendMail(target string, subject string, content string) error {
mail := &email.Email{
To: []string{target},
From: viper.GetString("mailer.name"),
Subject: subject,
Text: []byte(content),
Headers: textproto.MIMEHeader{},
}
return mail.SendWithTLS(
fmt.Sprintf("%s:%d", viper.GetString("mailer.smtp_host"), viper.GetInt("mailer.smtp_port")),
smtp.PlainAuth(
"",
viper.GetString("mailer.username"),
viper.GetString("mailer.password"),
viper.GetString("mailer.smtp_host"),
),
&tls.Config{ServerName: viper.GetString("mailer.smtp_host")},
)
}
func SendMailHTML(target string, subject string, content string) error {
mail := &email.Email{
To: []string{target},
From: viper.GetString("mailer.name"),
Subject: subject,
HTML: []byte(content),
Headers: textproto.MIMEHeader{},
}
return mail.SendWithTLS(
fmt.Sprintf("%s:%d", viper.GetString("mailer.smtp_host"), viper.GetInt("mailer.smtp_port")),
smtp.PlainAuth(
"",
viper.GetString("mailer.username"),
viper.GetString("mailer.password"),
viper.GetString("mailer.smtp_host"),
),
&tls.Config{ServerName: viper.GetString("mailer.smtp_host")},
)
}

View File

@ -1,79 +0,0 @@
package services
import (
"context"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hypernet/messaging/pkg/internal/database"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
"git.solsynth.dev/hypernet/messaging/pkg/internal/models"
"github.com/samber/lo"
"github.com/spf13/viper"
)
func GetRealmWithExtID(id uint) (models.Realm, error) {
var realm models.Realm
pc, err := gap.Nx.GetClientGrpcConn(nex.ServiceTypeAuth)
if err != nil {
return realm, err
}
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
Id: lo.ToPtr(uint64(id)),
})
if err != nil {
return realm, err
}
prefix := viper.GetString("database.prefix")
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
return models.Realm{BaseRealm: rm}, err
}
func GetRealmWithAlias(alias string) (models.Realm, error) {
var realm models.Realm
pc, err := gap.Nx.GetClientGrpcConn(nex.ServiceTypeAuth)
if err != nil {
return realm, err
}
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
Alias: &alias,
})
if err != nil {
return realm, err
}
prefix := viper.GetString("database.prefix")
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
return models.Realm{BaseRealm: rm}, err
}
func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) {
pc, err := gap.Nx.GetClientGrpcConn(nex.ServiceTypeAuth)
if err != nil {
return nil, err
}
response, err := proto.NewRealmClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
RealmId: lo.ToPtr(uint64(realmId)),
UserId: lo.ToPtr(uint64(userId)),
})
if err != nil {
return nil, err
} else {
return response, nil
}
}
func ListRealmMember(realmId uint) ([]*proto.RealmMemberInfo, error) {
pc, err := gap.Nx.GetClientGrpcConn(nex.ServiceTypeAuth)
if err != nil {
return nil, err
}
response, err := proto.NewRealmClient(pc).ListRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
RealmId: lo.ToPtr(uint64(realmId)),
})
if err != nil {
return nil, err
} else {
return response.Data, nil
}
}

View File

@ -3,12 +3,12 @@ package services
import ( import (
"context" "context"
"fmt" "fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
localCache "git.solsynth.dev/hypernet/messaging/pkg/internal/cache" localCache "git.solsynth.dev/hypernet/messaging/pkg/internal/cache"
"git.solsynth.dev/hypernet/messaging/pkg/internal/database" "git.solsynth.dev/hypernet/messaging/pkg/internal/database"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
"git.solsynth.dev/hypernet/messaging/pkg/internal/models" "git.solsynth.dev/hypernet/messaging/pkg/internal/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"github.com/eko/gocache/lib/v4/cache" "github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler" "github.com/eko/gocache/lib/v4/marshaler"
"github.com/eko/gocache/lib/v4/store" "github.com/eko/gocache/lib/v4/store"
@ -50,7 +50,6 @@ func SetTypingStatus(channelId uint, userId uint) error {
var channel models.Channel var channel models.Channel
if err := database.C. if err := database.C.
Preload("Members"). Preload("Members").
Preload("Members.Account").
Where("id = ?", channelId). Where("id = ?", channelId).
First(&channel).Error; err != nil { First(&channel).Error; err != nil {
return fmt.Errorf("channel not found: %v", err) return fmt.Errorf("channel not found: %v", err)
@ -77,10 +76,10 @@ func SetTypingStatus(channelId uint, userId uint) error {
) )
} }
sc := proto.NewStreamControllerClient(gap.Nx.GetNexusGrpcConn()) sc := proto.NewStreamServiceClient(gap.Nx.GetNexusGrpcConn())
_, err := sc.PushStreamBatch(context.Background(), &proto.PushStreamBatchRequest{ _, err := sc.PushStreamBatch(context.Background(), &proto.PushStreamBatchRequest{
UserId: broadcastTarget, UserId: broadcastTarget,
Body: hyper.NetworkPackage{ Body: nex.WebSocketPackage{
Action: "status.typing", Action: "status.typing",
Payload: data, Payload: data,
}.Marshal(), }.Marshal(),

View File

@ -7,8 +7,8 @@ import (
"github.com/samber/lo" "github.com/samber/lo"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
) )
func PushCommand(userId uint, task nex.WebSocketPackage) { func PushCommand(userId uint, task nex.WebSocketPackage) {
@ -16,7 +16,7 @@ func PushCommand(userId uint, task nex.WebSocketPackage) {
defer cancel() defer cancel()
pc := gap.Nx.GetNexusGrpcConn() pc := gap.Nx.GetNexusGrpcConn()
_, _ = proto.NewStreamControllerClient(pc).PushStream(ctx, &proto.PushStreamRequest{ _, _ = proto.NewStreamServiceClient(pc).PushStream(ctx, &proto.PushStreamRequest{
UserId: lo.ToPtr(uint64(userId)), UserId: lo.ToPtr(uint64(userId)),
Body: task.Marshal(), Body: task.Marshal(),
}) })
@ -27,7 +27,7 @@ func PushCommandBatch(userId []uint64, task nex.WebSocketPackage) {
defer cancel() defer cancel()
pc := gap.Nx.GetNexusGrpcConn() pc := gap.Nx.GetNexusGrpcConn()
_, _ = proto.NewStreamControllerClient(pc).PushStreamBatch(ctx, &proto.PushStreamBatchRequest{ _, _ = proto.NewStreamServiceClient(pc).PushStreamBatch(ctx, &proto.PushStreamBatchRequest{
UserId: userId, UserId: userId,
Body: task.Marshal(), Body: task.Marshal(),
}) })