|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/mtepenner/brevity-sharing/internal/models" |
| 11 | +) |
| 12 | + |
| 13 | +// userByID looks up a user by their ID across the username-keyed Users map. |
| 14 | +func (s *Server) userByID(id string) *models.User { |
| 15 | + for _, u := range s.Users { |
| 16 | + if u.ID == id { |
| 17 | + return u |
| 18 | + } |
| 19 | + } |
| 20 | + return nil |
| 21 | +} |
| 22 | + |
| 23 | +// HandleSendMessage sends a private message from one user to another. |
| 24 | +func (s *Server) HandleSendMessage(w http.ResponseWriter, r *http.Request) { |
| 25 | + var req struct { |
| 26 | + SenderID string `json:"sender_id"` |
| 27 | + RecipientID string `json:"recipient_id"` |
| 28 | + Content string `json:"content"` |
| 29 | + } |
| 30 | + |
| 31 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 32 | + http.Error(w, "Invalid request payload", http.StatusBadRequest) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + if req.SenderID == "" || req.RecipientID == "" || req.Content == "" { |
| 37 | + http.Error(w, "sender_id, recipient_id, and content are required", http.StatusBadRequest) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + if len(req.Content) > 1000 { |
| 42 | + http.Error(w, "Message too long (max 1000 characters)", http.StatusBadRequest) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + if req.SenderID == req.RecipientID { |
| 47 | + http.Error(w, "Cannot send a message to yourself", http.StatusBadRequest) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + msg := &models.Message{ |
| 52 | + ID: generateID(), |
| 53 | + SenderID: req.SenderID, |
| 54 | + RecipientID: req.RecipientID, |
| 55 | + Content: req.Content, |
| 56 | + CreatedAt: time.Now(), |
| 57 | + } |
| 58 | + |
| 59 | + s.Messages[msg.ID] = msg |
| 60 | + |
| 61 | + w.Header().Set("Content-Type", "application/json") |
| 62 | + w.WriteHeader(http.StatusCreated) |
| 63 | + json.NewEncoder(w).Encode(msg) |
| 64 | +} |
| 65 | + |
| 66 | +// Conversation summarises the last message and unread count for a conversation partner. |
| 67 | +type Conversation struct { |
| 68 | + PartnerID string `json:"partner_id"` |
| 69 | + PartnerUsername string `json:"partner_username"` |
| 70 | + LastMessage *models.Message `json:"last_message"` |
| 71 | + UnreadCount int `json:"unread_count"` |
| 72 | +} |
| 73 | + |
| 74 | +// HandleGetConversations returns a list of conversations for a given user. |
| 75 | +func (s *Server) HandleGetConversations(w http.ResponseWriter, r *http.Request) { |
| 76 | + userID := r.URL.Query().Get("user_id") |
| 77 | + if userID == "" { |
| 78 | + http.Error(w, "user_id required", http.StatusBadRequest) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + convMap := make(map[string]*Conversation) |
| 83 | + |
| 84 | + for _, msg := range s.Messages { |
| 85 | + if msg.SenderID != userID && msg.RecipientID != userID { |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + partnerID := msg.RecipientID |
| 90 | + if msg.RecipientID == userID { |
| 91 | + partnerID = msg.SenderID |
| 92 | + } |
| 93 | + |
| 94 | + conv, exists := convMap[partnerID] |
| 95 | + if !exists { |
| 96 | + partnerUsername := partnerID |
| 97 | + if partner := s.userByID(partnerID); partner != nil { |
| 98 | + partnerUsername = partner.Username |
| 99 | + } |
| 100 | + conv = &Conversation{ |
| 101 | + PartnerID: partnerID, |
| 102 | + PartnerUsername: partnerUsername, |
| 103 | + } |
| 104 | + convMap[partnerID] = conv |
| 105 | + } |
| 106 | + |
| 107 | + if conv.LastMessage == nil || msg.CreatedAt.After(conv.LastMessage.CreatedAt) { |
| 108 | + msgCopy := *msg |
| 109 | + conv.LastMessage = &msgCopy |
| 110 | + } |
| 111 | + |
| 112 | + if msg.RecipientID == userID && msg.ReadAt == nil { |
| 113 | + conv.UnreadCount++ |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + conversations := make([]*Conversation, 0, len(convMap)) |
| 118 | + for _, conv := range convMap { |
| 119 | + conversations = append(conversations, conv) |
| 120 | + } |
| 121 | + |
| 122 | + sort.Slice(conversations, func(i, j int) bool { |
| 123 | + if conversations[i].LastMessage == nil { |
| 124 | + return false |
| 125 | + } |
| 126 | + if conversations[j].LastMessage == nil { |
| 127 | + return true |
| 128 | + } |
| 129 | + return conversations[i].LastMessage.CreatedAt.After(conversations[j].LastMessage.CreatedAt) |
| 130 | + }) |
| 131 | + |
| 132 | + w.Header().Set("Content-Type", "application/json") |
| 133 | + json.NewEncoder(w).Encode(conversations) |
| 134 | +} |
| 135 | + |
| 136 | +// HandleGetConversation returns all messages exchanged between two users. |
| 137 | +func (s *Server) HandleGetConversation(w http.ResponseWriter, r *http.Request) { |
| 138 | + user1 := r.URL.Query().Get("user1") |
| 139 | + user2 := r.URL.Query().Get("user2") |
| 140 | + |
| 141 | + if user1 == "" || user2 == "" { |
| 142 | + http.Error(w, "user1 and user2 required", http.StatusBadRequest) |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + messages := make([]models.Message, 0) |
| 147 | + for _, msg := range s.Messages { |
| 148 | + if (msg.SenderID == user1 && msg.RecipientID == user2) || |
| 149 | + (msg.SenderID == user2 && msg.RecipientID == user1) { |
| 150 | + messages = append(messages, *msg) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + sort.Slice(messages, func(i, j int) bool { |
| 155 | + return messages[i].CreatedAt.Before(messages[j].CreatedAt) |
| 156 | + }) |
| 157 | + |
| 158 | + w.Header().Set("Content-Type", "application/json") |
| 159 | + json.NewEncoder(w).Encode(messages) |
| 160 | +} |
| 161 | + |
| 162 | +// HandleGetTrending returns the top trending hashtags extracted from stored tweets. |
| 163 | +func (s *Server) HandleGetTrending(w http.ResponseWriter, r *http.Request) { |
| 164 | + tweets := s.DB.GetTimeline() |
| 165 | + |
| 166 | + topicCounts := make(map[string]int) |
| 167 | + for _, tweet := range tweets { |
| 168 | + for _, word := range strings.Fields(tweet.Content) { |
| 169 | + word = strings.Trim(word, ".,!?\"';:()") |
| 170 | + if strings.HasPrefix(word, "#") && len(word) > 1 { |
| 171 | + topicCounts[strings.ToLower(word)]++ |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + type TrendingTopic struct { |
| 177 | + Topic string `json:"topic"` |
| 178 | + Count int `json:"count"` |
| 179 | + } |
| 180 | + |
| 181 | + topics := make([]TrendingTopic, 0, len(topicCounts)) |
| 182 | + for topic, count := range topicCounts { |
| 183 | + topics = append(topics, TrendingTopic{Topic: topic, Count: count}) |
| 184 | + } |
| 185 | + |
| 186 | + sort.Slice(topics, func(i, j int) bool { |
| 187 | + return topics[i].Count > topics[j].Count |
| 188 | + }) |
| 189 | + |
| 190 | + if len(topics) > 10 { |
| 191 | + topics = topics[:10] |
| 192 | + } |
| 193 | + |
| 194 | + w.Header().Set("Content-Type", "application/json") |
| 195 | + json.NewEncoder(w).Encode(topics) |
| 196 | +} |
0 commit comments