-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (61 loc) · 1.92 KB
/
main.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
package cloudfunctions
import (
"cloudfunctions/models"
"cloudfunctions/repository/comment"
"cloudfunctions/repository/flag"
"cloudfunctions/repository/notification"
"cloudfunctions/utils"
"context"
"log"
)
// CreateNotifications trigger to create a notification to user that created a white flag
func CreateNotifications(ctx context.Context, e models.EventComment) error {
path := e.Value.Name
flagID := utils.GetParams(path, "comments")
flagComment := e.Value.Fields.ParseComment()
flagNotification := map[string]interface{}{
"sender_name": flagComment.SenderName,
"sender_photo_url": flagComment.SenderPhotoURL,
"message": flagComment.Text,
"uid": flagComment.UID,
"flag_id": flagID,
}
flagSelected := <-flag.GetByID(flagID)
commenters := <-comment.GetUsers(flagID)
// Create Notifications
for _, userID := range commenters {
if userID != flagComment.UID && userID != flagSelected.UID {
err := <-notification.Save(userID, flagNotification)
if err != nil {
log.Fatalf("Error save notification: %v", err)
return err
}
}
}
if flagComment.UID != flagSelected.UID {
err := <-notification.Save(flagSelected.UID, flagNotification)
if err != nil {
log.Fatalf("Error send notification to flag creator: %v", err)
return err
}
}
err := <-notification.Send(flagID, &flagComment)
return err
}
// CreateImageMarked trigger to create a image with logo
func CreateImageMarked(ctx context.Context, e models.EventFlag) error {
path := e.Value.Name
flagID := utils.GetParams(path, "flags")
flagData := e.Value.Fields.ParseFlag()
imageMarkedURL, err := flag.UploadImageMarked(flagID, flagData)
if err != nil {
log.Fatalf("Error to upload flag image marked: %v", err)
return err
}
err = <-flag.Update(flagID, map[string]interface{}{"image_marked": imageMarkedURL})
if err != nil {
log.Fatalf("Error to update flag: %v", err)
return err
}
return nil
}