Forum Discussion

PerBonomi's avatar
11 years ago

Search your PMs

Hi there. I just made this component that lets you search your Private Messages. Thought I'd share it. Maybe someone wants to use or see if they can streamline it.

 

I've made it search max 500 records. Obviously you can make that less, or add some code to let users search more than just the first page.

 

You should just be able to plug it into a custom component on any page.

 

Username is case insensitive.

Start date I automatically insert the date the Spotify community started. End date should always automatically show today().

 

Have fun with it.

 

SearchPMs.jpg

 

		<form>
			<div class="lia-panel lia-panel-standard lia-component-pm-search">
				<div class="lia-decoration-border">
					<div class="lia-decoration-border-top">
						<div></div>
					</div>
					<div class="lia-decoration-border-content">
						<div>
							<div class="lia-panel-heading-bar-wrapper">
								<div class="lia-panel-heading-bar"><span class="lia-panel-heading-bar-title">Search PMs</span></div>
							</div>
							<div class="lia-panel-content-wrapper">
								<div class="lia-panel-content">
									<div>
										Search my <select id="InOrOut" onChange="FuncToFrom();" class="lia-search-form-granularity"><option>inbox</option><option>outbox</option></select>&nbsp;for&nbsp;PMs&nbsp;<span id="FromTo">from</span> <input type="text" id="UserLookup" class="lia-form-type-text search-input lia-search-input-message"/> between <input type="text" id="SearchDateStart" class="lia-form-type-text search-input lia-search-input-message" value="2012-02-08"/> and <input type="text" id="SearchDateEnd" class="lia-form-type-text search-input lia-search-input-message" value=""/>&nbsp;<input type="button" value="Go!" onClick="LookUpUserPM();" class="lia-button lia-button-secondary lia-button-searchForm-action"/>
									</div>
									<div id="PMs"></div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</form>
		<script>
		var today = new Date();
		var dd = today.getDate();
		var mm = today.getMonth()+1; //January is 0!
		var yyyy = today.getFullYear();
		if(dd<10) {dd='0'+dd}
		if(mm<10) {mm='0'+mm}
		vToday = yyyy+'-'+mm+'-'+dd;
		$(document).ready(function () {
			$("#SearchDateEnd").val(vToday);
		});
		function FuncToFrom() {
			var vInOut = $("#InOrOut").val();
			if (vInOut == 'inbox') { vLookin = 'from' ;}
			if (vInOut == 'outbox') { vLookin = 'to' ;}
			$("#FromTo").html(vLookin);
		}

		function LookUpUserPM() {
			FuncToFrom();
			var vInOut = $("#InOrOut").val();
			$("#PMs").html('Thinking...');
			if ($("#SearchDateStart").val() == '' || $("#SearchDateStart").val() == 'YYYY-MM-DD') {
				vDateSearchStart = 0;
			} else {
				vDateSearchStart = new Date($("#SearchDateStart").val());
			}
			if ($("#SearchDateEnd").val() == '' || $("#SearchDateEnd").val() == 'YYYY-MM-DD') {
				vDateSearchEnd = 10000000000000;
			} else {
				vDateSearchEnd = new Date($("#SearchDateEnd").val());
			}
			$.get("/restapi/vc/users/id/${user.id}/mailbox/notes/" + vInOut + "/?page_size=500&page=1&restapi.response_style=view", {}, function (xml) {
				vText = '';
				vTotalNotes = 0;
				$('note', xml).each(function (i) {
					vTotalNotes += 1;
					
					var vFromTo = $(this).find(vLookin).find('login').text();
					var vFromTo2 = vFromTo.toLowerCase();
					
					if ($("#UserLookup").val() == '') {
						vDate = $(this).find('sent_time').text();
						vDateNew = new Date(vDate.substr(0,vDate.indexOf("T")));
						vSubject = $(this).find('subject').text();
						vNoteId = $(this).find('id').text();
						if ( vDateSearchEnd >= vDateNew && vDateSearchStart <= vDateNew ) {
							vText += vLookin.charAt(0).toUpperCase() + vLookin.slice(1) + ': ' + vFromTo + ', Sent: ' + vDate + ', Subject: <a style="color:#7ab800 !important\;" href="/t5/notes/privatenotespage/tab/' + vInOut + '/note-id/' + vNoteId + '/notes-view-mode/single" target="_new">'+ vSubject + '</a><br/>';
						}
					} else if ( vFromTo2 == $("#UserLookup").val().toLowerCase()) {
						vDate = $(this).find('sent_time').text();
						vDateNew = new Date(vDate.substr(0,vDate.indexOf("T")));
						vSubject = $(this).find('subject').text();
						vNoteId = $(this).find('id').text();
						if ( vDateSearchEnd >= vDateNew && vDateSearchStart <= vDateNew ) {
							vText += vLookin.charAt(0).toUpperCase() + vLookin.slice(1) + ': ' + vFromTo + ', Sent: ' + vDate + ', Subject: <a style="color:#7ab800 !important\;" href="/t5/notes/privatenotespage/tab/' + vInOut + '/note-id/' + vNoteId + '/notes-view-mode/single" target="_new">'+ vSubject + '</a><br/>';
						}
					}
				});
			if (vText != '') { $("#PMs").html(vText);}
			if ($("#PMs").text() == 'Thinking...') { $("#PMs").text('I searched ' + vTotalNotes + ' records, but didn\'t find anything. Sorry :/'); }

			});
		}
		</script>