Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineF4C5 committed Mar 16, 2024
2 parents c62b9a6 + 2674ed0 commit 4eb8766
Show file tree
Hide file tree
Showing 26 changed files with 441 additions and 158 deletions.
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
)

require (
github.com/JamesLMilner/pip-go v0.0.0-20180711171552-99c4cbbc7deb // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/chrusty/go-circle-to-polygon v0.0.0-20220701033300-72c713e17a6e // indirect
Expand All @@ -33,6 +34,7 @@ require (
github.com/paulmach/go.geojson v1.4.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/twpayne/go-geom v1.5.4 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
Expand All @@ -50,16 +52,16 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/ostafen/clover/v2 v2.0.0-alpha.3.0.20240131202429-e6a58d6d36bf
github.com/paulmach/orb v0.11.1
github.com/pkg/errors v0.9.1 // indirect
github.com/satori/go.uuid v1.2.0
go.opencensus.io v0.24.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
golang.org/x/sys v0.18.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ cloud.google.com/go/workflows v1.9.0/go.mod h1:ZGkj1aFIOd9c8Gerkjjq7OW7I5+l6cSvT
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/JamesLMilner/pip-go v0.0.0-20180711171552-99c4cbbc7deb h1:bjNgq82N34iXnqnnKejMvzBlOPhVZIF/7wW4nN7IZAc=
github.com/JamesLMilner/pip-go v0.0.0-20180711171552-99c4cbbc7deb/go.mod h1:wGn12HkuV+58JRmRJS/5ARIsZIKULU14B6j16mkVqOo=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
Expand Down Expand Up @@ -521,6 +523,8 @@ github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
Expand Down Expand Up @@ -612,6 +616,8 @@ github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
Expand Down Expand Up @@ -698,6 +704,8 @@ github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/twpayne/go-geom v1.5.4 h1:b8fiZd0SsEmQEeUdz2atT6KggF1KHiaZIi3DGi5p+sI=
github.com/twpayne/go-geom v1.5.4/go.mod h1:Hw8RszQ2/d9Y/KfOm9CvUJo78BOoIA5g0e4P7JCVKvo=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
Expand Down Expand Up @@ -965,6 +973,8 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand Down Expand Up @@ -1286,6 +1296,8 @@ google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
3 changes: 3 additions & 0 deletions k8s/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ spec:
{{- with secret "kv/hackqc2024/default/secrets" -}}
export API_KEY="{{ .Data.data.api_key }}"
export JEU_DE_DONNEES="{{ .Data.data.jeu_de_donnees }}"
export VOIPMS_API_USERNAME="{{ .Data.data.voipms_api_username }}"
export VOIPMS_API_PASSWORD="{{ .Data.data.voipms_api_password }}"
export VOIPMS_API_DID="{{ .Data.data.voipms_api_did }}"
{{- end }}
spec:
Expand Down
2 changes: 1 addition & 1 deletion k8s/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: Kustomization
namespace: hackqc2024
images:
- name: ghcr.io/clubcedille/hackqc2024
newTag: commit-fc06e91285b0ff52d4a8ded95f09668433bbfaa5
newTag: commit-cce50c65d9b89a5ddae6a408e5db75b5a2cf45c8

resources:
- namespace.yaml
Expand Down
30 changes: 25 additions & 5 deletions pkg/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
)

type Account struct {
Id string `json:"_id" clover:"_id"`
UserName string `json:"user_name" clover:"user_name"`
FirstName string `json:"first_name" clover:"first_name"`
LastName string `json:"last_name" clover:"last_name"`
Email string `json:"email" clover:"email"`
Id string `json:"_id" clover:"_id"`
UserName string `json:"user_name" clover:"user_name"`
FirstName string `json:"first_name" clover:"first_name"`
LastName string `json:"last_name" clover:"last_name"`
Email string `json:"email" clover:"email"`
Coordinates []float64 `json:"coordinates" clover:"coordinates"`
}

func AccountExistsById(conn *clover.DB, id string) (bool, error) {
Expand Down Expand Up @@ -73,3 +74,22 @@ func UpdateAccount(conn *clover.DB, account Account) error {

return nil
}

func GetAllAccounts(conn *clover.DB) ([]Account, error) {
docs, err := conn.FindAll(query.NewQuery(database.AccountCollection))
if err != nil {
return []Account{}, err
}

accounts := []Account{}
for _, d := range docs {
var account Account
err = d.Unmarshal(&account)
if err != nil {
return []Account{}, err
}
accounts = append(accounts, account)
}

return accounts, nil
}
4 changes: 4 additions & 0 deletions pkg/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ClubCedille/hackqc2024/pkg/database"
mapobject "github.com/ClubCedille/hackqc2024/pkg/map_object"
"github.com/ClubCedille/hackqc2024/pkg/notifications"
"github.com/ostafen/clover/v2"
"github.com/ostafen/clover/v2/document"
"github.com/ostafen/clover/v2/query"
Expand Down Expand Up @@ -241,6 +242,9 @@ func CreateEvent(conn *clover.DB, event Event) error {
return err
}

message := fmt.Sprintf("Alert: Un nouvel évènement de type %s a été signalé près de vous", event.MapObject.Category)
err = notifications.NotifyNearby(conn, message, event.MapObject.Geometry)

return nil
}

Expand Down
46 changes: 46 additions & 0 deletions pkg/geometry/geometry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package geometry

import (
"strconv"
"strings"

mapobject "github.com/ClubCedille/hackqc2024/pkg/map_object"
pip "github.com/JamesLMilner/pip-go"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/xy"
)

func IsInGeom(point []float64, geometry mapobject.Geometry) bool {
if geometry.GeomType == "Point" {
const distCutoff = 0.01

coord1 := geometry.AsGeomCoord()
coord2 := geom.Coord(point[0:2])

distance := xy.Distance(coord1, coord2)

return distance < distCutoff
} else if geometry.GeomType == "Polygon" {
pipPoint := pip.Point{X: point[0], Y: point[1]}
polygon := *geometry.AsPipPolygon()

return pip.PointInPolygon(pipPoint, polygon)
}

return false
}

func ParseCoordinatesString(coordinates string) ([]float64, error) {
coordinatesArray := strings.Split(coordinates, ",")

var coordinatesArrayFloat []float64
for i := len(coordinatesArray) - 1; i >= 0; i-- {
coords, err := strconv.ParseFloat(strings.TrimSpace(coordinatesArray[i]), 64)
if err != nil {
return nil, err
}
coordinatesArrayFloat = append(coordinatesArrayFloat, coords)
}

return coordinatesArrayFloat, nil
}
2 changes: 2 additions & 0 deletions pkg/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Help struct {
HowToHelp string `json:"how_to_help" clover:"how_to_help"`
HowToUseHelp string `json:"how_to_use_help" clover:"how_to_use_help"`
EventId string `json:"event_id" clover:"event_id"`
Exported bool `json:"exported" clover:"exported"`
}

func GetHelpById(db *clover.DB, helpId string) (Help, error) {
Expand Down Expand Up @@ -160,6 +161,7 @@ func UpdateHelp(db *clover.DB, help Help) error {
doc.Set("need_help", help.NeedHelp)
doc.Set("how_to_help", help.HowToHelp)
doc.Set("how_to_use_help", help.HowToUseHelp)
doc.Set("exported", help.Exported)
return doc
})

Expand Down
23 changes: 23 additions & 0 deletions pkg/map_object/map_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package mapobject

import (
"time"

"github.com/JamesLMilner/pip-go"
"github.com/twpayne/go-geom"
)

type MapObject struct {
Expand Down Expand Up @@ -80,3 +83,23 @@ func (mapObject *MapObject) GetCategoryEmoji() string {
return ""
}
}

func (geometry *Geometry) AsGeomCoord() geom.Coord {
if geometry.GeomType == "Point" {
return geom.Coord(geometry.Coordinates[0:2])
}
return nil
}

func (geometry *Geometry) AsPipPolygon() *pip.Polygon {
if geometry.GeomType == "Polygon" {
points := []pip.Point{}
for i := 0; i < len(geometry.Coordinates); i += 2 {
x := geometry.Coordinates[i]
y := geometry.Coordinates[i+1]
points = append(points, pip.Point{X: x, Y: y})
}
return &pip.Polygon{Points: points}
}
return nil
}
104 changes: 103 additions & 1 deletion pkg/notifications/notifications.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,110 @@
package notifications

import "fmt"
import (
"fmt"
"log"

"io"
"net/http"
"net/url"
"os"

"github.com/ClubCedille/hackqc2024/pkg/account"
"github.com/ClubCedille/hackqc2024/pkg/geometry"
mapobject "github.com/ClubCedille/hackqc2024/pkg/map_object"
"github.com/ostafen/clover/v2"
)

type Notification struct {
APIUsername string `json:"api_username"`
APIPassword string `json:"api_password"`
Message string `json:"message"`
Dst string `json:"dst"`
Did string `json:"did"`
}

// Send notification to all phone numbers
func SendNotification(message string, recipients []string) {
// TODO: Implement notification sending
fmt.Println("Sending Notification, message: ", message, " recipients: ", recipients)

for _, recipient := range recipients {
fmt.Println("Sending notification to: ", recipient)

// go SendNotificationToPhoneNumber(message, recipient)
}
}

func SendNotificationToPhoneNumber(message string, recipient string) {

api_username := os.Getenv("VOIPMS_API_USERNAME")
api_password := os.Getenv("VOIPMS_API_PASSWORD")
api_did := os.Getenv("VOIPMS_API_DID")

params := url.Values{
"method": []string{"sendSMS"},
"message": []string{message},
"dst": []string{recipient},
"did": []string{api_did},
"api_username": []string{api_username},
"api_password": []string{api_password},
}

u := &url.URL{
Scheme: "https",
Host: "voip.ms",
Path: "/api/v1/rest.php",
RawQuery: params.Encode(),
}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
fmt.Println("Error creating request: ", err)
}

client := &http.Client{}
result, err := client.Do(req)

if err != nil || result.StatusCode != 200 {
fmt.Println("Error sending notification to phone number: ", err)
}
defer result.Body.Close()

body, err := io.ReadAll(result.Body)
if err != nil {
fmt.Println("Error reading response body: ", err)
}

fmt.Println("Result sending notification to phone number: ", string(body))
}

func NotifyNearby(db *clover.DB, message string, geom mapobject.Geometry) error {
accounts, err := account.GetAllAccounts(db)
if err != nil {
log.Println("Error getting accounts:", err)
return err
}

accountIds := []string{}
for _, account := range accounts {
if geometry.IsInGeom(account.Coordinates, geom) {
accountIds = append(accountIds, account.Id)
}
}

if len(accountIds) > 0 {
SendNotification(
message,
accountIds,
)
}

return nil
}

// what am I even doing here?
func NotifyEventSubscribers(db *clover.DB, message string, subscribers []string) {
SendNotification(
message,
subscribers,
)
}
Loading

0 comments on commit 4eb8766

Please sign in to comment.