Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from CastyLab/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mrjosh committed Nov 20, 2020
2 parents 3ce7c71 + 61b4faf commit 31bc810
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ config/*
!config.example.yml
!config/config.go
uploads
Caddyfile
Caddyfile
.DS_Store
14 changes: 8 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package config

import (
"fmt"
"gopkg.in/yaml.v2"
"log"
"os"

"gopkg.in/yaml.v2"
)

type ConfMap struct {
Expand All @@ -27,16 +28,17 @@ type ConfMap struct {
Spotify string `yaml:"spotify"`
} `yaml:"oauth"`
Redis struct {
MasterName string `yaml:"masterName"`
Sentinels []string `yaml:"sentinels"`
Port int `yaml:"port"`
Pass string `yaml:"pass"`
MasterName string `yaml:"masterName"`
Sentinels []string `yaml:"sentinels"`
Port int `yaml:"port"`
Pass string `yaml:"pass"`
SentinelPass string `yaml:"sentinel_pass"`
} `yaml:"redis"`
JWT struct {
ExpireTime int `yaml:"expire_time"`
RefreshTokenValidTime int `yaml:"refresh_token_valid_time"`
AccessTokenSecret string `yaml:"access_token_secret"`
RefreshTokenSecret string `yaml:"refresh_token_secret"`
RefreshTokenSecret string `yaml:"refresh_token_secret"`
} `yaml:"jwt"`
SentryDsn string `yaml:"sentry_dsn"`
HcaptchaSecret string `yaml:"hcaptcha_secret"`
Expand Down
Binary file removed db/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion db/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Configure() error {
opts.SetAuth(options.Credential{
Username: config.Map.Secrets.Db.User,
Password: config.Map.Secrets.Db.Pass,
PasswordSet: true,
AuthSource: config.Map.Secrets.Db.Name,
})

client, err := mongo.NewClient(opts)
Expand Down
3 changes: 3 additions & 0 deletions db/models/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"github.com/CastyLab/grpc.proto/proto"
"go.mongodb.org/mongo-driver/bson/primitive"
"golang.org/x/crypto/bcrypt"
"time"
Expand All @@ -24,6 +25,8 @@ type User struct {
TwoFactorAuthEnabled bool `bson:"two_fa_enabled, omitempty" json:"two_fa_enabled"`
TwoFactorAuthToken string `bson:"two_fa_token, omitempty" json:"_"`

State proto.PERSONAL_STATE `bson:"state,omitempty" json:"state,omitempty"`

Avatar string `bson:"avatar, omitempty" json:"avatar, omitempty"`
RoleId uint `bson:"role_id, omitempty" json:"role_id, omitempty"`
LastLogin time.Time `bson:"last_login, omitempty" json:"last_login, omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/CastyLab/grpc.server
go 1.14

require (
github.com/CastyLab/grpc.proto v0.0.0-20201024203352-28756769b0b7
github.com/CastyLab/grpc.proto v0.0.0-20201025004928-8ea87854d2b9
github.com/asticode/go-astisub v0.2.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/getsentry/sentry-go v0.5.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/CastyLab/grpc.proto v0.0.0-20201024184605-f612c4d394a8 h1:jxj9sjy627x
github.com/CastyLab/grpc.proto v0.0.0-20201024184605-f612c4d394a8/go.mod h1:5ypgijUv7rWXMsRbUZXEgTucAA+4BpHtXgGIvOyUGRU=
github.com/CastyLab/grpc.proto v0.0.0-20201024203352-28756769b0b7 h1:oZdji+YoaZSJo7dvC/IetcFYn2y5pdMomRKgkVInN74=
github.com/CastyLab/grpc.proto v0.0.0-20201024203352-28756769b0b7/go.mod h1:5ypgijUv7rWXMsRbUZXEgTucAA+4BpHtXgGIvOyUGRU=
github.com/CastyLab/grpc.proto v0.0.0-20201025004928-8ea87854d2b9 h1:o0RGaKL4UIPv7VExhmbWI4V/R1PtARkhHDEn0ZplOZQ=
github.com/CastyLab/grpc.proto v0.0.0-20201025004928-8ea87854d2b9/go.mod h1:5ypgijUv7rWXMsRbUZXEgTucAA+4BpHtXgGIvOyUGRU=
github.com/CloudyKit/fastprinter v0.0.0-20170127035650-74b38d55f37a/go.mod h1:EFZQ978U7x8IRnstaskI3IysnWY5Ao3QgZUKOXlsAdw=
github.com/CloudyKit/jet v2.1.3-0.20180809161101-62edd43e4f88+incompatible/go.mod h1:HPYO+50pSWkPoj9Q/eq0aRGByCL6ScRlUmiEX5Zgm+w=
github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=
Expand Down
25 changes: 18 additions & 7 deletions helpers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
)

func GetFriendsFromDatabase(ctx context.Context, user *models.User) ([]*proto.User, error) {
Expand Down Expand Up @@ -50,7 +51,7 @@ func GetFriendsFromDatabase(ctx context.Context, user *models.User) ([]*proto.Us
continue
}

friends = append(friends, NewProtoUser(friendUserObject))
friends = append(friends, NewProtoUserWithState(friendUserObject))
}

return friends, nil
Expand All @@ -66,18 +67,23 @@ func SendEventToFriends(ctx context.Context, event []byte, user *models.User) er
return nil
}

func SendEventToUser(ctx context.Context, event []byte, user *proto.User) {
redis.Client.Publish(ctx, fmt.Sprintf("user:events:%s", user.Id), event)
func SendEventToUser(ctx context.Context, event []byte, user *proto.User) (err error) {
_, err = redis.Client.Publish(ctx, fmt.Sprintf("user:events:%s", user.Id), event).Result()
return
}

func SendEventToUsers(ctx context.Context, event []byte, users []*proto.User) {
for _, user := range users {
redis.Client.Publish(ctx, fmt.Sprintf("user:events:%s", user.Id), event)
_, err := redis.Client.Publish(ctx, fmt.Sprintf("user:events:%s", user.Id), event).Result()
if err != nil {
log.Println(err)
}
}
}

func SendEventToTheaterMembers(ctx context.Context, event []byte, theater *models.Theater) {
redis.Client.Publish(ctx, fmt.Sprintf("theater:events:%s", theater.ID.Hex()), event)
func SendEventToTheaterMembers(ctx context.Context, event []byte, theater *models.Theater) (err error) {
_, err = redis.Client.Publish(ctx, fmt.Sprintf("theater:events:%s", theater.ID.Hex()), event).Result()
return
}

func NewProtoUser(user *models.User) *proto.User {
Expand All @@ -96,9 +102,14 @@ func NewProtoUser(user *models.User) *proto.User {
EmailVerified: user.EmailVerified,
Avatar: user.Avatar,
TwoFaEnabled: user.TwoFactorAuthEnabled,
TwoFaToken: user.TwoFactorAuthToken,
LastLogin: lastLogin,
JoinedAt: joinedAt,
UpdatedAt: updatedAt,
}
}

func NewProtoUserWithState(user *models.User) *proto.User {
protoUser := NewProtoUser(user)
protoUser.State = user.State
return protoUser
}
18 changes: 12 additions & 6 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package redis

import (
"context"
"fmt"
"log"

"github.com/CastyLab/grpc.server/config"
"github.com/getsentry/sentry-go"
"github.com/go-redis/redis/v8"
"log"
)

var (
Expand All @@ -13,15 +16,18 @@ var (

func Configure() error {
Client = redis.NewFailoverClient(&redis.FailoverOptions{
SentinelAddrs: config.Map.Secrets.Redis.Sentinels,
Password: config.Map.Secrets.Redis.Pass,
MasterName: config.Map.Secrets.Redis.MasterName,
DB: 0,
SentinelAddrs: config.Map.Secrets.Redis.Sentinels,
SentinelPassword: config.Map.Secrets.Redis.SentinelPass,
Password: config.Map.Secrets.Redis.Pass,
MasterName: config.Map.Secrets.Redis.MasterName,
DB: 0,
})
cmd := Client.Ping(context.Background())
if res := cmd.Val(); res != "PONG" {
log.Println("SentinelAddrs: ", config.Map.Secrets.Redis.Sentinels)
log.Fatalf("Could not ping the redis server: %v", cmd.Err())
mErr := fmt.Errorf("could not ping the redis server: %v", cmd.Err())
sentry.CaptureException(mErr)
return mErr
}
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ func init() {
log.Fatal(fmt.Errorf("could not load config: %v", err))
}

if err := redis.Configure(); err != nil {
log.Fatal(fmt.Errorf("could not configure redis : %v", err))
}

if err := sentry.Init(sentry.ClientOptions{ Dsn: config.Map.Secrets.SentryDsn }); err != nil {
log.Fatal(fmt.Errorf("could not initilize sentry: %v", err))
}

if err := redis.Configure(); err != nil {
log.Fatal(fmt.Errorf("could not configure redis : %v", err))
}

if err := jwt.Load(); err != nil {
err := fmt.Errorf("could not load jwt configuration: %v", err)
sentry.CaptureException(err)
Expand Down
2 changes: 1 addition & 1 deletion services/message/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *Service) CreateMessage(ctx context.Context, req *proto.CreateMessageReq
return nil, status.Error(codes.Internal, fmt.Errorf("could not marshal Reciever :%v", err).Error())
}

buffer, err := protocol.NewMsgProtobuf(proto.EMSG_NEW_CHAT_MESSAGE, &proto.ChatMsgEvent{
buffer, err := protocol.NewMsgProtobuf(proto.EMSG_CHAT_MESSAGES, &proto.ChatMsgEvent{
Message: []byte(protoMessage.Content),
From: string(fromUser),
To: string(toUser),
Expand Down
6 changes: 5 additions & 1 deletion services/theater/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
"net/http"
"time"
)
Expand Down Expand Up @@ -102,7 +103,10 @@ func (s *Service) Invite(ctx context.Context, req *proto.InviteFriendsTheaterReq
for _, friend := range friends {
event, err := protocol.NewMsgProtobuf(proto.EMSG_NEW_NOTIFICATION, &proto.NotificationMsgEvent{})
if err == nil {
helpers.SendEventToUser(ctx, event.Bytes(), &proto.User{Id: friend.ID.Hex()})
err := helpers.SendEventToUser(ctx, event.Bytes(), &proto.User{Id: friend.ID.Hex()})
if err != nil {
log.Println(err)
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions services/theater/media_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"io"
"log"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -74,7 +75,9 @@ func (s *Service) SelectMediaSource(ctx context.Context, req *proto.MediaSourceA
mediaSourceProto := helpers.NewMediaSourceProto(mediaSource)
event, err := protocol.NewMsgProtobuf(proto.EMSG_THEATER_MEDIA_SOURCE_CHANGED, mediaSourceProto)
if err == nil {
helpers.SendEventToTheaterMembers(ctx, event.Bytes(), theater)
if err := helpers.SendEventToTheaterMembers(ctx, event.Bytes(), theater); err != nil {
log.Println(err)
}
}

return &proto.TheaterMediaSourcesResponse{
Expand Down Expand Up @@ -203,7 +206,9 @@ func (s *Service) AddMediaSource(ctx context.Context, req *proto.MediaSourceAuth

event, err := protocol.NewMsgProtobuf(proto.EMSG_THEATER_MEDIA_SOURCE_CHANGED, mediaSourceProto)
if err == nil {
helpers.SendEventToTheaterMembers(ctx, event.Bytes(), theater)
if err := helpers.SendEventToTheaterMembers(ctx, event.Bytes(), theater); err != nil {
log.Println(err)
}
}

return &proto.TheaterMediaSourcesResponse{
Expand Down
5 changes: 4 additions & 1 deletion services/theater/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
"net/http"
"time"
)
Expand Down Expand Up @@ -56,7 +57,9 @@ func (s *Service) UpdateTheater(ctx context.Context, req *proto.TheaterAuthReque
}
event, err := protocol.NewMsgProtobuf(proto.EMSG_THEATER_UPDATED, req.Theater)
if err == nil {
helpers.SendEventToTheaterMembers(ctx, event.Bytes(), theater)
if err := helpers.SendEventToTheaterMembers(ctx, event.Bytes(), theater); err != nil {
log.Println(err)
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions services/user/friend_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
"net/http"
"time"
)
Expand Down Expand Up @@ -138,7 +139,9 @@ func (s *Service) AcceptFriendRequest(ctx context.Context, req *proto.FriendRequ
pms := &proto.FriendRequestAcceptedMsgEvent{Friend: protoUser}
buffer, err := protocol.NewMsgProtobuf(proto.EMSG_SELF_FRIEND_REQUEST_ACCEPTED, pms)
if err == nil {
helpers.SendEventToUser(ctx, buffer.Bytes(), protoUser)
if err := helpers.SendEventToUser(ctx, buffer.Bytes(), protoUser); err != nil {
log.Println(err)
}
}

// send event to friend clients
Expand All @@ -147,7 +150,9 @@ func (s *Service) AcceptFriendRequest(ctx context.Context, req *proto.FriendRequ
friendID = friendRequest.UserId.Hex()
}
if buffer, err := protocol.NewMsgProtobuf(proto.EMSG_FRIEND_REQUEST_ACCEPTED, pms); err == nil {
helpers.SendEventToUser(ctx, buffer.Bytes(), &proto.User{Id: friendID})
if err := helpers.SendEventToUser(ctx, buffer.Bytes(), &proto.User{Id: friendID}); err != nil {
log.Println(err)
}
}

return &proto.Response{
Expand Down Expand Up @@ -240,7 +245,9 @@ func (s *Service) SendFriendRequest(ctx context.Context, req *proto.FriendReques
// send event to friend clients
buffer, err := protocol.NewMsgProtobuf(proto.EMSG_NEW_NOTIFICATION, &proto.NotificationMsgEvent{})
if err == nil {
helpers.SendEventToUser(ctx, buffer.Bytes(), &proto.User{Id: friend.ID.Hex()})
if err := helpers.SendEventToUser(ctx, buffer.Bytes(), &proto.User{Id: friend.ID.Hex()}); err != nil {
log.Println(err)
}
}

return &proto.Response{
Expand Down
5 changes: 4 additions & 1 deletion services/user/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
"net/http"
)

Expand Down Expand Up @@ -63,7 +64,9 @@ func (s *Service) UpdateUser(ctx context.Context, req *proto.UpdateUserRequest)
// update self user with new activity to other clients
buffer, err := protocol.NewMsgProtobuf(proto.EMSG_SELF_USER_UPDATED, protoUser)
if err == nil {
helpers.SendEventToUser(ctx, buffer.Bytes(), protoUser)
if err := helpers.SendEventToUser(ctx, buffer.Bytes(), protoUser); err != nil {
log.Println(err)
}
}

// update friends with new activity of user
Expand Down
13 changes: 10 additions & 3 deletions services/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
"net/http"
)

Expand Down Expand Up @@ -39,7 +40,9 @@ func (s *Service) UpdateState(ctx context.Context, req *proto.UpdateStateRequest
pms := &proto.PersonalStateMsgEvent{State: req.State, User: protoUser}
buffer, err := protocol.NewMsgProtobuf(proto.EMSG_SELF_PERSONAL_STATE_CHANGED, pms)
if err == nil {
helpers.SendEventToUser(ctx, buffer.Bytes(), protoUser)
if err := helpers.SendEventToUser(ctx, buffer.Bytes(), protoUser); err != nil {
log.Println(err)
}
}

// update friends with new state of user
Expand Down Expand Up @@ -82,7 +85,9 @@ func (s *Service) RemoveActivity(ctx context.Context, req *proto.AuthenticateReq
pms := &proto.PersonalActivityMsgEvent{Activity: &proto.Activity{}, User: protoUser}
buffer, err := protocol.NewMsgProtobuf(proto.EMSG_SELF_PERSONAL_ACTIVITY_CHANGED, pms)
if err == nil {
helpers.SendEventToUser(ctx, buffer.Bytes(), protoUser)
if err := helpers.SendEventToUser(ctx, buffer.Bytes(), protoUser); err != nil {
log.Println(err)
}
}

// update friends with new activity of user
Expand Down Expand Up @@ -138,7 +143,9 @@ func (s *Service) UpdateActivity(ctx context.Context, req *proto.UpdateActivityR
pms := &proto.PersonalActivityMsgEvent{Activity: activity, User: protoUser}
buffer, err := protocol.NewMsgProtobuf(proto.EMSG_SELF_PERSONAL_ACTIVITY_CHANGED, pms)
if err == nil {
helpers.SendEventToUser(ctx, buffer.Bytes(), protoUser)
if err := helpers.SendEventToUser(ctx, buffer.Bytes(), protoUser); err != nil {
log.Println(err)
}
}

// update friends with new activity of user
Expand Down

0 comments on commit 31bc810

Please sign in to comment.