Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/spaces'
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 29, 2024
2 parents 689333b + b44d5c1 commit c565641
Show file tree
Hide file tree
Showing 13 changed files with 580 additions and 110 deletions.
3 changes: 2 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ var cmdDeletePortal = &commands.FullHandler{

func fnDeletePortal(ce *WrappedCommandEvent) {
ce.Portal.delete()
ce.Portal.cleanup(false)

ce.Bridge.cleanupRoom(ce.Portal.MainIntent(), ce.Portal.MXID, false, ce.Log)
ce.Log.Infofln("Deleted portal")
}
16 changes: 9 additions & 7 deletions database/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type Portal struct {
FirstEventID id.EventID
NextBatchID id.BatchID
FirstSlackID string

InSpace bool
}

func (p *Portal) Scan(row dbutil.Scannable) *Portal {
Expand All @@ -78,7 +80,7 @@ func (p *Portal) Scan(row dbutil.Scannable) *Portal {
err := row.Scan(&p.Key.TeamID, &p.Key.ChannelID, &mxid,
&p.Type, &dmUserID, &p.PlainName, &p.Name, &p.NameSet, &p.Topic,
&p.TopicSet, &p.Avatar, &avatarURL, &p.AvatarSet, &firstEventID,
&p.Encrypted, &nextBatchID, &firstSlackID)
&p.Encrypted, &nextBatchID, &firstSlackID, &p.InSpace)

if err != nil {
if err != sql.ErrNoRows {
Expand Down Expand Up @@ -110,13 +112,13 @@ func (p *Portal) Insert() {
query := "INSERT INTO portal" +
" (team_id, channel_id, mxid, type, dm_user_id, plain_name," +
" name, name_set, topic, topic_set, avatar, avatar_url, avatar_set," +
" first_event_id, encrypted, next_batch_id, first_slack_id)" +
" VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)"
" first_event_id, encrypted, next_batch_id, first_slack_id, in_space)" +
" VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)"

_, err := p.db.Exec(query, p.Key.TeamID, p.Key.ChannelID,
p.mxidPtr(), p.Type, p.DMUserID, p.PlainName, p.Name, p.NameSet,
p.Topic, p.TopicSet, p.Avatar, p.AvatarURL.String(), p.AvatarSet,
p.FirstEventID.String(), p.Encrypted, p.NextBatchID.String(), p.FirstSlackID)
p.FirstEventID.String(), p.Encrypted, p.NextBatchID.String(), p.FirstSlackID, p.InSpace)

if err != nil {
p.log.Warnfln("Failed to insert %s: %v", p.Key, err)
Expand All @@ -127,13 +129,13 @@ func (p *Portal) Update(txn dbutil.Transaction) {
query := "UPDATE portal SET" +
" mxid=$1, type=$2, dm_user_id=$3, plain_name=$4, name=$5, name_set=$6," +
" topic=$7, topic_set=$8, avatar=$9, avatar_url=$10, avatar_set=$11," +
" first_event_id=$12, encrypted=$13, next_batch_id=$14, first_slack_id=$15" +
" WHERE team_id=$16 AND channel_id=$17"
" first_event_id=$12, encrypted=$13, next_batch_id=$14, first_slack_id=$15, in_space=$16" +
" WHERE team_id=$17 AND channel_id=$18"

args := []interface{}{p.mxidPtr(), p.Type, p.DMUserID, p.PlainName,
p.Name, p.NameSet, p.Topic, p.TopicSet, p.Avatar, p.AvatarURL.String(),
p.AvatarSet, p.FirstEventID.String(), p.Encrypted, p.NextBatchID.String(), p.FirstSlackID,
p.Key.TeamID, p.Key.ChannelID}
p.InSpace, p.Key.TeamID, p.Key.ChannelID}

var err error
if txn != nil {
Expand Down
2 changes: 1 addition & 1 deletion database/portalquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
portalSelect = "SELECT team_id, channel_id, mxid, type, " +
" dm_user_id, plain_name, name, name_set, topic, topic_set," +
" avatar, avatar_url, avatar_set, first_event_id," +
" encrypted, next_batch_id, first_slack_id FROM portal"
" encrypted, next_batch_id, first_slack_id, in_space FROM portal"
)

type PortalQuery struct {
Expand Down
38 changes: 32 additions & 6 deletions database/teaminfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (tiq *TeamInfoQuery) New() *TeamInfo {
}

func (tiq *TeamInfoQuery) GetBySlackTeam(team string) *TeamInfo {
query := `SELECT team_id, team_domain, team_url, team_name, avatar, avatar_url FROM team_info WHERE team_id=$1`
query := `SELECT team_id, team_domain, team_url, team_name, avatar, avatar_url, space_room, name_set, avatar_set FROM team_info WHERE team_id=$1`

row := tiq.db.QueryRow(query, team)
if row == nil {
Expand All @@ -48,6 +48,17 @@ func (tiq *TeamInfoQuery) GetBySlackTeam(team string) *TeamInfo {
return tiq.New().Scan(row)
}

func (tiq *TeamInfoQuery) GetByMXID(mxid id.RoomID) *TeamInfo {
query := `SELECT team_id, team_domain, team_url, team_name, avatar, avatar_url, space_room, name_set, avatar_set FROM team_info WHERE space_room=$1`

row := tiq.db.QueryRow(query, mxid)
if row == nil {
return nil
}

return tiq.New().Scan(row)
}

type TeamInfo struct {
db *Database
log log.Logger
Expand All @@ -58,6 +69,9 @@ type TeamInfo struct {
TeamName string
Avatar string
AvatarUrl id.ContentURI
SpaceRoom id.RoomID
NameSet bool
AvatarSet bool
}

func (ti *TeamInfo) Scan(row dbutil.Scannable) *TeamInfo {
Expand All @@ -66,8 +80,9 @@ func (ti *TeamInfo) Scan(row dbutil.Scannable) *TeamInfo {
var teamName sql.NullString
var avatar sql.NullString
var avatarUrl sql.NullString
var spaceRoom sql.NullString

err := row.Scan(&ti.TeamID, &teamDomain, &teamUrl, &teamName, &avatar, &avatarUrl)
err := row.Scan(&ti.TeamID, &teamDomain, &teamUrl, &teamName, &avatar, &avatarUrl, &spaceRoom, &ti.NameSet, &ti.AvatarSet)
if err != nil {
if err != sql.ErrNoRows {
ti.log.Errorln("Database scan failed:", err)
Expand All @@ -91,25 +106,36 @@ func (ti *TeamInfo) Scan(row dbutil.Scannable) *TeamInfo {
if avatarUrl.Valid {
ti.AvatarUrl, _ = id.ParseContentURI(avatarUrl.String)
}
if spaceRoom.Valid {
ti.SpaceRoom = id.RoomID(spaceRoom.String)
}

return ti
}

func (ti *TeamInfo) Upsert() {
query := `
INSERT INTO team_info (team_id, team_domain, team_url, team_name, avatar, avatar_url)
VALUES ($1, $2, $3, $4, $5, $6)
INSERT INTO team_info (team_id, team_domain, team_url, team_name, avatar, avatar_url, space_room, name_set, avatar_set)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (team_id) DO UPDATE
SET team_domain=excluded.team_domain, team_url=excluded.team_url, team_name=excluded.team_name, avatar=excluded.avatar, avatar_url=excluded.avatar_url
SET team_domain=excluded.team_domain,
team_url=excluded.team_url,
team_name=excluded.team_name,
avatar=excluded.avatar,
avatar_url=excluded.avatar_url,
space_room=excluded.space_room,
name_set=excluded.name_set,
avatar_set=excluded.avatar_set
`

teamDomain := sqlNullString(ti.TeamDomain)
teamUrl := sqlNullString(ti.TeamUrl)
teamName := sqlNullString(ti.TeamName)
avatar := sqlNullString(ti.Avatar)
avatarUrl := sqlNullString(ti.AvatarUrl.String())
spaceRoom := sqlNullString(ti.SpaceRoom.String())

_, err := ti.db.Exec(query, ti.TeamID, teamDomain, teamUrl, teamName, avatar, avatarUrl)
_, err := ti.db.Exec(query, ti.TeamID, teamDomain, teamUrl, teamName, avatar, avatarUrl, spaceRoom, ti.NameSet, ti.AvatarSet)

if err != nil {
ti.log.Warnfln("Failed to upsert team %s: %v", ti.TeamID, err)
Expand Down
52 changes: 30 additions & 22 deletions database/upgrades/00-latest-revision.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- v0 -> v15: Latest revision
-- v0 -> v16: Latest revision

CREATE TABLE portal (
team_id TEXT,
Expand All @@ -23,6 +23,8 @@ CREATE TABLE portal (
next_batch_id TEXT,
first_slack_id TEXT,

in_space BOOLEAN DEFAULT false,

PRIMARY KEY (team_id, channel_id)
);

Expand Down Expand Up @@ -54,28 +56,31 @@ CREATE TABLE puppet (
CREATE TABLE "user" (
mxid TEXT PRIMARY KEY,

management_room TEXT
management_room TEXT,
space_room TEXT
);

CREATE TABLE "user_team" (
mxid TEXT NOT NULL,

slack_email TEXT NOT NULL,
slack_id TEXT NOT NULL,
slack_id TEXT NOT NULL,

team_name TEXT NOT NULL,
team_id TEXT NOT NULL,
team_id TEXT NOT NULL,

token TEXT,
token TEXT,
cookie_token TEXT,

in_space BOOLEAN DEFAULT false,

PRIMARY KEY(mxid, slack_id, team_id)
);

CREATE TABLE user_team_portal (
matrix_user_id TEXT NOT NULL,
slack_user_id TEXT NOT NULL,
slack_team_id TEXT NOT NULL,
matrix_user_id TEXT NOT NULL,
slack_user_id TEXT NOT NULL,
slack_team_id TEXT NOT NULL,
portal_channel_id TEXT NOT NULL,
FOREIGN KEY(matrix_user_id, slack_user_id, slack_team_id) REFERENCES "user_team"(mxid, slack_id, team_id) ON DELETE CASCADE,
FOREIGN KEY(slack_team_id, portal_channel_id) REFERENCES portal(team_id, channel_id) ON DELETE CASCADE
Expand All @@ -85,9 +90,9 @@ CREATE TABLE message (
team_id TEXT NOT NULL,
channel_id TEXT NOT NULL,

slack_message_id TEXT NOT NULL,
slack_thread_id TEXT,
matrix_message_id TEXT NOT NULL UNIQUE,
slack_message_id TEXT NOT NULL,
slack_thread_id TEXT,
matrix_message_id TEXT NOT NULL UNIQUE,

author_id TEXT NOT NULL,

Expand Down Expand Up @@ -118,30 +123,33 @@ CREATE TABLE attachment (
channel_id TEXT NOT NULL,

slack_message_id TEXT NOT NULL,
slack_file_id TEXT NOT NULL,
matrix_event_id TEXT NOT NULL UNIQUE,
slack_thread_id TEXT,
slack_file_id TEXT NOT NULL,
matrix_event_id TEXT NOT NULL UNIQUE,
slack_thread_id TEXT,

PRIMARY KEY(slack_message_id, slack_file_id, matrix_event_id),
FOREIGN KEY(team_id, channel_id) REFERENCES portal(team_id, channel_id) ON DELETE CASCADE
);

CREATE TABLE "team_info" (
team_id TEXT NOT NULL UNIQUE,
team_id TEXT NOT NULL UNIQUE,
team_domain TEXT,
team_url TEXT,
team_name TEXT,
avatar TEXT,
avatar_url TEXT
team_url TEXT,
team_name TEXT,
avatar TEXT,
avatar_url TEXT,
space_room TEXT,
name_set BOOLEAN DEFAULT false,
avatar_set BOOLEAN DEFAULT false
);

CREATE TABLE backfill_state (
team_id TEXT,
channel_id TEXT,
backfill_complete BOOLEAN,
dispatched BOOLEAN,
message_count INTEGER,
immediate_complete BOOLEAN,
dispatched BOOLEAN,
message_count INTEGER,
immediate_complete BOOLEAN,
PRIMARY KEY (team_id, channel_id),
FOREIGN KEY (team_id, channel_id) REFERENCES portal (team_id, channel_id) ON DELETE CASCADE
);
Expand Down
8 changes: 8 additions & 0 deletions database/upgrades/16-add-spaces.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- v16: Add spaces

ALTER TABLE "user" ADD space_room TEXT;
ALTER TABLE team_info ADD space_room TEXT;
ALTER TABLE team_info ADD name_set BOOLEAN DEFAULT false;
ALTER TABLE team_info ADD avatar_set BOOLEAN DEFAULT false;
ALTER TABLE portal ADD in_space BOOLEAN DEFAULT false;
ALTER TABLE user_team ADD in_space BOOLEAN DEFAULT false;
15 changes: 10 additions & 5 deletions database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type User struct {

MXID id.UserID
ManagementRoom id.RoomID
SpaceRoom id.RoomID

TeamsLock sync.Mutex
Teams map[string]*UserTeam
Expand All @@ -47,7 +48,9 @@ func (user *User) loadTeams() {
}

func (u *User) Scan(row dbutil.Scannable) *User {
err := row.Scan(&u.MXID, &u.ManagementRoom)
var spaceRoom sql.NullString

err := row.Scan(&u.MXID, &u.ManagementRoom, &spaceRoom)
if err != nil {
if err != sql.ErrNoRows {
u.log.Errorln("Database scan failed:", err)
Expand All @@ -56,6 +59,8 @@ func (u *User) Scan(row dbutil.Scannable) *User {
return nil
}

u.SpaceRoom = id.RoomID(spaceRoom.String)

u.loadTeams()

return u
Expand All @@ -79,9 +84,9 @@ func (u *User) SyncTeams() {
}

func (u *User) Insert() {
query := "INSERT INTO \"user\" (mxid, management_room) VALUES ($1, $2);"
query := "INSERT INTO \"user\" (mxid, management_room, space_room) VALUES ($1, $2, $3);"

_, err := u.db.Exec(query, u.MXID, u.ManagementRoom)
_, err := u.db.Exec(query, u.MXID, u.ManagementRoom, u.SpaceRoom)

if err != nil {
u.log.Warnfln("Failed to insert %s: %v", u.MXID, err)
Expand All @@ -91,9 +96,9 @@ func (u *User) Insert() {
}

func (u *User) Update() {
query := "UPDATE \"user\" SET management_room=$1 WHERE mxid=$2;"
query := "UPDATE \"user\" SET management_room=$1 AND space_room=$2 WHERE mxid=$3;"

_, err := u.db.Exec(query, u.ManagementRoom, u.MXID)
_, err := u.db.Exec(query, u.ManagementRoom, u.SpaceRoom, u.MXID)

if err != nil {
u.log.Warnfln("Failed to update %q: %v", u.MXID, err)
Expand Down
6 changes: 3 additions & 3 deletions database/userquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (uq *UserQuery) New() *User {
}

func (uq *UserQuery) GetByMXID(userID id.UserID) *User {
query := `SELECT mxid, management_room FROM "user" WHERE mxid=$1`
query := `SELECT mxid, management_room, space_room FROM "user" WHERE mxid=$1`
row := uq.db.QueryRow(query, userID)
if row == nil {
return nil
Expand All @@ -45,7 +45,7 @@ func (uq *UserQuery) GetByMXID(userID id.UserID) *User {
}

func (uq *UserQuery) GetBySlackID(teamID, userID string) *User {
query := `SELECT u.mxid, u.management_room FROM "user" u` +
query := `SELECT u.mxid, u.management_room, u.space_room FROM "user" u` +
` INNER JOIN user_team ut ON u.mxid = ut.mxid` +
` WHERE ut.team_id=$1 AND ut.slack_id=$2`
row := uq.db.QueryRow(query, teamID, userID)
Expand All @@ -57,7 +57,7 @@ func (uq *UserQuery) GetBySlackID(teamID, userID string) *User {
}

func (uq *UserQuery) GetAll() []*User {
rows, err := uq.db.Query(`SELECT mxid, management_room FROM "user"`)
rows, err := uq.db.Query(`SELECT mxid, management_room, space_room FROM "user"`)
if err != nil || rows == nil {
return nil
}
Expand Down
Loading

0 comments on commit c565641

Please sign in to comment.