Skip to content

Commit

Permalink
Feature redact raw (#182)
Browse files Browse the repository at this point in the history
* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

Co-authored-by: chenzr <[email protected]>
  • Loading branch information
charizer and chenzr authored Nov 8, 2021
1 parent c667624 commit 3907f98
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 16 deletions.
45 changes: 45 additions & 0 deletions clientapi/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func init() {
apiconsumer.SetAPIProcessor(ReqPutRoomStateWidthType{})
apiconsumer.SetAPIProcessor(ReqPutRoomStateWidthTypeAndKey{})
apiconsumer.SetAPIProcessor(ReqPutRoomRedact{})
apiconsumer.SetAPIProcessor(ReqPutRoomRawRedact{})
apiconsumer.SetAPIProcessor(ReqPutRoomRedactWithTxnID{})
apiconsumer.SetAPIProcessor(ReqPutRoomUpdate{})
apiconsumer.SetAPIProcessor(ReqPutRoomUpdateWithTxnID{})
Expand Down Expand Up @@ -505,6 +506,50 @@ func (ReqPutRoomRedact) Process(consumer interface{}, msg core.Coder, device *au
)
}

type ReqPutRoomRawRedact struct{}

func (ReqPutRoomRawRedact) GetRoute() string { return "/rooms/{roomID}/rawredact/{eventId}" }
func (ReqPutRoomRawRedact) GetMetricsName() string { return "raw_redact_event" }
func (ReqPutRoomRawRedact) GetMsgType() int32 { return internals.MSG_PUT_ROOM_RAW_REDACT }
func (ReqPutRoomRawRedact) GetAPIType() int8 { return apiconsumer.APITypeAuth }
func (ReqPutRoomRawRedact) GetMethod() []string {
return []string{http.MethodPut, http.MethodPost, http.MethodOptions}
}
func (ReqPutRoomRawRedact) GetTopic(cfg *config.Dendrite) string { return getProxyRpcTopic(cfg) }
func (ReqPutRoomRawRedact) NewRequest() core.Coder {
return new(external.PutRedactEventRequest)
}
func (ReqPutRoomRawRedact) FillRequest(coder core.Coder, req *http.Request, vars map[string]string) error {
msg := coder.(*external.PutRedactEventRequest)
if vars != nil {
msg.EventID = vars["eventId"]
msg.RoomID = vars["roomID"]
}
data, err := ioutil.ReadAll(req.Body)
if err != nil {
return err
}
if err := json.Unmarshal(data, msg); err != nil {
return err
}
msg.Content = data
return nil
}
func (ReqPutRoomRawRedact) NewResponse(code int) core.Coder {
return new(external.PutRedactEventResponse)
}
func (ReqPutRoomRawRedact) GetPrefix() []string { return []string{"r0", "v1", "inr0"} }
func (ReqPutRoomRawRedact) Process(consumer interface{}, msg core.Coder, device *authtypes.Device) (int, core.Coder) {
c := consumer.(*InternalMsgConsumer)
req := msg.(*external.PutRedactEventRequest)
userID := device.UserID
deviceID := device.ID
return routing.RedactEvent(
context.Background(), req.Content, userID, deviceID, req.RoomID,
nil, nil, c.Cfg, c.localcache, c.rsRpcCli, req.EventID, "m.room.rawredact", c.idg,
)
}

type ReqPutRoomRedactWithTxnID struct{}

func (ReqPutRoomRedactWithTxnID) GetRoute() string {
Expand Down
8 changes: 6 additions & 2 deletions clientapi/routing/redactevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (
"github.com/finogeeks/ligase/common/jsonerror"
"github.com/finogeeks/ligase/common/uid"
"github.com/finogeeks/ligase/core"
"github.com/finogeeks/ligase/skunkworks/gomatrixserverlib"
"github.com/finogeeks/ligase/model/roomservertypes"
"github.com/finogeeks/ligase/model/service"
"github.com/finogeeks/ligase/model/service/roomserverapi"
"github.com/finogeeks/ligase/plugins/message/external"
"github.com/finogeeks/ligase/skunkworks/gomatrixserverlib"

log "github.com/finogeeks/ligase/skunkworks/log"
)
Expand Down Expand Up @@ -84,17 +84,21 @@ func RedactEvent(
EventID: redacts,
RoomID: roomID,
}
log.Infof("RedactEvent type:%s event_id:%s room_id:%s", eventType, redacts, roomID)

var queryRoomEventByIDResponse roomserverapi.QueryRoomEventByIDResponse

if err := rsRpcCli.QueryRoomEventByID(ctx, &queryRoomEventByIDRequest, &queryRoomEventByIDResponse); err != nil {
log.Errorf("RedactEvent rsRpcCli.QueryRoomEventByID event_id:%s room_id:%s err:%s", redacts, roomID, err.Error())
return 404, jsonerror.Unknown(err.Error())
}

if queryRoomEventByIDResponse.Event == nil {
log.Errorf("RedactEvent rsRpcCli.QueryRoomEventByID event_id:%s room_id:%s not found", redacts, roomID)
return 404, jsonerror.NotFound("can't find original event")
}

log.Infof("------------------------RedactEvent get target ev %s sender %v", queryRoomEventByIDResponse.Event, queryRoomEventByIDResponse.Event.Sender())
log.Infof("------------------------RedactEvent get target event_id:%s room_id:%s ev %v sender %v", queryRoomEventByIDResponse.Event.EventID(), queryRoomEventByIDResponse.Event.RoomID(), queryRoomEventByIDResponse.Event, queryRoomEventByIDResponse.Event.Sender())

builder := gomatrixserverlib.EventBuilder{
Sender: userID,
Expand Down
2 changes: 1 addition & 1 deletion clientapi/routing/userinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func SetUserInfo(
Landline: r.Landline,
Email: r.Email,
State: r.State,
//Presence: "online",
Presence: "online",
CurrentlyActive: true,
UserID: userID,
LastActiveAgo: 0,
Expand Down
2 changes: 1 addition & 1 deletion clientapi/rpc/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (s *Server) UpdateProfile(ctx context.Context, req *pb.UpdateProfileReq) (r
if oldUserInfo != nil {
if oldUserInfo.UserName == req.UserName && oldUserInfo.JobNumber == req.JobNumber &&
oldUserInfo.Mobile == req.Mobile && oldUserInfo.Landline == req.Landline &&
oldUserInfo.Email == req.Email {
oldUserInfo.Email == req.Email && oldUserInfo.State == int(req.State) {
upUserInfo = false
}
}
Expand Down
1 change: 1 addition & 0 deletions plugins/message/internals/msgdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const (
MSG_PUT_ROOM_REDACT_WITH_TXNID int32 = 0x00090002
MSG_PUT_ROOM_UPDATE int32 = 0x00090003
MSG_PUT_ROOM_UPDATE_WITH_TXNID int32 = 0x00090004
MSG_PUT_ROOM_RAW_REDACT int32 = 0x00090005

MSG_POST_CREATEROOM int32 = 0x000a0002

Expand Down
7 changes: 5 additions & 2 deletions roomserver/processors/roomQryProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"math"

"github.com/finogeeks/ligase/common/config"
"github.com/finogeeks/ligase/skunkworks/gomatrixserverlib"
"github.com/finogeeks/ligase/model/repos"
"github.com/finogeeks/ligase/model/service/roomserverapi"
"github.com/finogeeks/ligase/skunkworks/gomatrixserverlib"
"github.com/finogeeks/ligase/storage/model"

log "github.com/finogeeks/ligase/skunkworks/log"
Expand Down Expand Up @@ -132,15 +132,18 @@ func (s *RoomQryProcessor) QueryRoomEventByID(
) error {
response.EventID = request.EventID
response.RoomID = request.RoomID
log.Infof("############## begin QueryRoomEventByID, eventID: %s, roomID: %s", response.EventID, response.RoomID)

eventNID, err := s.DB.EventNID(ctx, request.EventID)
if err != nil {
log.Errorf("############## QueryRoomEventByID from db nid, eventID: %s, roomID: %s, err:%s", response.EventID, response.RoomID, err.Error())
return err
}
log.Infof("############## QueryRoomEventByID, EventNID: %d, eventID: %d, roomID: %d", eventNID, response.EventID, response.RoomID)
log.Infof("############## after QueryRoomEventByID, EventNID: %d, eventID: %s, roomID: %s", eventNID, response.EventID, response.RoomID)

evs, _, err := s.DB.Events(ctx, []int64{eventNID})
if err != nil {
log.Errorf("############## QueryRoomEventByID from db event, eventID: %s, roomID: %s, err:%s", response.EventID, response.RoomID, err.Error())
return err
}

Expand Down
2 changes: 2 additions & 0 deletions rpc/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,11 +866,13 @@ func (r *Client) QueryRoomEventByID(ctx context.Context, req *roomserverapi.Quer
cl := newCallLog("QueryRoomEventByID", req)
conn, err := r.connGetter.GetConn(r.connMgr, &r.cfg.Rpc.RoomServer, 0)
if err != nil {
log.Errorf("QueryRoomEventByID get conn err:%s", err.Error())
return nil, err
}
c := pb.NewRoomServerClient(conn)
resp, err := c.QueryRoomEventByID(ctx, &pb.QueryRoomEventByIDReq{EventID: req.EventID, RoomID: req.RoomID})
if err != nil {
log.Errorf("QueryRoomEventByID query err:%s", err.Error())
return nil, err
}
cl.end(resp)
Expand Down
3 changes: 2 additions & 1 deletion skunkworks/gomatrixserverlib/eventauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const (
// channel archive
MRoomArchive = "m.room.archive"
MRoomUpdate = "m.room.update"
MRoomRawRedact = "m.room.rawredact"
MRoomMessage = "m.room.message"
)

Expand Down Expand Up @@ -349,7 +350,7 @@ func Allowed(event Event, authEvents AuthEventProvider) error {
return memberEventAllowed(event, authEvents)
case MRoomPowerLevels:
return powerLevelsEventAllowed(event, authEvents)
case MRoomRedaction, MRoomUpdate:
case MRoomRedaction, MRoomUpdate, MRoomRawRedact:
return redactEventAllowed(event, authEvents)
default:
if event.Type() == MRoomMessage {
Expand Down
4 changes: 2 additions & 2 deletions syncaggregate/consumers/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ func (s *ProfileConsumer) onMessage(output *types.ProfileStreamUpdate) {
log.Errorf("ProfileConsumer onMessage panic recovered err %#v", e)
}
}()
log.Infof("recv Profile update, user:%s presence:%v", output.UserID, output.Presence.Presence)
log.Infof("recv Profile update, user:%s presence:%v", output.UserID, output.Presence)

if !s.checkUpdate(output) {
return
}
log.Infof("do Profile update, user:%s presence:%v", output.UserID, output.Presence.Presence)
log.Infof("do Profile update, user:%s presence:%v", output.UserID, output.Presence)
event := &gomatrixserverlib.ClientEvent{}
event.Type = "m.presence"
event.Sender = output.UserID
Expand Down
6 changes: 3 additions & 3 deletions syncaggregate/sync/syncMng.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ func (sm *SyncMng) addOneTimeKeyCountInfo(req *request, res *syncapitypes.Respon
func (sm *SyncMng) addPresence(req *request, response *syncapitypes.Response) {
maxPos := int64(-1)
if sm.presenceStreamRepo.ExistsPresence(req.device.UserID, req.marks.preRecv) {
log.Debugf("add presence for %s", req.device.UserID)
log.Infof("add presence for %s", req.device.UserID)
friendShipMap := sm.userTimeLine.GetFriendShip(req.device.UserID, true)
if friendShipMap != nil {
var presenceEvent gomatrixserverlib.ClientEvent
Expand All @@ -969,7 +969,7 @@ func (sm *SyncMng) addPresence(req *request, response *syncapitypes.Response) {

response.Presence.Events = append(response.Presence.Events, presenceEvent)
data, _ := json.Marshal(presenceEvent)
log.Debugf("add presence for %s %d %d %s", req.device.UserID, feed.GetOffset(), req.marks.preRecv, data)
log.Infof( "add presence for %s %d %d %s", req.device.UserID, feed.GetOffset(), req.marks.preRecv, data)

if maxPos < feed.GetOffset() {
maxPos = feed.GetOffset()
Expand All @@ -985,7 +985,7 @@ func (sm *SyncMng) addPresence(req *request, response *syncapitypes.Response) {
}

req.marks.preProcess = maxPos
log.Debugf("process precense user:%s cur:%d", req.device.UserID, req.marks.preProcess)
log.Infof("process precense user:%s cur:%d", req.device.UserID, req.marks.preProcess)
return
}

Expand Down
7 changes: 5 additions & 2 deletions syncserver/consumers/feedserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ func (s *RoomEventFeedConsumer) processRedactEv(ev *gomatrixserverlib.ClientEven
redactEv.Content = empty
redactEv.Hint = fmt.Sprintf("%s撤回了一条消息", extra.GetDisplayName(s.displayNameRepo, ev.Sender))
unsigned.RedactedBecause = ev
} else if ev.Type == "m.room.rawredact" {
redactEv.Hint = "该消息已被撤回"
unsigned.RedactedBecause = ev
} else {
redactEv.Content = ev.Content
unsigned.UpdatedBecause = ev
Expand Down Expand Up @@ -546,7 +549,7 @@ func (s *RoomEventFeedConsumer) onNewRoomEvent(

if common.IsStateClientEv(&ev) == true { //state ev
ev, _ = s.processStateEv(&ev)
} else if ev.Type == "m.room.redaction" || ev.Type == "m.room.update" {
} else if ev.Type == "m.room.redaction" || ev.Type == "m.room.rawredact" || ev.Type == "m.room.update" {
s.processRedactEv(&ev)
}
log.Debugf("feedserver onNewRoomEvent update unsigned roomID:%s eventID:%s sender:%s type:%s eventoffset:%d", ev.RoomID, ev.EventID, ev.Sender, ev.Type, ev.EventOffset)
Expand Down Expand Up @@ -776,7 +779,7 @@ func (s *RoomEventFeedConsumer) onBackFillEvent(
if common.IsStateClientEv(&ev) {
ev, _ = s.processStateEv(&ev)
s.roomStateTimeLine.AddBackfillEv(&ev, ev.EventOffset, true)
} else if ev.Type == "m.room.redaction" || ev.Type == "m.room.update" {
} else if ev.Type == "m.room.redaction" || ev.Type == "m.room.rawredact" || ev.Type == "m.room.update" {
s.processRedactEv(&ev)
}
if ev.Type == "m.room.message" || ev.Type == "m.room.encrypted" {
Expand Down
7 changes: 5 additions & 2 deletions syncwriter/consumers/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ func (s *RoomEventConsumer) processRedactEv(ev *gomatrixserverlib.ClientEvent) {
redactEv.Hint = fmt.Sprintf("%s撤回了一条消息", extra.GetDisplayName(s.displayNameRepo, ev.Sender))
log.Infof("syncwrite, edactEv.Hint: %s", redactEv.Hint)
unsigned.RedactedBecause = ev
} else if ev.Type == "m.room.rawredact" {
redactEv.Hint = "该消息已被撤回"
unsigned.RedactedBecause = ev
} else {
redactEv.Content = ev.Content
unsigned.UpdatedBecause = ev
Expand Down Expand Up @@ -518,7 +521,7 @@ func (s *RoomEventConsumer) onNewRoomEvent(

if common.IsStateClientEv(&ev) == true { //state ev
ev, _ = s.processStateEv(&ev)
} else if ev.Type == "m.room.redaction" || ev.Type == "m.room.update" {
} else if ev.Type == "m.room.redaction" || ev.Type == "m.room.rawredact" || ev.Type == "m.room.update" {
s.processRedactEv(&ev)
}

Expand Down Expand Up @@ -598,7 +601,7 @@ func (s *RoomEventConsumer) onBackFillEvent(

if common.IsStateClientEv(&ev) == true { //state ev
ev, _ = s.processStateEv(&ev)
} else if ev.Type == "m.room.redaction" || ev.Type == "m.room.update" {
} else if ev.Type == "m.room.redaction" || ev.Type == "m.room.rawredact" || ev.Type == "m.room.update" {
s.processRedactEv(&ev)
}

Expand Down

0 comments on commit 3907f98

Please sign in to comment.