This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

feat: implement getting receivers for a bunch of event types

+254
+254
users.go
··· 34 34 switch message.Event { 35 35 case EventNewPost: 36 36 return receiversForPost(message) 37 + case EventNewComment: 38 + return receiversForNewComment(message) 39 + case EventBookingPaid: 40 + return receiversForBookingPaid(message) 41 + case EventCommentReply: 42 + return receiversForCommentReply(message) 43 + case EventContributionPaid: 44 + return receiversForContributionPaid(message) 45 + case EventGodchildAccepted: 46 + case EventGodchildRejected: 47 + return receiversForGodchildResponse(message) 48 + case EventGodchildRequest: 49 + return receiversForGodchildRequest(message) 50 + case EventShotgunOpensSoon: 51 + return receiversForShotgunOpens(message) 52 + case EventShotgunClosesSoon: 53 + return receiversForShotgunCloses(message) 54 + } 55 + 56 + // For other events, assume the message churros object id is the user id 57 + if message.ChurrosObjectId != "" { 58 + _, err := prisma.User.FindUnique( 59 + db.User.ID.Equals(message.ChurrosObjectId), 60 + ).Exec(context.Background()) 61 + if err != nil { 62 + return []string{message.ChurrosObjectId}, nil 63 + } 37 64 } 38 65 39 66 return []string{}, nil ··· 84 111 85 112 return userIds, fmt.Errorf("unknown post visibility %q", post.Visibility) 86 113 } 114 + 115 + func receiversForNewComment(message Message) (userIds []string, err error) { 116 + comment, err := prisma.Comment.FindUnique( 117 + db.Comment.ID.Equals(message.ChurrosObjectId), 118 + ).With( 119 + db.Comment.Article.Fetch(), 120 + ).Exec(context.Background()) 121 + if err != nil { 122 + err = fmt.Errorf("while getting comment: %w", err) 123 + return 124 + } 125 + 126 + post, ok := comment.Article() 127 + if !ok { 128 + err = fmt.Errorf("comment %q has no parent post", comment.ID) 129 + return 130 + } 131 + 132 + authorId, ok := post.AuthorID() 133 + if !ok { 134 + err = fmt.Errorf("post %q has no author", post.ID) 135 + return 136 + } 137 + 138 + return []string{authorId}, nil 139 + } 140 + 141 + func receiversForBookingPaid(message Message) (userIds []string, err error) { 142 + booking, err := prisma.Registration.FindUnique( 143 + db.Registration.ID.Equals(message.ChurrosObjectId), 144 + ).Exec(context.Background()) 145 + 146 + if err != nil { 147 + err = fmt.Errorf("while getting booking: %w", err) 148 + return 149 + } 150 + 151 + authorId, ok := booking.AuthorID() 152 + if ok { 153 + userIds = append(userIds, authorId) 154 + } 155 + 156 + beneficiaryId, ok := booking.InternalBeneficiaryID() 157 + if ok { 158 + userIds = append(userIds, beneficiaryId) 159 + } 160 + 161 + return 162 + } 163 + 164 + func receiversForCommentReply(message Message) (userIds []string, err error) { 165 + comment, err := prisma.Comment.FindUnique( 166 + db.Comment.ID.Equals(message.ChurrosObjectId), 167 + ).With( 168 + db.Comment.InReplyTo.Fetch(), 169 + ).Exec(context.Background()) 170 + if err != nil { 171 + err = fmt.Errorf("while getting comment: %w", err) 172 + return 173 + } 174 + 175 + parent, ok := comment.InReplyTo() 176 + if !ok { 177 + err = fmt.Errorf("comment %q has no parent", comment.ID) 178 + return 179 + } 180 + 181 + authorId, ok := parent.AuthorID() 182 + if !ok { 183 + err = fmt.Errorf("comment %q has no author", parent.ID) 184 + return 185 + } 186 + 187 + return []string{authorId}, nil 188 + } 189 + 190 + func receiversForContributionPaid(message Message) (userIds []string, err error) { 191 + contribution, err := prisma.Contribution.FindUnique( 192 + db.Contribution.ID.Equals(message.ChurrosObjectId), 193 + ).Exec(context.Background()) 194 + 195 + if err != nil { 196 + err = fmt.Errorf("while getting contribution: %w", err) 197 + return 198 + } 199 + 200 + return []string{contribution.UserID}, nil 201 + } 202 + 203 + func receiversForGodchildResponse(message Message) (userIds []string, err error) { 204 + godchildRequest, err := prisma.GodparentRequest.FindUnique( 205 + db.GodparentRequest.ID.Equals(message.ChurrosObjectId), 206 + ).Exec(context.Background()) 207 + 208 + if err != nil { 209 + err = fmt.Errorf("while getting godchild request: %w", err) 210 + return 211 + } 212 + 213 + return []string{godchildRequest.GodchildID}, nil 214 + } 215 + 216 + func receiversForGodchildRequest(message Message) (userIds []string, err error) { 217 + godchildRequest, err := prisma.GodparentRequest.FindUnique( 218 + db.GodparentRequest.ID.Equals(message.ChurrosObjectId), 219 + ).Exec(context.Background()) 220 + 221 + if err != nil { 222 + err = fmt.Errorf("while getting godchild request: %w", err) 223 + return 224 + } 225 + 226 + return []string{godchildRequest.GodparentID}, nil 227 + } 228 + 229 + func receiversForShotgunOpens(message Message) (userIds []string, err error) { 230 + shotgun, err := prisma.Event.FindUnique( 231 + db.Event.ID.Equals(message.ChurrosObjectId), 232 + ).With( 233 + db.Event.Group.Fetch().With( 234 + db.Group.Members.Fetch().Select( 235 + db.GroupMember.MemberID.Field(), 236 + ), 237 + db.Group.StudentAssociation.Fetch().With( 238 + db.StudentAssociation.School.Fetch().With( 239 + db.School.Majors.Fetch().With( 240 + db.Major.Students.Fetch().Select( 241 + db.User.ID.Field(), 242 + ), 243 + ), 244 + ), 245 + ), 246 + ), 247 + ).Exec(context.Background()) 248 + 249 + switch shotgun.Visibility { 250 + case db.VisibilityPublic: 251 + return AllUsers() 252 + case db.VisibilitySchoolRestricted: 253 + for _, major := range shotgun.Group().StudentAssociation().School().Majors() { 254 + for _, student := range major.Students() { 255 + userIds = append(userIds, student.ID) 256 + } 257 + } 258 + return 259 + case db.VisibilityGroupRestricted: 260 + for _, member := range shotgun.Group().Members() { 261 + userIds = append(userIds, member.MemberID) 262 + } 263 + return 264 + } 265 + 266 + return 267 + } 268 + 269 + func receiversForShotgunCloses(message Message) (userIds []string, err error) { 270 + shotgun, err := prisma.Event.FindUnique( 271 + db.Event.ID.Equals(message.ChurrosObjectId), 272 + ).With( 273 + db.Event.Group.Fetch().With( 274 + db.Group.Members.Fetch().Select( 275 + db.GroupMember.MemberID.Field(), 276 + ), 277 + db.Group.StudentAssociation.Fetch().With( 278 + db.StudentAssociation.School.Fetch().With( 279 + db.School.Majors.Fetch().With( 280 + db.Major.Students.Fetch().Select( 281 + db.User.ID.Field(), 282 + ), 283 + ), 284 + ), 285 + ), 286 + ), 287 + db.Event.Tickets.Fetch().With( 288 + db.Ticket.Registrations.Fetch().Select( 289 + db.Registration.AuthorID.Field(), 290 + db.Registration.InternalBeneficiaryID.Field(), 291 + ), 292 + ), 293 + ).Exec(context.Background()) 294 + 295 + switch shotgun.Visibility { 296 + case db.VisibilityPublic: 297 + return AllUsers() 298 + case db.VisibilitySchoolRestricted: 299 + for _, major := range shotgun.Group().StudentAssociation().School().Majors() { 300 + for _, student := range major.Students() { 301 + userIds = append(userIds, student.ID) 302 + } 303 + } 304 + return 305 + case db.VisibilityGroupRestricted: 306 + for _, member := range shotgun.Group().Members() { 307 + userIds = append(userIds, member.MemberID) 308 + } 309 + return 310 + } 311 + 312 + // Remove users that are booked to the event 313 + 314 + usersToRemove := make([]string, 0) 315 + 316 + for _, ticket := range shotgun.Tickets() { 317 + for _, registration := range ticket.Registrations() { 318 + authorId, ok := registration.AuthorID() 319 + if ok { 320 + usersToRemove = append(usersToRemove, authorId) 321 + } 322 + 323 + beneficiaryId, ok := registration.InternalBeneficiaryID() 324 + if ok { 325 + usersToRemove = append(usersToRemove, beneficiaryId) 326 + } 327 + } 328 + } 329 + 330 + for _, user := range usersToRemove { 331 + for i, id := range userIds { 332 + if id == user { 333 + userIds = append(userIds[:i], userIds[i+1:]...) 334 + break 335 + } 336 + } 337 + } 338 + 339 + return 340 + }