Skip to content

Commit

Permalink
Merge branch 'downstream-changes'
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFerr committed Nov 23, 2023
2 parents 8f3f50e + d5c1c13 commit 1c128d4
Show file tree
Hide file tree
Showing 53 changed files with 469 additions and 141 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @vector-im/integrations
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## Element fork

The Element fork includes the following changes:
- User activity tracking
- Add additional metrics to the bridge

Some changes that appear here may get upstreamed to https://github.com/mautrix/signalgo, and will be removed from
the list when they appear in both versions.

Tagged versions will appear as `v{UPSTREAM-VERSION}-mod-{VERSION}`

E.g. The third modification release to 1.0 of the upstream bridge would be `v1.0-mod-3`.

# mautrix-signalgo
![Languages](https://img.shields.io/github/languages/top/mautrix/signalgo.svg)
[![License](https://img.shields.io/github/license/mautrix/signalgo.svg)](LICENSE)
Expand Down
2 changes: 1 addition & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"

"github.com/skip2/go-qrcode"
"go.mau.fi/mautrix-signal/pkg/signalmeow"
"github.com/vector-im/mautrix-signal/pkg/signalmeow"
"maunium.net/go/mautrix/bridge/commands"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
Expand Down
10 changes: 10 additions & 0 deletions config/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ type BridgeConfig struct {

usernameTemplate *template.Template `yaml:"-"`
displaynameTemplate *template.Template `yaml:"-"`

Limits struct {
MaxPuppetLimit uint `yaml:"max_puppet_limit"`
MinPuppetActivityDays uint `yaml:"min_puppet_activity_days"`
PuppetInactivityDays *uint `yaml:"puppet_inactivity_days"`
BlockOnLimitReached bool `yaml:"block_on_limit_reached"`
BlockBeginsNotification string `yaml:"block_begins_notification"`
BlockEndsNotification string `yaml:"block_ends_notification"`
BlockNotificationIntervalSeconds uint `yaml:"block_notification_interval_seconds"`
} `yaml:"limits"`
}

func (bc *BridgeConfig) GetResendBridgeInfo() bool {
Expand Down
9 changes: 9 additions & 0 deletions config/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func DoUpgrade(helper *up.Helper) {
}

helper.Copy(up.Map, "bridge", "permissions")

helper.Copy(up.Int, "bridge", "limits", "max_puppet_limit")
helper.Copy(up.Int, "bridge", "limits", "min_puppet_activity_days")
helper.Copy(up.Int|up.Null, "bridge", "limits", "puppet_inactivity_days")
helper.Copy(up.Bool, "bridge", "limits", "block_on_limit_reached")
helper.Copy(up.Str, "bridge", "limits", "block_begins_notification")
helper.Copy(up.Str, "bridge", "limits", "block_ends_notification")
helper.Copy(up.Int, "bridge", "limits", "block_notification_interval_seconds")
}

var SpacedBlocks = [][]string{
Expand All @@ -92,5 +100,6 @@ var SpacedBlocks = [][]string{
{"bridge", "encryption"},
{"bridge", "provisioning"},
{"bridge", "permissions"},
{"bridge", "limits"},
{"logging"},
}
2 changes: 1 addition & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
_ "github.com/mattn/go-sqlite3"
"maunium.net/go/maulogger/v2"

"go.mau.fi/mautrix-signal/database/upgrades"
"github.com/vector-im/mautrix-signal/database/upgrades"
"go.mau.fi/util/dbutil"
)

Expand Down
48 changes: 47 additions & 1 deletion database/puppet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"database/sql"
"errors"
"fmt"
"time"

"go.mau.fi/util/dbutil"
log "maunium.net/go/maulogger/v2"

"maunium.net/go/mautrix/id"
)

const oneDayMs = 24 * 60 * 60 * 1000

type PuppetQuery struct {
db *Database
log log.Logger
Expand Down Expand Up @@ -43,6 +46,9 @@ type Puppet struct {
NextBatch string
BaseURL string
ContactInfoSet bool

FirstActivityTs int64
LastActivityTs int64
}

func (p *Puppet) values() []interface{} {
Expand All @@ -66,6 +72,7 @@ func (p *Puppet) values() []interface{} {

func (p *Puppet) Scan(row dbutil.Scannable) *Puppet {
var number, name, avatarHash, avatarURL, customMXID, accessToken, nextBatch, baseURL sql.NullString
var firstActivityTs, lastActivityTs sql.NullInt64
err := row.Scan(
&p.SignalID,
&number,
Expand All @@ -81,6 +88,8 @@ func (p *Puppet) Scan(row dbutil.Scannable) *Puppet {
&accessToken,
&nextBatch,
&baseURL,
&firstActivityTs,
&lastActivityTs,
)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
Expand All @@ -107,6 +116,8 @@ func (p *Puppet) Scan(row dbutil.Scannable) *Puppet {
p.AccessToken = accessToken.String
p.NextBatch = nextBatch.String
p.BaseURL = baseURL.String
p.FirstActivityTs = firstActivityTs.Int64
p.LastActivityTs = lastActivityTs.Int64
return p
}

Expand Down Expand Up @@ -168,6 +179,26 @@ func (p *Puppet) UpdateNumber() error {
return nil
}

func (p *Puppet) UpdateActivityTs(activityTs int64) {
if p.LastActivityTs > activityTs {
return
}
p.log.Debugfln("Updating activity time for %s to %d", p.SignalID, activityTs)
p.LastActivityTs = activityTs
_, err := p.db.Exec("UPDATE puppet SET last_activity_ts=$1 WHERE uuid=$2", p.LastActivityTs, p.SignalID)
if err != nil {
p.log.Warnfln("Failed to update last_activity_ts for %s: %v", p.SignalID, err)
}

if p.FirstActivityTs == 0 {
p.FirstActivityTs = activityTs
_, err = p.db.Exec("UPDATE puppet SET first_activity_ts=$1 WHERE uuid=$2 AND first_activity_ts is NULL", p.FirstActivityTs, p.SignalID)
if err != nil {
p.log.Warnfln("Failed to update first_activity_ts %s: %v", p.SignalID, err)
}
}
}

func (p *Puppet) Update() error {
q := `
UPDATE puppet SET
Expand All @@ -190,7 +221,8 @@ func (p *Puppet) Update() error {
const (
selectBase = `
SELECT uuid, number, name, name_quality, avatar_hash, avatar_url, name_set, avatar_set,
contact_info_set, is_registered, custom_mxid, access_token, next_batch, base_url
contact_info_set, is_registered, custom_mxid, access_token, next_batch, base_url,
first_activity_ts, last_activity_ts
FROM puppet
`
)
Expand Down Expand Up @@ -227,3 +259,17 @@ func (pq *PuppetQuery) GetAllWithCustomMXID() ([]*Puppet, error) {
}
return puppets, nil
}

func (pq *PuppetQuery) GetRecentlyActiveCount(minActivityDays uint, maxActivityDays *uint) (count uint, err error) {
q := "SELECT COUNT(*) FROM puppet WHERE (last_activity_ts - first_activity_ts) > $1"
var row *sql.Row
lastActivityTs := oneDayMs * minActivityDays
if maxActivityDays == nil {
row = pq.db.QueryRow(q, lastActivityTs)
} else {
q += " AND ($2 - last_activity_ts) <= $3"
row = pq.db.QueryRow(q, lastActivityTs, time.Now().UnixMilli(), oneDayMs*(*maxActivityDays))
}
err = row.Scan(&count)
return
}
7 changes: 5 additions & 2 deletions database/upgrades/00-initial.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- v0 -> v14: Latest revision
-- v0 -> v15: Latest revision

CREATE TABLE portal (
chat_id TEXT,
Expand Down Expand Up @@ -34,7 +34,10 @@ CREATE TABLE puppet (
access_token TEXT,
next_batch TEXT,
base_url TEXT,
contact_info_set BOOLEAN NOT NULL DEFAULT false
contact_info_set BOOLEAN NOT NULL DEFAULT false,

first_activity_ts BIGINT,
last_activity_ts BIGINT
);

CREATE TABLE "user" (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- v13: Switch mx_room_state from Python to Go format
-- v14: Switch mx_room_state from Python to Go format
ALTER TABLE mx_room_state DROP COLUMN is_encrypted;
ALTER TABLE mx_room_state DROP COLUMN has_full_member_list;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-- v14: Remove redundant notice_room column from users
-- v15: Remove redundant notice_room column from users
UPDATE "user" SET management_room = COALESCE(management_room, notice_room);
ALTER TABLE "user" DROP COLUMN notice_room;
2 changes: 1 addition & 1 deletion disappearing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/rs/zerolog"
"go.mau.fi/mautrix-signal/database"
"github.com/vector-im/mautrix-signal/database"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
)
Expand Down
16 changes: 16 additions & 0 deletions example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ bridge:
"example.com": user
"@admin:example.com": admin

# Limit usage of the bridge
limits:
# The maximum number of bridge puppets that can be "active" before the limit is reached
max_puppet_limit: 0
# The minimum amount of days a puppet must be active for before they are considered "active".
min_puppet_activity_days: 0
# The number of days after a puppets last activity where they are considered inactive again.
puppet_inactivity_days: 30
# Should the bridge block traffic when a limit has been reached
block_on_limit_reached: true
# The message sent to bridge admins when the bridges starts/stops being blocked
block_begins_notification: 'The bridge is currently blocking messages. You may need to increase your usage limits in EMS.'
block_ends_notification: 'The bridge is no longer blocking messages.'
# The minimum interval between blocking notification being sent
block_notification_interval_seconds: 3600

# Logging config. See https://github.com/tulir/zeroconfig for details.
logging:
min_level: debug
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module go.mau.fi/mautrix-signal
module github.com/vector-im/mautrix-signal

go 1.20

Expand Down
Loading

0 comments on commit 1c128d4

Please sign in to comment.