move user registration to linkbot menu file

This commit is contained in:
mappu 2018-06-06 19:41:08 +12:00
parent c9615cbdfb
commit ed13dd5216
2 changed files with 37 additions and 37 deletions

View File

@ -148,43 +148,6 @@ func NewNTFServer(configFile string, verbose bool) (*NTFServer, error) {
return &ret, nil return &ret, nil
} }
// registerUser lodges a user mapping in the configuration file.
// This allows them to join the group chat (unbanning them if necessary).
// An actual NMDC connection will occur once the user joins for the first time.
func (this *NTFServer) registerUser(telegramUserId int64, hubUsername string) error {
if existingHubNick, ok := this.config.KnownUsers[telegramUserId]; ok {
if existingHubNick == hubUsername {
return nil
}
return fmt.Errorf("Telegram account is already registered with hub nick '%s'", existingHubNick)
}
for _, v := range this.config.KnownUsers {
if v == hubUsername {
return fmt.Errorf("Requested hub nick '%s' is already used by another member", hubUsername)
}
}
this.config.KnownUsers[telegramUserId] = hubUsername
err := this.config.Save(this.configFile)
if err != nil {
return err
}
// Unban from groupchat, if necessary
// Ignore errors because the user might not have been banned
_, err = this.bot.UnbanChatMember(telegram.ChatMemberConfig{
ChatID: this.config.GroupChatID,
UserID: int(telegramUserId),
})
if err != nil {
log.Printf("Couldn't unban user '%s' from groupchat because: %s (assuming OK, continuing)", hubUsername, err.Error())
}
return nil
}
// LaunchUpstreamWorker opens an NMDC connection. // LaunchUpstreamWorker opens an NMDC connection.
func (this *NTFServer) LaunchUpstreamWorker(telegramUserId int64, hubUsername string) error { func (this *NTFServer) LaunchUpstreamWorker(telegramUserId int64, hubUsername string) error {

View File

@ -10,6 +10,43 @@ import (
telegram "github.com/go-telegram-bot-api/telegram-bot-api" telegram "github.com/go-telegram-bot-api/telegram-bot-api"
) )
// registerUser lodges a user mapping in the configuration file.
// This allows them to join the group chat (unbanning them if necessary).
// An actual NMDC connection will occur once the user joins for the first time.
func (this *NTFServer) registerUser(telegramUserId int64, hubUsername string) error {
if existingHubNick, ok := this.config.KnownUsers[telegramUserId]; ok {
if existingHubNick == hubUsername {
return nil
}
return fmt.Errorf("Telegram account is already registered with hub nick '%s'", existingHubNick)
}
for _, v := range this.config.KnownUsers {
if v == hubUsername {
return fmt.Errorf("Requested hub nick '%s' is already used by another member", hubUsername)
}
}
this.config.KnownUsers[telegramUserId] = hubUsername
err := this.config.Save(this.configFile)
if err != nil {
return err
}
// Unban from groupchat, if necessary
// Ignore errors because the user might not have been banned
_, err = this.bot.UnbanChatMember(telegram.ChatMemberConfig{
ChatID: this.config.GroupChatID,
UserID: int(telegramUserId),
})
if err != nil {
log.Printf("Couldn't unban user '%s' from groupchat because: %s (assuming OK, continuing)", hubUsername, err.Error())
}
return nil
}
func (this *NTFServer) HandleDirectMessage(update telegram.Update) error { func (this *NTFServer) HandleDirectMessage(update telegram.Update) error {
// Registration workflow // Registration workflow