Make Network.Nick optional

Make Network.Nick optional, default to the user's username. This
will allow adding a global setting to set the nickname in the
future, just like we have for the real name.

References: https://todo.sr.ht/~emersion/soju/110
This commit is contained in:
Simon Ser 2021-11-02 23:33:17 +01:00
parent ce3d6df6b8
commit a6aa7f0008
6 changed files with 60 additions and 14 deletions

9
db.go
View File

@ -112,6 +112,13 @@ func (net *Network) GetUsername() string {
return net.Nick return net.Nick
} }
func GetNick(user *User, net *Network) string {
if net.Nick != "" {
return net.Nick
}
return user.Username
}
func GetRealname(user *User, net *Network) string { func GetRealname(user *User, net *Network) string {
if net.Realname != "" { if net.Realname != "" {
return net.Realname return net.Realname
@ -119,7 +126,7 @@ func GetRealname(user *User, net *Network) string {
if user.Realname != "" { if user.Realname != "" {
return user.Realname return user.Realname
} }
return net.Nick return GetNick(user, net)
} }
type MessageFilter int type MessageFilter int

View File

@ -36,7 +36,7 @@ CREATE TABLE "Network" (
name VARCHAR(255), name VARCHAR(255),
"user" INTEGER NOT NULL REFERENCES "User"(id) ON DELETE CASCADE, "user" INTEGER NOT NULL REFERENCES "User"(id) ON DELETE CASCADE,
addr VARCHAR(255) NOT NULL, addr VARCHAR(255) NOT NULL,
nick VARCHAR(255) NOT NULL, nick VARCHAR(255),
username VARCHAR(255), username VARCHAR(255),
realname VARCHAR(255), realname VARCHAR(255),
pass VARCHAR(255), pass VARCHAR(255),
@ -77,6 +77,7 @@ CREATE TABLE "DeliveryReceipt" (
var postgresMigrations = []string{ var postgresMigrations = []string{
"", // migration #0 is reserved for schema initialization "", // migration #0 is reserved for schema initialization
`ALTER TABLE "Network" ALTER COLUMN nick DROP NOT NULL`,
} }
type PostgresDB struct { type PostgresDB struct {
@ -259,15 +260,16 @@ func (db *PostgresDB) ListNetworks(ctx context.Context, userID int64) ([]Network
var networks []Network var networks []Network
for rows.Next() { for rows.Next() {
var net Network var net Network
var name, username, realname, pass, connectCommands sql.NullString var name, nick, username, realname, pass, connectCommands sql.NullString
var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString
err := rows.Scan(&net.ID, &name, &net.Addr, &net.Nick, &username, &realname, err := rows.Scan(&net.ID, &name, &net.Addr, &nick, &username, &realname,
&pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword, &pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword,
&net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob, &net.Enabled) &net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob, &net.Enabled)
if err != nil { if err != nil {
return nil, err return nil, err
} }
net.Name = name.String net.Name = name.String
net.Nick = nick.String
net.Username = username.String net.Username = username.String
net.Realname = realname.String net.Realname = realname.String
net.Pass = pass.String net.Pass = pass.String
@ -291,6 +293,7 @@ func (db *PostgresDB) StoreNetwork(ctx context.Context, userID int64, network *N
defer cancel() defer cancel()
netName := toNullString(network.Name) netName := toNullString(network.Name)
nick := toNullString(network.Nick)
netUsername := toNullString(network.Username) netUsername := toNullString(network.Username)
realname := toNullString(network.Realname) realname := toNullString(network.Realname)
pass := toNullString(network.Pass) pass := toNullString(network.Pass)
@ -320,7 +323,7 @@ func (db *PostgresDB) StoreNetwork(ctx context.Context, userID int64, network *N
sasl_external_key, enabled) sasl_external_key, enabled)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
RETURNING id`, RETURNING id`,
userID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands, userID, netName, network.Addr, nick, netUsername, realname, pass, connectCommands,
saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob, saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob,
network.SASL.External.PrivKeyBlob, network.Enabled).Scan(&network.ID) network.SASL.External.PrivKeyBlob, network.Enabled).Scan(&network.ID)
} else { } else {
@ -331,7 +334,7 @@ func (db *PostgresDB) StoreNetwork(ctx context.Context, userID int64, network *N
sasl_plain_password = $11, sasl_external_cert = $12, sasl_external_key = $13, sasl_plain_password = $11, sasl_external_cert = $12, sasl_external_key = $13,
enabled = $14 enabled = $14
WHERE id = $1`, WHERE id = $1`,
network.ID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands, network.ID, netName, network.Addr, nick, netUsername, realname, pass, connectCommands,
saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob, saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob,
network.SASL.External.PrivKeyBlob, network.Enabled) network.SASL.External.PrivKeyBlob, network.Enabled)
} }

View File

@ -28,7 +28,7 @@ CREATE TABLE Network (
name TEXT, name TEXT,
user INTEGER NOT NULL, user INTEGER NOT NULL,
addr TEXT NOT NULL, addr TEXT NOT NULL,
nick TEXT NOT NULL, nick TEXT,
username TEXT, username TEXT,
realname TEXT, realname TEXT,
pass TEXT, pass TEXT,
@ -138,6 +138,36 @@ var sqliteMigrations = []string{
"ALTER TABLE Channel ADD COLUMN detached_internal_msgid VARCHAR(255)", "ALTER TABLE Channel ADD COLUMN detached_internal_msgid VARCHAR(255)",
"ALTER TABLE Network ADD COLUMN enabled INTEGER NOT NULL DEFAULT 1", "ALTER TABLE Network ADD COLUMN enabled INTEGER NOT NULL DEFAULT 1",
"ALTER TABLE User ADD COLUMN realname VARCHAR(255)", "ALTER TABLE User ADD COLUMN realname VARCHAR(255)",
`
CREATE TABLE NetworkNew (
id INTEGER PRIMARY KEY,
name TEXT,
user INTEGER NOT NULL,
addr TEXT NOT NULL,
nick TEXT,
username TEXT,
realname TEXT,
pass TEXT,
connect_commands TEXT,
sasl_mechanism TEXT,
sasl_plain_username TEXT,
sasl_plain_password TEXT,
sasl_external_cert BLOB,
sasl_external_key BLOB,
enabled INTEGER NOT NULL DEFAULT 1,
FOREIGN KEY(user) REFERENCES User(id),
UNIQUE(user, addr, nick),
UNIQUE(user, name)
);
INSERT INTO NetworkNew
SELECT id, name, user, addr, nick, username, realname, pass,
connect_commands, sasl_mechanism, sasl_plain_username,
sasl_plain_password, sasl_external_cert, sasl_external_key,
enabled
FROM Network;
DROP TABLE Network;
ALTER TABLE NetworkNew RENAME TO Network;
`,
} }
type SqliteDB struct { type SqliteDB struct {
@ -393,15 +423,16 @@ func (db *SqliteDB) ListNetworks(ctx context.Context, userID int64) ([]Network,
var networks []Network var networks []Network
for rows.Next() { for rows.Next() {
var net Network var net Network
var name, username, realname, pass, connectCommands sql.NullString var name, nick, username, realname, pass, connectCommands sql.NullString
var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString
err := rows.Scan(&net.ID, &name, &net.Addr, &net.Nick, &username, &realname, err := rows.Scan(&net.ID, &name, &net.Addr, &nick, &username, &realname,
&pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword, &pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword,
&net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob, &net.Enabled) &net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob, &net.Enabled)
if err != nil { if err != nil {
return nil, err return nil, err
} }
net.Name = name.String net.Name = name.String
net.Nick = nick.String
net.Username = username.String net.Username = username.String
net.Realname = realname.String net.Realname = realname.String
net.Pass = pass.String net.Pass = pass.String
@ -446,7 +477,7 @@ func (db *SqliteDB) StoreNetwork(ctx context.Context, userID int64, network *Net
args := []interface{}{ args := []interface{}{
sql.Named("name", toNullString(network.Name)), sql.Named("name", toNullString(network.Name)),
sql.Named("addr", network.Addr), sql.Named("addr", network.Addr),
sql.Named("nick", network.Nick), sql.Named("nick", toNullString(network.Nick)),
sql.Named("username", toNullString(network.Username)), sql.Named("username", toNullString(network.Username)),
sql.Named("realname", toNullString(network.Realname)), sql.Named("realname", toNullString(network.Realname)),
sql.Named("pass", toNullString(network.Pass)), sql.Named("pass", toNullString(network.Pass)),

View File

@ -102,7 +102,7 @@ func getNetworkAttrs(network *network) irc.Tags {
attrs := irc.Tags{ attrs := irc.Tags{
"name": irc.TagValue(network.GetName()), "name": irc.TagValue(network.GetName()),
"state": irc.TagValue(state), "state": irc.TagValue(state),
"nickname": irc.TagValue(network.Nick), "nickname": irc.TagValue(GetNick(&network.user.User, &network.Network)),
} }
if network.Username != "" { if network.Username != "" {
@ -335,7 +335,7 @@ func isOurNick(net *network, nick string) bool {
// know whether this name is our nickname. Best-effort: use the network's // know whether this name is our nickname. Best-effort: use the network's
// configured nickname and hope it was the one being used when we were // configured nickname and hope it was the one being used when we were
// connected. // connected.
return net.casemap(nick) == net.casemap(net.Nick) return net.casemap(nick) == net.casemap(GetNick(&net.user.User, &net.Network))
} }
// marshalEntity converts an upstream entity name (ie. channel or nick) into a // marshalEntity converts an upstream entity name (ie. channel or nick) into a
@ -2414,6 +2414,9 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
return err return err
} }
if record.Nick == dc.user.Username {
record.Nick = ""
}
if record.Realname == dc.user.Realname { if record.Realname == dc.user.Realname {
record.Realname = "" record.Realname = ""
} }
@ -2455,6 +2458,9 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
return err return err
} }
if record.Nick == dc.user.Username {
record.Nick = ""
}
if record.Realname == dc.user.Realname { if record.Realname == dc.user.Realname {
record.Realname = "" record.Realname = ""
} }

View File

@ -484,7 +484,6 @@ func handleServiceNetworkCreate(dc *downstreamConn, params []string) error {
record := &Network{ record := &Network{
Addr: *fs.Addr, Addr: *fs.Addr,
Nick: dc.nick,
Enabled: true, Enabled: true,
} }
if err := fs.update(record); err != nil { if err := fs.update(record); err != nil {

View File

@ -1671,7 +1671,7 @@ func splitSpace(s string) []string {
} }
func (uc *upstreamConn) register() { func (uc *upstreamConn) register() {
uc.nick = uc.network.Nick uc.nick = GetNick(&uc.user.User, &uc.network.Network)
uc.nickCM = uc.network.casemap(uc.nick) uc.nickCM = uc.network.casemap(uc.nick)
uc.username = uc.network.GetUsername() uc.username = uc.network.GetUsername()
uc.realname = GetRealname(&uc.user.User, &uc.network.Network) uc.realname = GetRealname(&uc.user.User, &uc.network.Network)