From e3a4a318197a6b1fdc05ecb74e6ce3a79d3392a0 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Fri, 20 Apr 2012 20:40:33 +0400 Subject: [PATCH] Mentions dataobject implemented --- FLocal.Common/FLocal.Common.csproj | 1 + FLocal.Common/actions/ChangeSet.cs | 1 + FLocal.Common/dataobjects/Mention.cs | 69 ++++++++++++++ FLocal.Common/dataobjects/Post.cs | 43 ++++++++- FLocal.Common/dataobjects/Thread.cs | 16 ++++ .../FLocal.Patcher.Common.csproj | 3 + .../Resources/Patch_00002_mentions.xml | 91 +++++++++++++++++++ 7 files changed, 221 insertions(+), 3 deletions(-) create mode 100644 FLocal.Common/dataobjects/Mention.cs create mode 100644 FLocal.Patcher.Common/Resources/Patch_00002_mentions.xml diff --git a/FLocal.Common/FLocal.Common.csproj b/FLocal.Common/FLocal.Common.csproj index ce6788c..7841395 100644 --- a/FLocal.Common/FLocal.Common.csproj +++ b/FLocal.Common/FLocal.Common.csproj @@ -100,6 +100,7 @@ + diff --git a/FLocal.Common/actions/ChangeSet.cs b/FLocal.Common/actions/ChangeSet.cs index 2503344..f2ba2a1 100644 --- a/FLocal.Common/actions/ChangeSet.cs +++ b/FLocal.Common/actions/ChangeSet.cs @@ -41,6 +41,7 @@ namespace FLocal.Common.actions { dataobjects.PunishmentLayerChange.TableSpec.TABLE, dataobjects.Restriction.TableSpec.TABLE, dataobjects.TexImage.TableSpec.TABLE, + dataobjects.Mention.TableSpec.TABLE, dataobjects.Session.TableSpec.TABLE, } ); diff --git a/FLocal.Common/dataobjects/Mention.cs b/FLocal.Common/dataobjects/Mention.cs new file mode 100644 index 0000000..e278fa6 --- /dev/null +++ b/FLocal.Common/dataobjects/Mention.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml.Linq; +using Web.Core; +using Web.Core.DB; +using Web.Core.DB.conditions; +using FLocal.Common.actions; + +namespace FLocal.Common.dataobjects { + public class Mention : SqlObject { + + public class TableSpec : ISqlObjectTableSpec { + public const string TABLE = "Mentions"; + public const string FIELD_ID = "Id"; + public const string FIELD_MENTIONEDUSERID = "MentionedUserId"; + public const string FIELD_POSTID = "PostId"; + public const string FIELD_DATE = "Date"; + public static readonly TableSpec instance = new TableSpec(); + public string name { get { return TABLE; } } + public string idName { get { return FIELD_ID; } } + public void refreshSqlObject(int id) { Refresh(id); } + } + + protected override ISqlObjectTableSpec table { get { return TableSpec.instance; } } + + private int _mentionedUserId; + public int mentionedUserId { + get { + this.LoadIfNotLoaded(); + return this._mentionedUserId; + } + } + public User mentionedUser { + get { + return User.LoadById(this.mentionedUserId); + } + } + + private int _postId; + public int postId { + get { + this.LoadIfNotLoaded(); + return this._postId; + } + } + public Post post { + get { + return Post.LoadById(this.postId); + } + } + + private DateTime _date; + public DateTime date { + get { + this.LoadIfNotLoaded(); + return this._date; + } + } + + protected override void doFromHash(Dictionary data) { + this._mentionedUserId = int.Parse(data[TableSpec.FIELD_MENTIONEDUSERID]); + this._postId = int.Parse(data[TableSpec.FIELD_POSTID]); + this._date = Util.ParseDateTimeFromTimestamp(data[TableSpec.FIELD_DATE]).Value; + } + + } +} diff --git a/FLocal.Common/dataobjects/Post.cs b/FLocal.Common/dataobjects/Post.cs index 3bbd102..1d11385 100644 --- a/FLocal.Common/dataobjects/Post.cs +++ b/FLocal.Common/dataobjects/Post.cs @@ -330,12 +330,21 @@ namespace FLocal.Common.dataobjects { actualLayer = this.layer; } lock(this.Edit_locker) { + + DateTime date = DateTime.Now; + + HashSet newMentionedUsersIds = new HashSet(); + if(parentPost != null && parentPost.poster.id != poster.id) { + newMentionedUsersIds.Add(parentPost.poster.id); + } + string newBodyIntermediate = UBBParser.UBBToIntermediate(newBody); + List changes = new List { new InsertChange( Revision.TableSpec.instance, new Dictionary { { Revision.TableSpec.FIELD_POSTID, new ScalarFieldValue(this.id.ToString()) }, - { Revision.TableSpec.FIELD_CHANGEDATE, new ScalarFieldValue(DateTime.Now.ToUTCString()) }, + { Revision.TableSpec.FIELD_CHANGEDATE, new ScalarFieldValue(date.ToUTCString()) }, { Revision.TableSpec.FIELD_TITLE, new ScalarFieldValue(newTitle) }, { Revision.TableSpec.FIELD_BODY, new ScalarFieldValue(newBody) }, { Revision.TableSpec.FIELD_NUMBER, new ScalarFieldValue((this.revision + 1).ToString()) }, @@ -345,8 +354,8 @@ namespace FLocal.Common.dataobjects { TableSpec.instance, new Dictionary { { TableSpec.FIELD_TITLE, new ScalarFieldValue(newTitle) }, - { TableSpec.FIELD_BODY, new ScalarFieldValue(UBBParser.UBBToIntermediate(newBody)) }, - { TableSpec.FIELD_LASTCHANGEDATE, new ScalarFieldValue(DateTime.Now.ToUTCString()) }, + { TableSpec.FIELD_BODY, new ScalarFieldValue(newBodyIntermediate) }, + { TableSpec.FIELD_LASTCHANGEDATE, new ScalarFieldValue(date.ToUTCString()) }, { TableSpec.FIELD_REVISION, new IncrementFieldValue() }, { TableSpec.FIELD_LAYERID, new ScalarFieldValue(actualLayer.id.ToString()) }, }, @@ -364,10 +373,38 @@ namespace FLocal.Common.dataobjects { ) ); } + foreach(var mentionedUserId in newMentionedUsersIds.Except(this.mentionedUsersIds)) { + changes.Add( + new InsertChange( + Mention.TableSpec.instance, + new Dictionary { + { Mention.TableSpec.FIELD_MENTIONEDUSERID, new ScalarFieldValue(mentionedUserId.ToString()) }, + { Mention.TableSpec.FIELD_POSTID, new ScalarFieldValue(this.id.ToString()) }, + { Mention.TableSpec.FIELD_DATE, new ScalarFieldValue(date.ToUTCString()) }, + } + ) + ); + } ChangeSetUtil.ApplyChanges(changes.ToArray()); } } + private IEnumerable mentionedUsersIds { + get { + return from mention in Mention.LoadByIds( + from stringId in Config.instance.mainConnection.LoadIdsByConditions( + Mention.TableSpec.instance, + new ComparisonCondition( + Mention.TableSpec.instance.getColumnSpec(Mention.TableSpec.FIELD_POSTID), + ComparisonType.EQUAL, + this.id.ToString() + ), + Diapasone.unlimited + ) select int.Parse(stringId) + ) select mention.mentionedUserId; + } + } + private IEnumerable subPosts { get { return Post.LoadByIds( diff --git a/FLocal.Common/dataobjects/Thread.cs b/FLocal.Common/dataobjects/Thread.cs index 23d9fc5..039f0f2 100644 --- a/FLocal.Common/dataobjects/Thread.cs +++ b/FLocal.Common/dataobjects/Thread.cs @@ -411,6 +411,10 @@ namespace FLocal.Common.dataobjects { string parentPostId = null; if(parentPost != null) parentPostId = parentPost.id.ToString(); bool isNewThread = (parentPost == null); + HashSet mentionedUsersIds = new HashSet(); + if(parentPost != null && parentPost.poster.id != poster.id) { + mentionedUsersIds.Add(parentPost.poster.id); + } string bodyIntermediate; if(forcedPostId.HasValue) { //dirty hack @@ -484,6 +488,18 @@ namespace FLocal.Common.dataobjects { changes.Add(threadUpdate); changes.Add(userUpdate); + foreach(var mentionedUserId in mentionedUsersIds) { + changes.Add( + new InsertChange( + Mention.TableSpec.instance, + new Dictionary { + { Mention.TableSpec.FIELD_MENTIONEDUSERID, new ScalarFieldValue(mentionedUserId.ToString()) }, + { Mention.TableSpec.FIELD_POSTID, new ReferenceFieldValue(postInsert) }, + { Mention.TableSpec.FIELD_DATE, new ScalarFieldValue(date.ToUTCString()) }, + } + ) + ); + } Dictionary boardData = new Dictionary { { Board.TableSpec.FIELD_TOTALPOSTS, new IncrementFieldValue() }, diff --git a/FLocal.Patcher.Common/FLocal.Patcher.Common.csproj b/FLocal.Patcher.Common/FLocal.Patcher.Common.csproj index 3c429df..7693f53 100644 --- a/FLocal.Patcher.Common/FLocal.Patcher.Common.csproj +++ b/FLocal.Patcher.Common/FLocal.Patcher.Common.csproj @@ -69,6 +69,9 @@ + + +