/
new_user_job.go
52 lines (41 loc) · 1.43 KB
/
new_user_job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package job
import (
"context"
"cloud.google.com/go/firestore"
"github.com/broothie/slink.chat/db"
"github.com/broothie/slink.chat/model"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/pkg/errors"
"go.uber.org/zap"
)
type NewUserJob struct {
UserID string
}
func (j NewUserJob) Name() string {
return typeName(j)
}
func (s *Server) NewUserJob(ctx context.Context, payload NewUserJob) error {
logger := ctxzap.Extract(ctx).With(zap.String("user_id", payload.UserID))
userFetcher := db.NewFetcher[model.User](s.DB)
user, err := userFetcher.Fetch(ctx, payload.UserID)
if err != nil {
return errors.Wrap(err, "failed to fetch user")
}
if err := s.Search.IndexUser(user); err != nil {
return errors.Wrap(err, "failed to index user")
}
logger.Info("indexed user")
channelFetcher := db.NewFetcher[model.Channel](s.DB)
worldChat, err := channelFetcher.FetchFirst(ctx, func(query firestore.Query) firestore.Query {
return query.Where("name", "==", model.WorldChatName).OrderBy("created_at", firestore.Asc)
})
if err != nil {
return errors.Wrap(err, "failed to get world chat")
}
updates := []firestore.Update{{Path: "user_ids", Value: firestore.ArrayUnion(payload.UserID)}}
if _, err = s.DB.CollectionFor(worldChat.Type()).Doc(worldChat.ID).Update(ctx, updates); err != nil {
return errors.Wrap(err, "failed to create world chat subscription")
}
logger.Info("added user to World Chat")
return nil
}