2018-01-21 11:17:43 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
2019-10-26 23:11:09 +00:00
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
2018-01-21 11:17:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var IPSearch IPSearcher
|
|
|
|
|
|
|
|
type IPSearcher interface {
|
|
|
|
Lookup(ip string) (uids []int, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultIPSearcher struct {
|
2018-01-22 08:15:45 +00:00
|
|
|
searchUsers *sql.Stmt
|
|
|
|
searchTopics *sql.Stmt
|
|
|
|
searchReplies *sql.Stmt
|
|
|
|
searchUsersReplies *sql.Stmt
|
2018-01-21 11:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultIPSearcher gives you a new instance of DefaultIPSearcher
|
|
|
|
func NewDefaultIPSearcher() (*DefaultIPSearcher, error) {
|
2018-08-04 11:46:36 +00:00
|
|
|
acc := qgen.NewAcc()
|
2020-01-02 21:52:41 +00:00
|
|
|
uu := "users"
|
2018-01-21 11:17:43 +00:00
|
|
|
return &DefaultIPSearcher{
|
2020-01-02 21:52:41 +00:00
|
|
|
searchUsers: acc.Select(uu).Columns("uid").Where("last_ip=? OR last_ip LIKE CONCAT('%-',?)").Prepare(),
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
searchTopics: acc.Select(uu).Columns("uid").InQ("uid", acc.Select("topics").Columns("createdBy").Where("ip=?")).Prepare(),
|
|
|
|
searchReplies: acc.Select(uu).Columns("uid").InQ("uid", acc.Select("replies").Columns("createdBy").Where("ip=?")).Prepare(),
|
|
|
|
searchUsersReplies: acc.Select(uu).Columns("uid").InQ("uid", acc.Select("users_replies").Columns("createdBy").Where("ip=?")).Prepare(),
|
2018-01-21 11:17:43 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
2019-10-26 23:11:09 +00:00
|
|
|
func (s *DefaultIPSearcher) Lookup(ip string) (uids []int, err error) {
|
2018-01-21 11:17:43 +00:00
|
|
|
var uid int
|
2019-09-29 04:56:39 +00:00
|
|
|
reqUserList := make(map[int]bool)
|
2019-12-31 21:57:54 +00:00
|
|
|
runQuery2 := func(rows *sql.Rows, err error) error {
|
2018-01-21 11:17:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(&uid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reqUserList[uid] = true
|
|
|
|
}
|
|
|
|
return rows.Err()
|
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
runQuery := func(stmt *sql.Stmt) error {
|
|
|
|
return runQuery2(stmt.Query(ip))
|
|
|
|
}
|
2018-01-21 11:17:43 +00:00
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
err = runQuery2(s.searchUsers.Query(ip, ip))
|
2018-01-21 11:17:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return uids, err
|
|
|
|
}
|
2019-10-26 23:11:09 +00:00
|
|
|
err = runQuery(s.searchTopics)
|
2018-01-21 11:17:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return uids, err
|
|
|
|
}
|
2019-10-26 23:11:09 +00:00
|
|
|
err = runQuery(s.searchReplies)
|
2018-01-21 11:17:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return uids, err
|
|
|
|
}
|
2019-10-26 23:11:09 +00:00
|
|
|
err = runQuery(s.searchUsersReplies)
|
2018-01-22 08:15:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return uids, err
|
|
|
|
}
|
2018-01-21 11:17:43 +00:00
|
|
|
|
|
|
|
// Convert the user ID map to a slice, then bulk load the users
|
|
|
|
uids = make([]int, len(reqUserList))
|
|
|
|
var i int
|
|
|
|
for userID := range reqUserList {
|
|
|
|
uids[i] = userID
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return uids, nil
|
|
|
|
}
|