2019-10-13 08:50:55 +00:00
|
|
|
package common
|
|
|
|
|
2019-10-13 09:07:05 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
)
|
2019-10-13 08:50:55 +00:00
|
|
|
|
2019-10-18 00:35:13 +00:00
|
|
|
var UserBlocks BlockStore
|
|
|
|
|
|
|
|
//var UserFriends FriendStore
|
|
|
|
|
2019-10-13 08:50:55 +00:00
|
|
|
type BlockStore interface {
|
2019-10-13 10:36:50 +00:00
|
|
|
IsBlockedBy(blocker, blockee int) (bool, error)
|
2021-02-27 06:13:03 +00:00
|
|
|
BulkIsBlockedBy(blockers []int, blockee int) (bool, error)
|
2019-10-13 10:36:50 +00:00
|
|
|
Add(blocker, blockee int) error
|
2019-10-18 00:35:13 +00:00
|
|
|
Remove(blocker, blockee int) error
|
2019-11-12 06:17:46 +00:00
|
|
|
BlockedByOffset(blocker, offset, perPage int) ([]int, error)
|
|
|
|
BlockedByCount(blocker int) int
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultBlockStore struct {
|
2019-11-12 06:17:46 +00:00
|
|
|
isBlocked *sql.Stmt
|
|
|
|
add *sql.Stmt
|
|
|
|
remove *sql.Stmt
|
|
|
|
blockedBy *sql.Stmt
|
|
|
|
blockedByCount *sql.Stmt
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultBlockStore(acc *qgen.Accumulator) (*DefaultBlockStore, error) {
|
2019-10-19 10:33:59 +00:00
|
|
|
ub := "users_blocks"
|
2019-10-13 09:07:05 +00:00
|
|
|
return &DefaultBlockStore{
|
2020-02-11 03:13:38 +00:00
|
|
|
isBlocked: acc.Select(ub).Cols("blocker").Where("blocker=? AND blockedUser=?").Prepare(),
|
2019-11-12 06:17:46 +00:00
|
|
|
add: acc.Insert(ub).Columns("blocker,blockedUser").Fields("?,?").Prepare(),
|
2020-02-11 03:13:38 +00:00
|
|
|
remove: acc.Delete(ub).Where("blocker=? AND blockedUser=?").Prepare(),
|
|
|
|
blockedBy: acc.Select(ub).Columns("blockedUser").Where("blocker=?").Limit("?,?").Prepare(),
|
|
|
|
blockedByCount: acc.Count(ub).Where("blocker=?").Prepare(),
|
2019-10-13 09:07:05 +00:00
|
|
|
}, acc.FirstError()
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 10:36:50 +00:00
|
|
|
func (s *DefaultBlockStore) IsBlockedBy(blocker, blockee int) (bool, error) {
|
2021-04-07 14:23:11 +00:00
|
|
|
e := s.isBlocked.QueryRow(blocker, blockee).Scan(&blocker)
|
|
|
|
if e == ErrNoRows {
|
2019-10-19 10:33:59 +00:00
|
|
|
return false, nil
|
2019-10-13 09:07:05 +00:00
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
return e == nil, e
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 06:13:03 +00:00
|
|
|
// TODO: Optimise the query to avoid preparing it on the spot? Maybe, use knowledge of the most common IN() parameter counts?
|
|
|
|
func (s *DefaultBlockStore) BulkIsBlockedBy(blockers []int, blockee int) (bool, error) {
|
|
|
|
if len(blockers) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if len(blockers) == 1 {
|
|
|
|
return s.IsBlockedBy(blockers[0], blockee)
|
|
|
|
}
|
|
|
|
idList, q := inqbuild(blockers)
|
2021-04-07 14:23:11 +00:00
|
|
|
count, e := qgen.NewAcc().Count("users_blocks").Where("blocker IN(" + q + ") AND blockedUser=?").TotalP(idList...)
|
|
|
|
if e == ErrNoRows {
|
2021-02-27 06:13:03 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
return count == 0, e
|
2021-02-27 06:13:03 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 10:36:50 +00:00
|
|
|
func (s *DefaultBlockStore) Add(blocker, blockee int) error {
|
2021-04-07 14:23:11 +00:00
|
|
|
_, e := s.add.Exec(blocker, blockee)
|
|
|
|
return e
|
2019-10-13 10:36:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 00:35:13 +00:00
|
|
|
func (s *DefaultBlockStore) Remove(blocker, blockee int) error {
|
2021-04-07 14:23:11 +00:00
|
|
|
_, e := s.remove.Exec(blocker, blockee)
|
|
|
|
return e
|
2019-10-18 00:35:13 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 06:17:46 +00:00
|
|
|
func (s *DefaultBlockStore) BlockedByOffset(blocker, offset, perPage int) (uids []int, err error) {
|
2021-04-07 14:23:11 +00:00
|
|
|
rows, e := s.blockedBy.Query(blocker, offset, perPage)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
2019-11-12 06:17:46 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var uid int
|
2021-04-07 14:23:11 +00:00
|
|
|
if e := rows.Scan(&uid); e != nil {
|
|
|
|
return nil, e
|
2019-11-12 06:17:46 +00:00
|
|
|
}
|
|
|
|
uids = append(uids, uid)
|
|
|
|
}
|
|
|
|
return uids, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultBlockStore) BlockedByCount(blocker int) (count int) {
|
2021-04-07 14:23:11 +00:00
|
|
|
e := s.blockedByCount.QueryRow(blocker).Scan(&count)
|
|
|
|
if e != nil {
|
|
|
|
LogError(e)
|
2019-11-12 06:17:46 +00:00
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2019-10-13 10:36:50 +00:00
|
|
|
type FriendInvite struct {
|
|
|
|
Requester int
|
|
|
|
Target int
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:50:55 +00:00
|
|
|
type FriendStore interface {
|
2019-10-13 10:36:50 +00:00
|
|
|
AddInvite(requester, target int) error
|
|
|
|
Confirm(requester, target int) error
|
|
|
|
GetOwSentInvites(uid int) ([]FriendInvite, error)
|
|
|
|
GetOwnRecvInvites(uid int) ([]FriendInvite, error)
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultFriendStore struct {
|
2019-10-13 10:36:50 +00:00
|
|
|
addInvite *sql.Stmt
|
|
|
|
confirm *sql.Stmt
|
|
|
|
getOwnSentInvites *sql.Stmt
|
|
|
|
getOwnRecvInvites *sql.Stmt
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultFriendStore(acc *qgen.Accumulator) (*DefaultFriendStore, error) {
|
2020-02-11 03:13:38 +00:00
|
|
|
ufi := "users_friends_invites"
|
2019-10-13 10:36:50 +00:00
|
|
|
return &DefaultFriendStore{
|
2020-02-11 03:13:38 +00:00
|
|
|
addInvite: acc.Insert(ufi).Columns("requester,target").Fields("?,?").Prepare(),
|
2019-10-13 10:36:50 +00:00
|
|
|
confirm: acc.Insert("users_friends").Columns("uid,uid2").Fields("?,?").Prepare(),
|
2020-02-11 03:13:38 +00:00
|
|
|
getOwnSentInvites: acc.Select(ufi).Cols("requester,target").Where("requester=?").Prepare(),
|
|
|
|
getOwnRecvInvites: acc.Select(ufi).Cols("requester,target").Where("target=?").Prepare(),
|
2019-10-13 10:36:50 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultFriendStore) AddInvite(requester, target int) error {
|
2021-04-07 14:23:11 +00:00
|
|
|
_, e := s.addInvite.Exec(requester, target)
|
|
|
|
return e
|
2019-10-13 10:36:50 +00:00
|
|
|
}
|
2019-10-13 10:50:15 +00:00
|
|
|
|
2019-10-13 10:36:50 +00:00
|
|
|
func (s *DefaultFriendStore) Confirm(requester, target int) error {
|
2021-04-07 14:23:11 +00:00
|
|
|
_, e := s.confirm.Exec(requester, target)
|
|
|
|
return e
|
2019-10-13 10:36:50 +00:00
|
|
|
}
|
2019-10-13 10:50:15 +00:00
|
|
|
|
2019-10-13 10:36:50 +00:00
|
|
|
func (s *DefaultFriendStore) GetOwnSentInvites(uid int) ([]FriendInvite, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (s *DefaultFriendStore) GetOwnRecvInvites(uid int) ([]FriendInvite, error) {
|
|
|
|
return nil, nil
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|