Skip to content

Commit

Permalink
add notify for meeting methods and models
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad2002 committed Dec 4, 2024
1 parent ad14868 commit 4848f09
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 0 deletions.
27 changes: 27 additions & 0 deletions controller/notification_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func notificationController() {

router.POST("/notification/test/:device", sendTestNotification)
router.POST("/notification/:device", sendNotification)
router.POST("/notification/meet/:meeting", sendNotificationForMeeting)

router.POST("/notification/broadcast/:channel", sendBroadcast)
router.POST("/notification/broadcast/meeting/:meeting", sendMeetingBroadcast)
Expand Down Expand Up @@ -63,6 +64,32 @@ func sendNotification(c *gin.Context) {
})
}

func sendNotificationForMeeting(c *gin.Context) {

if failIfNotRoot(c) {
return
}

meeting := c.Param("meeting")

var request dto.NotificationRequestDto
if err := c.BindJSON(&request); err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}

users, notificationUsers, err := service.SendPushNotificationForMeeting(meeting, request.Title, request.Subtitle, request.Message)
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}

c.IndentedJSON(http.StatusOK, dto.NotificationsResponseDto{
UserCount: users,
NotificationUserCount: notificationUsers,
})
}

func sendBroadcast(c *gin.Context) {

if failIfNotRoot(c) {
Expand Down
5 changes: 5 additions & 0 deletions dto/notification_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ type NotificationResponseDto struct {
ApnsId string `json:"apns_id"`
Body string `json:"body"`
}

type NotificationsResponseDto struct {
UserCount int `json:"user_count"`
NotificationUserCount int `json:"notification_user_count"`
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ require (
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/swimresults/athlete-service v0.3.0 // indirect
github.com/swimresults/meeting-service v0.7.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -74,6 +76,10 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/swimresults/athlete-service v0.3.0 h1:j0JT29Xe1kFZVMUnNRVIxAuZzYKcuMzcYWKPFwv55xM=
github.com/swimresults/athlete-service v0.3.0/go.mod h1:Qle3iMWqW9uFWSCkjy7oFVMZ2uFry5tC2JX9BzKkpNg=
github.com/swimresults/athlete-service v1.6.0 h1:rWI5k+eSbTZSHJ5cHDSItaQglH6ak/KfD+4V73nRF0k=
github.com/swimresults/athlete-service v1.6.0/go.mod h1:eTpOGlbX9Tk/xVE0Nf1jpwxO7yg1rbfsKIHweN0CRVg=
github.com/swimresults/meeting-service v0.5.0 h1:RtSuEBVI5kSyl1aequJHGwa9BuoPnJ5MXeN3jUvCIy0=
github.com/swimresults/meeting-service v0.5.0/go.mod h1:60kU5gbAV253krD6q5nlLsyvFLTRcbA7zLHOXRSfkRA=
github.com/swimresults/meeting-service v0.6.0 h1:VPlMgL6k0NXj1z4JNETq1uja/xWhN48y3InxHB8+vuM=
Expand Down Expand Up @@ -129,6 +135,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
48 changes: 48 additions & 0 deletions service/notification_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"fmt"
"github.com/swimresults/user-service/apns"
"go.mongodb.org/mongo-driver/bson/primitive"
"golang.org/x/net/http2"
"io/ioutil"
"net/http"
"os"
"sync"
)

var apnsUrl = os.Getenv("SR_APNS_URL")
Expand Down Expand Up @@ -61,6 +63,52 @@ func SendTestPushNotification(receiver string) error {
return nil
}

func SendPushNotificationForMeeting(meeting string, title string, subtitle string, message string) (int, int, error) {
athletes, err := ac.GetAthletesByMeeting(meeting)
if err != nil {
return 0, 0, err
}

var athleteIds []primitive.ObjectID
for _, athlete := range athletes {
athleteIds = append(athleteIds, athlete.Identifier)
}

users, err := GetUsersByIsFollowerOrMe(athleteIds)
if err != nil {
return 0, 0, err
}

var userIds []primitive.ObjectID
for _, user := range users {
userIds = append(userIds, user.Identifier)
}

notificationUsers, err := GetNotificationUsersByUserIds(userIds)
if err != nil {
return 0, 0, err
}

var wg sync.WaitGroup

success := 0
for _, user := range notificationUsers {
wg.Add(1)
go func(receiver string, title string, subtitle string, message string, success *int) {
defer wg.Done()
_, _, _, err := SendPushNotification(receiver, title, subtitle, message)
if err == nil {
*success++
}
}(user.Token, title, subtitle, message, &success)
}

wg.Wait() // Wait for all goroutines to finish

fmt.Printf("notified %d users with %d/%d devices", len(users), success, len(notificationUsers))
return len(users), success, nil
}

func SendPushNotification(receiver string, title string, subtitle string, message string) (string, string, int, error) {
token := apns.GetToken()

Expand Down
4 changes: 4 additions & 0 deletions service/notification_user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func GetNotificationUserByToken(token string) (model.NotificationUser, error) {
return getNotificationUserByBsonDocument(bson.D{{"token", token}})
}

func GetNotificationUsersByUserIds(userIds []primitive.ObjectID) ([]model.NotificationUser, error) {
return getNotificationUsersByBsonDocument(bson.M{"user_id": bson.M{"$in": userIds}})
}

func RemoveNotificationUserById(id primitive.ObjectID) error {

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand Down
3 changes: 3 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"context"
"fmt"
athleteClient "github.com/swimresults/athlete-service/client"
meetingClient "github.com/swimresults/meeting-service/client"
meetingModel "github.com/swimresults/meeting-service/model"
"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -12,8 +13,10 @@ import (
)

var meetingServiceUrl = os.Getenv("SR_USER_MEETING_URL")
var athleteServiceUrl = os.Getenv("SR_USER_ATHLETE_URL")

var mc = meetingClient.NewMeetingClient(meetingServiceUrl)
var ac = athleteClient.NewAthleteClient(athleteServiceUrl)

var client *mongo.Client

Expand Down
9 changes: 9 additions & 0 deletions service/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ func GetUserById(id primitive.ObjectID) (model.User, error) {
return getUserByBsonDocument(bson.D{{"_id", id}})
}

func GetUsersByIsFollowerOrMe(athleteIds []primitive.ObjectID) ([]model.User, error) {
return getUsersByBsonDocument(bson.M{
"$or": []interface{}{
bson.M{"own_athlete_id": bson.M{"$in": athleteIds}},
bson.M{"following.athlete_id": athleteIds},
},
})
}

// GetUserByKeycloakId gets a user by keycloak id, creates new one if not existing so far
func GetUserByKeycloakId(id uuid.UUID) (model.User, error) {
user, err := getUserByBsonDocument(bson.D{{"keycloak_id", id.String()}})
Expand Down

0 comments on commit 4848f09

Please sign in to comment.