diff --git a/go.mod b/go.mod index db16b59..fa4aa0b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module git.solsynth.dev/hypernet/messaging go 1.23.2 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/passport v0.0.0-20241102044832-40a040352174 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/gofiber/fiber/v2 v2.52.5 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/livekit/protocol v1.14.0 github.com/livekit/server-sdk-go v1.1.8 diff --git a/go.sum b/go.sum index d72d7a8..58ffb73 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= 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/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/go.mod h1:EjUDX5HdTo3J1GfAN5mfDf0JxRspzASj8uysa4V/ow0= 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/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= 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/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= diff --git a/pkg/internal/database/migrator.go b/pkg/internal/database/migrator.go index 4813f73..d8c395b 100644 --- a/pkg/internal/database/migrator.go +++ b/pkg/internal/database/migrator.go @@ -2,11 +2,12 @@ package database import ( "git.solsynth.dev/hypernet/messaging/pkg/internal/models" + authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" "gorm.io/gorm" ) var AutoMaintainRange = []any{ - &models.Realm{}, + &authm.Realm{}, &models.Channel{}, &models.ChannelMember{}, &models.Call{}, diff --git a/pkg/internal/http/api/channel_members_api.go b/pkg/internal/http/api/channel_members_api.go index 7fa2062..edb2379 100644 --- a/pkg/internal/http/api/channel_members_api.go +++ b/pkg/internal/http/api/channel_members_api.go @@ -2,10 +2,11 @@ package api import ( "fmt" + "git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" + "git.solsynth.dev/hypernet/passport/pkg/authkit" 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/database" @@ -19,7 +20,7 @@ func listChannelMembers(c *fiber.Ctx) error { var err error 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) } else { channel, err = services.GetChannelWithAlias(alias) @@ -44,7 +45,7 @@ func getMyChannelMembership(c *fiber.Ctx) error { var err error 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) } else { channel, err = services.GetChannelWithAlias(alias) @@ -91,9 +92,7 @@ func addChannelMember(c *fiber.Ctx) error { } var account authm.Account - if err := database.C.Where(&hyper.BaseUser{ - Name: data.Target, - }).First(&account).Error; err != nil { + if err := database.C.Where("name = ?", data.Target).First(&account).Error; err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } @@ -136,9 +135,7 @@ func removeChannelMember(c *fiber.Ctx) error { } var account authm.Account - if err := database.C.Where(&hyper.BaseUser{ - Name: data.Target, - }).First(&account).Error; err != nil { + if err := database.C.Where("name = ?", data.Target).First(&account).Error; err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } @@ -167,7 +164,7 @@ func editMyChannelMembership(c *fiber.Ctx) error { var err error 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) } else { channel, err = services.GetChannelWithAlias(alias) @@ -218,9 +215,7 @@ func joinChannel(c *fiber.Ctx) error { } if channel.RealmID != nil { - if realm, err := services.GetRealmWithExtID(channel.Realm.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 { + if _, err := authkit.GetRealmMember(gap.Nx, *channel.RealmID, user.ID); err != nil { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a part of the realm: %v", err)) } } diff --git a/pkg/internal/http/api/channels_api.go b/pkg/internal/http/api/channels_api.go index 40c8b79..a029033 100644 --- a/pkg/internal/http/api/channels_api.go +++ b/pkg/internal/http/api/channels_api.go @@ -2,10 +2,11 @@ package api import ( "fmt" + "git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" + "git.solsynth.dev/hypernet/passport/pkg/authkit" 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/database" @@ -20,7 +21,7 @@ func getChannel(c *fiber.Ctx) error { var err error 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) } else { channel, err = services.GetChannelWithAlias(alias) @@ -42,7 +43,7 @@ func getChannelIdentity(c *fiber.Ctx) error { var err error 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) } else { _, member, err = services.GetChannelIdentity(alias, user.ID) @@ -63,7 +64,7 @@ func listChannel(c *fiber.Ctx) error { var err error 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) } else { channels, err = services.ListChannel(user) @@ -83,7 +84,7 @@ func listOwnedChannel(c *fiber.Ctx) error { var err error 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) } else { channels, err = services.ListChannelWithUser(user) @@ -111,7 +112,7 @@ func listAvailableChannel(c *fiber.Ctx) error { var err error 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) } else { channels, err = services.ListAvailableChannel(tx, user) @@ -143,11 +144,11 @@ func createChannel(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } - var realm *models.Realm - if val, ok := c.Locals("realm").(models.Realm); ok { - if info, err := services.GetRealmMember(val.ID, user.ID); err != nil { + var realm *authm.Realm + if val, ok := c.Locals("realm").(authm.Realm); ok { + 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") - } 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") } else { realm = &val @@ -198,12 +199,12 @@ func editChannel(c *fiber.Ctx) error { 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 info, err := services.GetRealmMember(val.ID, user.ID); err != nil { + if val, ok := c.Locals("realm").(authm.Realm); ok { + 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") - } 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") } else { tx = tx.Where("realm_id = ?", val.ID) @@ -240,12 +241,12 @@ func deleteChannel(c *fiber.Ctx) error { user := c.Locals("user").(authm.Account) 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 info, err := services.GetRealmMember(val.ID, user.ID); err != nil { + if val, ok := c.Locals("realm").(authm.Realm); ok { + 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") - } 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") } else { tx = tx.Where("realm_id = ?", val.ID) diff --git a/pkg/internal/http/api/direct_channels_api.go b/pkg/internal/http/api/direct_channels_api.go index cfd0b95..3a0628d 100644 --- a/pkg/internal/http/api/direct_channels_api.go +++ b/pkg/internal/http/api/direct_channels_api.go @@ -2,7 +2,9 @@ package api import ( "fmt" + "git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" + "git.solsynth.dev/hypernet/passport/pkg/authkit" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" "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()) } - var realm *models.Realm - if val, ok := c.Locals("realm").(models.Realm); ok { - if info, err := services.GetRealmMember(val.ID, user.ID); err != nil { + var realm *authm.Realm + if val, ok := c.Locals("realm").(authm.Realm); ok { + 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") - } 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") } else { realm = &val diff --git a/pkg/internal/http/api/events_api.go b/pkg/internal/http/api/events_api.go index aa7ec30..bcb1895 100644 --- a/pkg/internal/http/api/events_api.go +++ b/pkg/internal/http/api/events_api.go @@ -21,7 +21,7 @@ func getEvent(c *fiber.Ctx) error { var err error 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) } else { channel, err = services.GetChannelWithAlias(alias) @@ -51,7 +51,7 @@ func listEvent(c *fiber.Ctx) error { var err error 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) } else { channel, err = services.GetChannelWithAlias(alias) @@ -97,7 +97,7 @@ func newRawEvent(c *fiber.Ctx) error { var channel models.Channel 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) } else { channel, member, err = services.GetChannelIdentity(alias, user.ID) diff --git a/pkg/internal/http/api/events_message_api.go b/pkg/internal/http/api/events_message_api.go index 1b9229d..68ba748 100644 --- a/pkg/internal/http/api/events_message_api.go +++ b/pkg/internal/http/api/events_message_api.go @@ -40,7 +40,7 @@ func newMessageEvent(c *fiber.Ctx) error { var channel models.Channel 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) } else { channel, member, err = services.GetChannelIdentity(alias, user.ID) @@ -99,7 +99,7 @@ func editMessageEvent(c *fiber.Ctx) error { var channel models.Channel 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) } else { channel, member, err = services.GetChannelIdentity(alias, user.ID) @@ -134,7 +134,7 @@ func deleteMessageEvent(c *fiber.Ctx) error { var channel models.Channel 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) } else { channel, member, err = services.GetChannelIdentity(alias, user.ID) diff --git a/pkg/internal/http/api/realms_api.go b/pkg/internal/http/api/realms_api.go index 980fc80..879f229 100644 --- a/pkg/internal/http/api/realms_api.go +++ b/pkg/internal/http/api/realms_api.go @@ -2,15 +2,16 @@ package api import ( "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" ) func realmMiddleware(c *fiber.Ctx) error { realmAlias := c.Params("realm") if len(realmAlias) > 0 && realmAlias != "global" { - realm, err := services.GetRealmWithAlias(realmAlias) + realm, err := authkit.GetRealmByAlias(gap.Nx, realmAlias) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("requested channel with realm, but realm was not found: %v", err)) } else { diff --git a/pkg/internal/http/api/whats_new_api.go b/pkg/internal/http/api/whats_new_api.go index e0e260c..f1a67e4 100644 --- a/pkg/internal/http/api/whats_new_api.go +++ b/pkg/internal/http/api/whats_new_api.go @@ -55,7 +55,6 @@ func getWhatsNew(c *fiber.Ctx) error { Limit(take). Order("created_at DESC"). Preload("Sender"). - Preload("Sender.Account"). Preload("Channel"). Preload("Channel.Realm"). Find(&items).Error; err != nil { diff --git a/pkg/internal/models/calls.go b/pkg/internal/models/calls.go index b48c323..2266bf1 100644 --- a/pkg/internal/models/calls.go +++ b/pkg/internal/models/calls.go @@ -1,14 +1,13 @@ package models import ( - "time" - - "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" "github.com/livekit/protocol/livekit" + "time" ) type Call struct { - hyper.BaseModel + cruda.BaseModel EndedAt *time.Time `json:"ended_at"` diff --git a/pkg/internal/models/channels.go b/pkg/internal/models/channels.go index 275c34c..2c66d00 100644 --- a/pkg/internal/models/channels.go +++ b/pkg/internal/models/channels.go @@ -2,8 +2,8 @@ package models import ( "fmt" - - "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" + authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" ) type ChannelType = uint8 @@ -14,7 +14,7 @@ const ( ) type Channel struct { - hyper.BaseModel + cruda.BaseModel Alias string `json:"alias"` Name string `json:"name"` @@ -27,8 +27,8 @@ type Channel struct { IsPublic bool `json:"is_public"` IsCommunity bool `json:"is_community"` - Realm Realm `json:"realm"` - RealmID *uint `json:"realm_id"` + Realm *authm.Realm `json:"realm" gorm:"-"` + RealmID *uint `json:"realm_id"` } func (v Channel) DisplayText() string { @@ -50,7 +50,7 @@ const ( ) type ChannelMember struct { - hyper.BaseModel + cruda.BaseModel Name string `json:"name"` Nick string `json:"nick"` diff --git a/pkg/internal/models/events.go b/pkg/internal/models/events.go index 047b47e..186ad2e 100644 --- a/pkg/internal/models/events.go +++ b/pkg/internal/models/events.go @@ -1,7 +1,7 @@ package models import ( - "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" "gorm.io/datatypes" ) @@ -13,7 +13,7 @@ const ( ) type Event struct { - hyper.BaseModel + cruda.BaseModel Uuid string `json:"uuid"` Body datatypes.JSONMap `json:"body"` diff --git a/pkg/internal/models/realms.go b/pkg/internal/models/realms.go deleted file mode 100644 index 63a1652..0000000 --- a/pkg/internal/models/realms.go +++ /dev/null @@ -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"` -} diff --git a/pkg/internal/services/accounts.go b/pkg/internal/services/accounts.go deleted file mode 100644 index a927600..0000000 --- a/pkg/internal/services/accounts.go +++ /dev/null @@ -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 -} diff --git a/pkg/internal/services/calls.go b/pkg/internal/services/calls.go index caea4da..1df79f6 100644 --- a/pkg/internal/services/calls.go +++ b/pkg/internal/services/calls.go @@ -6,12 +6,12 @@ import ( "fmt" "git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "git.solsynth.dev/hypernet/nexus/pkg/nex" + "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" "git.solsynth.dev/hypernet/passport/pkg/authkit" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" "git.solsynth.dev/hypernet/pusher/pkg/pushkit" "time" - "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hypernet/messaging/pkg/internal/database" "git.solsynth.dev/hypernet/messaging/pkg/internal/models" jsoniter "github.com/json-iterator/go" @@ -30,7 +30,6 @@ func ListCall(channel models.Channel, take, offset int) ([]models.Call, error) { Limit(take). Offset(offset). Preload("Founder"). - Preload("Founder.Account"). Preload("Channel"). Order("created_at DESC"). Find(&calls).Error; err != nil { @@ -44,11 +43,10 @@ func GetCall(channel models.Channel, id uint) (models.Call, error) { var call models.Call if err := database.C. Where(models.Call{ - BaseModel: hyper.BaseModel{ID: id}, + BaseModel: cruda.BaseModel{ID: id}, ChannelID: channel.ID, }). Preload("Founder"). - Preload("Founder.Account"). Preload("Channel"). Order("created_at DESC"). First(&call).Error; err != nil { @@ -125,6 +123,12 @@ func NewCall(channel models.Channel, founder models.ChannelMember) (models.Call, } 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( gap.Nx, diff --git a/pkg/internal/services/channels.go b/pkg/internal/services/channels.go index 0241ebb..95dd765 100644 --- a/pkg/internal/services/channels.go +++ b/pkg/internal/services/channels.go @@ -10,7 +10,6 @@ import ( "github.com/eko/gocache/lib/v4/store" "regexp" - "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hypernet/messaging/pkg/internal/database" "git.solsynth.dev/hypernet/messaging/pkg/internal/models" "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) marshal := marshaler.New(cacheManager) contx := context.Background() @@ -93,9 +92,7 @@ func GetChannelAliasAvailability(alias string) error { func GetChannel(id uint) (models.Channel, error) { var channel models.Channel - tx := database.C.Where(models.Channel{ - BaseModel: hyper.BaseModel{ID: id}, - }).Preload("Account").Preload("Realm") + tx := database.C.Where("id = ?", id).Preload("Account").Preload("Realm") tx = PreloadDirectChannelMembers(tx) if err := tx.First(&channel).Error; err != nil { return channel, err @@ -165,7 +162,7 @@ func PreloadDirectChannelMembers(tx *gorm.DB) *gorm.DB { ), models.ChannelTypeDirect, ) - }).Preload("Members.Account") + }) } func ListChannel(user *authm.Account, realmId ...uint) ([]models.Channel, error) { diff --git a/pkg/internal/services/encoder.go b/pkg/internal/services/encoder.go deleted file mode 100644 index c203b02..0000000 --- a/pkg/internal/services/encoder.go +++ /dev/null @@ -1,8 +0,0 @@ -package services - -import jsoniter "github.com/json-iterator/go" - -func EncodeJSONBody(in any) []byte { - out, _ := jsoniter.Marshal(in) - return out -} diff --git a/pkg/internal/services/events.go b/pkg/internal/services/events.go index e4d2a32..bdf99e2 100644 --- a/pkg/internal/services/events.go +++ b/pkg/internal/services/events.go @@ -4,11 +4,11 @@ import ( "fmt" "git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "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/pusher/pkg/pushkit" "strings" - "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hypernet/messaging/pkg/internal/database" "git.solsynth.dev/hypernet/messaging/pkg/internal/models" 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). Order("created_at DESC"). Preload("Sender"). - Preload("Sender.Account"). Find(&events).Error; err != nil { return events, err } 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) { var event models.Event if err := database.C. - Where(models.Event{ - BaseModel: hyper.BaseModel{ID: id}, - ChannelID: channel.ID, - }). + Where("id = ? AND channel_id = ?", id, channel.ID). Preload("Sender"). - Preload("Sender.Account"). First(&event).Error; err != nil { return event, err } 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) { var event models.Event if err := database.C.Where(models.Event{ - BaseModel: hyper.BaseModel{ID: id}, + BaseModel: cruda.BaseModel{ID: id}, ChannelID: channel.ID, SenderID: member.ID, }).First(&event).Error; err != nil { @@ -98,6 +93,12 @@ func NewEvent(event models.Event) (models.Event, error) { if strings.HasPrefix(event.Type, "messages") { 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) } diff --git a/pkg/internal/services/mailer.go b/pkg/internal/services/mailer.go deleted file mode 100644 index 74301fe..0000000 --- a/pkg/internal/services/mailer.go +++ /dev/null @@ -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")}, - ) -} diff --git a/pkg/internal/services/realms.go b/pkg/internal/services/realms.go deleted file mode 100644 index 27d7549..0000000 --- a/pkg/internal/services/realms.go +++ /dev/null @@ -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 - } -} diff --git a/pkg/internal/services/status.go b/pkg/internal/services/status.go index e2f236a..270dfa6 100644 --- a/pkg/internal/services/status.go +++ b/pkg/internal/services/status.go @@ -3,12 +3,12 @@ package services import ( "context" "fmt" - "git.solsynth.dev/hydrogen/dealer/pkg/hyper" - "git.solsynth.dev/hydrogen/dealer/pkg/proto" localCache "git.solsynth.dev/hypernet/messaging/pkg/internal/cache" "git.solsynth.dev/hypernet/messaging/pkg/internal/database" "git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "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/marshaler" "github.com/eko/gocache/lib/v4/store" @@ -50,7 +50,6 @@ func SetTypingStatus(channelId uint, userId uint) error { var channel models.Channel if err := database.C. Preload("Members"). - Preload("Members.Account"). Where("id = ?", channelId). First(&channel).Error; err != nil { 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{ UserId: broadcastTarget, - Body: hyper.NetworkPackage{ + Body: nex.WebSocketPackage{ Action: "status.typing", Payload: data, }.Marshal(), diff --git a/pkg/internal/services/websocket.go b/pkg/internal/services/websocket.go index 1cbdff9..ddc0072 100644 --- a/pkg/internal/services/websocket.go +++ b/pkg/internal/services/websocket.go @@ -7,8 +7,8 @@ import ( "github.com/samber/lo" - "git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hypernet/messaging/pkg/internal/gap" + "git.solsynth.dev/hypernet/nexus/pkg/proto" ) func PushCommand(userId uint, task nex.WebSocketPackage) { @@ -16,7 +16,7 @@ func PushCommand(userId uint, task nex.WebSocketPackage) { defer cancel() pc := gap.Nx.GetNexusGrpcConn() - _, _ = proto.NewStreamControllerClient(pc).PushStream(ctx, &proto.PushStreamRequest{ + _, _ = proto.NewStreamServiceClient(pc).PushStream(ctx, &proto.PushStreamRequest{ UserId: lo.ToPtr(uint64(userId)), Body: task.Marshal(), }) @@ -27,7 +27,7 @@ func PushCommandBatch(userId []uint64, task nex.WebSocketPackage) { defer cancel() pc := gap.Nx.GetNexusGrpcConn() - _, _ = proto.NewStreamControllerClient(pc).PushStreamBatch(ctx, &proto.PushStreamBatchRequest{ + _, _ = proto.NewStreamServiceClient(pc).PushStreamBatch(ctx, &proto.PushStreamBatchRequest{ UserId: userId, Body: task.Marshal(), })