reportRoom.go 3.2 KB
Newer Older
T
tanggen 已提交
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
// Copyright (C) 2020 Finogeeks Co., Ltd
//
// This program is free software: you can redistribute it and/or  modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package api

import (
	"context"
	"fmt"
	"github.com/finogeeks/ligase/common"
	"github.com/finogeeks/ligase/common/apiconsumer"
	"github.com/finogeeks/ligase/common/config"
	"github.com/finogeeks/ligase/common/jsonerror"
	"github.com/finogeeks/ligase/core"
	"github.com/finogeeks/ligase/model/authtypes"
	"github.com/finogeeks/ligase/plugins/message/external"
	"github.com/finogeeks/ligase/plugins/message/internals"
	"github.com/finogeeks/ligase/skunkworks/log"
	"net/http"
)

func init() {
	apiconsumer.SetAPIProcessor(ReqPostReportRoom{})
}

type ReqPostReportRoom struct{}

func (ReqPostReportRoom) GetRoute() string       { return "/report/user/room" }
func (ReqPostReportRoom) GetMetricsName() string { return "report user cur device roomId" }
func (ReqPostReportRoom) GetMsgType() int32      { return internals.MSG_POST_REPORT_ROOM }
func (ReqPostReportRoom) GetAPIType() int8       { return apiconsumer.APITypeAuth }
func (ReqPostReportRoom) GetMethod() []string {
	return []string{http.MethodPost, http.MethodOptions}
}
func (ReqPostReportRoom) GetTopic(cfg *config.Dendrite) string { return getProxyRpcTopic(cfg) }
func (ReqPostReportRoom) GetPrefix() []string                  { return []string{"r0", "unstable"} }
func (ReqPostReportRoom) NewRequest() core.Coder {
	return new(external.PostReportRoomRequest)
}
func (ReqPostReportRoom) FillRequest(coder core.Coder, req *http.Request, vars map[string]string) error {
	msg := coder.(*external.PostReportRoomRequest)
	err := common.UnmarshalJSON(req, msg)
	if err != nil {
		return err
	}
	return nil
}
func (ReqPostReportRoom) NewResponse(code int) core.Coder {
	return nil
}
func (ReqPostReportRoom) Process(ctx context.Context, consumer interface{}, msg core.Coder, device *authtypes.Device) (int, core.Coder) {
	c := consumer.(*InternalMsgConsumer)
	if !common.IsRelatedRequest(device.UserID, c.Cfg.MultiInstance.Instance, c.Cfg.MultiInstance.Total, c.Cfg.MultiInstance.MultiWrite) {
		return internals.HTTP_RESP_DISCARD, jsonerror.MsgDiscard("msg discard")
	}
	req := msg.(*external.PostReportRoomRequest)
	if req.RoomID != "" {
		isJoin := c.userTimeLine.CheckIsJoinRoom(ctx, device.UserID, req.RoomID)
		if !isJoin {
			return http.StatusForbidden, jsonerror.BadJSON(fmt.Sprintf("user:%s is not in room:%s", device.UserID, req.RoomID))
		}
	} else {
		//compatibility leave room and previous version
		req.RoomID = "none"
	}
	c.userTimeLine.SetUserCurRoom(device.UserID, device.ID, req.RoomID)
	log.Infof("user:%s,device:%s,report roomID:%s", device.UserID, device.ID, req.RoomID)
	return http.StatusOK, nil
}