NewPeer now handles nil
This commit is contained in:
parent
42693e2a3b
commit
9e37c6af02
1 changed files with 32 additions and 15 deletions
|
@ -38,6 +38,38 @@ type Peer struct {
|
||||||
LastAnnounce int64 `json:"last_announce"`
|
LastAnnounce int64 `json:"last_announce"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewPeer returns the Peer representation of an Announce. When provided nil
|
||||||
|
// for the announce parameter, it panics. When provided nil for the user or
|
||||||
|
// torrent parameter, it returns a Peer{UserID: 0} or Peer{TorrentID: 0}
|
||||||
|
// respectively.
|
||||||
|
func NewPeer(a *Announce, u *User, t *Torrent) *Peer {
|
||||||
|
if a == nil {
|
||||||
|
panic("models: announce cannot equal nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
var userID uint64
|
||||||
|
if u == nil {
|
||||||
|
userID = u.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
var torrentID uint64
|
||||||
|
if t == nil {
|
||||||
|
torrentID = u.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Peer{
|
||||||
|
ID: a.PeerID,
|
||||||
|
UserID: userID,
|
||||||
|
TorrentID: torrentID,
|
||||||
|
IP: a.IP,
|
||||||
|
Port: a.Port,
|
||||||
|
Uploaded: a.Uploaded,
|
||||||
|
Downloaded: a.Downloaded,
|
||||||
|
Left: a.Left,
|
||||||
|
LastAnnounce: time.Now().Unix(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Key returns the unique key used to look-up a peer in a swarm (i.e
|
// Key returns the unique key used to look-up a peer in a swarm (i.e
|
||||||
// Torrent.Seeders & Torrent.Leechers).
|
// Torrent.Seeders & Torrent.Leechers).
|
||||||
func (p Peer) Key() string {
|
func (p Peer) Key() string {
|
||||||
|
@ -71,21 +103,6 @@ func (t *Torrent) InLeecherPool(p *Peer) bool {
|
||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPeer returns the Peer representation of an Announce.
|
|
||||||
func NewPeer(a *Announce, u *User, t *Torrent) *Peer {
|
|
||||||
return &Peer{
|
|
||||||
ID: a.PeerID,
|
|
||||||
UserID: u.ID,
|
|
||||||
TorrentID: t.ID,
|
|
||||||
IP: a.IP,
|
|
||||||
Port: a.Port,
|
|
||||||
Uploaded: a.Uploaded,
|
|
||||||
Downloaded: a.Downloaded,
|
|
||||||
Left: a.Left,
|
|
||||||
LastAnnounce: time.Now().Unix(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// User is a registered user for private trackers.
|
// User is a registered user for private trackers.
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uint64 `json:"id"`
|
ID uint64 `json:"id"`
|
||||||
|
|
Loading…
Add table
Reference in a new issue