in go/showcase/pkg/showcase/part6/user_fn.go [57:95]
func (u UserFn) Invoke(ctx statefun.Context, message statefun.Message) error {
if !message.Is(part1.UserLoginType) {
return fmt.Errorf("unexpected message type %s", message.ValueTypeName())
}
var login part1.UserLogin
_ = message.As(part1.UserLoginType, &login)
var count int32
_ = ctx.Storage().Get(u.Seen, &count)
count++
now := time.Now().UnixNano() / int64(time.Millisecond)
var lastSeenTimestampMillis int64
if exists := ctx.Storage().Get(u.SeenTimestampMillis, &lastSeenTimestampMillis); !exists {
lastSeenTimestampMillis = now
}
ctx.Storage().Set(u.Seen, count)
ctx.Storage().Set(u.SeenTimestampMillis, lastSeenTimestampMillis)
profile := UserProfile{
Name: login.UserName,
LoginLocation: login.LoginType.String(),
SeenCount: count,
LastSeenDeltaMs: now - lastSeenTimestampMillis,
}
ctx.Send(statefun.MessageBuilder{
Target: statefun.Address{
FunctionType: GreetingTypeName,
Id: login.UserName,
},
Value: profile,
ValueType: UserProfileType,
})
return nil
}