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)
|
|
|
|
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{
|
2019-11-12 06:17:46 +00:00
|
|
|
isBlocked: acc.Select(ub).Cols("blocker").Where("blocker = ? AND blockedUser = ?").Prepare(),
|
|
|
|
add: acc.Insert(ub).Columns("blocker,blockedUser").Fields("?,?").Prepare(),
|
|
|
|
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) {
|
2019-10-13 09:07:05 +00:00
|
|
|
err := s.isBlocked.QueryRow(blocker, blockee).Scan(&blocker)
|
2019-10-19 10:33:59 +00:00
|
|
|
if err == ErrNoRows {
|
|
|
|
return false, nil
|
2019-10-13 09:07:05 +00:00
|
|
|
}
|
2019-10-19 10:33:59 +00:00
|
|
|
return err == nil, err
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 10:36:50 +00:00
|
|
|
func (s *DefaultBlockStore) Add(blocker, blockee int) error {
|
2019-10-13 10:50:15 +00:00
|
|
|
_, err := s.add.Exec(blocker, blockee)
|
|
|
|
return err
|
2019-10-13 10:36:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 00:35:13 +00:00
|
|
|
func (s *DefaultBlockStore) Remove(blocker, blockee int) error {
|
|
|
|
_, err := s.remove.Exec(blocker, blockee)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-12 06:17:46 +00:00
|
|
|
func (s *DefaultBlockStore) BlockedByOffset(blocker, offset, perPage int) (uids []int, err error) {
|
|
|
|
rows, err := s.blockedBy.Query(blocker, offset, perPage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var uid int
|
|
|
|
err := rows.Scan(&uid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uids = append(uids, uid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return uids, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultBlockStore) BlockedByCount(blocker int) (count int) {
|
|
|
|
err := s.blockedByCount.QueryRow(blocker).Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
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) {
|
2019-10-13 10:36:50 +00:00
|
|
|
return &DefaultFriendStore{
|
2019-10-18 00:35:13 +00:00
|
|
|
addInvite: acc.Insert("users_friends_invites").Columns("requester,target").Fields("?,?").Prepare(),
|
2019-10-13 10:36:50 +00:00
|
|
|
confirm: acc.Insert("users_friends").Columns("uid,uid2").Fields("?,?").Prepare(),
|
2019-10-18 00:35:13 +00:00
|
|
|
getOwnSentInvites: acc.Select("users_friends_invites").Cols("requester,target").Where("requester = ?").Prepare(),
|
|
|
|
getOwnRecvInvites: acc.Select("users_friends_invites").Cols("requester,target").Where("target = ?").Prepare(),
|
2019-10-13 10:36:50 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultFriendStore) AddInvite(requester, target int) error {
|
2019-10-13 10:50:15 +00:00
|
|
|
_, err := s.addInvite.Exec(requester, target)
|
|
|
|
return err
|
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 {
|
2019-10-13 10:50:15 +00:00
|
|
|
_, err := s.confirm.Exec(requester, target)
|
|
|
|
return err
|
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
|
|
|
}
|