-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathpost.go
65 lines (58 loc) · 1.2 KB
/
post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package socialnetworkingservice
import (
"sync"
"time"
)
type Post struct {
ID string
UserID string
Content string
ImageURLs []string
VideoURLs []string
Timestamp time.Time
likes map[string]bool
comments []*Comment
mu sync.RWMutex
}
func NewPost(id, userID, content string, imageURLs, videoURLs []string) *Post {
return &Post{
ID: id,
UserID: userID,
Content: content,
ImageURLs: imageURLs,
VideoURLs: videoURLs,
Timestamp: time.Now(),
likes: make(map[string]bool),
comments: make([]*Comment, 0),
}
}
func (p *Post) AddLike(userID string) bool {
p.mu.Lock()
defer p.mu.Unlock()
if !p.likes[userID] {
p.likes[userID] = true
return true
}
return false
}
func (p *Post) AddComment(comment *Comment) {
p.mu.Lock()
defer p.mu.Unlock()
p.comments = append(p.comments, comment)
}
func (p *Post) GetLikes() []string {
p.mu.RLock()
defer p.mu.RUnlock()
likes := make([]string, 0, len(p.likes))
for userID := range p.likes {
likes = append(likes, userID)
}
return likes
}
func (p *Post) GetComments() []*Comment {
p.mu.RLock()
defer p.mu.RUnlock()
comments := make([]*Comment, len(p.comments))
copy(comments, p.comments)
return comments
}