Skip to content

Commit

Permalink
write unprocessed users to the same file, drop langCode
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Oct 29, 2022
1 parent 218d202 commit 101511c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
14 changes: 5 additions & 9 deletions ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"io"
"os"
"strconv"
"time"

log "github.com/go-pkgz/lgr"
"github.com/gotd/td/tg"
)

// bans users from given file, cleans up their messages and kicks them afterwards
// bans users from given file, cleans up their messages and kicks them afterwards.
// in case of errors during the run, writes unprocessed errors back to the same file.
func banAndKickUsers(ctx context.Context, api *tg.Client, channel *tg.Channel, filePath string) {

users, err := readUserIDsFromCSV(filePath)
if err != nil {
log.Printf("[ERROR] error reading users from the file %s: %v", filePath, err)
Expand All @@ -31,21 +30,18 @@ func banAndKickUsers(ctx context.Context, api *tg.Client, channel *tg.Channel, f
if stoppedIndex == 0 {
log.Printf("[INFO] Canceled without processing any entries, restart the same command to ban users")
}
fileName := fmt.Sprintf("./ban/%s.unprocessed.users.csv", time.Now().Format("2006-01-02T15-04-05"))

usersToBan := make([]banUserInfo, len(users[stoppedIndex:]))
for i, user := range users[stoppedIndex:] {
usersToBan[i] = banUserInfo{userID: user.UserID, accessHash: user.AccessHash}
}
if e := writeUsersToFile(usersToBan, fileName); err != nil {
if e := writeUsersToFile(usersToBan, filePath); err != nil {
log.Printf("[ERROR] Error writing rest of users to ban after context cancel to file: %v", e)
} else {
log.Printf("[INFO] Success, rest of users (%d-%d) to ban after context cancel written to %s",
log.Printf("[INFO] Success, rest of users (%d-%d) to ban after context cancel written to the same file %s, restart the same command to ban users",
stoppedIndex,
len(users),
fileName)
log.Printf("[INFO] To continue, run the same command with the new filename:")
log.Printf("[INFO] --ban_and_kick_filepath %s", fileName)
filePath)
}
}

Expand Down
41 changes: 22 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func main() {
}
}()

// credentials are stored per phone number
telegramOptions := telegram.Options{
Middlewares: []telegram.Middleware{waiter},
SessionStorage: &telegram.FileSessionStorage{Path: fmt.Sprintf("./ban/%s.json", opts.Phone)},
Expand Down Expand Up @@ -123,25 +124,28 @@ func main() {
return err
}

if opts.BanAndKickFilePath == "" {
if opts.BanToTimestamp == 0 {
log.Printf("[ERROR] ban_to must be set when searching for users")
return nil
}
if opts.BanSearchDuration.Seconds() <= 0 {
log.Printf("[ERROR] ban_search_duration must be non-zero when searching for users")
return nil
}
searchAndStoreUsersToBan(ctx, api, channel, searchParams{
endUnixTime: opts.BanToTimestamp,
duration: opts.BanSearchDuration,
offset: opts.BanSearchOffset,
limit: opts.BanSearchLimit,
ignoreMessages: opts.SearchIgnoreMessages,
})
} else {
// ban users case
if opts.BanAndKickFilePath != "" {
banAndKickUsers(ctx, api, channel, opts.BanAndKickFilePath)
return nil
}

// retrieve users to ban case
if opts.BanToTimestamp == 0 {
log.Printf("[ERROR] ban_to must be set when searching for users")
return nil
}
if opts.BanSearchDuration.Seconds() <= 0 {
log.Printf("[ERROR] ban_search_duration must be non-zero when searching for users")
return nil
}
searchAndStoreUsersToBan(ctx, api, channel, searchParams{
endUnixTime: opts.BanToTimestamp,
duration: opts.BanSearchDuration,
offset: opts.BanSearchOffset,
limit: opts.BanSearchLimit,
ignoreMessages: opts.SearchIgnoreMessages,
})

return nil
}); err != nil {
Expand Down Expand Up @@ -224,7 +228,7 @@ func writeUsersToFile(users []banUserInfo, fileName string) error {
}()
}

data := [][]string{{"joined", "userID", "access_hash", "username", "firstName", "lastName", "langCode", "message"}}
data := [][]string{{"joined", "userID", "access_hash", "username", "firstName", "lastName", "message"}}

for _, user := range users {
data = append(data, []string{
Expand All @@ -234,7 +238,6 @@ func writeUsersToFile(users []banUserInfo, fileName string) error {
user.username, // username
strings.ReplaceAll(user.firstName, "\t", " "), // firstName
strings.ReplaceAll(user.lastName, "\t", " "), // lastName
user.langCode, // langCode
strings.ReplaceAll(user.message, "\t", " "), // message
})
}
Expand Down
2 changes: 0 additions & 2 deletions retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type banUserInfo struct {
username string
firstName string
lastName string
langCode string
}

type channelParticipantInfo struct {
Expand Down Expand Up @@ -158,7 +157,6 @@ func getSingleUserStoreInfo(ctx context.Context, api *tg.Client, channel *tg.Cha
userInfoToStore.username = userToBan.info.Username
userInfoToStore.firstName = userToBan.info.FirstName
userInfoToStore.lastName = userToBan.info.LastName
userInfoToStore.langCode = userToBan.info.LangCode
userInfoToStore.accessHash = userToBan.info.AccessHash

var message string
Expand Down

0 comments on commit 101511c

Please sign in to comment.