Skip to content

Commit

Permalink
Ios push (#143)
Browse files Browse the repository at this point in the history
* log: add log

* fix: set global default

* log: delete log
  • Loading branch information
naiveperson authored Jun 22, 2021
1 parent 3e1a85d commit a99593a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 39 deletions.
83 changes: 44 additions & 39 deletions pushapi/routing/push_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ package routing
import (
"context"
"fmt"
"github.com/finogeeks/ligase/model/repos"
"net/http"
"sort"
"strings"

"github.com/finogeeks/ligase/model/repos"

"github.com/finogeeks/ligase/clientapi/httputil"
"github.com/finogeeks/ligase/common"
"github.com/finogeeks/ligase/common/config"
Expand Down Expand Up @@ -190,16 +191,20 @@ func FormatRuleResponse(rules pushapitypes.Rules) pushapitypes.RuleSet {
func GetUserPushRules(userID string, pushDataRepo *repos.PushDataRepo, forRequest bool, static *pushapitypes.StaticObj) (global pushapitypes.Rules) {
ctx := context.TODO()
global = pushapitypes.Rules{
Default: true,
ContentDefault: true,
OverrideDefault: true,
RoomDefault: true,
SenderDefault: true,
Default: true,
ContentDefault: true,
OverrideDefault: true,
RoomDefault: true,
SenderDefault: true,
UnderRideDefault: true,
}
bases := make(map[string]pushapitypes.PushRuleData)
var rules pushapitypes.PushRuleDataArray
rulesData, _ := pushDataRepo.GetPushRule(ctx, userID)
if len(rulesData) > 0 {
global.Default = false
}

for _, rule := range rulesData {
if rule.PriorityClass == -1 {
bases[rule.RuleId] = rule
Expand Down Expand Up @@ -406,24 +411,24 @@ func PutPushRuleActions(
return http.StatusBadRequest, jsonerror.BadJSON("Actions is malformed JSON")
}
pushRuleData := pushapitypes.PushRuleData{
UserName: device.UserID,
RuleId: insertRuleID,
UserName: device.UserID,
RuleId: insertRuleID,
PriorityClass: -1,
Priority: 1,
Conditions: conditionsJSON,
Actions: actionsJSON,
Priority: 1,
Conditions: conditionsJSON,
Actions: actionsJSON,
}
if err := pushDataRepo.AddPushRule(ctx, pushRuleData, true); err != nil {
return httputil.LogThenErrorCtx(ctx, err)
}
} else {
pushRuleData := pushapitypes.PushRuleData{
UserName: pushRule.UserName,
RuleId: pushRule.RuleId,
UserName: pushRule.UserName,
RuleId: pushRule.RuleId,
PriorityClass: pushRule.PriorityClass,
Priority: pushRule.Priority,
Conditions: pushRule.Conditions,
Actions: actionsJSON,
Priority: pushRule.Priority,
Conditions: pushRule.Conditions,
Actions: actionsJSON,
}
if err := pushDataRepo.AddPushRule(ctx, pushRuleData, true); err != nil {
return httputil.LogThenErrorCtx(ctx, err)
Expand Down Expand Up @@ -514,10 +519,10 @@ func PutPushRuleEnabled(
}

if err := pushDataRepo.AddPushRuleEnable(ctx, pushapitypes.PushRuleEnable{
UserID: device.UserID,
RuleID: insertedRuleID,
UserID: device.UserID,
RuleID: insertedRuleID,
Enabled: enableValue,
},true); err != nil {
}, true); err != nil {
return httputil.LogThenErrorCtx(ctx, err)
}
sendPushRuleUpdate(cfg, device.UserID)
Expand Down Expand Up @@ -805,7 +810,7 @@ func PutPushRule(
}

if before != "" || after != "" {
return addPushRuleWithRelated(ctx, cfg, insertRuleID, beforeID, afterID, priorityClass, conditionsJSON, actionsJSON,device.UserID, pushDataRepo)
return addPushRuleWithRelated(ctx, cfg, insertRuleID, beforeID, afterID, priorityClass, conditionsJSON, actionsJSON, device.UserID, pushDataRepo)
} else {
return addPushRuleWithoutRelated(ctx, cfg, insertRuleID, priorityClass, conditionsJSON, actionsJSON, device.UserID, pushDataRepo)
}
Expand Down Expand Up @@ -834,13 +839,13 @@ func addPushRuleWithoutRelated(
priority = priority + 1
}
if err := pushDataRepo.AddPushRule(ctx, pushapitypes.PushRuleData{
UserName: userID,
RuleId: ruleID,
UserName: userID,
RuleId: ruleID,
PriorityClass: priorityClass,
Priority: priority,
Conditions: conditions,
Actions: actions,
},true); err != nil {
Priority: priority,
Conditions: conditions,
Actions: actions,
}, true); err != nil {
return httputil.LogThenErrorCtx(ctx, err)
}
sendPushRuleUpdate(cfg, userID)
Expand Down Expand Up @@ -887,12 +892,12 @@ func addPushRuleWithRelated(
if rule.PriorityClass == priorityClass {
if rule.Priority >= priority {
ruleData := pushapitypes.PushRuleData{
UserName: rule.UserName,
RuleId: rule.RuleId,
UserName: rule.UserName,
RuleId: rule.RuleId,
PriorityClass: rule.PriorityClass,
Priority: rule.Priority + 1,
Conditions: rule.Conditions,
Actions: rule.Actions,
Priority: rule.Priority + 1,
Conditions: rule.Conditions,
Actions: rule.Actions,
}
if err := pushDataRepo.AddPushRule(ctx, ruleData, true); err != nil {
return httputil.LogThenErrorCtx(ctx, err)
Expand All @@ -902,20 +907,20 @@ func addPushRuleWithRelated(
}

if err := pushDataRepo.AddPushRule(ctx, pushapitypes.PushRuleData{
UserName: userID,
RuleId: ruleID,
UserName: userID,
RuleId: ruleID,
PriorityClass: priorityClass,
Priority: priority,
Conditions: conditions,
Actions: actions,
},true); err != nil {
Priority: priority,
Conditions: conditions,
Actions: actions,
}, true); err != nil {
return httputil.LogThenErrorCtx(ctx, err)
}
sendPushRuleUpdate(cfg, userID)
return http.StatusOK, nil
}

func sendPushRuleUpdate(cfg config.Dendrite, userID string){
func sendPushRuleUpdate(cfg config.Dendrite, userID string) {
data := new(types.ActDataStreamUpdate)
data.UserID = userID
data.RoomID = ""
Expand All @@ -928,4 +933,4 @@ func sendPushRuleUpdate(cfg config.Dendrite, userID string){
Keys: []byte(userID),
Obj: data,
})
}
}
26 changes: 26 additions & 0 deletions syncserver/consumers/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,25 @@ func (s *PushConsumer) processMessageEvent(
pushContents *push.PushPubContents,
static *push.StaticObj,
) {
var rule string
if global.Default {
highlight := s.isSignalRule(userID, helperEvent)
if highlight {
s.countRepo.UpdateRoomReadCount(helperEvent.RoomID, helperEvent.EventID, *userID, "increase_hl")
rule = ".m.rule.signals"
} else if memCount == 2 {
rule = ".m.rule.room_one_to_one"
} else if helperEvent.Type == "m.room.message" {
rule = ".m.rule.message"
} else if helperEvent.Type == "m.room.encrypted" {
rule = ".m.rule.encrypted"
} else {
rule = ".m.rule.video"
}

log.Debugw("PushConsumer.processMessageEvent, global is default",
log.KeysAndValues{"roomID", helperEvent.RoomID, "eventID", helperEvent.EventID, "userID", *userID, "rule", rule})

action := s.getDefaultAction(highlight)
s.updateReadCountAndNotify(pushers, helperEvent, userID, pushContents, action, true, true, static)
return
Expand Down Expand Up @@ -646,6 +659,19 @@ func (s *PushConsumer) processMessageEvent(
}

if global.UnderRideDefault {
if memCount == 2 {
rule = ".m.rule.room_one_to_one"
} else if helperEvent.Type == "m.room.message" {
rule = ".m.rule.message"
} else if helperEvent.Type == "m.room.encrypted" {
rule = ".m.rule.encrypted"
} else {
rule = ".m.rule.video"
}

log.Debugw("PushConsumer.processMessageEvent, global is default",
log.KeysAndValues{"roomID", helperEvent.RoomID, "eventID", helperEvent.EventID, "userID", *userID, "rule", rule})

action := s.getDefaultAction(false)
s.updateReadCountAndNotify(pushers, helperEvent, userID, pushContents, action, true, true, static)
return
Expand Down

0 comments on commit a99593a

Please sign in to comment.