wide/notification/notifications.go

111 lines
2.7 KiB
Go
Raw Normal View History

2014-10-29 13:15:18 +03:00
// Notification manipulations.
2014-09-15 10:24:40 +04:00
package notification
2014-09-15 09:03:44 +04:00
import (
"net/http"
2014-09-15 14:03:52 +04:00
"time"
2014-09-15 09:03:44 +04:00
2014-09-15 19:20:01 +04:00
"strconv"
2014-10-23 17:43:35 +04:00
"github.com/b3log/wide/conf"
2014-09-15 14:03:52 +04:00
"github.com/b3log/wide/event"
2014-09-15 19:20:01 +04:00
"github.com/b3log/wide/i18n"
2014-09-19 15:21:13 +04:00
"github.com/b3log/wide/session"
2014-09-16 11:06:52 +04:00
"github.com/b3log/wide/util"
2014-09-15 09:03:44 +04:00
"github.com/golang/glog"
"github.com/gorilla/websocket"
)
2014-09-15 19:20:01 +04:00
const (
2014-10-29 13:15:18 +03:00
Error = "ERROR" // notification.severity: ERROR
Warn = "WARN" // notification.severity: WARN
Info = "INFO" // notification.severity: INFO
2014-09-15 19:20:01 +04:00
2014-10-29 13:15:18 +03:00
Setup = "Setup" // notification.type: setup
Server = "Server" // notification.type: server
2014-09-15 19:20:01 +04:00
)
2014-10-29 13:15:18 +03:00
// Notification.
2014-09-15 09:03:44 +04:00
type Notification struct {
2014-09-15 19:20:01 +04:00
event *event.Event
Type string `json:"type"`
Severity string `json:"severity"`
Message string `json:"message"`
2014-09-15 09:03:44 +04:00
}
2014-10-29 13:15:18 +03:00
// event2Notification processes user event by converting the specified event to a notification, and then push it to front
// browser with notification channel.
2014-09-15 19:20:01 +04:00
func event2Notification(e *event.Event) {
2014-09-20 06:39:29 +04:00
if nil == session.NotificationWS[e.Sid] {
2014-09-15 19:20:01 +04:00
return
}
2014-09-20 06:39:29 +04:00
wsChannel := session.NotificationWS[e.Sid]
2014-10-28 19:04:46 +03:00
if nil == wsChannel {
return
}
httpSession, _ := session.HTTPSession.Get(wsChannel.Request, "wide-session")
username := httpSession.Values["username"].(string)
locale := conf.Wide.GetUser(username).Locale
2014-09-15 19:20:01 +04:00
2014-10-28 19:04:46 +03:00
var notification *Notification
2014-09-15 19:20:01 +04:00
switch e.Code {
case event.EvtCodeGocodeNotFound:
2014-10-28 19:04:46 +03:00
fallthrough
2014-09-15 19:20:01 +04:00
case event.EvtCodeIDEStubNotFound:
2014-10-28 19:04:46 +03:00
notification = &Notification{event: e, Type: Setup, Severity: Error,
Message: i18n.Get(locale, "notification_"+strconv.Itoa(e.Code)).(string)}
case event.EvtCodeServerInternalError:
notification = &Notification{event: e, Type: Server, Severity: Error,
Message: i18n.Get(locale, "notification_"+strconv.Itoa(e.Code)).(string) + " [" + e.Data.(string) + "]"}
2014-09-15 19:20:01 +04:00
default:
glog.Warningf("Can't handle event[code=%d]", e.Code)
2014-10-28 19:04:46 +03:00
2014-09-15 19:20:01 +04:00
return
}
2014-10-28 19:04:46 +03:00
wsChannel.Conn.WriteJSON(notification)
2014-10-23 17:43:35 +04:00
2014-10-28 19:04:46 +03:00
wsChannel.Refresh()
2014-09-15 19:20:01 +04:00
}
2014-09-15 09:03:44 +04:00
2014-10-29 13:15:18 +03:00
// WSHandler handles request of creating notification channel.
2014-09-15 09:03:44 +04:00
func WSHandler(w http.ResponseWriter, r *http.Request) {
2014-09-17 06:54:57 +04:00
sid := r.URL.Query()["sid"][0]
2014-09-15 09:03:44 +04:00
2014-09-19 15:21:13 +04:00
wSession := session.WideSessions.Get(sid)
2014-11-02 10:44:24 +03:00
if nil == wSession {
2014-09-19 15:21:13 +04:00
return
}
2014-09-15 14:03:52 +04:00
conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024)
2014-09-16 11:06:52 +04:00
wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
2014-09-15 14:03:52 +04:00
2014-09-20 06:39:29 +04:00
session.NotificationWS[sid] = &wsChan
2014-09-15 09:03:44 +04:00
2014-09-20 06:39:29 +04:00
glog.V(4).Infof("Open a new [Notification] with session [%s], %d", sid, len(session.NotificationWS))
2014-09-15 09:03:44 +04:00
2014-10-29 13:15:18 +03:00
// add user event handler
2014-09-19 15:21:13 +04:00
wSession.EventQueue.AddHandler(event.HandleFunc(event2Notification))
2014-09-15 09:03:44 +04:00
input := map[string]interface{}{}
for {
2014-09-15 14:03:52 +04:00
if err := wsChan.Conn.ReadJSON(&input); err != nil {
2014-09-15 09:03:44 +04:00
if err.Error() == "EOF" {
return
}
if err.Error() == "unexpected EOF" {
return
}
2014-09-15 14:03:52 +04:00
glog.Error("Notification WS ERROR: " + err.Error())
2014-10-28 19:04:46 +03:00
2014-09-15 09:03:44 +04:00
return
}
}
}