Compare commits
2 Commits
f2f75269d5
...
a96910edd2
Author | SHA1 | Date | |
---|---|---|---|
a96910edd2 | |||
b2719ae302 |
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module git.solsynth.dev/hydrogen/messaging
|
|||||||
go 1.21.6
|
go 1.21.6
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.solsynth.dev/hydrogen/identity v0.0.0-20240331080359-e8aac7bb6627
|
git.solsynth.dev/hydrogen/identity v0.0.0-20240406034845-44d2ec9c4ace
|
||||||
github.com/go-playground/validator/v10 v10.17.0
|
github.com/go-playground/validator/v10 v10.17.0
|
||||||
github.com/gofiber/fiber/v2 v2.52.4
|
github.com/gofiber/fiber/v2 v2.52.4
|
||||||
github.com/gofiber/template/html/v2 v2.1.1
|
github.com/gofiber/template/html/v2 v2.1.1
|
||||||
|
2
go.sum
2
go.sum
@ -2,6 +2,8 @@ git.solsynth.dev/hydrogen/identity v0.0.0-20240320131628-6ac77f36957f h1:6vRGU5b
|
|||||||
git.solsynth.dev/hydrogen/identity v0.0.0-20240320131628-6ac77f36957f/go.mod h1:rmh5biOQLvoIE2iRFbOfD0TITMP1orYpqzhUw50Z4ck=
|
git.solsynth.dev/hydrogen/identity v0.0.0-20240320131628-6ac77f36957f/go.mod h1:rmh5biOQLvoIE2iRFbOfD0TITMP1orYpqzhUw50Z4ck=
|
||||||
git.solsynth.dev/hydrogen/identity v0.0.0-20240331080359-e8aac7bb6627 h1:BUhDqy/Whw1yVbDpmGk7clzRAC1jo+AOsCwuwhCVwkg=
|
git.solsynth.dev/hydrogen/identity v0.0.0-20240331080359-e8aac7bb6627 h1:BUhDqy/Whw1yVbDpmGk7clzRAC1jo+AOsCwuwhCVwkg=
|
||||||
git.solsynth.dev/hydrogen/identity v0.0.0-20240331080359-e8aac7bb6627/go.mod h1:GxcduEpQWQ2mO37A9uRtseS680uMLi957GDywRBAJHg=
|
git.solsynth.dev/hydrogen/identity v0.0.0-20240331080359-e8aac7bb6627/go.mod h1:GxcduEpQWQ2mO37A9uRtseS680uMLi957GDywRBAJHg=
|
||||||
|
git.solsynth.dev/hydrogen/identity v0.0.0-20240406034845-44d2ec9c4ace h1:bXbBjM56vA3BxfyuD0IrlJabpVx5bLi4qCv3/RsPa1c=
|
||||||
|
git.solsynth.dev/hydrogen/identity v0.0.0-20240406034845-44d2ec9c4ace/go.mod h1:GxcduEpQWQ2mO37A9uRtseS680uMLi957GDywRBAJHg=
|
||||||
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
||||||
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
|
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
|
||||||
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.solsynth.dev/hydrogen/messaging/pkg/services"
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
@ -50,6 +52,11 @@ func main() {
|
|||||||
server.NewServer()
|
server.NewServer()
|
||||||
go server.Listen()
|
go server.Listen()
|
||||||
|
|
||||||
|
// Configure timed tasks
|
||||||
|
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
|
||||||
|
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
|
||||||
|
quartz.Start()
|
||||||
|
|
||||||
// Messages
|
// Messages
|
||||||
log.Info().Msgf("Messaging v%s is started...", messaging.AppVersion)
|
log.Info().Msgf("Messaging v%s is started...", messaging.AppVersion)
|
||||||
|
|
||||||
@ -58,4 +65,6 @@ func main() {
|
|||||||
<-quit
|
<-quit
|
||||||
|
|
||||||
log.Info().Msgf("Messaging v%s is quitting...", messaging.AppVersion)
|
log.Info().Msgf("Messaging v%s is quitting...", messaging.AppVersion)
|
||||||
|
|
||||||
|
quartz.Stop()
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,16 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var DatabaseAutoActionRange = []any{
|
||||||
|
&models.Account{},
|
||||||
|
&models.Channel{},
|
||||||
|
&models.ChannelMember{},
|
||||||
|
&models.Message{},
|
||||||
|
&models.Attachment{},
|
||||||
|
}
|
||||||
|
|
||||||
func RunMigration(source *gorm.DB) error {
|
func RunMigration(source *gorm.DB) error {
|
||||||
if err := source.AutoMigrate(
|
if err := source.AutoMigrate(DatabaseAutoActionRange...); err != nil {
|
||||||
&models.Account{},
|
|
||||||
&models.Channel{},
|
|
||||||
&models.ChannelMember{},
|
|
||||||
&models.Message{},
|
|
||||||
&models.Attachment{},
|
|
||||||
); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Friendships idpb.FriendshipsClient
|
||||||
var Notify idpb.NotifyClient
|
var Notify idpb.NotifyClient
|
||||||
var Auth idpb.AuthClient
|
var Auth idpb.AuthClient
|
||||||
|
|
||||||
@ -16,6 +17,7 @@ func ConnectPassport() error {
|
|||||||
if conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials())); err != nil {
|
if conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials())); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
|
Friendships = idpb.NewFriendshipsClient(conn)
|
||||||
Notify = idpb.NewNotifyClient(conn)
|
Notify = idpb.NewNotifyClient(conn)
|
||||||
Auth = idpb.NewAuthClient(conn)
|
Auth = idpb.NewAuthClient(conn)
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ func inviteChannel(c *fiber.Ctx) error {
|
|||||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := services.AddChannelMember(account, channel); err != nil {
|
if err := services.InviteChannelMember(account, channel); err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||||
} else {
|
} else {
|
||||||
return c.SendStatus(fiber.StatusOK)
|
return c.SendStatus(fiber.StatusOK)
|
||||||
@ -84,3 +84,31 @@ func kickChannel(c *fiber.Ctx) error {
|
|||||||
return c.SendStatus(fiber.StatusOK)
|
return c.SendStatus(fiber.StatusOK)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func leaveChannel(c *fiber.Ctx) error {
|
||||||
|
user := c.Locals("principal").(models.Account)
|
||||||
|
channelId, _ := c.ParamsInt("channelId", 0)
|
||||||
|
|
||||||
|
var data struct {
|
||||||
|
AccountName string `json:"account_name" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := BindAndValidate(c, &data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var channel models.Channel
|
||||||
|
if err := database.C.Where(&models.Channel{
|
||||||
|
BaseModel: models.BaseModel{ID: uint(channelId)},
|
||||||
|
}).First(&channel).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
|
} else if user.ID == channel.AccountID {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "you cannot leave your own channel")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.RemoveChannelMember(user, channel); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||||
|
} else {
|
||||||
|
return c.SendStatus(fiber.StatusOK)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -88,6 +88,7 @@ func NewServer() {
|
|||||||
channels.Get("/:channelId/members", listChannelMembers)
|
channels.Get("/:channelId/members", listChannelMembers)
|
||||||
channels.Post("/:channelId/invite", authMiddleware, inviteChannel)
|
channels.Post("/:channelId/invite", authMiddleware, inviteChannel)
|
||||||
channels.Post("/:channelId/kick", authMiddleware, kickChannel)
|
channels.Post("/:channelId/kick", authMiddleware, kickChannel)
|
||||||
|
channels.Post("/:channelId/leave", authMiddleware, leaveChannel)
|
||||||
|
|
||||||
channels.Get("/:channel/messages", listMessage)
|
channels.Get("/:channel/messages", listMessage)
|
||||||
channels.Post("/:channel/messages", authMiddleware, newTextMessage)
|
channels.Post("/:channel/messages", authMiddleware, newTextMessage)
|
||||||
|
@ -10,6 +10,17 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetAccountFriend(userId, relatedId uint, status int) (*proto.FriendshipResponse, error) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
return grpc.Friendships.GetFriendship(ctx, &proto.FriendshipTwoSideLookupRequest{
|
||||||
|
AccountId: uint64(userId),
|
||||||
|
RelatedId: uint64(relatedId),
|
||||||
|
Status: uint32(status),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func NotifyAccount(user models.Account, subject, content string, realtime bool, links ...*proto.NotifyLink) error {
|
func NotifyAccount(user models.Account, subject, content string, realtime bool, links ...*proto.NotifyLink) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
@ -132,6 +132,20 @@ func ListChannelMember(channelId uint) ([]models.ChannelMember, error) {
|
|||||||
return members, nil
|
return members, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InviteChannelMember(user models.Account, target models.Channel) error {
|
||||||
|
if _, err := GetAccountFriend(user.ID, target.AccountID, 1); err != nil {
|
||||||
|
return fmt.Errorf("you only can invite your friends to your channel")
|
||||||
|
}
|
||||||
|
|
||||||
|
member := models.ChannelMember{
|
||||||
|
ChannelID: target.ID,
|
||||||
|
AccountID: user.ID,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := database.C.Save(&member).Error
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func AddChannelMember(user models.Account, target models.Channel) error {
|
func AddChannelMember(user models.Account, target models.Channel) error {
|
||||||
member := models.ChannelMember{
|
member := models.ChannelMember{
|
||||||
ChannelID: target.ID,
|
ChannelID: target.ID,
|
||||||
@ -139,7 +153,6 @@ func AddChannelMember(user models.Account, target models.Channel) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err := database.C.Save(&member).Error
|
err := database.C.Save(&member).Error
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
33
pkg/services/cleaner.go
Normal file
33
pkg/services/cleaner.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.solsynth.dev/hydrogen/messaging/pkg/database"
|
||||||
|
"git.solsynth.dev/hydrogen/messaging/pkg/models"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DoAutoDatabaseCleanup() {
|
||||||
|
deadline := time.Now().Add(60 * time.Minute)
|
||||||
|
log.Debug().Time("deadline", deadline).Msg("Now cleaning up entire database...")
|
||||||
|
|
||||||
|
// Deal soft-deletion
|
||||||
|
var count int64
|
||||||
|
for _, model := range database.DatabaseAutoActionRange {
|
||||||
|
tx := database.C.Unscoped().Delete(model, "deleted_at >= ?", deadline)
|
||||||
|
if tx.Error != nil {
|
||||||
|
log.Error().Err(tx.Error).Msg("An error occurred when running auth context cleanup...")
|
||||||
|
}
|
||||||
|
count += tx.RowsAffected
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up outdated chat history
|
||||||
|
tx := database.C.Unscoped().Delete(&models.Message{}, "created_at < ?", time.Now().Add(30*24*time.Hour))
|
||||||
|
if tx.Error != nil {
|
||||||
|
log.Error().Err(tx.Error).Msg("An error occurred when running auth context cleanup...")
|
||||||
|
} else {
|
||||||
|
count += tx.RowsAffected
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user