2019-05-17 06:28:50 +03:00
|
|
|
// Copyright (c) 2014-present, b3log.org
|
2014-11-20 06:30:18 +03:00
|
|
|
//
|
2014-11-12 18:13:14 +03:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-11-20 06:30:18 +03:00
|
|
|
//
|
2018-03-12 07:28:33 +03:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2014-11-20 06:30:18 +03:00
|
|
|
//
|
2014-11-12 18:13:14 +03:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-12-07 06:32:46 +03:00
|
|
|
// Package notification includes notification related manipulations.
|
2014-09-15 10:24:40 +04:00
|
|
|
package notification
|
2014-09-15 09:03:44 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2014-12-13 13:47:41 +03:00
|
|
|
"os"
|
2014-11-20 06:30:18 +03:00
|
|
|
"strconv"
|
2014-09-15 14:03:52 +04:00
|
|
|
"time"
|
2014-09-15 09:03:44 +04:00
|
|
|
|
2019-12-01 15:21:00 +03:00
|
|
|
"github.com/88250/gulu"
|
|
|
|
"github.com/88250/wide/conf"
|
|
|
|
"github.com/88250/wide/event"
|
|
|
|
"github.com/88250/wide/i18n"
|
|
|
|
"github.com/88250/wide/session"
|
|
|
|
"github.com/88250/wide/util"
|
2014-09-15 09:03:44 +04:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
2014-09-15 19:20:01 +04:00
|
|
|
const (
|
2014-12-07 06:32:46 +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-12-07 06:32:46 +03:00
|
|
|
setup = "Setup" // notification.type: setup
|
|
|
|
server = "Server" // notification.type: server
|
2014-09-15 19:20:01 +04:00
|
|
|
)
|
|
|
|
|
2014-12-13 13:47:41 +03:00
|
|
|
// Logger.
|
2019-05-24 16:04:25 +03:00
|
|
|
var logger = gulu.Log.NewLogger(os.Stdout)
|
2014-12-13 13:47:41 +03:00
|
|
|
|
2014-12-07 06:32:46 +03:00
|
|
|
// Notification represents a 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
|
|
|
|
}
|
|
|
|
|
2019-05-16 19:41:52 +03:00
|
|
|
httpSession, _ := session.HTTPSession.Get(wsChannel.Request, session.CookieName)
|
2019-05-16 18:17:25 +03:00
|
|
|
uid := httpSession.Values["uid"].(string)
|
|
|
|
locale := conf.GetUser(uid).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-12-07 06:32:46 +03:00
|
|
|
notification = &Notification{event: e, Type: setup, Severity: error,
|
2014-10-28 19:04:46 +03:00
|
|
|
Message: i18n.Get(locale, "notification_"+strconv.Itoa(e.Code)).(string)}
|
|
|
|
case event.EvtCodeServerInternalError:
|
2014-12-07 06:32:46 +03:00
|
|
|
notification = &Notification{event: e, Type: server, Severity: error,
|
2014-10-28 19:04:46 +03:00
|
|
|
Message: i18n.Get(locale, "notification_"+strconv.Itoa(e.Code)).(string) + " [" + e.Data.(string) + "]"}
|
2014-09-15 19:20:01 +04:00
|
|
|
default:
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Warnf("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-11-20 08:59:08 +03:00
|
|
|
wsChannel.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-11-20 06:30:18 +03:00
|
|
|
ret := map[string]interface{}{"notification": "Notification initialized", "cmd": "init-notification"}
|
2014-11-20 08:59:08 +03:00
|
|
|
err := wsChan.WriteJSON(&ret)
|
2014-11-20 06:30:18 +03:00
|
|
|
if nil != err {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-20 06:39:29 +04:00
|
|
|
session.NotificationWS[sid] = &wsChan
|
2014-09-15 09:03:44 +04:00
|
|
|
|
2014-12-14 18:05:54 +03:00
|
|
|
logger.Tracef("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-11-20 09:11:54 +03:00
|
|
|
if err := wsChan.ReadJSON(&input); err != nil {
|
2014-09-15 09:03:44 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|