MS: Conversations

This commit is contained in:
2020-06-23 10:39:24 +02:00
parent 238c8e0860
commit a45b81dc47
39 changed files with 1652 additions and 206 deletions

View File

@@ -7,118 +7,248 @@ Public Class ChatControl
Private ReadOnly IdentificationColumn As String = "USER_FROM"
Private ReadOnly MessageColumn As String = "MESSAGE_TEXT"
Private ReadOnly DateColumn As String = "ADDED_WHEN"
Public Delegate Sub ConversationEnded()
Public Event Conversation_Ended As ConversationEnded
Public Delegate Sub ConversationUsersAdded_Success()
Public Event Conversation_UsersAdded_Success As ConversationUsersAdded_Success
Private Db As MSSQLServer
Private LogConfig As LogConfig
Private Logger As Logger
Private ReadOnly UsernameColorsDict As New Dictionary(Of String, Color)
Private ReadOnly UsernameColors As New List(Of Color) From {
Color.Purple,
Color.Red,
Color.LightBlue,
Color.DarkSeaGreen
}
Private AddMessage As Boolean
Private oAddUSerForm As Form
'Private ReadOnly UsernameColorsDict As New Dictionary(Of String, Color)
'Private ReadOnly UsernameColors As New List(Of Color) From {
' Color.Purple,
' Color.Red,
' Color.LightBlue,
' Color.DarkSeaGreen
'}
Private IsAssistKeyPressed As Boolean = False
Private AssistListBox As ListBox = New ListBox()
Public IDBObjectId As Long
Public ConnStringIDB As String
Public ConversationIdentification As String
Public UserID As Long
Public Username As String
Public CurrentConversation As Long
Public User_Language As String
Public CurrentConversationID As Long
Public CurrentConversation_State As String
Public CurrentConversation_UserActive As Boolean
Public oDTConv_User As DataTable
Public CurrentMentionedUser As String
Public CurrentMentionedUserID As Long
Public CurrentIdentifier
Public User_Datatable_Select As DataTable
Public DT_Rights As DataTable
Public QU_Conv_End As DataTable
Public Sub New()
InitializeComponent()
End Sub
Public Sub Init(LogConfig As LogConfig, ConnectionStringIDB As String, ConversationIdentification As String, Username As String)
Me.LogConfig = LogConfig
Me.Logger = LogConfig.GetLogger()
Me.ConnStringIDB = ConnectionStringIDB
Me.ConversationIdentification = ConversationIdentification
Me.Username = Username
Public Function Init(LogConfig As LogConfig, ConnectionStringIDB As String, ConversationIdentification As String, UserID As Long, Username As String, USER_LANG As String,
Qust_Conv_End As DataTable, DTRIGHTS As DataTable)
Try
CurrentConversationID = 0
Me.LogConfig = LogConfig
Me.Logger = LogConfig.GetLogger()
Me.ConnStringIDB = ConnectionStringIDB
Me.ConversationIdentification = ConversationIdentification
Me.UserID = UserID
Me.Username = Username
Me.User_Language = USER_LANG
QU_Conv_End = Qust_Conv_End
DT_Rights = DTRIGHTS
Me.Db = New MSSQLServer(LogConfig, ConnStringIDB)
Return True
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function
Sub Check_Rights()
Dim oSQL As String
ToolStripMenuItemConv_end.Visible = False
tsmenitmHamburger.Visible = False
AddMessage = False
Dim oView As DataView = New DataView(DT_Rights)
oView.RowFilter = "CONF_TITLE = 'CONVERSATION_USER_ACTIVE'"
If oView.Count = 1 Then
CurrentConversation_UserActive = True
Else
CurrentConversation_UserActive = False
End If
For Each oRow As DataRow In DT_Rights.Rows
If oRow.Item("CONF_TITLE").ToString.Contains("CONVERSATION_RIGHT") Then
Select Case oRow.Item("CONF_VALUE")
Case "Start|Stop"
ToolStripMenuItemConv_end.Visible = True
tsmenitmHamburger.Visible = True
AddMessage = True
Case "AddMessage"
AddMessage = True
Case "Admin"
ToolStripMenuItemConv_end.Visible = True
BenutzerHinzufügenToolStripMenuItem.Visible = True
tsmenitmHamburger.Visible = True
AddMessage = True
Case "Start"
BenutzerHinzufügenToolStripMenuItem.Visible = True
tsmenitmHamburger.Visible = True
Case "Stop"
ToolStripMenuItemConv_end.Visible = True
End Select
ElseIf oRow.Item("CONF_TITLE") = "CONVERSATION_ADD_USER_SELECT" Then
oSQL = oRow.Item("CONF_VALUE")
oSQL = oSQL.Replace("@CONVID", CurrentConversationID)
User_Datatable_Select = Db.GetDatatable(oSQL)
Logger.Debug($"User_Datatable_Select [{User_Datatable_Select}]")
End If
Next
If CurrentConversation_State = "Started" Then
pnlMessage.Visible = AddMessage
Else
pnlMessage.Visible = False
tsmenitmHamburger.Visible = False
End If
Me.Db = New MSSQLServer(LogConfig, ConnStringIDB)
End Sub
Public Function GetConversations(IDBObjectId As Long)
Dim oSQL As String = $"SELECT * FROM VWIDB_CONVERSATION WHERE IDB_OBJ_ID = {IDBObjectId} ORDER BY CONVERSATION_ID DESC"
CurrentConversationID = 0
Dim oSQL As String = $"SELECT DISTINCT T.CONVERSATION_ID,T.TITLE,T.CONVERSATION_STATE,T.ADDED_WHEN FROM VWIDB_CONVERSATION T INNER JOIN VWIDB_CONVERSATION_USER T1 ON T.CONVERSATION_ID = T1.CONV_ID WHERE IDB_OBJ_ID = {IDBObjectId}
AND T1.USER_ID = {UserID}
ORDER BY CONVERSATION_ID DESC"
Dim oDatatable As DataTable = Db.GetDatatable(oSQL)
Dim oConversations As New List(Of String)
If Not IsNothing(oDatatable) Then
If oDatatable.Rows.Count = 1 Then
Dim oItem = oDatatable.Rows(0).Item("CONVERSATION_ID").ToString + "|" + oDatatable.Rows(0).Item("TITLE").ToString
oConversations.Insert(0, oItem)
CurrentConversation = oDatatable.Rows(0).Item("CONVERSATION_ID")
LoadConversation(CurrentConversation)
Else
For Each oROW As DataRow In oDatatable.Rows
Dim oItem = oROW.Item("CONVERSATION_ID").ToString + "|" + oROW.Item("TITLE").ToString
oConversations.Insert(0, oItem)
If oROW.Item("CONVERSATION_STATE") = "Started" Then
CurrentConversation = oROW.Item("CONVERSATION_ID")
LoadConversation(CurrentConversation)
Dim i = 0
For Each oROW As DataRow In oDatatable.Rows
Dim oItem = oROW.Item("CONVERSATION_ID").ToString + "|" + oROW.Item("TITLE").ToString + "|" + oROW.Item("CONVERSATION_STATE").ToString + "|" + oDatatable.Rows(0).Item("ADDED_WHEN").ToString
oConversations.Insert(i, oItem)
i += 1
If oROW.Item("CONVERSATION_STATE") = "Started" Then
If CurrentConversationID = 0 Then
CurrentConversationID = oROW.Item("CONVERSATION_ID")
LoadConversation(CurrentConversationID)
End If
Next
End If
End If
Next
End If
Return oConversations
End Function
Private Sub tsCBConversations_SelectedIndexChanged(sender As Object, e As EventArgs)
'If tsCBConversations.SelectedIndex <> -1 Then
' Dim oValues As List(Of String) = tsCBConversations.Text.Split("|").ToList()
' CurrentConversation = oValues(0)
' LoadConversation(CurrentConversation)
'End If
End Sub
Public Sub LoadConversation(ConversationId As Long)
ListBoxUserMention.Visible = False
RichTextBox1.Text = ""
Dim oSQL As String = $"SELECT * FROM VWIDB_CONV_MESSAGES WHERE CONV_ID = {ConversationId} ORDER BY GUID"
Dim oDatatable As DataTable = Db.GetDatatable(oSQL)
BuildUsernameColorDict(oDatatable)
' BuildUsernameColorDict(oDatatable)
oSQL = $"SELECT * FROM VWIDB_CONVERSATION WHERE CONVERSATION_ID = {ConversationId}"
Dim oDatatable2 As DataTable = Db.GetDatatable(oSQL)
GridChat.DataSource = ChatSource
ChatSource.DataSource = oDatatable
lblTitle.Text = oDatatable2.Rows(0).Item("TITLE")
tsmitmTitle.Text = oDatatable2.Rows(0).Item("TITLE")
CurrentConversationID = ConversationId
CurrentConversation_State = oDatatable2.Rows(0).Item("CONVERSATION_STATE")
oSQL = $"SELECT * FROM VWIDB_CONVERSATION_USER WHERE CONV_ID = {ConversationId}"
oDTConv_User = Db.GetDatatable(oSQL)
If TeilnehmerToolStripMenuItem.HasDropDownItems Then
TeilnehmerToolStripMenuItem.DropDownItems.Clear()
End If
If ConversationIdentification.Contains("@") Then
CurrentIdentifier = "EMAIL"
Else
CurrentIdentifier = "USERNAME"
End If
AssistListBox.Items.Clear()
ListBoxUserMention.Items.Clear()
Try
For Each oROW As DataRow In oDTConv_User.Rows
Dim oConvUser
oConvUser = oROW.Item(CurrentIdentifier)
If IsDBNull(oConvUser) Then
oConvUser = String.Empty
End If
If oConvUser.ToString = String.Empty Then
If CurrentIdentifier = "EMAIL" Then
oConvUser = $"No Email for User '{oROW.Item("USERNAME")}'"
Else
oConvUser = $"No Identification for UserID '{oROW.Item("USER_ID")}'"
End If
Else
ListBoxUserMention.Items.Add("@" & oConvUser.ToString.Substring(0, oConvUser.ToString.IndexOf("@")))
AssistListBox.Items.Add("@" & oConvUser.ToString.Substring(0, oConvUser.ToString.IndexOf("@")))
End If
Dim oToolStripItem As ToolStripMenuItem = New ToolStripMenuItem(oConvUser.ToString)
TeilnehmerToolStripMenuItem.DropDownItems.Add(oToolStripItem)
Next
AddHandler AssistListBox.SelectedIndexChanged, AddressOf AssistListBox_Changed
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error adding ConversationUsers")
End Try
Check_Rights()
End Sub
Public Sub NewMessage(MessageText As String)
Try
Dim oSQL As String = $"EXEC [PRIDB_NEW_CONVERSATION_MESSAGE] {CurrentConversation},'{MessageText}', '{ConversationIdentification}'"
Dim oSQL As String = $"EXEC [PRIDB_NEW_CONVERSATION_MESSAGE] {CurrentConversationID},'{MessageText}', '{ConversationIdentification}', {CurrentMentionedUserID},'{User_Language}'"
Logger.Debug($"NewMessageSQL: {oSQL}")
Dim oResult = Db.GetScalarValue(oSQL)
LoadConversation(CurrentConversation)
txtMessage.Text = String.Empty
LoadConversation(CurrentConversationID)
RichTextBox1.Text = String.Empty
ChatView.MoveLast()
Catch ex As Exception
Logger.Error(ex)
End Try
End Sub
Private Sub BuildUsernameColorDict(Datatable As DataTable)
Dim oIndex = 0
'Private Sub BuildUsernameColorDict(Datatable As DataTable)
' Dim oIndex = 0
UsernameColorsDict.Clear()
' UsernameColorsDict.Clear()
Datatable.AsEnumerable().
Select(Function(Row) Row.Item(IdentificationColumn)).
Distinct().ToList().
ForEach(Sub(Name)
UsernameColorsDict.Add(Name, UsernameColors.Item(oIndex))
oIndex += 1
End Sub)
End Sub
' Datatable.AsEnumerable().
' Select(Function(Row) Row.Item(IdentificationColumn)).
' Distinct().ToList().
' ForEach(Sub(Name)
' UsernameColorsDict.Add(Name, UsernameColors.Item(oIndex))
' oIndex += 1
' End Sub)
'End Sub
Private Sub ChatView_CustomItemTemplate(sender As Object, e As TileViewCustomItemTemplateEventArgs) Handles ChatView.CustomItemTemplate
Dim oRow As DataRow = ChatView.GetDataRow(e.RowHandle)
Dim oUsername As String = oRow.Item(IdentificationColumn)
If oUsername = ConversationIdentification Or oUsername = Username Then
e.Template = e.Templates.Item("ChatRight")
Dim oIdentification As String = oRow.Item(IdentificationColumn)
Dim oToUser = oRow.Item("TO_USER")
If Not IsDBNull(oToUser) Then
e.Template = e.Templates.Item("ChatTo")
Else
e.Template = e.Templates.Item("ChatLeft")
If oIdentification = ConversationIdentification Or oIdentification = Username Then
e.Template = e.Templates.Item("ChatRight")
Else
e.Template = e.Templates.Item("ChatLeft")
End If
End If
End Sub
Private Sub ChatView_ItemCustomize(sender As Object, e As TileViewItemCustomizeEventArgs) Handles ChatView.ItemCustomize
@@ -126,13 +256,17 @@ Public Class ChatControl
Dim oUsername As String = oRow.Item(IdentificationColumn)
Dim oMessage As String = oRow.Item(MessageColumn)
Dim oColor As Color = UsernameColorsDict.Item(oUsername)
' Dim oColor As Color = UsernameColorsDict.Item(oUsername)
If oUsername = ConversationIdentification Or oUsername = Username Then
e.Item.AppearanceItem.Normal.BackColor = Color.PaleTurquoise
e.Item.Item(IdentificationColumn).Appearance.Normal.ForeColor = Color.Purple
Else
' e.Item.AppearanceItem.Normal.BackColor = Color.whi
e.Item.Item(IdentificationColumn).Appearance.Normal.ForeColor = Color.Red
End If
e.Item.AppearanceItem.Focused.BackColor = Color.Turquoise
e.Item.Item(IdentificationColumn).Appearance.Normal.ForeColor = oColor
'e.Item.AppearanceItem.Focused.BackColor = Color.Turquoise
End Sub
@@ -142,15 +276,13 @@ Public Class ChatControl
End Sub
Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles btnSendMessage.Click
If txtMessage.Text.Count > 0 Then
NewMessage(txtMessage.Text)
If RichTextBox1.Text.Count > 0 Then
NewMessage(RichTextBox1.Text)
End If
End Sub
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles txtMessage.KeyUp
If e.Control And e.KeyCode = Keys.Enter And txtMessage.Text.Count > 0 Then
NewMessage(txtMessage.Text)
End If
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs)
End Sub
Private Sub ChatControl_Click(sender As Object, e As EventArgs) Handles MyBase.Click
@@ -160,4 +292,156 @@ Public Class ChatControl
Private Sub ChatControl_Leave(sender As Object, e As EventArgs) Handles Me.Leave
End Sub
Private Sub ToolStripMenuItemConv_end_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItemConv_end.Click
If CurrentConversationID <> 0 Then
Dim result As MsgBoxResult
result = MessageBox.Show(QU_Conv_End.Rows(0).Item("STRING1").ToString, QU_Conv_End.Rows(0).Item("STRING2").ToString, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
If result = MsgBoxResult.Yes Then
Dim oSQL = $"EXEC PRIDB_END_CONVERSATION {CurrentConversationID}, '{Username}', '{User_Language}'"
If Db.ExecuteNonQuery(oSQL) = True Then
RaiseEvent Conversation_Ended()
Else
MsgBox("Unexpected error in PRIDB_END_CONVERSATION - Check Your log!", MsgBoxStyle.Exclamation)
End If
End If
End If
End Sub
Private Sub BenutzerHinzufügenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BenutzerHinzufügenToolStripMenuItem.Click
Dim oForm = New Form()
Dim oaddUS As ChatAddUser.ChatAddUser = New ChatAddUser.ChatAddUser()
oaddUS.Init(LogConfig, ConnStringIDB, CurrentConversationID, Username, User_Language, User_Datatable_Select)
oForm.Controls.Add(oaddUS)
oaddUS.Dock = DockStyle.Fill
oForm.MaximizeBox = False
oForm.MinimizeBox = False
oForm.FormBorderStyle = FormBorderStyle.FixedDialog
oForm.StartPosition = FormStartPosition.CenterScreen
oForm.Text = ""
AddHandler oaddUS.User_Added, AddressOf onUsersAdded
oAddUSerForm = oForm
oForm.ShowDialog()
End Sub
Sub onUsersAdded()
RaiseEvent Conversation_UsersAdded_Success()
oAddUSerForm.Close()
End Sub
Private Function GetPoint(ByVal textBoxControl As RichTextBox) As Point
Dim graphics_1 As Graphics = Graphics.FromHwnd(textBoxControl.Handle)
Dim size As SizeF = graphics_1.MeasureString(textBoxControl.Text.Substring(0, textBoxControl.SelectionStart), textBoxControl.Font)
Dim coord As New Point(CInt(size.Width) + textBoxControl.Location.X, CInt(size.Height) + textBoxControl.Location.Y)
Return coord
End Function
Private Sub RichTextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyUp
If e.Control And e.KeyCode = Keys.Enter And RichTextBox1.Text.Count > 0 Then
NewMessage(RichTextBox1.Text)
ElseIf (Keys.Alt AndAlso Keys.Control) And e.KeyCode = keys.Q Then
With RichTextBox1
ListBoxUserMention.Left = .GetPositionFromCharIndex(
.SelectionStart).X + .Left + 10
ListBoxUserMention.Top = .GetPositionFromCharIndex(
.SelectionStart).Y + .Top + 20
' Jetzt ListBox einblendenden, Focus auf diese setzen
' und den ersten Eintrag selektieren
ListBoxUserMention.Visible = True
ListBoxUserMention.Focus()
ListBoxUserMention.SelectedIndex = 0
End With
'Dim opoint As Point = GetPoint(sender)
'AssistListBox.PointToClient(opoint)
'pnlMessage.Controls.Add(AssistListBox)
'AssistListBox.Show()
'AssistListBox.BringToFront()
' '@ was pressed
End If
End Sub
Sub AssistListBox_Changed()
Try
RichTextBox1.Text = RichTextBox1.Text + AssistListBox.SelectedItem + ": "
RichTextBox1.SelectionFont = New Font("Tahoma", 10, FontStyle.Bold And FontStyle.Underline)
RichTextBox1.Text = RichTextBox1.Text.Replace("@@", "@")
pnlMessage.Controls.Remove(AssistListBox)
'RichTextBox1.SelectedText = AssistListBox.SelectedItem
Catch ex As Exception
Logger.Error(ex)
End Try
End Sub
Sub ADD_MentionedUser(pUser As String)
Dim oPos As Integer = RichTextBox1.TextLength
Dim oUsertext = pUser & ": "
With RichTextBox1
.AppendText(Replace(.Text, "@", vbNullString) & oPos)
.Select(oPos, oUsertext.Length)
.SelectionColor = Color.Blue
.Select()
End With
End Sub
Private Sub ListBoxUserMention_KeyUp(sender As Object, e As KeyEventArgs) Handles ListBoxUserMention.KeyUp
If e.KeyCode = Keys.Return Then
Dim oMentionedUser As String = ListBoxUserMention.SelectedItem
'ADD_MentionedUser(strTag)
With RichTextBox1
CUrrentMentionedUser = oMentionedUser
' Logger.Debug($"ListBoxUserMention_KeyUp.CurrentMentionedUser = {CurrentMentionedUser}")
For Each oROW As DataRow In oDTConv_User.Rows
Dim oConvUser2beChecked
oConvUser2beChecked = oROW.Item(CurrentIdentifier)
If IsDBNull(oConvUser2beChecked) Then
oConvUser2beChecked = String.Empty
End If
If oConvUser2beChecked.ToString = String.Empty Then
If CurrentIdentifier = "EMAIL" Then
oConvUser2beChecked = $"No Email for User '{oROW.Item("USERNAME")}'"
Else
oConvUser2beChecked = $"No Identification for UserID '{oROW.Item("USER_ID")}'"
End If
End If
CurrentMentionedUser = CurrentMentionedUser.Substring(1, CurrentMentionedUser.Length - 1)
'Logger.Debug($"oConvUser2beChecked = {CurrentMentionedUser}")
If oConvUser2beChecked.ToString.Contains(CurrentMentionedUser) Then
CurrentMentionedUserID = oROW.Item("USER_ID")
Logger.Debug($"CurrentMentionedUserID Changed: {CurrentMentionedUserID}")
End If
Next
' Das "Steuerzeichen" # ersetzen durch einen NullString
.Text = Replace(.Text, "@", vbNullString) & oMentionedUser & ": "
' wo soll der Cursor nach dem Einfügen in der RTB stehen?
.SelectionStart = .TextLength
' Listbox wieder auf den ersten Eintrag setzen
' und ausblenden
ListBoxUserMention.SelectedItem = 0
ListBoxUserMention.Visible = False
' Focus wieder auf RTB setzen
.Focus()
End With
End If
End Sub
End Class