-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
policyeval: actually evalute policies
- Loading branch information
Showing
16 changed files
with
498 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,76 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.mau.fi/util/dbutil" | ||
"maunium.net/go/mautrix/event" | ||
"maunium.net/go/mautrix/id" | ||
) | ||
|
||
const ( | ||
getTakenActionBaseQuery = ` | ||
SELECT target_user, in_room_id, action_type, policy_list, rule_entity, action, taken_at | ||
FROM taken_action | ||
` | ||
getTakenActionsByPolicyListQuery = getTakenActionBaseQuery + `WHERE policy_list=$1` | ||
getTakenActionsByRuleEntityQuery = getTakenActionBaseQuery + `WHERE policy_list=$1 AND rule_entity=$2` | ||
getTakenActionByTargetUserQuery = getTakenActionBaseQuery + `WHERE target_user=$1 AND action_type=$2` | ||
insertTakenActionQuery = ` | ||
INSERT INTO taken_action (target_user, in_room_id, action_type, policy_list, rule_entity, action, taken_at) | ||
VALUES ($1, $2, $3, $4, $5, $6, $7) | ||
ON CONFLICT (target_user, in_room_id, action_type) DO UPDATE | ||
SET policy_list=excluded.policy_list, rule_entity=excluded.rule_entity, action=excluded.action, taken_at=excluded.taken_at | ||
` | ||
) | ||
|
||
type TakenActionQuery struct { | ||
*dbutil.QueryHelper[*TakenAction] | ||
} | ||
|
||
func (taq *TakenActionQuery) Put(ctx context.Context, ta *TakenAction) error { | ||
return taq.Exec(ctx, insertTakenActionQuery, ta.sqlVariables()...) | ||
} | ||
|
||
func (taq *TakenActionQuery) GetAllByPolicyList(ctx context.Context, policyList id.RoomID) ([]*TakenAction, error) { | ||
return taq.QueryMany(ctx, getTakenActionsByPolicyListQuery, policyList) | ||
} | ||
|
||
func (taq *TakenActionQuery) GetAllByRuleEntity(ctx context.Context, policyList id.RoomID, ruleEntity string) ([]*TakenAction, error) { | ||
return taq.QueryMany(ctx, getTakenActionsByRuleEntityQuery, policyList, ruleEntity) | ||
} | ||
|
||
func (taq *TakenActionQuery) GetAllByTargetUser(ctx context.Context, userID id.UserID, actionType TakenActionType) ([]*TakenAction, error) { | ||
return taq.QueryMany(ctx, getTakenActionByTargetUserQuery, userID, actionType) | ||
} | ||
|
||
type TakenActionType string | ||
|
||
const ( | ||
TakenActionTypeBanOrUnban TakenActionType = "ban_or_unban" | ||
) | ||
|
||
type TakenAction struct { | ||
TargetUser id.UserID | ||
InRoomID id.RoomID | ||
ActionType TakenActionType | ||
PolicyList id.RoomID | ||
RuleEntity string | ||
TargetUser id.UserID | ||
Action event.PolicyRecommendation | ||
TakenAt time.Time | ||
} | ||
|
||
func (t *TakenAction) sqlVariables() []any { | ||
return []any{t.TargetUser, t.InRoomID, t.ActionType, t.PolicyList, t.RuleEntity, t.Action, t.TakenAt.UnixMilli()} | ||
} | ||
|
||
func (t *TakenAction) Scan(row dbutil.Scannable) (*TakenAction, error) { | ||
var takenAt int64 | ||
err := row.Scan(&t.TargetUser, &t.InRoomID, &t.ActionType, &t.PolicyList, &t.RuleEntity, &t.Action, &takenAt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
t.TakenAt = time.UnixMilli(takenAt) | ||
return t, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
-- v0 -> v1 (compatible with v1+): Latest schema | ||
CREATE TABLE taken_action ( | ||
policy_list TEXT NOT NULL, | ||
rule_entity TEXT NOT NULL, | ||
target_user TEXT NOT NULL, | ||
action TEXT NOT NULL, | ||
taken_at BIGINT NOT NULL | ||
target_user TEXT NOT NULL, | ||
in_room_id TEXT NOT NULL, | ||
action_type TEXT NOT NULL, | ||
policy_list TEXT NOT NULL, | ||
rule_entity TEXT NOT NULL, | ||
action TEXT NOT NULL, | ||
taken_at BIGINT NOT NULL, | ||
|
||
PRIMARY KEY (target_user, in_room_id, action_type) | ||
); | ||
|
||
CREATE INDEX taken_action_list_idx ON taken_action (policy_list); | ||
CREATE INDEX taken_action_entity_idx ON taken_action (policy_list, rule_entity); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.