public PrimitiveApplicationInfo(Core core, Primitive owner, long applicationId) : base(core) { this.owner = owner; ItemLoad += new ItemLoadHandler(PrimitiveApplicationInfo_ItemLoad); SelectQuery query = new SelectQuery(PrimitiveApplicationInfo.GetTable(typeof(PrimitiveApplicationInfo))); query.AddFields(PrimitiveApplicationInfo.GetFieldsPrefixed(core, typeof(PrimitiveApplicationInfo))); query.AddCondition("application_id", applicationId); query.AddCondition("item_id", owner.Id); query.AddCondition("item_type_id", owner.TypeId); DataTable appDataTable = db.Query(query); if (appDataTable.Rows.Count == 1) { DataRow appRow = appDataTable.Rows[0]; try { loadItemInfo(appRow); } catch (InvalidItemException) { throw new InvalidPrimitiveAppInfoException(); } } else { throw new InvalidPrimitiveAppInfoException(); } }
public static SelectQuery GetSelectQueryStub(Core core, UserLoadOptions loadOptions) { long typeId = ItemType.GetTypeId(core, typeof(User)); /*if (loadOptions == UserLoadOptions.All && QueryCache.HasQuery(typeId)) { return (SelectQuery)QueryCache.GetQuery(typeof(User), typeId); } else*/ { #if DEBUG Stopwatch httpTimer = new Stopwatch(); httpTimer.Start(); #endif SelectQuery query = new SelectQuery(GetTable(typeof(User))); query.AddFields(User.GetFieldsPrefixed(core, typeof(User))); query.AddFields(ItemInfo.GetFieldsPrefixed(core, typeof(ItemInfo))); TableJoin join = query.AddJoin(JoinTypes.Left, new DataField(typeof(User), "user_id"), new DataField(typeof(ItemInfo), "info_item_id")); join.AddCondition(new DataField(typeof(ItemInfo), "info_item_type_id"), ItemKey.GetTypeId(core, typeof(User))); if ((loadOptions & UserLoadOptions.Info) == UserLoadOptions.Info) { query.AddJoin(JoinTypes.Inner, UserInfo.GetTable(typeof(UserInfo)), "user_id", "user_id"); query.AddFields(UserInfo.GetFieldsPrefixed(core, typeof(UserInfo))); /*if ((loadOptions & UserLoadOptions.Icon) == UserLoadOptions.Icon) { query.AddJoin(JoinTypes.Left, new DataField("user_info", "user_icon"), new DataField("gallery_items", "gallery_item_id")); query.AddField(new DataField("gallery_items", "gallery_item_uri")); query.AddField(new DataField("gallery_items", "gallery_item_parent_path")); }*/ } if ((loadOptions & UserLoadOptions.Profile) == UserLoadOptions.Profile) { query.AddJoin(JoinTypes.Inner, UserProfile.GetTable(typeof(UserProfile)), "user_id", "user_id"); query.AddFields(UserProfile.GetFieldsPrefixed(core, typeof(UserProfile))); // Countries are cached separately as they do not change /*if ((loadOptions & UserLoadOptions.Country) == UserLoadOptions.Country) { query.AddJoin(JoinTypes.Left, new DataField("user_profile", "profile_country"), new DataField("countries", "country_iso")); }*/ if ((loadOptions & UserLoadOptions.Religion) == UserLoadOptions.Religion) { query.AddJoin(JoinTypes.Left, new DataField("user_profile", "profile_religion"), new DataField("religions", "religion_id")); } } #if DEBUG httpTimer.Stop(); if (HttpContext.Current != null && core.Session != null && core.Session.SessionMethod != SessionMethods.OAuth) { HttpContext.Current.Response.Write(string.Format("<!-- Build user query stub in {0} -->\r\n", httpTimer.ElapsedTicks / 10000000.0)); } #endif /*if (loadOptions == UserLoadOptions.All) { QueryCache.AddQueryToCache(typeId, query); }*/ return query; } }
public List<NetworkMember> GetMembers(int page, int perPage, string filter) { List<NetworkMember> members = new List<NetworkMember>(); SelectQuery query = new SelectQuery(NetworkMember.GetTable(typeof(NetworkMember))); query.AddFields(NetworkMember.GetFieldsPrefixed(core, typeof(NetworkMember))); query.AddCondition("network_id", networkId); query.AddSort(SortOrder.Ascending, "member_join_date_ut"); if (!string.IsNullOrEmpty(filter)) { query.AddCondition(new DataField("user_keys", "user_name_first"), filter[0]); } query.LimitStart = (page - 1) * perPage; query.LimitCount = perPage; DataTable membersTable = db.Query(query); List<long> memberIds = new List<long>(); foreach (DataRow dr in membersTable.Rows) { memberIds.Add((long)dr["user_id"]); } core.LoadUserProfiles(memberIds); foreach (DataRow dr in membersTable.Rows) { members.Add(new NetworkMember(core, dr)); } return members; }
public Dictionary<string, long> LoadUserProfiles(List<string> usernames) { long userTypeId = ItemKey.GetTypeId(core, typeof(User)); List<string> usernameList = new List<string>(); Dictionary<string, long> userIds = new Dictionary<string, long>(8, StringComparer.Ordinal); foreach (string username in usernames) { PrimitiveKey key = new PrimitiveKey(username.ToLower(), userTypeId); if (!primitivesKeysCached.ContainsKey(key)) { usernameList.Add(username.ToLower()); } } if (usernameList.Count > 0) { SelectQuery query = new SelectQuery("user_keys"); query.AddFields(User.GetFieldsPrefixed(core, typeof(User))); query.AddFields(UserInfo.GetFieldsPrefixed(core, typeof(UserInfo))); query.AddFields(UserProfile.GetFieldsPrefixed(core, typeof(UserProfile))); query.AddJoin(JoinTypes.Inner, UserInfo.GetTable(typeof(UserInfo)), "user_id", "user_id"); query.AddJoin(JoinTypes.Inner, UserProfile.GetTable(typeof(UserProfile)), "user_id", "user_id"); query.AddJoin(JoinTypes.Left, new DataField("user_profile", "profile_country"), new DataField("countries", "country_iso")); query.AddJoin(JoinTypes.Left, new DataField("user_profile", "profile_religion"), new DataField("religions", "religion_id")); query.AddCondition(new DataField("user_keys", "user_name_lower"), ConditionEquality.In, usernameList); System.Data.Common.DbDataReader usersReader = db.ReaderQuery(query); while(usersReader.Read()) { User newUser = new User(core, usersReader, UserLoadOptions.All); // This will automatically cache itself when loadUser is called PrimitiveId pid = new PrimitiveId(newUser.Id, userTypeId); PrimitiveKey kid = new PrimitiveKey(newUser.UserName.ToLower(), userTypeId); if (!userIds.ContainsValue(newUser.Id)) { userIds.Add(newUser.UserName, newUser.Id); } } usersReader.Close(); usersReader.Dispose(); } return userIds; }
public static long GetUnseenNotificationCount(Core core) { if (core == null) { throw new NullCoreException(); } List<Notification> notificationItems = new List<Notification>(); SelectQuery query = new SelectQuery(GetTable(typeof(Notification))); query.AddFields("COUNT(*) as total"); query.AddCondition("notification_read", false); query.AddCondition("notification_seen", false); query.AddCondition("notification_primitive_id", core.LoggedInMemberId); query.AddCondition("notification_primitive_type_id", ItemKey.GetTypeId(core, typeof(User))); query.AddSort(SortOrder.Descending, "notification_time_ut"); System.Data.Common.DbDataReader notificationsReader = core.Db.ReaderQuery(query); long newNotifications = 0; if (notificationsReader.HasRows) { notificationsReader.Read(); newNotifications = (long)notificationsReader["total"]; } notificationsReader.Close(); notificationsReader.Dispose(); return newNotifications; }
void AccountPassword_Save(object sender, EventArgs e) { AuthoriseRequestSid(); string password = core.Http.Form["old-password"]; password = User.HashPassword(password); SelectQuery query = new SelectQuery(User.GetTable(typeof(User))); query.AddFields(User.GetFieldsPrefixed(core, typeof(User))); query.AddJoin(JoinTypes.Inner, new DataField(typeof(User), "user_id"), new DataField("user_info", "user_id")); query.AddCondition("user_keys.user_id", core.LoggedInMemberId); query.AddCondition("user_password", password); DataTable userTable = db.Query(query); if (userTable.Rows.Count != 1) { SetError("The old password you entered does not match your old password, make sure you have entered your old password correctly."); return; } else if (core.Http.Form["new-password"] != core.Http.Form["confirm-password"]) { SetError("The passwords you entered do not match, make sure you have entered your desired password correctly."); return; } else if (((string)core.Http.Form["new-password"]).Length < 6) { SetError("The password you entered is too short. Please choose a strong password of 6 characters or more."); return; } UpdateQuery uquery = new UpdateQuery("user_info"); uquery.AddField("user_password", User.HashPassword(core.Http.Form["new-password"])); uquery.AddCondition("user_id", core.LoggedInMemberId); long rowsChanged = db.Query(uquery); if (rowsChanged == 1) { SetInformation("You have successfully changed your password. Keep your password safe and do not share it with anyone."); //SetRedirectUri(BuildUri()); //Display.ShowMessage("Changed Password", "You have successfully changed your password. Keep your password safe and do not share it with anyone."); } else { DisplayGenericError(); return; } }
public static bool IsValidListType(Core core, short listType) { // verify that the type exists SelectQuery query = new SelectQuery("list_types"); query.AddFields("list_type_id"); query.AddCondition("list_type_id", listType); DataTable listTypeTable = core.Db.Query(query); if (listTypeTable.Rows.Count == 1) { return true; } return false; }
public static bool AuthorisedRequest(Core core) { if (core == null) { throw new NullCoreException(); } if (!core.Session.IsLoggedIn) { return false; } if (!string.IsNullOrEmpty(core.Http.Query["nvid"])) { SelectQuery query = new SelectQuery(typeof(Notification)); query.AddFields("notification_primitive_id"); query.AddFields("notification_primitive_type_id"); query.AddCondition("notification_verification_string", core.Http.Query["nvid"]); query.AddCondition("notification_primitive_id", core.Session.LoggedInMember.Id); query.AddCondition("notification_primitive_type_id", ItemType.GetTypeId(core, typeof(User))); if (core.Db.Query(query).Rows.Count == 0) { return false; } else { // This is OK return true; } } if (core.Http.Query["sid"] != core.Session.SessionId) { if (string.IsNullOrEmpty(core.Http.Query["sid"])) { return false; } SelectQuery query = new SelectQuery("user_sessions"); query.AddFields("user_id"); query.AddCondition("session_string", core.Http.Query["sid"]); query.AddCondition("user_id", core.Session.LoggedInMember.Id); query.AddCondition("session_signed_in", true); query.AddCondition("session_ip", core.Session.IPAddress.ToString()); if (core.Db.Query(query).Rows.Count == 0) { return false; } } return true; }
public void ApplicationSettings(object sender, EventArgs e) { template.SetTemplate("account_primitive_application_settings.html"); long id = core.Functions.RequestLong("id", 0); if (id == 0) { core.Display.ShowMessage("Error", "Error!"); return; } SelectQuery query = new SelectQuery("primitive_apps"); query.AddFields(ApplicationEntry.GetFieldsPrefixed(core, typeof(ApplicationEntry))); query.AddFields(PrimitiveApplicationInfo.GetFieldsPrefixed(core, typeof(PrimitiveApplicationInfo))); query.AddJoin(JoinTypes.Inner, new DataField("primitive_apps", "application_id"), new DataField("applications", "application_id")); query.AddCondition("primitive_apps.application_id", id); query.AddCondition("item_id", Owner.Id); query.AddCondition("item_type_id", Owner.TypeId); DataTable applicationTable = db.Query(query); if (applicationTable.Rows.Count == 1) { ApplicationEntry ae = new ApplicationEntry(core, applicationTable.Rows[0]); //List<string> applicationPermissions = new List<string>(); //applicationPermissions.Add("Can Access"); template.Parse("APPLICATION_NAME", ae.Title); //core.Display.ParsePermissionsBox(template, "S_GAPPLICATION_PERMS", ae.Permissions, applicationPermissions); template.Parse("S_APPLICATION_ID", ae.ApplicationId.ToString()); string radioChecked = " checked=\"checked\""; if (Owner is User) { template.Parse("S_USER", true); PrimitiveApplicationInfo ownerInfo = new PrimitiveApplicationInfo(core, Owner, ae.Id); if (ownerInfo.EmailNotifications) { template.Parse("S_EMAIL_NOTIFICATIONS_YES", radioChecked); } else { template.Parse("S_EMAIL_NOTIFICATIONS_NO", radioChecked); } } } else { core.Display.ShowMessage("Error", "Error!"); } }
/// <summary> /// Creates a new blog for the logged in user. /// </summary> /// <param name="core"></param> /// <returns></returns> public static Blog Create(Core core) { if (core == null) { throw new NullCoreException(); } SelectQuery query = new SelectQuery("user_blog ub"); query.AddFields("ub.user_id"); query.AddCondition("ub.user_id", core.LoggedInMemberId); if (core.Db.Query(query).Rows.Count == 0) { core.Db.UpdateQuery(string.Format("INSERT INTO user_blog (user_id) VALUES ({0});", core.LoggedInMemberId)); Blog newBlog = new Blog(core, core.Session.LoggedInMember); Access.CreateAllGrantsForOwner(core, newBlog); newBlog.Access.CreateGrantForPrimitive(Friend.GetFriendsGroupKey(core), "VIEW", "COMMENT_ITEMS", "RATE_ITEMS"); newBlog.Access.CreateGrantForPrimitive(User.GetEveryoneGroupKey(core), "VIEW"); return newBlog; } else { throw new CannotCreateBlogException(); } }
public void DisplayComments(Template template, Primitive owner, ICommentableItem item, List<User> commenters, int page, long commentCount, DisplayCommentHookHandler hook) { Mysql db = core.Db; long c = core.Functions.RequestLong("c", 0); if (c > 0) { SelectQuery query = new SelectQuery("comments"); query.AddFields("COUNT(*) AS total"); query.AddCondition("comment_item_id", item.Id); query.AddCondition("comment_item_type_id", item.ItemKey.TypeId); query.AddCondition("comment_id", ConditionEquality.LessThanEqual, c); if (commenters != null) { if (commenters.Count == 2) { if (item.Namespace == "USER") { QueryCondition qc1 = query.AddCondition("c.comment_item_id", commenters[0].Id); qc1.AddCondition("user_id", commenters[1].Id); QueryCondition qc2 = query.AddCondition(ConditionRelations.Or, "c.comment_item_id", commenters[1].Id); qc2.AddCondition("user_id", commenters[0].Id); query.AddCondition("c.comment_item_type_id", item.ItemKey.TypeId); } else { query.AddCondition("comment_item_id", item.Id); query.AddCondition("comment_item_type_id", item.ItemKey.TypeId); } } else { query.AddCondition("comment_item_id", item.Id); query.AddCondition("comment_item_type_id", item.ItemKey.TypeId); } } else { query.AddCondition("comment_item_id", item.Id); query.AddCondition("comment_item_type_id", item.ItemKey.TypeId); } query.AddSort(SortOrder.Ascending, "comment_time_ut"); DataRow commentsRow = db.Query(query).Rows[0]; long before = (long)commentsRow["total"]; long after = item.Comments - before - 1; if (item.CommentSortOrder == SortOrder.Ascending) { page = (int)(before / item.CommentsPerPage + 1); } else { page = (int)(after / item.CommentsPerPage + 1); } } if (core.Session.IsLoggedIn && core.Session.LoggedInMember != null) { template.Parse("LOGGED_IN", "TRUE"); template.Parse("LOGGED_IN_USER_DISPLAY_NAME", core.Session.LoggedInMember.DisplayName); template.Parse("LOGGED_IN_USER_ICON", core.Session.LoggedInMember.Icon); template.Parse("LOGGED_IN_USER_TILE", core.Session.LoggedInMember.Tile); template.Parse("LOGGED_IN_USER_SQUARE", core.Session.LoggedInMember.Square); } List<Comment> comments = Comment.GetComments(core, item.ItemKey, item.CommentSortOrder, page, item.CommentsPerPage, commenters); Comment.LoadUserInfoCache(core, comments); if (commentCount >= 0) { template.Parse("COMMENTS", commentCount.ToString()); } else if (((NumberedItem)item).Info.Comments >= 0) { template.Parse("COMMENTS", ((NumberedItem)item).Info.Comments.ToString()); } else { template.Parse("COMMENTS", comments.Count.ToString()); } template.Parse("ITEM_ID", item.Id.ToString()); template.Parse("ITEM_TYPE", item.ItemKey.TypeId.ToString()); if (item.CommentSortOrder == SortOrder.Ascending) { template.Parse("COMMENTS_ASC", "TRUE"); template.Parse("COMMENT_SORT", "asc"); } else { template.Parse("COMMENTS_DESC", "TRUE"); template.Parse("COMMENT_SORT", "desc"); } foreach (Comment comment in comments) { core.PrimitiveCache.LoadUserProfile(comment.UserId); } long lastId = 0; foreach (Comment comment in comments) { VariableCollection commentsVariableCollection = template.CreateChild("comment-list"); //commentsVariableCollection.ParseRaw("COMMENT", Bbcode.Parse(HttpUtility.HtmlEncode(comment.Body), core.session.LoggedInMember)); /*if ((!core.IsMobile) && (!string.IsNullOrEmpty(comment.BodyCache))) { core.Display.ParseBbcodeCache(commentsVariableCollection, "COMMENT", comment.BodyCache); } else*/ { core.Display.ParseBbcode(commentsVariableCollection, "COMMENT", comment.Body, true, null, null); } try { User commentPoster = core.PrimitiveCache[comment.UserId]; lastId = comment.Id; commentsVariableCollection.Parse("ID", comment.Id.ToString()); commentsVariableCollection.Parse("TYPE_ID", ItemKey.GetTypeId(core, typeof(Comment))); commentsVariableCollection.Parse("USERNAME", commentPoster.DisplayName); commentsVariableCollection.Parse("USER_ID", commentPoster.Id.ToString()); commentsVariableCollection.Parse("USER_DISPLAY_NAME", commentPoster.DisplayName); commentsVariableCollection.Parse("U_PROFILE", commentPoster.ProfileUri); commentsVariableCollection.Parse("U_QUOTE", core.Hyperlink.BuildCommentQuoteUri(comment.Id)); commentsVariableCollection.Parse("U_REPORT", core.Hyperlink.BuildCommentReportUri(comment.Id)); commentsVariableCollection.Parse("U_DELETE", core.Hyperlink.BuildCommentDeleteUri(comment.Id)); commentsVariableCollection.Parse("U_LIKE", core.Hyperlink.BuildLikeItemUri(comment.ItemTypeId, comment.Id)); commentsVariableCollection.Parse("TIME", core.Tz.DateTimeToString(comment.GetTime(core.Tz))); commentsVariableCollection.Parse("USER_ICON", commentPoster.Icon); commentsVariableCollection.Parse("USER_TILE", commentPoster.Tile); commentsVariableCollection.Parse("USER_SQUARE", commentPoster.Square); if (comment.Info.Likes > 0) { commentsVariableCollection.Parse("LIKES", string.Format("{0:d} ", comment.Info.Likes)); } if (comment.Info.Dislikes > 0) { commentsVariableCollection.Parse("DISLIKES", string.Format(" {0:d}", comment.Info.Dislikes)); } if (comment.Info.SharedTimes > 0) { commentsVariableCollection.Parse("SHARES", string.Format(" ({0:d})", comment.Info.SharedTimes)); } if (hook != null) { hook(new DisplayCommentHookEventArgs(core, owner, commentPoster, commentsVariableCollection)); } if (core.Session.IsLoggedIn) { if (owner.CanModerateComments(core.Session.LoggedInMember)) { commentsVariableCollection.Parse("MODERATE", "TRUE"); } } if (owner.IsItemOwner(commentPoster)) { commentsVariableCollection.Parse("OWNER", "TRUE"); commentsVariableCollection.Parse("NORMAL", "FALSE"); } else { commentsVariableCollection.Parse("OWNER", "FALSE"); commentsVariableCollection.Parse("NORMAL", "TRUE"); } if (comment.SpamScore >= 10) { commentsVariableCollection.Parse("IS_SPAM", "TRUE"); } } catch { // if userid is 0, anonymous commentsVariableCollection.Parse("USERNAME", "Anonymous"); commentsVariableCollection.Parse("TIME", core.Tz.DateTimeToString(comment.GetTime(core.Tz))); commentsVariableCollection.Parse("OWNER", "FALSE"); commentsVariableCollection.Parse("NORMAL", "TRUE"); } } template.Parse("LAST_ID", "lastId"); }
public User(Core core, string userName, UserLoadOptions loadOptions) : base(core) { ItemLoad += new ItemLoadHandler(User_ItemLoad); bool containsInfoData = false; bool containsProfileData = false; bool containsIconData = false; if (loadOptions == UserLoadOptions.Key) { try { LoadItem("user_name_lower", userName.ToLower(), true); } catch (InvalidItemException) { throw new InvalidUserException(); } } else { SelectQuery query = new SelectQuery(User.GetTable(typeof(User))); query.AddFields(User.GetFieldsPrefixed(core, typeof(User))); query.AddCondition(new DataField("user_keys", "user_name_lower"), userName.ToLower()); if ((loadOptions & UserLoadOptions.Info) == UserLoadOptions.Info) { containsInfoData = true; query.AddJoin(JoinTypes.Inner, UserInfo.GetTable(typeof(UserInfo)), "user_id", "user_id"); query.AddFields(UserInfo.GetFieldsPrefixed(core, typeof(UserInfo))); /*if ((loadOptions & UserLoadOptions.Icon) == UserLoadOptions.Icon) { containsIconData = true; query.AddJoin(JoinTypes.Left, new DataField("user_info", "user_icon"), new DataField("gallery_items", "gallery_item_id")); query.AddField(new DataField("gallery_items", "gallery_item_uri")); query.AddField(new DataField("gallery_items", "gallery_item_parent_path")); }*/ } if ((loadOptions & UserLoadOptions.Profile) == UserLoadOptions.Profile) { containsProfileData = true; query.AddJoin(JoinTypes.Inner, UserProfile.GetTable(typeof(UserProfile)), "user_id", "user_id"); query.AddFields(UserProfile.GetFieldsPrefixed(core, typeof(UserProfile))); if ((loadOptions & UserLoadOptions.Country) == UserLoadOptions.Country) { query.AddJoin(JoinTypes.Left, new DataField("user_profile", "profile_country"), new DataField("countries", "country_iso")); } if ((loadOptions & UserLoadOptions.Religion) == UserLoadOptions.Religion) { query.AddJoin(JoinTypes.Left, new DataField("user_profile", "profile_religion"), new DataField("religions", "religion_id")); } } DataTable memberTable = db.Query(query); if (memberTable.Rows.Count > 0) { loadItemInfo(typeof(User), memberTable.Rows[0]); if (containsInfoData) { userInfo = new UserInfo(core, memberTable.Rows[0]); } if (containsProfileData) { userProfile = new UserProfile(core, this, memberTable.Rows[0], loadOptions); } if (containsIconData) { loadUserIcon(memberTable.Rows[0]); } } else { throw new InvalidUserException(); } } }
public List<long> GetFriendsWithMeIds(int count) { List<long> friendIds = new List<long>(); SelectQuery query = new SelectQuery("user_relations uf"); query.AddFields("uf.relation_me"); query.AddCondition("uf.relation_you", userId); query.AddCondition("uf.relation_type", "FRIEND"); query.AddSort(SortOrder.Ascending, "(uf.relation_order - 1)"); query.AddSort(SortOrder.Ascending, "relation_time_ut"); query.LimitCount = count; System.Data.Common.DbDataReader friendsReader = db.ReaderQuery(query); while (friendsReader.Read()) { friendIds.Add((long)friendsReader["relation_me"]); } friendsReader.Close(); friendsReader.Dispose(); return friendIds; }
public List<Friend> GetFriends(int page, int perPage, string filter, bool sortOnline) { List<Friend> friends = new List<Friend>(); SelectQuery query = new SelectQuery("user_relations"); query.AddFields(User.GetFieldsPrefixed(core, typeof(User))); query.AddFields(UserInfo.GetFieldsPrefixed(core, typeof(UserInfo))); query.AddFields(UserProfile.GetFieldsPrefixed(core, typeof(UserProfile))); query.AddFields(UserRelation.GetFieldsPrefixed(core, typeof(UserRelation))); query.AddField(new DataField("gallery_items", "gallery_item_uri")); query.AddField(new DataField("gallery_items", "gallery_item_parent_path")); query.AddFields(ItemInfo.GetFieldsPrefixed(core, typeof(ItemInfo))); query.AddJoin(JoinTypes.Inner, User.GetTable(typeof(User)), "relation_you", "user_id"); query.AddJoin(JoinTypes.Inner, UserInfo.GetTable(typeof(UserInfo)), "relation_you", "user_id"); query.AddJoin(JoinTypes.Inner, UserProfile.GetTable(typeof(UserProfile)), "relation_you", "user_id"); query.AddJoin(JoinTypes.Left, new DataField("user_profile", "profile_country"), new DataField("countries", "country_iso")); query.AddJoin(JoinTypes.Left, new DataField("user_profile", "profile_religion"), new DataField("religions", "religion_id")); query.AddJoin(JoinTypes.Left, new DataField("user_info", "user_icon"), new DataField("gallery_items", "gallery_item_id")); TableJoin join = query.AddJoin(JoinTypes.Left, new DataField(typeof(UserRelation), "relation_you"), new DataField(typeof(ItemInfo), "info_item_id")); join.AddCondition(new DataField(typeof(ItemInfo), "info_item_type_id"), ItemKey.GetTypeId(core, typeof(User))); query.AddCondition("relation_me", userId); query.AddCondition("relation_type", "FRIEND"); if ((!string.IsNullOrEmpty(filter)) && filter.Length == 1) { query.AddCondition(new DataField(typeof(User), "user_name_first"), filter); } if (sortOnline) { query.AddSort(SortOrder.Descending, "user_last_visit_ut"); query.AddSort(SortOrder.Ascending, new QueryCondition("relation_order", ConditionEquality.Equal, 0)); } else { query.AddSort(SortOrder.Ascending, new QueryCondition("relation_order", ConditionEquality.Equal, 0)); } query.LimitStart = (page - 1) * perPage; query.LimitCount = perPage; if (sortOnline) { query.LimitOrder = SortOrder.Descending; } System.Data.Common.DbDataReader friendsReader = db.ReaderQuery(query); while (friendsReader.Read()) { friends.Add(new Friend(core, friendsReader, UserLoadOptions.All)); } friendsReader.Close(); friendsReader.Dispose(); return friends; }
public List<Friend> GetFriends(string namePart) { List<Friend> friends = new List<Friend>(); SelectQuery query = new SelectQuery("user_relations"); query.AddFields(User.GetFieldsPrefixed(core, typeof(User))); query.AddFields(UserInfo.GetFieldsPrefixed(core, typeof(UserInfo))); query.AddFields(UserProfile.GetFieldsPrefixed(core, typeof(UserProfile))); query.AddFields(UserRelation.GetFieldsPrefixed(core, typeof(UserRelation))); query.AddField(new DataField("gallery_items", "gallery_item_uri")); query.AddField(new DataField("gallery_items", "gallery_item_parent_path")); query.AddFields(ItemInfo.GetFieldsPrefixed(core, typeof(ItemInfo))); query.AddJoin(JoinTypes.Inner, User.GetTable(typeof(User)), "relation_you", "user_id"); query.AddJoin(JoinTypes.Inner, UserInfo.GetTable(typeof(UserInfo)), "relation_you", "user_id"); query.AddJoin(JoinTypes.Inner, UserProfile.GetTable(typeof(UserProfile)), "relation_you", "user_id"); query.AddJoin(JoinTypes.Left, new DataField("user_info", "user_icon"), new DataField("gallery_items", "gallery_item_id")); TableJoin join = query.AddJoin(JoinTypes.Left, new DataField(typeof(UserRelation), "relation_you"), new DataField(typeof(ItemInfo), "info_item_id")); join.AddCondition(new DataField(typeof(ItemInfo), "info_item_type_id"), ItemKey.GetTypeId(core, typeof(User))); query.AddCondition("relation_me", userId); query.AddCondition("relation_type", "FRIEND"); QueryCondition qc1 = query.AddCondition(new DataField("user_info", "user_name"), ConditionEquality.Like, namePart + "%"); qc1.AddCondition(ConditionRelations.Or, new DataField("user_profile", "profile_name_first"), ConditionEquality.Like, namePart + "%"); qc1.AddCondition(ConditionRelations.Or, new DataField("user_info", "user_name_display"), ConditionEquality.Like, namePart + "%"); query.AddSort(SortOrder.Ascending, "(relation_order - 1)"); query.LimitCount = 10; System.Data.Common.DbDataReader friendsReader = db.ReaderQuery(query); while (friendsReader.Read()) { friends.Add(new Friend(core, friendsReader, UserLoadOptions.All)); } friendsReader.Close(); friendsReader.Dispose(); return friends; }
public static SelectQuery GetSelectQueryStub(Core core, MusicianLoadOptions loadOptions) { SelectQuery query = new SelectQuery(GetTable(typeof(Musician))); query.AddFields(Musician.GetFieldsPrefixed(core, typeof(Musician))); if ((loadOptions & MusicianLoadOptions.Icon) == MusicianLoadOptions.Icon) { query.AddJoin(JoinTypes.Left, new DataField(typeof(Musician), "musician_icon"), new DataField("gallery_items", "gallery_item_id")); query.AddField(new DataField("gallery_items", "gallery_item_uri")); query.AddField(new DataField("gallery_items", "gallery_item_parent_path")); } return query; }
public List<Fan> GetFans(int page, int perPage, string filter) { List<Fan> fans = new List<Fan>(); SelectQuery query = new SelectQuery(typeof(Fan)); query.AddJoin(JoinTypes.Inner, "user_keys", "user_id", "user_id"); query.AddFields(Fan.GetFieldsPrefixed(core, typeof(Fan))); query.AddCondition("musician_id", musicianId); if (!string.IsNullOrEmpty(filter)) { query.AddCondition("user_keys.user_name_first", filter); } query.AddSort(SortOrder.Ascending, "fan_date_ut"); query.LimitStart = (page - 1) * perPage; query.LimitCount = perPage; DataTable fansTable = db.Query(query); List<long> fanIds = new List<long>(); foreach (DataRow dr in fansTable.Rows) { fanIds.Add((long)dr["user_id"]); } core.LoadUserProfiles(fanIds); foreach (DataRow dr in fansTable.Rows) { fans.Add(new Fan(core, dr)); } return fans; }
public List<long> GetInviteeIds() { List<long> ids = new List<long>(); SelectQuery query = new SelectQuery("event_invites"); query.AddFields("item_id", "item_type_id", "inviter_id", "event_id"); query.AddCondition("event_id", EventId); DataTable invitees = db.Query(query); foreach (DataRow dr in invitees.Rows) { if ((long)dr["item_type_id"] == ItemKey.GetTypeId(core, typeof(User))) { ids.Add((long)dr["item_id"]); } } return ids; }
public List(Core core, User owner, string listName) : base(core) { this.owner = owner; ItemLoad += new ItemLoadHandler(List_ItemLoad); SelectQuery query = new SelectQuery(List.GetTable(typeof(List))); query.AddFields(List.GetFieldsPrefixed(core, typeof(List))); query.AddCondition("list_path", listName); query.AddCondition("user_id", owner.Id); DataTable listTable = db.Query(query); if (listTable.Rows.Count == 1) { loadItemInfo(listTable.Rows[0]); } else { throw new InvalidListException(); } }
public static SelectQuery GetSelectQueryStub(Core core, UserGroupLoadOptions loadOptions) { SelectQuery query = new SelectQuery(UserGroup.GetTable(typeof(UserGroup))); query.AddFields(UserGroup.GetFieldsPrefixed(core, typeof(UserGroup))); query.AddFields(ItemInfo.GetFieldsPrefixed(core, typeof(ItemInfo))); TableJoin join = query.AddJoin(JoinTypes.Left, new DataField(typeof(UserGroup), "group_id"), new DataField(typeof(ItemInfo), "info_item_id")); join.AddCondition(new DataField(typeof(ItemInfo), "info_item_type_id"), ItemKey.GetTypeId(core, typeof(UserGroup))); if ((loadOptions & UserGroupLoadOptions.Info) == UserGroupLoadOptions.Info) { query.AddJoin(JoinTypes.Inner, UserGroupInfo.GetTable(typeof(UserGroupInfo)), "group_id", "group_id"); query.AddFields(UserGroupInfo.GetFieldsPrefixed(core, typeof(UserGroupInfo))); if ((loadOptions & UserGroupLoadOptions.Icon) == UserGroupLoadOptions.Icon) { query.AddJoin(JoinTypes.Left, new DataField("group_info", "group_icon"), new DataField("gallery_items", "gallery_item_id")); query.AddField(new DataField("gallery_items", "gallery_item_uri")); query.AddField(new DataField("gallery_items", "gallery_item_parent_path")); } } return query; }
/// <summary> /// /// </summary> /// <param name="core"></param> /// <param name="itemType"></param> /// <param name="itemId"></param> /// <param name="rating"></param> /// <remarks>ItemRated should implement a transaction.</remarks> public static void Vote(Core core, ItemKey itemKey, int rating) { if (core == null) { throw new NullCoreException(); } if (itemKey.Id < 1) { throw new InvalidItemException(); } if (rating < 1 || rating > 5) { throw new InvalidRatingException(); } /* after 7 days release the IP for dynamics ip fairness */ SelectQuery query = new SelectQuery("ratings r"); query.AddFields("user_id"); query.AddCondition("rate_item_id", itemKey.Id); query.AddCondition("rate_item_type_id", itemKey.TypeId); QueryCondition qc1 = query.AddCondition("user_id", core.LoggedInMemberId); QueryCondition qc2 = qc1.AddCondition(ConditionRelations.Or, "rate_ip", core.Session.IPAddress.ToString()); qc2.AddCondition("rate_time_ut", ConditionEquality.GreaterThan, UnixTime.UnixTimeStamp() - 60 * 60 * 24 * 7); /*DataTable ratingsTable = db.Query(string.Format("SELECT user_id FROM ratings WHERE rate_item_id = {0} AND rate_item_type = '{1}' AND (user_id = {2} OR (rate_ip = '{3}' AND rate_time_ut > UNIX_TIMESTAMP() - (60 * 60 * 24 * 7)))", itemId, Mysql.Escape(itemType), loggedInMember.UserId, session.IPAddress.ToString()));*/ DataTable ratingsTable = core.Db.Query(query); if (ratingsTable.Rows.Count > 0) { throw new AlreadyRatedException(); } /* Impossible using object query model */ /*UpdateQuery uQuery = new UpdateQuery(typeof(ItemInfo)); uQuery.AddField("rating", new QueryOperation("info_rating", QueryOperations.Division, new QueryOperation(QueryOperations.Addition, new QueryOperation("info_rating", QueryOperations.Multiplication, new DataField(typeof(ItemInfo), "info_ratings")))); uQuery.AddCondition("info_item_id", itemKey.Id); uQuery.AddCondition("info_item_type_id", itemKey.TypeId);*/ core.Db.UpdateQuery(string.Format("UPDATE item_info SET info_rating = (info_rating * info_ratings + {0}) / (info_ratings + 1), info_ratings = info_ratings + 1 WHERE info_item_id = {1} AND info_item_type_id = {2}", rating, itemKey.Id, itemKey.TypeId)); InsertQuery iQuery = new InsertQuery("ratings"); iQuery.AddField("rate_item_id", itemKey.Id); iQuery.AddField("rate_item_type_id", itemKey.TypeId); iQuery.AddField("user_id", core.LoggedInMemberId); iQuery.AddField("rate_time_ut", UnixTime.UnixTimeStamp()); iQuery.AddField("rate_rating", rating); iQuery.AddField("rate_ip", core.Session.IPAddress.ToString()); // commit the transaction core.Db.Query(iQuery); return; }
public UserGroup(Core core, string groupName, UserGroupLoadOptions loadOptions) : base(core) { ItemLoad += new ItemLoadHandler(UserGroup_ItemLoad); bool containsInfoData = false; bool containsIconData = false; if (loadOptions == UserGroupLoadOptions.Key) { try { LoadItem("group_name", groupName); } catch (InvalidItemException) { throw new InvalidGroupException(); } } else { SelectQuery query = new SelectQuery(UserGroup.GetTable(typeof(UserGroup))); query.AddFields(UserGroup.GetFieldsPrefixed(core, typeof(UserGroup))); query.AddCondition("`group_keys`.`group_name`", groupName); if ((loadOptions & UserGroupLoadOptions.Info) == UserGroupLoadOptions.Info) { containsInfoData = true; query.AddJoin(JoinTypes.Inner, UserGroupInfo.GetTable(typeof(UserGroupInfo)), "group_id", "group_id"); query.AddFields(UserGroupInfo.GetFieldsPrefixed(core, typeof(UserGroupInfo))); if ((loadOptions & UserGroupLoadOptions.Icon) == UserGroupLoadOptions.Icon) { containsIconData = true; query.AddJoin(JoinTypes.Left, new DataField("group_info", "group_icon"), new DataField("gallery_items", "gallery_item_id")); query.AddField(new DataField("gallery_items", "gallery_item_uri")); query.AddField(new DataField("gallery_items", "gallery_item_parent_path")); } } System.Data.Common.DbDataReader groupReader = db.ReaderQuery(query); if (groupReader.HasRows) { groupReader.Read(); loadItemInfo(typeof(UserGroup), groupReader); if (containsInfoData) { groupInfo = new UserGroupInfo(core, groupReader); } if (containsIconData) { loadUserGroupIcon(groupReader); } groupReader.Close(); groupReader.Dispose(); } else { groupReader.Close(); groupReader.Dispose(); throw new InvalidGroupException(); } } }
/// <summary> /// /// </summary> /// <param name="m">Page with message id#</param> /// <param name="perPage"></param> /// <returns></returns> public List<TopicPost> GetPosts(long m, int perPage) { int p = 1; // currentPage if (m > 0) { SelectQuery query = new SelectQuery(TopicPost.GetTable(typeof(TopicPost))); query.AddFields("COUNT(*) AS total"); query.AddCondition("topic_id", topicId); query.AddCondition("post_id", ConditionEquality.LessThan, m); query.AddSort(SortOrder.Ascending, "post_time_ut"); System.Data.Common.DbDataReader postsReader = db.ReaderQuery(query); long before = 0; long after = 0; if (postsReader.HasRows) { postsReader.Read(); before = (long)postsReader["total"]; after = Posts - before; } postsReader.Close(); postsReader.Dispose(); /*if (item.CommentSortOrder == SortOrder.Ascending) {*/ p = (int)(before / perPage + 1); /*} else { p = (int)(after / perPage + 1); }*/ } return GetPosts(p, perPage); }
public List<GroupMember> GetMembers(int page, int perPage, string filter) { List<GroupMember> members = new List<GroupMember>(); SelectQuery query = new SelectQuery("group_members"); query.AddFields(UserInfo.GetFieldsPrefixed(core, typeof(User))); query.AddJoin(JoinTypes.Inner, UserInfo.GetTable(typeof(User)), "user_id", "user_id"); query.AddFields(GroupMember.GetFieldsPrefixed(core, typeof(GroupMember))); query.AddField(new DataField("group_operators", "user_id", "user_id_go")); TableJoin tj = query.AddJoin(JoinTypes.Left, new DataField("group_members", "user_id"), new DataField("group_operators", "user_id")); tj.AddCondition(new DataField("group_members", "group_id"), new DataField("group_operators", "group_id")); query.AddCondition("`group_members`.`group_id`", groupId); query.AddCondition("group_member_approved", true); if (!string.IsNullOrEmpty(filter)) { query.AddCondition(new DataField("user_keys", "user_name_first"), filter[0]); } query.AddSort(SortOrder.Ascending, "group_member_date_ut"); query.LimitStart = (page - 1) * perPage; query.LimitCount = perPage; System.Data.Common.DbDataReader membersReader = db.ReaderQuery(query); while (membersReader.Read()) { members.Add(new GroupMember(core, membersReader)); } membersReader.Close(); membersReader.Dispose(); return members; }
public static new SelectQuery GetSelectQueryStub(Core core, UserLoadOptions loadOptions) { SelectQuery query = new SelectQuery("user_relations"); query.AddFields(UserRelation.GetFieldsPrefixed(core, typeof(UserRelation))); query.AddFields(User.GetFieldsPrefixed(core, typeof(User))); query.AddFields(ItemInfo.GetFieldsPrefixed(core, typeof(ItemInfo))); query.AddJoin(JoinTypes.Inner, User.GetTable(typeof(User)), "relation_you", "user_id"); TableJoin join = query.AddJoin(JoinTypes.Left, new DataField(typeof(UserRelation), "relation_you"), new DataField(typeof(ItemInfo), "info_item_id")); join.AddCondition(new DataField(typeof(ItemInfo), "info_item_type_id"), ItemKey.GetTypeId(core, typeof(User))); if ((loadOptions & UserLoadOptions.Info) == UserLoadOptions.Info) { query.AddFields(UserInfo.GetFieldsPrefixed(core, typeof(UserInfo))); query.AddJoin(JoinTypes.Inner, UserInfo.GetTable(typeof(UserInfo)), "relation_you", "user_id"); } if ((loadOptions & UserLoadOptions.Profile) == UserLoadOptions.Profile) { query.AddFields(UserProfile.GetFieldsPrefixed(core, typeof(UserProfile))); query.AddJoin(JoinTypes.Inner, UserProfile.GetTable(typeof(UserProfile)), "relation_you", "user_id"); query.AddJoin(JoinTypes.Left, new DataField("user_profile", "profile_country"), new DataField("countries", "country_iso")); query.AddJoin(JoinTypes.Left, new DataField("user_profile", "profile_religion"), new DataField("religions", "religion_id")); } /*if ((loadOptions & UserLoadOptions.Icon) == UserLoadOptions.Icon) { query.AddField(new DataField("gallery_items", "gallery_item_uri")); query.AddField(new DataField("gallery_items", "gallery_item_parent_path")); query.AddJoin(JoinTypes.Left, new DataField("user_info", "user_icon"), new DataField("gallery_items", "gallery_item_id")); }*/ return query; }
public List<GroupMember> GetOperators(int page, int perPage) { List<GroupMember> operators = new List<GroupMember>(); SelectQuery query = new SelectQuery("group_operators"); query.AddFields(GroupMember.GetFieldsPrefixed(core, typeof(GroupMember))); query.AddFields(UserInfo.GetFieldsPrefixed(core, typeof(User))); query.AddJoin(JoinTypes.Inner, UserInfo.GetTable(typeof(User)), "user_id", "user_id"); query.AddField(new DataField("group_operators", "user_id")); TableJoin tj = query.AddJoin(JoinTypes.Left, new DataField("group_operators", "user_id"), new DataField("group_members", "user_id")); tj.AddCondition(new DataField("group_members", "group_id"), groupId); query.AddCondition(new DataField("group_operators", "group_id"), groupId); System.Data.Common.DbDataReader membersReader = db.ReaderQuery(query); while(membersReader.Read()) { operators.Add(new GroupMember(core, membersReader)); } membersReader.Close(); membersReader.Dispose(); return operators; }
public static SelectQuery GetSelectQueryStub(Core core, NetworkLoadOptions loadOptions) { SelectQuery query = new SelectQuery(Network.GetTable(typeof(Network))); query.AddFields(Network.GetFieldsPrefixed(core, typeof(Network))); if ((loadOptions & NetworkLoadOptions.Info) == NetworkLoadOptions.Info) { query.AddJoin(JoinTypes.Inner, NetworkInfo.GetTable(typeof(NetworkInfo)), "network_id", "network_id"); query.AddFields(NetworkInfo.GetFieldsPrefixed(core, typeof(NetworkInfo))); if ((loadOptions & NetworkLoadOptions.Icon) == NetworkLoadOptions.Icon) { // TODO: Network Icon /*containsIconData = true; query.AddJoin(JoinTypes.Left, new DataField("network_info", "network_icon"), new DataField("gallery_items", "gallery_item_id")); query.AddField(new DataField("gallery_items", "gallery_item_uri")); query.AddField(new DataField("gallery_items", "gallery_item_parent_path"));*/ } } return query; }
private void preLoadMemberCache(ItemKey itemKey) { SelectQuery query = new SelectQuery("group_members"); query.AddFields("user_id", "group_member_approved"); query.AddCondition("group_id", groupId); query.AddCondition("user_id", itemKey.Id); System.Data.Common.DbDataReader memberReader = db.ReaderQuery(query); if (memberReader.HasRows) { memberReader.Read(); switch ((GroupMemberApproval)(byte)memberReader["group_member_approved"]) { case GroupMemberApproval.Pending: groupMemberCache.Add(itemKey, false); groupMemberPendingCache.Add(itemKey, true); groupMemberBannedCache.Add(itemKey, false); groupMemberAbsoluteCache.Add(itemKey, true); break; case GroupMemberApproval.Member: groupMemberCache.Add(itemKey, true); groupMemberPendingCache.Add(itemKey, false); groupMemberBannedCache.Add(itemKey, false); groupMemberAbsoluteCache.Add(itemKey, true); break; case GroupMemberApproval.Banned: groupMemberCache.Add(itemKey, false); groupMemberPendingCache.Add(itemKey, false); groupMemberBannedCache.Add(itemKey, true); groupMemberAbsoluteCache.Add(itemKey, false); break; } } else { groupMemberCache.Add(itemKey, false); groupMemberPendingCache.Add(itemKey, false); groupMemberBannedCache.Add(itemKey, false); groupMemberAbsoluteCache.Add(itemKey, false); } memberReader.Close(); memberReader.Dispose(); }
public Network(Core core, long networkId, NetworkLoadOptions loadOptions) : base(core) { ItemLoad += new ItemLoadHandler(Network_ItemLoad); bool containsInfoData = false; bool containsIconData = false; if (loadOptions == NetworkLoadOptions.Key) { try { LoadItem(networkId); } catch (InvalidItemException) { throw new InvalidNetworkException(); } } else { SelectQuery query = new SelectQuery(Network.GetTable(typeof(Network))); query.AddFields(Network.GetFieldsPrefixed(core, typeof(Network))); query.AddCondition("`network_keys`.`network_id`", networkId); if ((loadOptions & NetworkLoadOptions.Info) == NetworkLoadOptions.Info) { containsInfoData = true; query.AddJoin(JoinTypes.Inner, NetworkInfo.GetTable(typeof(NetworkInfo)), "network_id", "network_id"); query.AddFields(NetworkInfo.GetFieldsPrefixed(core, typeof(NetworkInfo))); if ((loadOptions & NetworkLoadOptions.Icon) == NetworkLoadOptions.Icon) { // TODO: Network Icon /*containsIconData = true; query.AddJoin(JoinTypes.Left, new DataField("network_info", "network_icon"), new DataField("gallery_items", "gallery_item_id")); query.AddField(new DataField("gallery_items", "gallery_item_uri")); query.AddField(new DataField("gallery_items", "gallery_item_parent_path"));*/ } } DataTable networkTable = db.Query(query); if (networkTable.Rows.Count > 0) { loadItemInfo(typeof(Network), networkTable.Rows[0]); if (containsInfoData) { networkInfo = new NetworkInfo(core, networkTable.Rows[0]); } if (containsIconData) { // TODO: Network Icon //loadNetworkIcon(networkTable.Rows[0]); } } else { throw new InvalidNetworkException(); } } }
public UserGroup(Core core, long groupId, UserGroupLoadOptions loadOptions) : base(core) { ItemLoad += new ItemLoadHandler(UserGroup_ItemLoad); bool containsInfoData = false; bool containsIconData = false; if (loadOptions == UserGroupLoadOptions.Key) { try { LoadItem(groupId); } catch (InvalidItemException) { throw new InvalidGroupException(); } } else { SelectQuery query = new SelectQuery(UserGroup.GetTable(typeof(UserGroup))); query.AddFields(UserGroup.GetFieldsPrefixed(core, typeof(UserGroup))); query.AddCondition("`group_keys`.`group_id`", groupId); if ((loadOptions & UserGroupLoadOptions.Info) == UserGroupLoadOptions.Info) { containsInfoData = true; query.AddJoin(JoinTypes.Inner, UserGroupInfo.GetTable(typeof(UserGroupInfo)), "group_id", "group_id"); query.AddFields(UserGroupInfo.GetFieldsPrefixed(core, typeof(UserGroupInfo))); if ((loadOptions & UserGroupLoadOptions.Icon) == UserGroupLoadOptions.Icon) { containsIconData = true; query.AddJoin(JoinTypes.Left, new DataField("group_info", "group_icon"), new DataField("gallery_items", "gallery_item_id")); query.AddField(new DataField("gallery_items", "gallery_item_uri")); query.AddField(new DataField("gallery_items", "gallery_item_parent_path")); } } DataTable groupTable = db.Query(query); if (groupTable.Rows.Count > 0) { loadItemInfo(typeof(UserGroup), groupTable.Rows[0]); if (containsInfoData) { groupInfo = new UserGroupInfo(core, groupTable.Rows[0]); } if (containsIconData) { loadUserGroupIcon(groupTable.Rows[0]); } } else { throw new InvalidGroupException(); } } }