-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
129 lines (94 loc) · 2.69 KB
/
message.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"time"
)
type CreateMessageType struct {
ChannelID string
Message string
}
type AddUser struct {
ChannelID primitive.ObjectID
NewUsers map[string]string
UserMap map[string]string
}
type WebsocketMessageType struct {
MessageType string
MessageContent interface{}
}
func CreateMessageRoute(c *gin.Context) {
var form CreateMessageType
c.BindJSON(&form)
if form.ChannelID == "" {
c.JSON(400, gin.H{"message": "You must send a channel id"})
return
}
if form.Message == "" {
c.JSON(400, gin.H{"message": "You must send a message"})
return
}
convertID, err := primitive.ObjectIDFromHex(form.ChannelID)
if err != nil {
c.JSON(500, gin.H{"message": "Error converting your id to a mongo id", "err": err})
return
}
var foundChannel ChannelSchema
err = Channels.FindOne(context.TODO(), bson.D{{"_id", convertID}}).Decode(&foundChannel)
if err != nil {
c.JSON(400, gin.H{ "message": "Channel does not exist" })
return
}
newMessage := MessageSchema{primitive.NewObjectID(), convertID, time.Now(), form.Message}
go (func() {
message := WebsocketMessageType{"NEW_MESSAGE", newMessage}
clients := make([]string, 0)
userContext, _ := c.Get("user")
user := userContext.(UserSchema)
userIDString := user.ID.String()[10:34]
for key := range foundChannel.PrivateKeys {
clients = append(clients, key)
if key != userIDString {
SendNotif(key, newMessage)
}
}
HubGlob.createMessage <- CreatedMessageStruct{message:&message, clients: &clients}
})()
_, err = Messages.InsertOne(context.TODO(), newMessage)
if err != nil {
c.JSON(400, gin.H{"message": "Could not insert message", "err": err})
return
}
c.JSON(200, gin.H{"message": "Message sent successfully", "inserted": newMessage})
}
func GetMessagesRoute(c * gin.Context) {
idQuery := c.Param("channelID")
if idQuery == "" {
c.JSON(400, gin.H{"message": "You must send a channelID in the path param"})
return
}
id, err := primitive.ObjectIDFromHex(idQuery)
if err != nil {
c.JSON(400, gin.H{"message": "Could not parse id into an ObjectID", "err": err})
return
}
messages := make([]MessageSchema, 0)
cursor, err := Messages.Find(context.TODO(), bson.M{"channelid": id})
if err != nil {
c.JSON(500, gin.H{"message": "Could not query messages", "err": err})
return
}
for cursor.Next(context.Background()) {
message := MessageSchema{}
err := cursor.Decode(&message)
if err != nil {
//handle err
fmt.Println("err", err)
}
messages = append(messages, message)
}
c.JSON(200, gin.H{"message": "Success", "messages": messages})
}