Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cmd/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 28 additions & 2 deletions service/notification/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/traPtitech/traQ/repository"
"github.com/traPtitech/traQ/service/fcm"
"github.com/traPtitech/traQ/service/qall"
"github.com/traPtitech/traQ/service/search"
"github.com/traPtitech/traQ/service/viewer"
"github.com/traPtitech/traQ/service/ws"
"github.com/traPtitech/traQ/utils/message"
Expand Down Expand Up @@ -284,16 +285,22 @@ func messageCreatedHandler(ns *Service, ev hub.Message) {

func messageUpdatedHandler(ns *Service, ev hub.Message) {
cid := ev.Fields["message"].(*model.Message).ChannelID
mid := ev.Fields["message_id"].(uuid.UUID)
citedChannels, err := ns.cache.Get(context.Background(), mid)
if err != nil {
return
}
wsEventType := "MESSAGE_UPDATED"
wsPayload := map[string]interface{}{
"id": ev.Fields["message_id"].(uuid.UUID),
"id": mid,
}

var targetFunc ws.TargetFunc
if ns.cm.IsPublicChannel(context.Background(), cid) {
// 公開チャンネル
targetFunc = ws.Or(
ws.TargetChannelViewers(cid),
ws.TargetChannelsViewers(citedChannels),
ws.TargetTimelineStreamingEnabled(),
)
} else {
Expand All @@ -306,16 +313,20 @@ func messageUpdatedHandler(ns *Service, ev hub.Message) {

func messageDeletedHandler(ns *Service, ev hub.Message) {
cid := ev.Fields["message"].(*model.Message).ChannelID
mid := ev.Fields["message_id"].(uuid.UUID)
citedChannels := ns.getCitedChannelIDs(context.Background(), mid)

wsEventType := "MESSAGE_DELETED"
wsPayload := map[string]interface{}{
"id": ev.Fields["message_id"].(uuid.UUID),
"id": mid,
}

var targetFunc ws.TargetFunc
if ns.cm.IsPublicChannel(context.Background(), cid) {
// 公開チャンネル
targetFunc = ws.Or(
ws.TargetChannelViewers(cid),
ws.TargetChannelsViewers(citedChannels),
ws.TargetTimelineStreamingEnabled(),
)
} else {
Expand Down Expand Up @@ -743,3 +754,18 @@ func broadcast(ns *Service, wsEventType string, wsPayload interface{}) {
func userMulticast(ns *Service, userID uuid.UUID, wsEventType string, wsPayload interface{}) {
go ns.ws.WriteMessage(wsEventType, wsPayload, ws.TargetUsers(userID))
}

func (ns *Service) getCitedChannelIDs(_ context.Context, messageID uuid.UUID) []uuid.UUID {
query := search.Query{}
query.Citation = optional.From(messageID)

res, err := ns.search.Do(&query)
if err != nil {
return nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラーをそのまま握り潰しているため、可能であればエラーハンドリングを改善していただきたいです

}
result := []uuid.UUID{}
for _, hit := range res.Hits() {
result = append(result, hit.GetChannelID())
}
return result
}
19 changes: 18 additions & 1 deletion service/notification/service.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package notification

import (
"context"
"time"

"github.com/gofrs/uuid"
"github.com/leandro-lugaresi/hub"
"github.com/motoki317/sc"
"go.uber.org/zap"

"github.com/traPtitech/traQ/repository"
"github.com/traPtitech/traQ/service/channel"
"github.com/traPtitech/traQ/service/fcm"
"github.com/traPtitech/traQ/service/file"
"github.com/traPtitech/traQ/service/message"
"github.com/traPtitech/traQ/service/search"
"github.com/traPtitech/traQ/service/variable"
"github.com/traPtitech/traQ/service/viewer"
"github.com/traPtitech/traQ/service/ws"
Expand All @@ -26,10 +32,12 @@ type Service struct {
ws *ws.Streamer
vm *viewer.Manager
origin string
search search.Engine
cache *sc.Cache[uuid.UUID, []uuid.UUID]
}

// NewService 通知サービスを作成して起動します
func NewService(repo repository.Repository, cm channel.Manager, mm message.Manager, fm file.Manager, hub *hub.Hub, logger *zap.Logger, fcm fcm.Client, ws *ws.Streamer, vm *viewer.Manager, origin variable.ServerOriginString) *Service {
func NewService(repo repository.Repository, cm channel.Manager, mm message.Manager, fm file.Manager, hub *hub.Hub, logger *zap.Logger, fcm fcm.Client, ws *ws.Streamer, vm *viewer.Manager, origin variable.ServerOriginString, search search.Engine) *Service {
service := &Service{
repo: repo,
cm: cm,
Expand All @@ -41,7 +49,16 @@ func NewService(repo repository.Repository, cm channel.Manager, mm message.Manag
ws: ws,
vm: vm,
origin: string(origin),
search: search,
}
service.cache = sc.NewMust(
func(_ context.Context, messageID uuid.UUID) ([]uuid.UUID, error) {
return service.getCitedChannelIDs(context.Background(), messageID), nil
},
time.Minute,
time.Minute,
sc.WithLRUBackend(1000),
)
go func() {
topics := make([]string, 0, len(handlerMap))
for k := range handlerMap {
Expand Down
13 changes: 13 additions & 0 deletions service/ws/target_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ func TargetChannelViewers(channelID uuid.UUID) TargetFunc {
}
}

// TargetChannelViewersの複数形
func TargetChannelsViewers(channelIDs []uuid.UUID) TargetFunc {
return func(s Session) bool {
c, _ := s.ViewState()
for _, id := range channelIDs {
if c == id {
return true
}
}
return false
}
}

// TargetTimelineStreamingEnabled タイムラインストリーミングが有効なコネクションを対象に送信します
func TargetTimelineStreamingEnabled() TargetFunc {
return func(s Session) bool {
Expand Down