/// <summary> /// Gets the forum user info as JSON string for the hover cards /// </summary> /// <param name="context">The context.</param> public void GetUserInfo([NotNull] HttpContext context) { try { var userId = context.Request.QueryString.GetFirstOrDefaultAs <int>("userinfo"); var user = this.GetRepository <User>().GetBoardUser(userId); if (user == null || user.Item1.ID == 0) { context.Response.Write( "Error: Resource has been moved or is unavailable. Please contact the forum admin."); return; } // Check if user has access if (!this.Get <IPermissions>().Check(this.Get <BoardSettings>().ProfileViewPermissions)) { context.Response.Write(string.Empty); return; } context.Response.Clear(); context.Response.ContentType = "application/json"; context.Response.ContentEncoding = Encoding.UTF8; context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetExpires( System.DateTime.UtcNow.AddMilliseconds(BoardContext.Current.Get <BoardSettings>().OnlineStatusCacheTimeout)); context.Response.Cache.SetLastModified(System.DateTime.UtcNow); var avatarUrl = this.Get <IAvatars>().GetAvatarUrlForUser(user.Item1); avatarUrl = avatarUrl.IsNotSet() ? $"{BoardInfo.ForumClientFileRoot}images/noavatar.svg" : avatarUrl; var activeUsers = this.Get <IDataCache>().GetOrSet( Constants.Cache.UsersOnlineStatus, () => this.GetRepository <Active>().ListAsDataTable( false, this.Get <BoardSettings>().ShowCrawlersInActiveList, this.Get <BoardSettings>().ActiveListTime, this.Get <BoardSettings>().UseStyledNicks), TimeSpan.FromMilliseconds(BoardContext.Current.Get <BoardSettings>().OnlineStatusCacheTimeout)); var userIsOnline = activeUsers.AsEnumerable().Any( x => x.Field <int>("UserId").Equals(userId) && !x.Field <bool>("IsHidden")); var userName = this.Get <IUserDisplayName>().GetName(user.Item1); userName = HttpUtility.HtmlEncode(userName); var location = user.Item2.Profile_Country.IsSet() ? BoardContext.Current.Get <IHaveLocalization>().GetText( "COUNTRY", user.Item2.Profile_Country.Trim()) : user.Item2.Profile_Location; if (user.Item2.Profile_Region.IsSet() && user.Item2.Profile_Country.IsSet()) { var tag = $"RGN_{user.Item2.Profile_Country.Trim()}_{user.Item2.Profile_Region}"; location += $", {this.Get<IHaveLocalization>().GetText("REGION", tag)}"; } var userInfo = new ForumUserInfo { Name = userName, RealName = HttpUtility.HtmlEncode(user.Item2.Profile_RealName), Avatar = avatarUrl, Interests = HttpUtility.HtmlEncode(user.Item2.Profile_Interests), HomePage = user.Item2.Profile_Homepage, Posts = $"{user.Item1.NumPosts:N0}", Rank = user.Item3.Name, Location = location, Joined = $"{this.Get<IHaveLocalization>().GetText("PROFILE", "JOINED")} {this.Get<IDateTime>().FormatDateLong(user.Item1.Joined)}", Online = userIsOnline }; if (BoardContext.Current.Get <BoardSettings>().EnableUserReputation) { userInfo.Points = (user.Item1.Points > 0 ? "+" : string.Empty) + user.Item1.Points; } context.Response.Write(userInfo.ToJson()); HttpContext.Current.ApplicationInstance.CompleteRequest(); } catch (Exception x) { this.Get <ILogger>().Log(this.Get <IUserDisplayName>().GetName(BoardContext.Current.CurrentUser), this, x, EventLogTypes.Information); context.Response.Write( "Error: Resource has been moved or is unavailable. Please contact the forum admin."); } }
/// <summary> /// Gets the forum user info as JSON string for the hover cards /// </summary> /// <param name="context">The context.</param> public void GetUserInfo([NotNull] HttpContext context) { try { var userId = context.Request.QueryString.GetFirstOrDefaultAs <int>("userinfo"); var boardId = context.Request.QueryString.GetFirstOrDefaultAs <int>("boardId"); var user = UserMembershipHelper.GetMembershipUserById(userId, boardId); if (user == null || user.ProviderUserKey.ToString() == "0") { context.Response.Write( "Error: Resource has been moved or is unavailable. Please contact the forum admin."); return; } // Check if user has access if (!this.Get <IPermissions>().Check(this.Get <BoardSettings>().ProfileViewPermissions)) { context.Response.Write(string.Empty); return; } var userData = new CombinedUserDataHelper(user, userId); context.Response.Clear(); context.Response.ContentType = "application/json"; context.Response.ContentEncoding = Encoding.UTF8; context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetExpires( DateTime.UtcNow.AddMilliseconds(YafContext.Current.Get <BoardSettings>().OnlineStatusCacheTimeout)); context.Response.Cache.SetLastModified(DateTime.UtcNow); var avatarUrl = this.Get <IAvatars>().GetAvatarUrlForUser(userId); avatarUrl = avatarUrl.IsNotSet() ? $"{BoardInfo.ForumClientFileRoot}images/noavatar.svg" : avatarUrl; var activeUsers = this.Get <IDataCache>().GetOrSet( Constants.Cache.UsersOnlineStatus, () => this.Get <YafDbBroker>().GetActiveList( false, YafContext.Current.Get <BoardSettings>().ShowCrawlersInActiveList), TimeSpan.FromMilliseconds(YafContext.Current.Get <BoardSettings>().OnlineStatusCacheTimeout)); var userIsOnline = activeUsers.AsEnumerable().Any( x => x.Field <int>("UserId").Equals(userId) && !x.Field <bool>("IsHidden")); var userName = this.Get <BoardSettings>().EnableDisplayName ? userData.DisplayName : userData.UserName; userName = HttpUtility.HtmlEncode(userName); var location = userData.Profile.Country.IsSet() ? YafContext.Current.Get <IHaveLocalization>().GetText( "COUNTRY", userData.Profile.Country.Trim()) : userData.Profile.Location; if (userData.Profile.Region.IsSet() && userData.Profile.Country.IsSet()) { var tag = $"RGN_{userData.Profile.Country.Trim()}_{userData.Profile.Region}"; location += $", {YafContext.Current.Get<IHaveLocalization>().GetText("REGION", tag)}"; } var userInfo = new ForumUserInfo { Name = userName, RealName = HttpUtility.HtmlEncode(userData.Profile.RealName), Avatar = avatarUrl, Interests = HttpUtility.HtmlEncode(userData.Profile.Interests), HomePage = userData.Profile.Homepage, Posts = $"{userData.NumPosts:N0}", Rank = userData.RankName, Location = location, Joined = this.Get <IDateTime>().FormatDateLong(userData.Joined), Online = userIsOnline }; if (YafContext.Current.Get <BoardSettings>().EnableUserReputation) { userInfo.Points = (userData.Points.ToType <int>() > 0 ? "+" : string.Empty) + userData.Points; } context.Response.Write(userInfo.ToJson()); HttpContext.Current.ApplicationInstance.CompleteRequest(); } catch (Exception x) { this.Get <ILogger>().Log(YafContext.Current.PageUserID, this, x, EventLogTypes.Information); context.Response.Write( "Error: Resource has been moved or is unavailable. Please contact the forum admin."); } }