| 1 | package slacktest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "time" |
| 8 | |
| 9 | websocket "github.com/gorilla/websocket" |
| 10 | |
| 11 | slack "github.com/slack-go/slack" |
| 12 | ) |
| 13 | |
| 14 | func (sts *Server) queueForWebsocket(s, hubname string) { |
| 15 | channel, err := getHubForServer(hubname) |
| 16 | if err != nil { |
| 17 | log.Printf("Unable to get server's channels: %s", err.Error()) |
| 18 | } |
| 19 | sts.seenOutboundMessages.Lock() |
| 20 | sts.seenOutboundMessages.messages = append(sts.seenOutboundMessages.messages, s) |
| 21 | sts.seenOutboundMessages.Unlock() |
| 22 | channel.sent <- s |
| 23 | } |
| 24 | |
| 25 | func handlePendingMessages(c *websocket.Conn, hubname string) { |
| 26 | channel, err := getHubForServer(hubname) |
| 27 | if err != nil { |
| 28 | log.Printf("Unable to get server's channels: %s", err.Error()) |
| 29 | return |
| 30 | } |
| 31 | for m := range channel.sent { |
| 32 | err := c.WriteMessage(websocket.TextMessage, []byte(m)) |
| 33 | if err != nil { |
| 34 | log.Printf("error writing message to websocket: %s", err.Error()) |
| 35 | continue |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func (sts *Server) postProcessMessage(m, hubname string) { |
| 41 | channel, err := getHubForServer(hubname) |
| 42 | if err != nil { |
| 43 | log.Printf("Unable to get server's channels: %s", err.Error()) |
| 44 | return |
| 45 | } |
| 46 | sts.seenInboundMessages.Lock() |
| 47 | sts.seenInboundMessages.messages = append(sts.seenInboundMessages.messages, m) |
| 48 | sts.seenInboundMessages.Unlock() |
| 49 | // send to firehose |
| 50 | channel.seen <- m |
| 51 | } |
| 52 | |
| 53 | func newHub() *hub { |
| 54 | h := &hub{} |
| 55 | c := make(map[string]*messageChannels) |
| 56 | h.serverChannels = c |
| 57 | return h |
| 58 | } |
| 59 | |
| 60 | func addServerToHub(s *Server, channels *messageChannels) error { |
| 61 | if s.ServerAddr == "" { |
| 62 | return ErrEmptyServerToHub |
| 63 | } |
| 64 | masterHub.Lock() |
| 65 | masterHub.serverChannels[s.ServerAddr] = channels |
| 66 | masterHub.Unlock() |
| 67 | return nil |
| 68 | } |
| 69 | |
| 70 | func getHubForServer(serverAddr string) (*messageChannels, error) { |
| 71 | if serverAddr == "" { |
| 72 | return &messageChannels{}, ErrPassedEmptyServerAddr |
| 73 | } |
| 74 | masterHub.RLock() |
| 75 | defer masterHub.RUnlock() |
| 76 | channels, ok := masterHub.serverChannels[serverAddr] |
| 77 | if !ok { |
| 78 | return &messageChannels{}, ErrNoQueuesRegisteredForServer |
| 79 | } |
| 80 | return channels, nil |
| 81 | } |
| 82 | |
| 83 | // BotNameFromContext returns the botname from a provided context |
| 84 | func BotNameFromContext(ctx context.Context) string { |
| 85 | botname, ok := ctx.Value(ServerBotNameContextKey).(string) |
| 86 | if !ok { |
| 87 | return defaultBotName |
| 88 | } |
| 89 | return botname |
| 90 | } |
| 91 | |
| 92 | // BotIDFromContext returns the bot userid from a provided context |
| 93 | func BotIDFromContext(ctx context.Context) string { |
| 94 | botname, ok := ctx.Value(ServerBotIDContextKey).(string) |
| 95 | if !ok { |
| 96 | return defaultBotID |
| 97 | } |
| 98 | return botname |
| 99 | } |
| 100 | |
| 101 | // generate a full rtminfo response for initial rtm connections |
| 102 | func generateRTMInfo(ctx context.Context, wsurl string) *fullInfoSlackResponse { |
| 103 | rtmInfo := slack.Info{ |
| 104 | URL: wsurl, |
| 105 | Team: defaultTeam, |
| 106 | User: defaultBotInfo, |
| 107 | } |
| 108 | rtmInfo.User.ID = BotIDFromContext(ctx) |
| 109 | rtmInfo.User.Name = BotNameFromContext(ctx) |
| 110 | return &fullInfoSlackResponse{ |
| 111 | rtmInfo, |
| 112 | okWebResponse, |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func nowAsJSONTime() slack.JSONTime { |
| 117 | return slack.JSONTime(time.Now().Unix()) |
| 118 | } |
| 119 | |
| 120 | func defaultBotInfoJSON(ctx context.Context) string { |
| 121 | botid := BotIDFromContext(ctx) |
| 122 | botname := BotNameFromContext(ctx) |
| 123 | return fmt.Sprintf(` |
| 124 | { |
| 125 | "ok":true, |
| 126 | "bot":{ |
| 127 | "id": "%s", |
| 128 | "app_id": "A4H1JB4AZ", |
| 129 | "deleted": false, |
| 130 | "name": "%s", |
| 131 | "icons": { |
| 132 | "image_36": "https://localhost.localdomain/img36.png", |
| 133 | "image_48": "https://localhost.localdomain/img48.png", |
| 134 | "image_72": "https://localhost.localdomain/img72.png" |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | `, botid, botname) |
| 139 | } |