|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/commandlinecoding/elephant/server/middlewares" |
| 10 | + "github.com/commandlinecoding/elephant/server/models" |
| 11 | + "github.com/commandlinecoding/elephant/server/repository" |
| 12 | + "github.com/go-chi/chi/v5" |
| 13 | +) |
| 14 | + |
| 15 | +type CreateGroupReq struct { |
| 16 | + Name string `json:"name"` |
| 17 | +} |
| 18 | + |
| 19 | +func HandleCreateGroup(w http.ResponseWriter, r *http.Request) { |
| 20 | + w.Header().Set("Content-Type", "application/json") |
| 21 | + uid, _ := r.Context().Value(middlewares.UserIDKey).(string) |
| 22 | + |
| 23 | + var req CreateGroupReq |
| 24 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.Name == "" { |
| 25 | + w.WriteHeader(http.StatusBadRequest) |
| 26 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Invalid payload group name configuration"}) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + repo := repository.NewGroupRepository() |
| 31 | + group, err := repo.Create(r.Context(), req.Name, uid) |
| 32 | + if err != nil { |
| 33 | + w.WriteHeader(http.StatusInternalServerError) |
| 34 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Failed to create group channel"}) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + w.WriteHeader(http.StatusCreated) |
| 39 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: true, Data: group}) |
| 40 | +} |
| 41 | + |
| 42 | +func HandleListMyGroups(w http.ResponseWriter, r *http.Request) { |
| 43 | + w.Header().Set("Content-Type", "application/json") |
| 44 | + uid, _ := r.Context().Value(middlewares.UserIDKey).(string) |
| 45 | + |
| 46 | + repo := repository.NewGroupRepository() |
| 47 | + groups, err := repo.ListByUser(r.Context(), uid) |
| 48 | + if err != nil { |
| 49 | + w.WriteHeader(http.StatusInternalServerError) |
| 50 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Failed to pull your joined channels list"}) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: true, Data: groups}) |
| 55 | +} |
| 56 | + |
| 57 | +type GroupMemberReq struct { |
| 58 | + UserID string `json:"user_id"` |
| 59 | +} |
| 60 | + |
| 61 | +func HandleAddMember(w http.ResponseWriter, r *http.Request) { |
| 62 | + w.Header().Set("Content-Type", "application/json") |
| 63 | + groupID := chi.URLParam(r, "id") |
| 64 | + |
| 65 | + var req GroupMemberReq |
| 66 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.UserID == "" { |
| 67 | + w.WriteHeader(http.StatusBadRequest) |
| 68 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Missing user target value"}) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + repo := repository.NewGroupRepository() |
| 73 | + if err := repo.AddMember(r.Context(), groupID, req.UserID); err != nil { |
| 74 | + w.WriteHeader(http.StatusInternalServerError) |
| 75 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Failed to add member to channel"}) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: true, Data: "User attached to channel context cleanly"}) |
| 80 | +} |
| 81 | + |
| 82 | +func HandleRemoveMember(w http.ResponseWriter, r *http.Request) { |
| 83 | + w.Header().Set("Content-Type", "application/json") |
| 84 | + groupID := chi.URLParam(r, "id") |
| 85 | + targetUserID := chi.URLParam(r, "userId") |
| 86 | + |
| 87 | + if targetUserID == "" { |
| 88 | + w.WriteHeader(http.StatusBadRequest) |
| 89 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Missing path target user selection identifier"}) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + repo := repository.NewGroupRepository() |
| 94 | + if err := repo.RemoveMember(r.Context(), groupID, targetUserID); err != nil { |
| 95 | + w.WriteHeader(http.StatusInternalServerError) |
| 96 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Failed to drop target user from channel"}) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: true, Data: "Member detached from group context cleanly"}) |
| 101 | +} |
| 102 | + |
| 103 | +func HandleGetGroupMessages(w http.ResponseWriter, r *http.Request) { |
| 104 | + w.Header().Set("Content-Type", "application/json") |
| 105 | + groupID := chi.URLParam(r, "id") |
| 106 | + beforeStr := r.URL.Query().Get("before") |
| 107 | + limitStr := r.URL.Query().Get("limit") |
| 108 | + |
| 109 | + limit, _ := strconv.Atoi(limitStr) |
| 110 | + if limit <= 0 || limit > 100 { |
| 111 | + limit = 50 |
| 112 | + } |
| 113 | + |
| 114 | + before := time.Now() |
| 115 | + if beforeStr != "" { |
| 116 | + if t, err := time.Parse(time.RFC3339, beforeStr); err == nil { |
| 117 | + before = t |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + repo := repository.NewGroupRepository() |
| 122 | + history, err := repo.GetGroupMessages(r.Context(), groupID, before, limit) |
| 123 | + if err != nil { |
| 124 | + w.WriteHeader(http.StatusInternalServerError) |
| 125 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Failed to pull channel message history stream"}) |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: true, Data: history}) |
| 130 | +} |
| 131 | + |
| 132 | +func HandleListGroupMembers(w http.ResponseWriter, r *http.Request) { |
| 133 | + w.Header().Set("Content-Type", "application/json") |
| 134 | + groupID := chi.URLParam(r, "id") |
| 135 | + |
| 136 | + repo := repository.NewGroupRepository() |
| 137 | + members, err := repo.GetMembersDetails(r.Context(), groupID) |
| 138 | + if err != nil { |
| 139 | + w.WriteHeader(http.StatusInternalServerError) |
| 140 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Failed to retrieve group members roster"}) |
| 141 | + return |
| 142 | + } |
| 143 | + |
| 144 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: true, Data: members}) |
| 145 | +} |
| 146 | + |
| 147 | +func HandleLeaveGroup(w http.ResponseWriter, r *http.Request) { |
| 148 | + w.Header().Set("Content-Type", "application/json") |
| 149 | + groupID := chi.URLParam(r, "id") |
| 150 | + uid, _ := r.Context().Value(middlewares.UserIDKey).(string) |
| 151 | + |
| 152 | + repo := repository.NewGroupRepository() |
| 153 | + |
| 154 | + if err := repo.RemoveMember(r.Context(), groupID, uid); err != nil { |
| 155 | + w.WriteHeader(http.StatusInternalServerError) |
| 156 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Failed to leave the group"}) |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: true, Data: "You have left the group"}) |
| 161 | +} |
| 162 | + |
| 163 | +func HandleGetGroupDetails(w http.ResponseWriter, r *http.Request) { |
| 164 | + w.Header().Set("Content-Type", "application/json") |
| 165 | + groupID := chi.URLParam(r, "id") |
| 166 | + |
| 167 | + repo := repository.NewGroupRepository() |
| 168 | + group, err := repo.GetByID(r.Context(), groupID) |
| 169 | + if err != nil { |
| 170 | + w.WriteHeader(http.StatusNotFound) |
| 171 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Group not found"}) |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: true, Data: group}) |
| 176 | +} |
| 177 | + |
| 178 | +type UpdateGroupReq struct { |
| 179 | + Name string `json:"name"` |
| 180 | +} |
| 181 | + |
| 182 | +func HandleUpdateGroup(w http.ResponseWriter, r *http.Request) { |
| 183 | + w.Header().Set("Content-Type", "application/json") |
| 184 | + groupID := chi.URLParam(r, "id") |
| 185 | + |
| 186 | + var req UpdateGroupReq |
| 187 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.Name == "" { |
| 188 | + w.WriteHeader(http.StatusBadRequest) |
| 189 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Invalid group name"}) |
| 190 | + return |
| 191 | + } |
| 192 | + |
| 193 | + repo := repository.NewGroupRepository() |
| 194 | + if err := repo.UpdateName(r.Context(), groupID, req.Name); err != nil { |
| 195 | + w.WriteHeader(http.StatusInternalServerError) |
| 196 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: false, Error: "Failed to update group name"}) |
| 197 | + return |
| 198 | + } |
| 199 | + |
| 200 | + _ = json.NewEncoder(w).Encode(models.JSONResponse{Success: true, Data: "Group details updated successfully"}) |
| 201 | +} |
0 commit comments