Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ID-519]Consolidate the information, and make it accessible with a single API call #520

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- Consolidate the information, and make it accessible with a single API call [#519](https://github.com/rokwire/groups-building-block/issues/519)

## [1.55.0] - 2024-11-13
### Added
- BBs API to Get groups by group_ids [#521] (https://github.com/rokwire/groups-building-block/issues/521)
Expand Down
8 changes: 8 additions & 0 deletions core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Services interface {
GetGroupMembershipsByGroupID(groupID string) ([]string, error)

GetGroupsEvents(eventIDs []string) ([]model.GetGroupsEvents, error)
GetUserData(userID string) (*model.UserDataResponse, error)

GetPosts(clientID string, current *model.User, filter model.PostsFilter, filterPrivatePostsValue *bool, filterByToMembers bool) ([]model.Post, error)
GetPost(clientID string, userID *string, groupID string, postID string, skipMembershipCheck bool, filterByToMembers bool) (*model.Post, error)
Expand Down Expand Up @@ -226,6 +227,10 @@ func (s *servicesImpl) GetGroupsByGroupIDs(groupIDs []string) ([]model.Group, er
return s.app.findGroupsByGroupIDs(groupIDs)
}

func (s *servicesImpl) GetUserData(userID string) (*model.UserDataResponse, error) {
return s.app.getUserData(userID)
}

func (s *servicesImpl) GetPosts(clientID string, current *model.User, filter model.PostsFilter, filterPrivatePostsValue *bool, filterByToMembers bool) ([]model.Post, error) {
return s.app.getPosts(clientID, current, filter, filterPrivatePostsValue, filterByToMembers)
}
Expand Down Expand Up @@ -441,8 +446,10 @@ type Storage interface {
PullMembersFromEventsByUserIDs(log *logs.Logger, context storage.TransactionContext, accountsIDs []string) error

FindEventUserIDs(context storage.TransactionContext, eventID string) ([]string, error)
GetEventByUserID(userID string) ([]model.Event, error)
FindGroupMembershipStatusAndGroupTitle(context storage.TransactionContext, userID string) ([]model.GetGroupMembershipsResponse, error)
FindGroupMembershipByGroupID(context storage.TransactionContext, groupID string) ([]string, error)
GetGroupMembershipByUserID(userID string) ([]model.GroupMembership, error)

FindGroupsEvents(context storage.TransactionContext, eventIDs []string) ([]model.GetGroupsEvents, error)

Expand All @@ -452,6 +459,7 @@ type Storage interface {
FindPosts(clientID string, current *model.User, filter model.PostsFilter, filterPrivatePostsValue *bool, filterByToMembers bool) ([]model.Post, error)
FindPost(context storage.TransactionContext, clientID string, userID *string, groupID string, postID string, skipMembershipCheck bool, filterByToMembers bool) (*model.Post, error)
FindPostsByParentID(context storage.TransactionContext, clientID string, userID *string, groupID string, parentID string, skipMembershipCheck bool, filterByToMembers bool, recursive bool, order *string) ([]model.Post, error)
GetPostsByUserID(userID string) ([]model.Post, error)

CreatePost(clientID string, current *model.User, post *model.Post) (*model.Post, error)
UpdatePost(clientID string, userID string, post *model.Post) (*model.Post, error)
Expand Down
31 changes: 31 additions & 0 deletions core/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,34 @@ type DeletedMembership struct {
AccountID string `json:"account_id"`
Context *map[string]interface{} `json:"context,omitempty"`
}

// UserDataResponse defines the user data
type UserDataResponse struct {
EventResponse []EventResponse `json:"events"`
GroupMembershipsResponse []GroupMembershipResponse `json:"group_memberships"`
GroupResponse []GroupResponse `json:"groups"`
PostResponse []PostResponse `json:"posts"`
}

// EventResponse represents event entity
type EventResponse struct {
UserID string `json:"user_id" bson:"user_id"`
EventID string `json:"event_id" bson:"event_id"`
} // @name EventResponse

// GroupMembershipResponse represents the membership of a user to a given group
type GroupMembershipResponse struct {
ID string `json:"id" bson:"_id"`
UserID string `json:"user_id" bson:"user_id"`
} //@name GroupMembershipResponse

// GroupResponse represents the user groups
type GroupResponse struct {
ID string `json:"id" bson:"_id"`
} //@name GroupResponse

// PostResponse represents the user posts
type PostResponse struct {
ID string `json:"id" bson:"_id"`
UserID string `json:"user_id" bson:"user_id"`
} //@name PostResponse
85 changes: 85 additions & 0 deletions core/services_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"groups/driven/storage"
"log"
"strings"
"sync"
)

func (app *Application) findAdminGroupsForEvent(clientID string, current *model.User, eventID string) ([]string, error) {
Expand All @@ -46,6 +47,90 @@ func (app *Application) findGroupsEvents(eventIDs []string) ([]model.GetGroupsEv
return app.storage.FindGroupsEvents(nil, eventIDs)
}

func (app *Application) getUserData(userID string) (*model.UserDataResponse, error) {
var (
eventResponse []model.EventResponse
groupMembershipResponse []model.GroupMembershipResponse
groupResponse []model.GroupResponse
postResponse []model.PostResponse
mu sync.Mutex // Mutex to safely update shared slices
wg sync.WaitGroup // WaitGroup to wait for all goroutines
errChan = make(chan error, 3) // Channel for error handling
)

// Fetch events asynchronously
wg.Add(1)
go func() {
defer wg.Done()
events, err := app.storage.GetEventByUserID(userID)
if err != nil {
errChan <- err
return
}
if events != nil {
mu.Lock()
for _, ev := range events {
eventResponse = append(eventResponse, model.EventResponse{UserID: userID, EventID: ev.EventID})
}
mu.Unlock()
}
}()

// Fetch group memberships asynchronously
wg.Add(1)
go func() {
defer wg.Done()
groupMemberships, err := app.storage.GetGroupMembershipByUserID(userID)
if err != nil {
errChan <- err
return
}
if groupMemberships != nil {
mu.Lock()
for _, gr := range groupMemberships {
groupMembershipResponse = append(groupMembershipResponse, model.GroupMembershipResponse{ID: gr.ID, UserID: gr.UserID})
groupResponse = append(groupResponse, model.GroupResponse{ID: gr.GroupID})
}
mu.Unlock()
}
}()

// Fetch posts asynchronously
wg.Add(1)
go func() {
defer wg.Done()
posts, err := app.storage.GetPostsByUserID(userID)
if err != nil {
errChan <- err
return
}
if posts != nil {
mu.Lock()
for _, p := range posts {
postResponse = append(postResponse, model.PostResponse{ID: p.ID, UserID: userID})
}
mu.Unlock()
}
}()

// Wait for all goroutines to finish
wg.Wait()
close(errChan) // Close error channel once all goroutines complete

// Check if any errors were received
if len(errChan) > 0 {
return nil, <-errChan // Return the first error encountered
}

// Create and return the final response
userData := model.UserDataResponse{
EventResponse: eventResponse,
GroupMembershipsResponse: groupMembershipResponse,
GroupResponse: groupResponse,
PostResponse: postResponse,
}
return &userData, nil
}
func (app *Application) findGroupsByGroupIDs(groupIDs []string) ([]model.Group, error) {
return app.storage.FindGroupsByGroupIDs(groupIDs)
}
Expand Down
Loading
Loading