コード例 #1
0
ファイル: PostService.cs プロジェクト: JuganD/OurTrace
        public async Task <bool> CreateNewPostAsync(string username, CreatePostInputModel model, bool saveMedia)
        {
            if (await IsUserCanPostToWallAsync(username, model.Location))
            {
                var wall = await wallService
                           .GetWallWithIncludables(model.Location)
                           .SingleOrDefaultAsync();

                var tags = GetPostTags(model.Tags);

                Post post = automapper.Map <Post>(model);
                post.Location = wall;
                post.User     = await identityService.GetUserByName(username).SingleOrDefaultAsync();

                post.Tags = tags;

                if (await wallService.IsWallBelongsToGroup(model.Location))
                {
                    post.VisibilityType = PostVisibilityType.Public;
                }

                // FILE SAVING PROCEDURE
                if (saveMedia)
                {
                    if (model.MediaFile != null && model.MediaFile.Length > 0)
                    {
                        await this.fileService.SaveImageAsync(model.MediaFile, username, post.Id);

                        post.IsImageOnFileSystem = true;
                    }
                    else if (model.ExternalMediaUrl != null)
                    {
                        post.MediaUrl = model.ExternalMediaUrl;
                    }
                }



                wall.Posts.Add(post);
                await this.dbContext.SaveChangesAsync();

                return(true);
            }
            return(false);
        }
コード例 #2
0
        public async Task AddFriendshipAsync(string senderUsername, string receiverUsername)
        {
            if (await AreFriendsWithAsync(senderUsername, receiverUsername))
            {
                return;
            }

            var friendship = await GetFriendshipAsync(senderUsername, receiverUsername);

            if (friendship != null)
            {
                friendship.AcceptedOn = DateTime.UtcNow;
                await this.dbContext.SaveChangesAsync();

                return;
            }

            var sender = await identityService.GetUserByName(senderUsername).SingleOrDefaultAsync();

            var receiver = await identityService.GetUserByName(receiverUsername).SingleOrDefaultAsync();


            if (sender != null && receiver != null)
            {
                if (friendship != null)
                {
                    friendship.AcceptedOn = DateTime.UtcNow;
                }
                else
                {
                    await this.dbContext.Friendships.AddAsync(new Friendship()
                    {
                        Sender    = sender,
                        Recipient = receiver
                    });
                }
                await this.dbContext.SaveChangesAsync();
            }
        }
コード例 #3
0
        public async Task MarkAllNotificationsAsSeenAsync(string username)
        {
            var user = await identityService.GetUserByName(username)
                       .Include(x => x.Notifications)
                       .SingleOrDefaultAsync();

            if (user != null)
            {
                foreach (var notification in user.Notifications.Where(x => x.Seen == false))
                {
                    notification.Seen = true;
                }
                await this.dbContext.SaveChangesAsync();
            }
        }
コード例 #4
0
        public async Task <bool> CreateNewGroupAsync(string name, string ownerUsername)
        {
            var user = await identityService.GetUserByName(ownerUsername).SingleOrDefaultAsync();

            if (user != null)
            {
                if (!await GroupExistAsync(name))
                {
                    Group group = new Group()
                    {
                        Creator = user,
                        Name    = name,
                        Url     = StringifyGroupName(name)
                    };
                    this.dbContext.Groups.Add(group);
                    this.dbContext.UserGroups.Add(new UserGroup()
                    {
                        Group           = group,
                        User            = user,
                        ConfirmedMember = true
                    });
                    this.dbContext.GroupAdmins.Add(new GroupAdmin()
                    {
                        Group     = group,
                        User      = user,
                        AdminType = GroupAdminType.Admin
                    });
                    await this.dbContext.SaveChangesAsync();

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(false);
        }
コード例 #5
0
ファイル: UserService.cs プロジェクト: JuganD/OurTrace
        public async Task <ProfileViewModel> PrepareUserProfileForViewAsync(string actualUserName, string visitingUserName)
        {
            var visitingUser = await IncludeAllProfileDetails(identityService.GetUserByName(visitingUserName))
                               .SingleOrDefaultAsync();

            if (visitingUser == null)
            {
                return(null);
            }

            ProfileViewModel model = mapper.Map <ProfileViewModel>(visitingUser);

            model.Posts = mapper.Map <ICollection <PostViewModel> >(await wallService.GetPostsFromWallDescendingAsync(model.WallId));

            if (actualUserName != visitingUser.UserName)
            {
                var actualUser = await IncludeFriendship(identityService.GetUserByName(actualUserName))
                                 .SingleOrDefaultAsync();

                if (await relationsService.AreFriendsWithAsync(actualUserName, visitingUser.UserName))
                {
                    model.Posts = model.Posts
                                  .Where(x => x.VisibilityType == PostVisibilityType.FriendsOnly ||
                                         x.VisibilityType == PostVisibilityType.Public)
                                  .ToList();
                    model.AreFriends = true;
                }
                else
                {
                    model.Posts = model.Posts
                                  .Where(x => x.VisibilityType == PostVisibilityType.Public)
                                  .ToList();

                    if (actualUser.SentFriendships.Any(x =>
                                                       x.Recipient == visitingUser && x.AcceptedOn == null))
                    {
                        model.PendingFriendship = true;
                    }
                    if (actualUser.ReceivedFriendships.Any(x =>
                                                           x.Sender == visitingUser && x.AcceptedOn == null))
                    {
                        model.CanAcceptFriendship = true;
                    }
                }

                if (await relationsService.IsFollowingAsync(actualUser, visitingUser))
                {
                    model.IsFollowing = true;
                }
            }
            else
            {
                model.FriendSuggestions = await relationsService.GetFriendsOfFriendsAsync(actualUserName, 15);

                model.IsHimself = true;
            }

            if (model.IsHimself || model.AreFriends)
            {
                model.LastPictures = await GetLastNPicturesAsync(visitingUserName, 15);
            }

            return(model);
        }